From 87017432f8bfb398fc13e280fee0ad473028881a Mon Sep 17 00:00:00 2001 From: Taku Kudo Date: Sun, 12 Jul 2026 17:05:35 +0000 Subject: [PATCH 1/5] Migrate SentencePiece to UPB (protobuf-free) mode. - Remove protobuf-lite dependency and integrate upb runtime. - Refactor CMakeLists.txt to support SPM_USE_UPB by default and clean source file lists. - Simplify python/setup.py to compile in UPB mode and link train library. - Add index bounds checks to upb wrappers for safety. TAG=agy CONV=e46efd90-ae25-4cfc-8385-5381e11ee2ec --- CMakeLists.txt | 11 +- python/setup.py | 34 +- .../src/sentencepiece/sentencepiece_pybind.cc | 10 +- src/CMakeLists.txt | 204 +- src/builder_test.cc | 4 +- src/builtin_pb/sentencepiece.pb.cc | 923 - src/builtin_pb/sentencepiece.pb.h | 1041 - src/builtin_pb/sentencepiece_model.pb.cc | 3499 --- src/builtin_pb/sentencepiece_model.pb.h | 5030 ----- src/builtin_upb/README.txt | 20 + src/builtin_upb/sentencepiece.pb.h | 20 + src/builtin_upb/sentencepiece.upb.h | 457 + src/builtin_upb/sentencepiece.upb_minitable.c | 106 + src/builtin_upb/sentencepiece.upb_minitable.h | 28 + src/builtin_upb/sentencepiece.upbdefs.c | 60 + src/builtin_upb/sentencepiece.upbdefs.h | 47 + src/builtin_upb/sentencepiece_model.pb.h | 20 + src/builtin_upb/sentencepiece_model.upb.h | 1933 ++ .../sentencepiece_model.upb_minitable.c | 284 + .../sentencepiece_model.upb_minitable.h | 33 + src/builtin_upb/sentencepiece_model.upbdefs.c | 318 + src/builtin_upb/sentencepiece_model.upbdefs.h | 62 + src/builtin_upb/upb_wrapper.h | 1686 ++ src/filesystem.cc | 6 +- src/init.cc | 4 - src/model_interface.h | 2 +- src/normalizer_test.cc | 4 +- src/sentencepiece_processor.cc | 46 +- src/sentencepiece_processor.h | 8 +- src/sentencepiece_processor_test.cc | 8 +- src/sentencepiece_trainer.cc | 45 +- src/trainer_interface.cc | 4 +- src/util.cc | 33 + src/util.h | 9 + third_party/protobuf-lite/LICENSE | 32 - third_party/protobuf-lite/arena.cc | 449 - third_party/protobuf-lite/arenastring.cc | 254 - third_party/protobuf-lite/bytestream.cc | 198 - third_party/protobuf-lite/coded_stream.cc | 956 - third_party/protobuf-lite/common.cc | 329 - third_party/protobuf-lite/extension_set.cc | 2143 -- .../protobuf-lite/generated_enum_util.cc | 95 - .../generated_message_table_driven_lite.cc | 106 - .../protobuf-lite/generated_message_util.cc | 804 - .../protobuf-lite/google/protobuf/any.h | 150 - .../protobuf-lite/google/protobuf/arena.h | 694 - .../google/protobuf/arena_impl.h | 491 - .../google/protobuf/arenastring.h | 375 - .../google/protobuf/descriptor.h | 2324 -- .../google/protobuf/extension_set.h | 1591 -- .../google/protobuf/extension_set_inl.h | 276 - .../protobuf/generated_enum_reflection.h | 98 - .../google/protobuf/generated_enum_util.h | 83 - .../protobuf/generated_message_table_driven.h | 336 - .../generated_message_table_driven_lite.h | 802 - .../google/protobuf/generated_message_util.h | 270 - .../protobuf-lite/google/protobuf/has_bits.h | 116 - .../google/protobuf/implicit_weak_message.h | 190 - .../google/protobuf/io/coded_stream.h | 1714 -- .../google/protobuf/io/io_win32.h | 139 - .../google/protobuf/io/zero_copy_stream.h | 253 - .../protobuf/io/zero_copy_stream_impl.h | 327 - .../protobuf/io/zero_copy_stream_impl_lite.h | 408 - .../protobuf-lite/google/protobuf/map.h | 1362 -- .../google/protobuf/map_entry_lite.h | 653 - .../google/protobuf/map_field_lite.h | 183 - .../google/protobuf/map_type_handler.h | 688 - .../google/protobuf/message_lite.h | 621 - .../google/protobuf/metadata_lite.h | 248 - .../google/protobuf/parse_context.h | 869 - .../protobuf-lite/google/protobuf/port.h | 43 - .../google/protobuf/port_def.inc | 606 - .../google/protobuf/port_undef.inc | 116 - .../google/protobuf/repeated_field.h | 2847 --- .../google/protobuf/stubs/bytestream.h | 351 - .../google/protobuf/stubs/callback.h | 583 - .../google/protobuf/stubs/casts.h | 139 - .../google/protobuf/stubs/common.h | 202 - .../google/protobuf/stubs/hash.h | 114 - .../google/protobuf/stubs/int128.h | 387 - .../google/protobuf/stubs/logging.h | 241 - .../google/protobuf/stubs/macros.h | 120 - .../google/protobuf/stubs/map_util.h | 769 - .../google/protobuf/stubs/mutex.h | 186 - .../google/protobuf/stubs/once.h | 55 - .../google/protobuf/stubs/platform_macros.h | 134 - .../google/protobuf/stubs/port.h | 405 - .../google/protobuf/stubs/status.h | 125 - .../google/protobuf/stubs/statusor.h | 272 - .../google/protobuf/stubs/stl_util.h | 71 - .../google/protobuf/stubs/stringpiece.h | 489 - .../google/protobuf/stubs/stringprintf.h | 85 - .../google/protobuf/stubs/strutil.h | 953 - .../google/protobuf/stubs/time.h | 80 - .../google/protobuf/unknown_field_set.h | 411 - .../google/protobuf/wire_format_lite.h | 1866 -- .../protobuf-lite/implicit_weak_message.cc | 65 - third_party/protobuf-lite/int128.cc | 192 - third_party/protobuf-lite/io_win32.cc | 470 - third_party/protobuf-lite/message_lite.cc | 583 - third_party/protobuf-lite/parse_context.cc | 593 - third_party/protobuf-lite/repeated_field.cc | 136 - third_party/protobuf-lite/status.cc | 134 - third_party/protobuf-lite/statusor.cc | 48 - third_party/protobuf-lite/stringpiece.cc | 270 - third_party/protobuf-lite/stringprintf.cc | 176 - .../protobuf-lite/structurally_valid.cc | 615 - third_party/protobuf-lite/strutil.cc | 2477 --- third_party/protobuf-lite/time.cc | 365 - third_party/protobuf-lite/wire_format_lite.cc | 778 - third_party/protobuf-lite/zero_copy_stream.cc | 55 - .../protobuf-lite/zero_copy_stream_impl.cc | 366 - .../zero_copy_stream_impl_lite.cc | 467 - third_party/upb/LICENSE | 25 + third_party/upb/google/protobuf/arena.h | 56 + .../upb/google/protobuf/message_lite.h | 26 + third_party/upb/upb.c | 15419 +++++++++++++ third_party/upb/upb.h | 18141 ++++++++++++++++ third_party/upb/utf8_range.c | 207 + third_party/upb/utf8_range.h | 23 + 120 files changed, 39214 insertions(+), 50749 deletions(-) delete mode 100644 src/builtin_pb/sentencepiece.pb.cc delete mode 100644 src/builtin_pb/sentencepiece.pb.h delete mode 100644 src/builtin_pb/sentencepiece_model.pb.cc delete mode 100644 src/builtin_pb/sentencepiece_model.pb.h create mode 100644 src/builtin_upb/README.txt create mode 100644 src/builtin_upb/sentencepiece.pb.h create mode 100644 src/builtin_upb/sentencepiece.upb.h create mode 100644 src/builtin_upb/sentencepiece.upb_minitable.c create mode 100644 src/builtin_upb/sentencepiece.upb_minitable.h create mode 100644 src/builtin_upb/sentencepiece.upbdefs.c create mode 100644 src/builtin_upb/sentencepiece.upbdefs.h create mode 100644 src/builtin_upb/sentencepiece_model.pb.h create mode 100644 src/builtin_upb/sentencepiece_model.upb.h create mode 100644 src/builtin_upb/sentencepiece_model.upb_minitable.c create mode 100644 src/builtin_upb/sentencepiece_model.upb_minitable.h create mode 100644 src/builtin_upb/sentencepiece_model.upbdefs.c create mode 100644 src/builtin_upb/sentencepiece_model.upbdefs.h create mode 100644 src/builtin_upb/upb_wrapper.h delete mode 100644 third_party/protobuf-lite/LICENSE delete mode 100644 third_party/protobuf-lite/arena.cc delete mode 100644 third_party/protobuf-lite/arenastring.cc delete mode 100644 third_party/protobuf-lite/bytestream.cc delete mode 100644 third_party/protobuf-lite/coded_stream.cc delete mode 100644 third_party/protobuf-lite/common.cc delete mode 100644 third_party/protobuf-lite/extension_set.cc delete mode 100644 third_party/protobuf-lite/generated_enum_util.cc delete mode 100644 third_party/protobuf-lite/generated_message_table_driven_lite.cc delete mode 100644 third_party/protobuf-lite/generated_message_util.cc delete mode 100644 third_party/protobuf-lite/google/protobuf/any.h delete mode 100644 third_party/protobuf-lite/google/protobuf/arena.h delete mode 100644 third_party/protobuf-lite/google/protobuf/arena_impl.h delete mode 100644 third_party/protobuf-lite/google/protobuf/arenastring.h delete mode 100644 third_party/protobuf-lite/google/protobuf/descriptor.h delete mode 100644 third_party/protobuf-lite/google/protobuf/extension_set.h delete mode 100644 third_party/protobuf-lite/google/protobuf/extension_set_inl.h delete mode 100644 third_party/protobuf-lite/google/protobuf/generated_enum_reflection.h delete mode 100644 third_party/protobuf-lite/google/protobuf/generated_enum_util.h delete mode 100644 third_party/protobuf-lite/google/protobuf/generated_message_table_driven.h delete mode 100644 third_party/protobuf-lite/google/protobuf/generated_message_table_driven_lite.h delete mode 100644 third_party/protobuf-lite/google/protobuf/generated_message_util.h delete mode 100644 third_party/protobuf-lite/google/protobuf/has_bits.h delete mode 100644 third_party/protobuf-lite/google/protobuf/implicit_weak_message.h delete mode 100644 third_party/protobuf-lite/google/protobuf/io/coded_stream.h delete mode 100644 third_party/protobuf-lite/google/protobuf/io/io_win32.h delete mode 100644 third_party/protobuf-lite/google/protobuf/io/zero_copy_stream.h delete mode 100644 third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl.h delete mode 100644 third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl_lite.h delete mode 100644 third_party/protobuf-lite/google/protobuf/map.h delete mode 100644 third_party/protobuf-lite/google/protobuf/map_entry_lite.h delete mode 100644 third_party/protobuf-lite/google/protobuf/map_field_lite.h delete mode 100644 third_party/protobuf-lite/google/protobuf/map_type_handler.h delete mode 100644 third_party/protobuf-lite/google/protobuf/message_lite.h delete mode 100644 third_party/protobuf-lite/google/protobuf/metadata_lite.h delete mode 100644 third_party/protobuf-lite/google/protobuf/parse_context.h delete mode 100644 third_party/protobuf-lite/google/protobuf/port.h delete mode 100644 third_party/protobuf-lite/google/protobuf/port_def.inc delete mode 100644 third_party/protobuf-lite/google/protobuf/port_undef.inc delete mode 100644 third_party/protobuf-lite/google/protobuf/repeated_field.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/bytestream.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/callback.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/casts.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/common.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/hash.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/int128.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/logging.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/macros.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/map_util.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/mutex.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/once.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/platform_macros.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/port.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/status.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/statusor.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/stl_util.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/stringpiece.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/stringprintf.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/strutil.h delete mode 100644 third_party/protobuf-lite/google/protobuf/stubs/time.h delete mode 100644 third_party/protobuf-lite/google/protobuf/unknown_field_set.h delete mode 100644 third_party/protobuf-lite/google/protobuf/wire_format_lite.h delete mode 100644 third_party/protobuf-lite/implicit_weak_message.cc delete mode 100644 third_party/protobuf-lite/int128.cc delete mode 100644 third_party/protobuf-lite/io_win32.cc delete mode 100644 third_party/protobuf-lite/message_lite.cc delete mode 100644 third_party/protobuf-lite/parse_context.cc delete mode 100644 third_party/protobuf-lite/repeated_field.cc delete mode 100644 third_party/protobuf-lite/status.cc delete mode 100644 third_party/protobuf-lite/statusor.cc delete mode 100644 third_party/protobuf-lite/stringpiece.cc delete mode 100644 third_party/protobuf-lite/stringprintf.cc delete mode 100644 third_party/protobuf-lite/structurally_valid.cc delete mode 100644 third_party/protobuf-lite/strutil.cc delete mode 100644 third_party/protobuf-lite/time.cc delete mode 100644 third_party/protobuf-lite/wire_format_lite.cc delete mode 100644 third_party/protobuf-lite/zero_copy_stream.cc delete mode 100644 third_party/protobuf-lite/zero_copy_stream_impl.cc delete mode 100644 third_party/protobuf-lite/zero_copy_stream_impl_lite.cc create mode 100644 third_party/upb/LICENSE create mode 100644 third_party/upb/google/protobuf/arena.h create mode 100644 third_party/upb/google/protobuf/message_lite.h create mode 100644 third_party/upb/upb.c create mode 100644 third_party/upb/upb.h create mode 100644 third_party/upb/utf8_range.c create mode 100644 third_party/upb/utf8_range.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 200e39484..01c516905 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ project(sentencepiece VERSION ${SPM_VERSION} LANGUAGES C CXX) option(SPM_ENABLE_NFKC_COMPILE "Enables NFKC compile" OFF) option(SPM_ENABLE_SHARED "Builds shared libaries in addition to static libraries." ON) +option(SPM_USE_UPB "Use upb for core engine" ON) option(SPM_BUILD_TEST "Builds test binaries." OFF) option(SPM_ENABLE_TCMALLOC "Enable TCMalloc if available." ON) option(SPM_TCMALLOC_STATIC "Link static library of TCMALLOC." OFF) @@ -35,8 +36,8 @@ option(SPM_NLCODEC_BPE "Build the NLCodec fast BPE trainer from contrib/nlcodec (adds --nlcodec_bpe flag)." OFF) -set(SPM_PROTOBUF_PROVIDER "internal" CACHE STRING "Provider of protobuf library") -set_property(CACHE SPM_PROTOBUF_PROVIDER PROPERTY STRINGS "internal" "package") +set(SPM_PROTOBUF_PROVIDER "package" CACHE STRING "Provider of protobuf library") +set_property(CACHE SPM_PROTOBUF_PROVIDER PROPERTY STRINGS "package") set(SPM_ABSL_PROVIDER "module" CACHE STRING "Provider of absl library") set_property(CACHE SPM_ABSL_PROVIDER PROPERTY STRINGS "module" "package") @@ -188,10 +189,8 @@ endif() if (SPM_ABSL_PROVIDER STREQUAL "module") include(FetchContent) FetchContent_Populate(abseil-cpp - GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git - SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/abseil-cpp - GIT_PROGRESS TRUE - GIT_TAG 20260526.0) + URL https://github.com/abseil/abseil-cpp/archive/refs/tags/20260526.0.tar.gz + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/abseil-cpp) add_subdirectory(third_party/abseil-cpp) if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/absl) file(CREATE_LINK diff --git a/python/setup.py b/python/setup.py index 60e0811bd..3fdc15b13 100755 --- a/python/setup.py +++ b/python/setup.py @@ -60,30 +60,29 @@ def find_abseil_lib(search_root): def get_protobuf_includes(): prefix = '/I' if os.name == 'nt' else '-I' paths = [ - '../src/builtin_pb', - './sentencepiece/src/builtin_pb', - '../third_party/protobuf-lite', - './sentencepiece/third_party/protobuf-lite', + '../src', + '../src/builtin_upb', + '../third_party/upb', ] return [prefix + os.path.normpath(p) for p in paths] def get_cflags_and_libs(root): + lib_dir = 'lib' + if not os.path.exists(os.path.join(root, 'lib/libsentencepiece.a')) and os.path.exists(os.path.join(root, 'lib64/libsentencepiece.a')): + lib_dir = 'lib64' cflags = [ '-std=c++17', '-I' + os.path.normpath(os.path.join(root, 'include')), + '-DSPM_USE_UPB=1', ] + get_protobuf_includes() libs = [] - if os.path.exists(os.path.join(root, 'lib/libsentencepiece.a')): - libs = [ - os.path.join(root, 'lib/libsentencepiece.a'), - os.path.join(root, 'lib/libsentencepiece_train.a'), - ] - elif os.path.exists(os.path.join(root, 'lib64/libsentencepiece.a')): - libs = [ - os.path.join(root, 'lib64/libsentencepiece.a'), - os.path.join(root, 'lib64/libsentencepiece_train.a'), - ] + sp_lib = os.path.join(root, lib_dir, 'libsentencepiece.a') + train_lib = os.path.join(root, lib_dir, 'libsentencepiece_train.a') + if os.path.exists(sp_lib): + libs.append(sp_lib) + if os.path.exists(train_lib): + libs.append(train_lib) return cflags, libs @@ -167,20 +166,20 @@ def build_extension(self, ext): cflags = [ '/std:c++17', '/I' + os.path.normpath('..\\build_{}\\root\\include'.format(arch)), + '/DSPM_USE_UPB=1', ] + get_protobuf_includes() libs = [ '..\\build_{}\\root\\lib\\sentencepiece.lib'.format(arch), - '..\\build_{}\\root\\lib\\sentencepiece_train.lib'.format(arch), ] libs.extend(find_abseil_lib('..\\build_{}\\third_party'.format(arch))) elif os.path.exists('..\\build\\root\\lib'): cflags = [ '/std:c++17', '/I' + os.path.normpath('..\\build\\root\\include'), + '/DSPM_USE_UPB=1', ] + get_protobuf_includes() libs = [ '..\\build\\root\\lib\\sentencepiece.lib', - '..\\build\\root\\lib\\sentencepiece_train.lib', ] libs.extend(find_abseil_lib('..\\build\\third_party')) else: @@ -200,6 +199,7 @@ def build_extension(self, ext): '-B', 'build', '-DSPM_ENABLE_SHARED=OFF', + '-DSPM_USE_UPB=ON', # '-DCMAKE_SHARED_LINKER_FLAGS="/OPT:REF /OPT:ICF /LTCG"', '-DCMAKE_INSTALL_PREFIX=build\\root', ]) @@ -217,10 +217,10 @@ def build_extension(self, ext): cflags = [ '/std:c++17', '/I' + os.path.normpath('.\\build\\root\\include'), + '/DSPM_USE_UPB=1', ] + get_protobuf_includes() libs = [ '.\\build\\root\\lib\\sentencepiece.lib', - '.\\build\\root\\lib\\sentencepiece_train.lib', ] libs.extend(find_abseil_lib('.\\build\\third_party')) diff --git a/python/src/sentencepiece/sentencepiece_pybind.cc b/python/src/sentencepiece/sentencepiece_pybind.cc index af4c8dc13..79b563499 100644 --- a/python/src/sentencepiece/sentencepiece_pybind.cc +++ b/python/src/sentencepiece/sentencepiece_pybind.cc @@ -39,11 +39,11 @@ struct PyInputStringView { }; // Helper to convert std::string to py::str or py::bytes based on flag. -py::object ToPyString(const std::string& str, bool is_bytes) { +py::object ToPyString(std::string_view str, bool is_bytes) { if (is_bytes) { - return py::bytes(str); + return py::bytes(str.data(), str.size()); } else { - return py::str(str); + return py::str(str.data(), str.size()); } } @@ -169,10 +169,10 @@ absl::Status RewriteIds(const sentencepiece::SentencePieceProcessor& sp, return absl::OkStatus(); if (reverse) std::reverse(pieces->begin(), pieces->end()); if (add_bos) { - pieces->insert(pieces->begin(), sp.IdToPiece(sp.bos_id())); + pieces->insert(pieces->begin(), std::string(sp.IdToPiece(sp.bos_id()))); } if (add_eos) { - pieces->push_back(sp.IdToPiece(sp.eos_id())); + pieces->push_back(std::string(sp.IdToPiece(sp.eos_id()))); } if (emit_unk_piece) { const auto& unk = sp.IdToPiece(sp.unk_id()); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4b0e1f32a..f7d03409d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.! +# SPM_BUILD_TEST can be enabled manually in upb mode now list(APPEND SPM_LIBS absl::status) list(APPEND SPM_LIBS absl::status_builder) @@ -23,68 +24,38 @@ list(APPEND SPM_LIBS absl::check) list(APPEND SPM_LIBS absl::random_random) list(APPEND SPM_LIBS absl::time) -if (SPM_PROTOBUF_PROVIDER STREQUAL "internal") - set(SPM_PROTO_HDRS builtin_pb/sentencepiece.pb.h) - set(SPM_PROTO_SRCS builtin_pb/sentencepiece.pb.cc) - set(SPM_MODEL_PROTO_HDRS builtin_pb/sentencepiece_model.pb.h) - set(SPM_MODEL_PROTO_SRCS builtin_pb/sentencepiece_model.pb.cc) - set(PROTOBUF_LITE_LIBRARY "") - set(PROTOBUF_LITE_SRCS - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/arena.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/arenastring.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/bytestream.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/coded_stream.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/common.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/extension_set.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/generated_enum_util.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/generated_message_table_driven_lite.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/generated_message_util.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/implicit_weak_message.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/int128.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/io_win32.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/message_lite.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/parse_context.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/repeated_field.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/status.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/statusor.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/stringpiece.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/stringprintf.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/structurally_valid.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/strutil.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/time.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/wire_format_lite.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/zero_copy_stream.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/zero_copy_stream_impl.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite/zero_copy_stream_impl_lite.cc) - if (MSVC) - add_definitions("/DHAVE_PTHREAD /wd4018 /wd4514") - elseif (WIN32) - # Windows but not cl.exe (e.g. Clang targeting *-pc-windows-msvc, MinGW). - add_definitions("-DHAVE_PTHREAD=1 -Wno-sign-compare -Wno-deprecated-declarations") +if (NOT SPM_USE_UPB) + if (SPM_PROTOBUF_PROVIDER STREQUAL "package") + find_package(Protobuf REQUIRED) + include_directories(${Protobuf_INCLUDE_DIRS}) + protobuf_generate_cpp(SPM_PROTO_SRCS SPM_PROTO_HDRS sentencepiece.proto) + protobuf_generate_cpp(SPM_MODEL_PROTO_SRCS SPM_MODEL_PROTO_HDRS sentencepiece_model.proto) + set(PROTOBUF_LITE_SRCS "") + include_directories(${PROTOBUF_INCLUDE_DIR}) + add_compile_definitions(_USE_EXTERNAL_PROTOBUF) else() - add_definitions("-pthread -DHAVE_PTHREAD=1 -Wno-sign-compare -Wno-deprecated-declarations") + message(FATAL_ERROR "Unsupported protobuf provider: ${SPM_PROTOBUF_PROVIDER}") endif() - include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third_party/protobuf-lite) - include_directories(builtin_pb) -elseif (SPM_PROTOBUF_PROVIDER STREQUAL "package") - find_package(Protobuf REQUIRED) - include_directories(${Protobuf_INCLUDE_DIRS}) - protobuf_generate_cpp(SPM_PROTO_SRCS SPM_PROTO_HDRS sentencepiece.proto) - protobuf_generate_cpp(SPM_MODEL_PROTO_SRCS SPM_MODEL_PROTO_HDRS sentencepiece_model.proto) - set(PROTOBUF_LITE_SRCS "") - include_directories(${PROTOBUF_INCLUDE_DIR}) - add_compile_definitions(_USE_EXTERNAL_PROTOBUF) endif() -include_directories(${CMAKE_CURRENT_BINARY_DIR}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third_party) -set(SPM_SRCS - ${PROTOBUF_LITE_SRCS} - ${SPM_PROTO_HDRS} - ${SPM_PROTO_SRCS} - ${SPM_MODEL_PROTO_HDRS} - ${SPM_MODEL_PROTO_SRCS} +set(SPM_COMMON_SRCS + bpe_model.cc + char_model.cc + filesystem.cc + init.cc + model_factory.cc + model_interface.cc + normalizer.cc + sentencepiece_processor.cc + unigram_model.cc + util.cc + word_model.cc +) + +set(SPM_COMMON_HDRS bpe_model.h common.h normalizer.h @@ -99,17 +70,32 @@ set(SPM_SRCS model_interface.h testharness.h unigram_model.h - bpe_model.cc - char_model.cc - filesystem.cc - init.cc - model_factory.cc - model_interface.cc - normalizer.cc - sentencepiece_processor.cc - unigram_model.cc - util.cc - word_model.cc) +) + +if (SPM_USE_UPB) + set(UPB_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb/upb.c + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb/utf8_range.c + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/sentencepiece_model.upb_minitable.c + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/sentencepiece.upb_minitable.c + ) + + set(SPM_SRCS + ${UPB_SRCS} + ${SPM_COMMON_SRCS} + ${SPM_COMMON_HDRS} + ) +else() + set(SPM_SRCS + ${PROTOBUF_LITE_SRCS} + ${SPM_PROTO_HDRS} + ${SPM_PROTO_SRCS} + ${SPM_MODEL_PROTO_HDRS} + ${SPM_MODEL_PROTO_SRCS} + ${SPM_COMMON_SRCS} + ${SPM_COMMON_HDRS} + ) +endif() set(SPM_TRAIN_SRCS ${SPM_PROTO_HDRS} @@ -222,33 +208,82 @@ endif() if (SPM_ENABLE_SHARED) add_library(sentencepiece SHARED ${SPM_SRCS}) - add_library(sentencepiece_train SHARED ${SPM_TRAIN_SRCS}) + if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) + target_include_directories(sentencepiece PUBLIC ${SPM_INTERNAL_PROTO_INC}) + endif() if (ANDROID) target_link_libraries(sentencepiece PRIVATE log) + endif() + add_library(sentencepiece_train SHARED ${SPM_TRAIN_SRCS}) + if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) + target_include_directories(sentencepiece_train PUBLIC ${SPM_INTERNAL_PROTO_INC}) + endif() + if (ANDROID) target_link_libraries(sentencepiece_train PRIVATE log) endif() endif() add_library(sentencepiece-static STATIC ${SPM_SRCS}) +if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) + target_include_directories(sentencepiece-static PUBLIC ${SPM_INTERNAL_PROTO_INC}) +endif() add_library(sentencepiece_train-static STATIC ${SPM_TRAIN_SRCS}) +if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) + target_include_directories(sentencepiece_train-static PUBLIC ${SPM_INTERNAL_PROTO_INC}) +endif() -target_link_libraries(sentencepiece-static INTERFACE ${SPM_LIBS}) -target_link_libraries(sentencepiece_train-static INTERFACE sentencepiece-static ${SPM_LIBS}) +if (SPM_USE_UPB) + if (SPM_ENABLE_SHARED) + target_link_libraries(sentencepiece PUBLIC ${SPM_LIBS}) + target_compile_definitions(sentencepiece PUBLIC SPM_USE_UPB=1) + target_include_directories(sentencepiece PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb + ) + target_link_libraries(sentencepiece_train PUBLIC ${SPM_LIBS} sentencepiece) + target_compile_definitions(sentencepiece_train PUBLIC SPM_USE_UPB=1) + target_include_directories(sentencepiece_train PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb + ) + endif() + target_link_libraries(sentencepiece-static INTERFACE ${SPM_LIBS}) + target_compile_definitions(sentencepiece-static PUBLIC SPM_USE_UPB=1) + target_include_directories(sentencepiece-static PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb + ) + target_link_libraries(sentencepiece_train-static INTERFACE sentencepiece-static ${SPM_LIBS}) + target_compile_definitions(sentencepiece_train-static PUBLIC SPM_USE_UPB=1) + target_include_directories(sentencepiece_train-static PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb + ) +else() + target_link_libraries(sentencepiece-static INTERFACE ${SPM_LIBS}) + if (SPM_ENABLE_SHARED) + target_link_libraries(sentencepiece PUBLIC ${SPM_LIBS}) + target_link_libraries(sentencepiece_train PUBLIC ${SPM_LIBS} sentencepiece) + endif() + target_link_libraries(sentencepiece_train-static INTERFACE sentencepiece-static ${SPM_LIBS}) +endif() if (SPM_ENABLE_SHARED) - target_link_libraries(sentencepiece PUBLIC ${SPM_LIBS}) - target_link_libraries(sentencepiece_train PUBLIC ${SPM_LIBS} sentencepiece) set(SPM_INSTALLTARGETS sentencepiece sentencepiece_train sentencepiece-static sentencepiece_train-static) - set_target_properties(sentencepiece sentencepiece_train PROPERTIES SOVERSION 0 VERSION 0.0.0) - set_target_properties(sentencepiece PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES) + set_target_properties(sentencepiece_train PROPERTIES SOVERSION 0 VERSION 0.0.0) set_target_properties(sentencepiece_train PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES) if (MSVC) - set_target_properties(sentencepiece PROPERTIES IMPORT_SUFFIX "_import.lib") set_target_properties(sentencepiece_train PROPERTIES IMPORT_SUFFIX "_import.lib") elseif (MINGW) - set_target_properties(sentencepiece PROPERTIES IMPORT_SUFFIX ".dll.a") set_target_properties(sentencepiece_train PROPERTIES IMPORT_SUFFIX ".dll.a") endif() + set_target_properties(sentencepiece PROPERTIES SOVERSION 0 VERSION 0.0.0) + set_target_properties(sentencepiece PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES) + if (MSVC) + set_target_properties(sentencepiece PROPERTIES IMPORT_SUFFIX "_import.lib") + elseif (MINGW) + set_target_properties(sentencepiece PROPERTIES IMPORT_SUFFIX ".dll.a") + endif() else() add_library(sentencepiece ALIAS sentencepiece-static) add_library(sentencepiece_train ALIAS sentencepiece_train-static) @@ -260,9 +295,11 @@ set_target_properties(sentencepiece_train-static PROPERTIES OUTPUT_NAME "sentenc if (NOT WIN32) set(CMAKE_CXX_FLAGS "-O3 -Wall -fPIC ${CMAKE_CXX_FLAGS}") - set_source_files_properties( - sentencepiece.pb.cc sentencepiece_model.pb.cc - PROPERTIES COMPILE_FLAGS "-Wno-misleading-indentation") + if (NOT SPM_USE_UPB) + set_source_files_properties( + sentencepiece.pb.cc sentencepiece_model.pb.cc + PROPERTIES COMPILE_FLAGS "-Wno-misleading-indentation") + endif() set_source_files_properties(${SPM_TEST_SRCS} PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") if (SPM_ENABLE_SHARED) @@ -273,15 +310,16 @@ endif() add_executable(spm_encode spm_encode_main.cc) add_executable(spm_decode spm_decode_main.cc) -add_executable(spm_normalize spm_normalize_main.cc) -add_executable(spm_train spm_train_main.cc) add_executable(spm_export_vocab spm_export_vocab_main.cc) target_link_libraries(spm_encode sentencepiece) target_link_libraries(spm_decode sentencepiece) +target_link_libraries(spm_export_vocab sentencepiece) + +add_executable(spm_normalize spm_normalize_main.cc) +add_executable(spm_train spm_train_main.cc) target_link_libraries(spm_normalize sentencepiece sentencepiece_train) target_link_libraries(spm_train sentencepiece sentencepiece_train) -target_link_libraries(spm_export_vocab sentencepiece) if (SPM_ENABLE_NFKC_COMPILE) add_executable(compile_charsmap compile_charsmap_main.cc) @@ -333,7 +371,7 @@ add_custom_target(generate_installed_headers ALL install(FILES "${GENERATED_HEADERS_DIR}/sentencepiece_trainer.h" "${GENERATED_HEADERS_DIR}/sentencepiece_processor.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) -if (NOT SPM_PROTOBUF_PROVIDER STREQUAL "internal") +if (SPM_PROTO_HDRS) install(FILES ${SPM_PROTO_HDRS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) endif() diff --git a/src/builder_test.cc b/src/builder_test.cc index 8b785727e..1114db6aa 100644 --- a/src/builder_test.cc +++ b/src/builder_test.cc @@ -118,9 +118,11 @@ TEST(BuilderTest, CompileCharsMap) { chars_map[{0x3048, 0x304A}] = {}; NormalizerSpec spec; + std::string compiled; EXPECT_TRUE( - Builder::CompileCharsMap(chars_map, spec.mutable_precompiled_charsmap()) + Builder::CompileCharsMap(chars_map, &compiled) .ok()); + spec.set_precompiled_charsmap(compiled); Builder::CharsMap decompiled_chars_map; EXPECT_TRUE(Builder::DecompileCharsMap(spec.precompiled_charsmap(), &decompiled_chars_map) diff --git a/src/builtin_pb/sentencepiece.pb.cc b/src/builtin_pb/sentencepiece.pb.cc deleted file mode 100644 index 7bc549444..000000000 --- a/src/builtin_pb/sentencepiece.pb.cc +++ /dev/null @@ -1,923 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: sentencepiece.proto - -#include "sentencepiece.pb.h" - -#include - -#include -#include -#include -#include -// @@protoc_insertion_point(includes) -#include -extern PROTOBUF_INTERNAL_EXPORT_sentencepiece_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_SentencePieceText_sentencepiece_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_sentencepiece_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SentencePieceText_SentencePiece_sentencepiece_2eproto; -namespace sentencepiece { -class SentencePieceText_SentencePieceDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _SentencePieceText_SentencePiece_default_instance_; -class SentencePieceTextDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _SentencePieceText_default_instance_; -class NBestSentencePieceTextDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _NBestSentencePieceText_default_instance_; -} // namespace sentencepiece -static void InitDefaultsscc_info_NBestSentencePieceText_sentencepiece_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_NBestSentencePieceText_default_instance_; - new (ptr) ::sentencepiece::NBestSentencePieceText(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_NBestSentencePieceText_sentencepiece_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, 0, InitDefaultsscc_info_NBestSentencePieceText_sentencepiece_2eproto}, { - &scc_info_SentencePieceText_sentencepiece_2eproto.base,}}; - -static void InitDefaultsscc_info_SentencePieceText_sentencepiece_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_SentencePieceText_default_instance_; - new (ptr) ::sentencepiece::SentencePieceText(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_SentencePieceText_sentencepiece_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, 0, InitDefaultsscc_info_SentencePieceText_sentencepiece_2eproto}, { - &scc_info_SentencePieceText_SentencePiece_sentencepiece_2eproto.base,}}; - -static void InitDefaultsscc_info_SentencePieceText_SentencePiece_sentencepiece_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_SentencePieceText_SentencePiece_default_instance_; - new (ptr) ::sentencepiece::SentencePieceText_SentencePiece(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SentencePieceText_SentencePiece_sentencepiece_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_SentencePieceText_SentencePiece_sentencepiece_2eproto}, {}}; - -namespace sentencepiece { - -// =================================================================== - -class SentencePieceText_SentencePiece::_Internal { - public: - using HasBits = decltype(std::declval()._has_bits_); - static void set_has_piece(HasBits* has_bits) { - (*has_bits)[0] |= 1u; - } - static void set_has_id(HasBits* has_bits) { - (*has_bits)[0] |= 4u; - } - static void set_has_surface(HasBits* has_bits) { - (*has_bits)[0] |= 2u; - } - static void set_has_begin(HasBits* has_bits) { - (*has_bits)[0] |= 8u; - } - static void set_has_end(HasBits* has_bits) { - (*has_bits)[0] |= 16u; - } -}; - -SentencePieceText_SentencePiece::SentencePieceText_SentencePiece(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - _extensions_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.SentencePieceText.SentencePiece) -} -SentencePieceText_SentencePiece::SentencePieceText_SentencePiece(const SentencePieceText_SentencePiece& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - _has_bits_(from._has_bits_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - _extensions_.MergeFrom(from._extensions_); - piece_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_piece()) { - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_piece(), - GetArena()); - } - surface_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_surface()) { - surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_surface(), - GetArena()); - } - ::memcpy(&id_, &from.id_, - static_cast(reinterpret_cast(&end_) - - reinterpret_cast(&id_)) + sizeof(end_)); - // @@protoc_insertion_point(copy_constructor:sentencepiece.SentencePieceText.SentencePiece) -} - -void SentencePieceText_SentencePiece::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SentencePieceText_SentencePiece_sentencepiece_2eproto.base); - piece_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - surface_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - ::memset(reinterpret_cast(this) + static_cast( - reinterpret_cast(&id_) - reinterpret_cast(this)), - 0, static_cast(reinterpret_cast(&end_) - - reinterpret_cast(&id_)) + sizeof(end_)); -} - -SentencePieceText_SentencePiece::~SentencePieceText_SentencePiece() { - // @@protoc_insertion_point(destructor:sentencepiece.SentencePieceText.SentencePiece) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void SentencePieceText_SentencePiece::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); - piece_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - surface_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); -} - -void SentencePieceText_SentencePiece::ArenaDtor(void* object) { - SentencePieceText_SentencePiece* _this = reinterpret_cast< SentencePieceText_SentencePiece* >(object); - (void)_this; -} -void SentencePieceText_SentencePiece::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void SentencePieceText_SentencePiece::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const SentencePieceText_SentencePiece& SentencePieceText_SentencePiece::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_SentencePieceText_SentencePiece_sentencepiece_2eproto.base); - return *internal_default_instance(); -} - - -void SentencePieceText_SentencePiece::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.SentencePieceText.SentencePiece) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _extensions_.Clear(); - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x00000003u) { - if (cached_has_bits & 0x00000001u) { - piece_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000002u) { - surface_.ClearNonDefaultToEmpty(); - } - } - if (cached_has_bits & 0x0000001cu) { - ::memset(&id_, 0, static_cast( - reinterpret_cast(&end_) - - reinterpret_cast(&id_)) + sizeof(end_)); - } - _has_bits_.Clear(); - _internal_metadata_.Clear(); -} - -const char* SentencePieceText_SentencePiece::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - _Internal::HasBits has_bits{}; - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // optional string piece = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - auto str = _internal_mutable_piece(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional uint32 id = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { - _Internal::set_has_id(&has_bits); - id_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string surface = 3; - case 3: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { - auto str = _internal_mutable_surface(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional uint32 begin = 4; - case 4: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 32)) { - _Internal::set_has_begin(&has_bits); - begin_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional uint32 end = 5; - case 5: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 40)) { - _Internal::set_has_end(&has_bits); - end_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - if ((1600u <= tag)) { - ptr = _extensions_.ParseField(tag, ptr, - internal_default_instance(), &_internal_metadata_, ctx); - CHK_(ptr != nullptr); - continue; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - _has_bits_.Or(has_bits); - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* SentencePieceText_SentencePiece::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.SentencePieceText.SentencePiece) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string piece = 1; - if (cached_has_bits & 0x00000001u) { - target = stream->WriteStringMaybeAliased( - 1, this->_internal_piece(), target); - } - - // optional uint32 id = 2; - if (cached_has_bits & 0x00000004u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt32ToArray(2, this->_internal_id(), target); - } - - // optional string surface = 3; - if (cached_has_bits & 0x00000002u) { - target = stream->WriteStringMaybeAliased( - 3, this->_internal_surface(), target); - } - - // optional uint32 begin = 4; - if (cached_has_bits & 0x00000008u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt32ToArray(4, this->_internal_begin(), target); - } - - // optional uint32 end = 5; - if (cached_has_bits & 0x00000010u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt32ToArray(5, this->_internal_end(), target); - } - - // Extension range [200, 536870912) - target = _extensions_._InternalSerialize( - 200, 536870912, target, stream); - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.SentencePieceText.SentencePiece) - return target; -} - -size_t SentencePieceText_SentencePiece::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.SentencePieceText.SentencePiece) - size_t total_size = 0; - - total_size += _extensions_.ByteSize(); - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x0000001fu) { - // optional string piece = 1; - if (cached_has_bits & 0x00000001u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_piece()); - } - - // optional string surface = 3; - if (cached_has_bits & 0x00000002u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_surface()); - } - - // optional uint32 id = 2; - if (cached_has_bits & 0x00000004u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt32Size( - this->_internal_id()); - } - - // optional uint32 begin = 4; - if (cached_has_bits & 0x00000008u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt32Size( - this->_internal_begin()); - } - - // optional uint32 end = 5; - if (cached_has_bits & 0x00000010u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt32Size( - this->_internal_end()); - } - - } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void SentencePieceText_SentencePiece::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void SentencePieceText_SentencePiece::MergeFrom(const SentencePieceText_SentencePiece& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.SentencePieceText.SentencePiece) - GOOGLE_DCHECK_NE(&from, this); - _extensions_.MergeFrom(from._extensions_); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = from._has_bits_[0]; - if (cached_has_bits & 0x0000001fu) { - if (cached_has_bits & 0x00000001u) { - _internal_set_piece(from._internal_piece()); - } - if (cached_has_bits & 0x00000002u) { - _internal_set_surface(from._internal_surface()); - } - if (cached_has_bits & 0x00000004u) { - id_ = from.id_; - } - if (cached_has_bits & 0x00000008u) { - begin_ = from.begin_; - } - if (cached_has_bits & 0x00000010u) { - end_ = from.end_; - } - _has_bits_[0] |= cached_has_bits; - } -} - -void SentencePieceText_SentencePiece::CopyFrom(const SentencePieceText_SentencePiece& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.SentencePieceText.SentencePiece) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool SentencePieceText_SentencePiece::IsInitialized() const { - if (!_extensions_.IsInitialized()) { - return false; - } - - return true; -} - -void SentencePieceText_SentencePiece::InternalSwap(SentencePieceText_SentencePiece* other) { - using std::swap; - _extensions_.Swap(&other->_extensions_); - _internal_metadata_.Swap(&other->_internal_metadata_); - swap(_has_bits_[0], other->_has_bits_[0]); - piece_.Swap(&other->piece_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - surface_.Swap(&other->surface_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - ::PROTOBUF_NAMESPACE_ID::internal::memswap< - PROTOBUF_FIELD_OFFSET(SentencePieceText_SentencePiece, end_) - + sizeof(SentencePieceText_SentencePiece::end_) - - PROTOBUF_FIELD_OFFSET(SentencePieceText_SentencePiece, id_)>( - reinterpret_cast(&id_), - reinterpret_cast(&other->id_)); -} - -std::string SentencePieceText_SentencePiece::GetTypeName() const { - return "sentencepiece.SentencePieceText.SentencePiece"; -} - - -// =================================================================== - -class SentencePieceText::_Internal { - public: - using HasBits = decltype(std::declval()._has_bits_); - static void set_has_text(HasBits* has_bits) { - (*has_bits)[0] |= 1u; - } - static void set_has_score(HasBits* has_bits) { - (*has_bits)[0] |= 2u; - } -}; - -SentencePieceText::SentencePieceText(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - _extensions_(arena), - pieces_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.SentencePieceText) -} -SentencePieceText::SentencePieceText(const SentencePieceText& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - _has_bits_(from._has_bits_), - pieces_(from.pieces_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - _extensions_.MergeFrom(from._extensions_); - text_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_text()) { - text_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_text(), - GetArena()); - } - score_ = from.score_; - // @@protoc_insertion_point(copy_constructor:sentencepiece.SentencePieceText) -} - -void SentencePieceText::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SentencePieceText_sentencepiece_2eproto.base); - text_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - score_ = 0; -} - -SentencePieceText::~SentencePieceText() { - // @@protoc_insertion_point(destructor:sentencepiece.SentencePieceText) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void SentencePieceText::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); - text_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); -} - -void SentencePieceText::ArenaDtor(void* object) { - SentencePieceText* _this = reinterpret_cast< SentencePieceText* >(object); - (void)_this; -} -void SentencePieceText::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void SentencePieceText::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const SentencePieceText& SentencePieceText::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_SentencePieceText_sentencepiece_2eproto.base); - return *internal_default_instance(); -} - - -void SentencePieceText::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.SentencePieceText) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _extensions_.Clear(); - pieces_.Clear(); - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x00000001u) { - text_.ClearNonDefaultToEmpty(); - } - score_ = 0; - _has_bits_.Clear(); - _internal_metadata_.Clear(); -} - -const char* SentencePieceText::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - _Internal::HasBits has_bits{}; - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // optional string text = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - auto str = _internal_mutable_text(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // repeated .sentencepiece.SentencePieceText.SentencePiece pieces = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { - ptr -= 1; - do { - ptr += 1; - ptr = ctx->ParseMessage(_internal_add_pieces(), ptr); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<18>(ptr)); - } else goto handle_unusual; - continue; - // optional float score = 3; - case 3: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 29)) { - _Internal::set_has_score(&has_bits); - score_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad(ptr); - ptr += sizeof(float); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - if ((1600u <= tag)) { - ptr = _extensions_.ParseField(tag, ptr, - internal_default_instance(), &_internal_metadata_, ctx); - CHK_(ptr != nullptr); - continue; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - _has_bits_.Or(has_bits); - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* SentencePieceText::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.SentencePieceText) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string text = 1; - if (cached_has_bits & 0x00000001u) { - target = stream->WriteStringMaybeAliased( - 1, this->_internal_text(), target); - } - - // repeated .sentencepiece.SentencePieceText.SentencePiece pieces = 2; - for (unsigned int i = 0, - n = static_cast(this->_internal_pieces_size()); i < n; i++) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage(2, this->_internal_pieces(i), target, stream); - } - - // optional float score = 3; - if (cached_has_bits & 0x00000002u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteFloatToArray(3, this->_internal_score(), target); - } - - // Extension range [200, 536870912) - target = _extensions_._InternalSerialize( - 200, 536870912, target, stream); - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.SentencePieceText) - return target; -} - -size_t SentencePieceText::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.SentencePieceText) - size_t total_size = 0; - - total_size += _extensions_.ByteSize(); - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - // repeated .sentencepiece.SentencePieceText.SentencePiece pieces = 2; - total_size += 1UL * this->_internal_pieces_size(); - for (const auto& msg : this->pieces_) { - total_size += - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize(msg); - } - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x00000003u) { - // optional string text = 1; - if (cached_has_bits & 0x00000001u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_text()); - } - - // optional float score = 3; - if (cached_has_bits & 0x00000002u) { - total_size += 1 + 4; - } - - } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void SentencePieceText::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void SentencePieceText::MergeFrom(const SentencePieceText& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.SentencePieceText) - GOOGLE_DCHECK_NE(&from, this); - _extensions_.MergeFrom(from._extensions_); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - pieces_.MergeFrom(from.pieces_); - cached_has_bits = from._has_bits_[0]; - if (cached_has_bits & 0x00000003u) { - if (cached_has_bits & 0x00000001u) { - _internal_set_text(from._internal_text()); - } - if (cached_has_bits & 0x00000002u) { - score_ = from.score_; - } - _has_bits_[0] |= cached_has_bits; - } -} - -void SentencePieceText::CopyFrom(const SentencePieceText& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.SentencePieceText) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool SentencePieceText::IsInitialized() const { - if (!_extensions_.IsInitialized()) { - return false; - } - - if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(pieces_)) return false; - return true; -} - -void SentencePieceText::InternalSwap(SentencePieceText* other) { - using std::swap; - _extensions_.Swap(&other->_extensions_); - _internal_metadata_.Swap(&other->_internal_metadata_); - swap(_has_bits_[0], other->_has_bits_[0]); - pieces_.InternalSwap(&other->pieces_); - text_.Swap(&other->text_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - swap(score_, other->score_); -} - -std::string SentencePieceText::GetTypeName() const { - return "sentencepiece.SentencePieceText"; -} - - -// =================================================================== - -class NBestSentencePieceText::_Internal { - public: -}; - -NBestSentencePieceText::NBestSentencePieceText(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - nbests_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.NBestSentencePieceText) -} -NBestSentencePieceText::NBestSentencePieceText(const NBestSentencePieceText& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - nbests_(from.nbests_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - // @@protoc_insertion_point(copy_constructor:sentencepiece.NBestSentencePieceText) -} - -void NBestSentencePieceText::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_NBestSentencePieceText_sentencepiece_2eproto.base); -} - -NBestSentencePieceText::~NBestSentencePieceText() { - // @@protoc_insertion_point(destructor:sentencepiece.NBestSentencePieceText) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void NBestSentencePieceText::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); -} - -void NBestSentencePieceText::ArenaDtor(void* object) { - NBestSentencePieceText* _this = reinterpret_cast< NBestSentencePieceText* >(object); - (void)_this; -} -void NBestSentencePieceText::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void NBestSentencePieceText::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const NBestSentencePieceText& NBestSentencePieceText::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_NBestSentencePieceText_sentencepiece_2eproto.base); - return *internal_default_instance(); -} - - -void NBestSentencePieceText::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.NBestSentencePieceText) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - nbests_.Clear(); - _internal_metadata_.Clear(); -} - -const char* NBestSentencePieceText::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // repeated .sentencepiece.SentencePieceText nbests = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - ptr -= 1; - do { - ptr += 1; - ptr = ctx->ParseMessage(_internal_add_nbests(), ptr); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<10>(ptr)); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* NBestSentencePieceText::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.NBestSentencePieceText) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .sentencepiece.SentencePieceText nbests = 1; - for (unsigned int i = 0, - n = static_cast(this->_internal_nbests_size()); i < n; i++) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage(1, this->_internal_nbests(i), target, stream); - } - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.NBestSentencePieceText) - return target; -} - -size_t NBestSentencePieceText::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.NBestSentencePieceText) - size_t total_size = 0; - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - // repeated .sentencepiece.SentencePieceText nbests = 1; - total_size += 1UL * this->_internal_nbests_size(); - for (const auto& msg : this->nbests_) { - total_size += - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize(msg); - } - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void NBestSentencePieceText::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void NBestSentencePieceText::MergeFrom(const NBestSentencePieceText& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.NBestSentencePieceText) - GOOGLE_DCHECK_NE(&from, this); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - nbests_.MergeFrom(from.nbests_); -} - -void NBestSentencePieceText::CopyFrom(const NBestSentencePieceText& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.NBestSentencePieceText) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool NBestSentencePieceText::IsInitialized() const { - if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(nbests_)) return false; - return true; -} - -void NBestSentencePieceText::InternalSwap(NBestSentencePieceText* other) { - using std::swap; - _internal_metadata_.Swap(&other->_internal_metadata_); - nbests_.InternalSwap(&other->nbests_); -} - -std::string NBestSentencePieceText::GetTypeName() const { - return "sentencepiece.NBestSentencePieceText"; -} - - -// @@protoc_insertion_point(namespace_scope) -} // namespace sentencepiece -PROTOBUF_NAMESPACE_OPEN -template<> PROTOBUF_NOINLINE ::sentencepiece::SentencePieceText_SentencePiece* Arena::CreateMaybeMessage< ::sentencepiece::SentencePieceText_SentencePiece >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::SentencePieceText_SentencePiece >(arena); -} -template<> PROTOBUF_NOINLINE ::sentencepiece::SentencePieceText* Arena::CreateMaybeMessage< ::sentencepiece::SentencePieceText >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::SentencePieceText >(arena); -} -template<> PROTOBUF_NOINLINE ::sentencepiece::NBestSentencePieceText* Arena::CreateMaybeMessage< ::sentencepiece::NBestSentencePieceText >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::NBestSentencePieceText >(arena); -} -PROTOBUF_NAMESPACE_CLOSE - -// @@protoc_insertion_point(global_scope) -#include diff --git a/src/builtin_pb/sentencepiece.pb.h b/src/builtin_pb/sentencepiece.pb.h deleted file mode 100644 index 24ec3370c..000000000 --- a/src/builtin_pb/sentencepiece.pb.h +++ /dev/null @@ -1,1041 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: sentencepiece.proto - -#ifndef GOOGLE_PROTOBUF_INCLUDED_sentencepiece_2eproto -#define GOOGLE_PROTOBUF_INCLUDED_sentencepiece_2eproto - -#include -#include - -#include -#if PROTOBUF_VERSION < 3014000 -#error This file was generated by a newer version of protoc which is -#error incompatible with your Protocol Buffer headers. Please update -#error your headers. -#endif -#if 3014000 < PROTOBUF_MIN_PROTOC_VERSION -#error This file was generated by an older version of protoc which is -#error incompatible with your Protocol Buffer headers. Please -#error regenerate this file with a newer version of protoc. -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include // IWYU pragma: export -#include // IWYU pragma: export -// @@protoc_insertion_point(includes) -#include -#define PROTOBUF_INTERNAL_EXPORT_sentencepiece_2eproto -PROTOBUF_NAMESPACE_OPEN -namespace internal { -class AnyMetadata; -} // namespace internal -PROTOBUF_NAMESPACE_CLOSE - -// Internal implementation detail -- do not use these members. -struct TableStruct_sentencepiece_2eproto { - static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::AuxiliaryParseTableField aux[] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[3] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; - static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; - static const ::PROTOBUF_NAMESPACE_ID::uint32 offsets[]; -}; -namespace sentencepiece { -class NBestSentencePieceText; -class NBestSentencePieceTextDefaultTypeInternal; -extern NBestSentencePieceTextDefaultTypeInternal _NBestSentencePieceText_default_instance_; -class SentencePieceText; -class SentencePieceTextDefaultTypeInternal; -extern SentencePieceTextDefaultTypeInternal _SentencePieceText_default_instance_; -class SentencePieceText_SentencePiece; -class SentencePieceText_SentencePieceDefaultTypeInternal; -extern SentencePieceText_SentencePieceDefaultTypeInternal _SentencePieceText_SentencePiece_default_instance_; -} // namespace sentencepiece -PROTOBUF_NAMESPACE_OPEN -template<> ::sentencepiece::NBestSentencePieceText* Arena::CreateMaybeMessage<::sentencepiece::NBestSentencePieceText>(Arena*); -template<> ::sentencepiece::SentencePieceText* Arena::CreateMaybeMessage<::sentencepiece::SentencePieceText>(Arena*); -template<> ::sentencepiece::SentencePieceText_SentencePiece* Arena::CreateMaybeMessage<::sentencepiece::SentencePieceText_SentencePiece>(Arena*); -PROTOBUF_NAMESPACE_CLOSE -namespace sentencepiece { - -// =================================================================== - -class SentencePieceText_SentencePiece PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.SentencePieceText.SentencePiece) */ { - public: - inline SentencePieceText_SentencePiece() : SentencePieceText_SentencePiece(nullptr) {} - virtual ~SentencePieceText_SentencePiece(); - - SentencePieceText_SentencePiece(const SentencePieceText_SentencePiece& from); - SentencePieceText_SentencePiece(SentencePieceText_SentencePiece&& from) noexcept - : SentencePieceText_SentencePiece() { - *this = ::std::move(from); - } - - inline SentencePieceText_SentencePiece& operator=(const SentencePieceText_SentencePiece& from) { - CopyFrom(from); - return *this; - } - inline SentencePieceText_SentencePiece& operator=(SentencePieceText_SentencePiece&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const SentencePieceText_SentencePiece& default_instance(); - - static inline const SentencePieceText_SentencePiece* internal_default_instance() { - return reinterpret_cast( - &_SentencePieceText_SentencePiece_default_instance_); - } - static constexpr int kIndexInFileMessages = - 0; - - friend void swap(SentencePieceText_SentencePiece& a, SentencePieceText_SentencePiece& b) { - a.Swap(&b); - } - inline void Swap(SentencePieceText_SentencePiece* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(SentencePieceText_SentencePiece* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline SentencePieceText_SentencePiece* New() const final { - return CreateMaybeMessage(nullptr); - } - - SentencePieceText_SentencePiece* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const SentencePieceText_SentencePiece& from); - void MergeFrom(const SentencePieceText_SentencePiece& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(SentencePieceText_SentencePiece* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.SentencePieceText.SentencePiece"; - } - protected: - explicit SentencePieceText_SentencePiece(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - enum : int { - kPieceFieldNumber = 1, - kSurfaceFieldNumber = 3, - kIdFieldNumber = 2, - kBeginFieldNumber = 4, - kEndFieldNumber = 5, - }; - // optional string piece = 1; - bool has_piece() const; - private: - bool _internal_has_piece() const; - public: - void clear_piece(); - const std::string& piece() const; - void set_piece(const std::string& value); - void set_piece(std::string&& value); - void set_piece(const char* value); - void set_piece(const char* value, size_t size); - std::string* mutable_piece(); - std::string* release_piece(); - void set_allocated_piece(std::string* piece); - private: - const std::string& _internal_piece() const; - void _internal_set_piece(const std::string& value); - std::string* _internal_mutable_piece(); - public: - - // optional string surface = 3; - bool has_surface() const; - private: - bool _internal_has_surface() const; - public: - void clear_surface(); - const std::string& surface() const; - void set_surface(const std::string& value); - void set_surface(std::string&& value); - void set_surface(const char* value); - void set_surface(const char* value, size_t size); - std::string* mutable_surface(); - std::string* release_surface(); - void set_allocated_surface(std::string* surface); - private: - const std::string& _internal_surface() const; - void _internal_set_surface(const std::string& value); - std::string* _internal_mutable_surface(); - public: - - // optional uint32 id = 2; - bool has_id() const; - private: - bool _internal_has_id() const; - public: - void clear_id(); - ::PROTOBUF_NAMESPACE_ID::uint32 id() const; - void set_id(::PROTOBUF_NAMESPACE_ID::uint32 value); - private: - ::PROTOBUF_NAMESPACE_ID::uint32 _internal_id() const; - void _internal_set_id(::PROTOBUF_NAMESPACE_ID::uint32 value); - public: - - // optional uint32 begin = 4; - bool has_begin() const; - private: - bool _internal_has_begin() const; - public: - void clear_begin(); - ::PROTOBUF_NAMESPACE_ID::uint32 begin() const; - void set_begin(::PROTOBUF_NAMESPACE_ID::uint32 value); - private: - ::PROTOBUF_NAMESPACE_ID::uint32 _internal_begin() const; - void _internal_set_begin(::PROTOBUF_NAMESPACE_ID::uint32 value); - public: - - // optional uint32 end = 5; - bool has_end() const; - private: - bool _internal_has_end() const; - public: - void clear_end(); - ::PROTOBUF_NAMESPACE_ID::uint32 end() const; - void set_end(::PROTOBUF_NAMESPACE_ID::uint32 value); - private: - ::PROTOBUF_NAMESPACE_ID::uint32 _internal_end() const; - void _internal_set_end(::PROTOBUF_NAMESPACE_ID::uint32 value); - public: - - GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(SentencePieceText_SentencePiece) - // @@protoc_insertion_point(class_scope:sentencepiece.SentencePieceText.SentencePiece) - private: - class _Internal; - - ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr piece_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr surface_; - ::PROTOBUF_NAMESPACE_ID::uint32 id_; - ::PROTOBUF_NAMESPACE_ID::uint32 begin_; - ::PROTOBUF_NAMESPACE_ID::uint32 end_; - friend struct ::TableStruct_sentencepiece_2eproto; -}; -// ------------------------------------------------------------------- - -class SentencePieceText PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.SentencePieceText) */ { - public: - inline SentencePieceText() : SentencePieceText(nullptr) {} - virtual ~SentencePieceText(); - - SentencePieceText(const SentencePieceText& from); - SentencePieceText(SentencePieceText&& from) noexcept - : SentencePieceText() { - *this = ::std::move(from); - } - - inline SentencePieceText& operator=(const SentencePieceText& from) { - CopyFrom(from); - return *this; - } - inline SentencePieceText& operator=(SentencePieceText&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const SentencePieceText& default_instance(); - - static inline const SentencePieceText* internal_default_instance() { - return reinterpret_cast( - &_SentencePieceText_default_instance_); - } - static constexpr int kIndexInFileMessages = - 1; - - friend void swap(SentencePieceText& a, SentencePieceText& b) { - a.Swap(&b); - } - inline void Swap(SentencePieceText* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(SentencePieceText* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline SentencePieceText* New() const final { - return CreateMaybeMessage(nullptr); - } - - SentencePieceText* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const SentencePieceText& from); - void MergeFrom(const SentencePieceText& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(SentencePieceText* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.SentencePieceText"; - } - protected: - explicit SentencePieceText(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - typedef SentencePieceText_SentencePiece SentencePiece; - - // accessors ------------------------------------------------------- - - enum : int { - kPiecesFieldNumber = 2, - kTextFieldNumber = 1, - kScoreFieldNumber = 3, - }; - // repeated .sentencepiece.SentencePieceText.SentencePiece pieces = 2; - int pieces_size() const; - private: - int _internal_pieces_size() const; - public: - void clear_pieces(); - ::sentencepiece::SentencePieceText_SentencePiece* mutable_pieces(int index); - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText_SentencePiece >* - mutable_pieces(); - private: - const ::sentencepiece::SentencePieceText_SentencePiece& _internal_pieces(int index) const; - ::sentencepiece::SentencePieceText_SentencePiece* _internal_add_pieces(); - public: - const ::sentencepiece::SentencePieceText_SentencePiece& pieces(int index) const; - ::sentencepiece::SentencePieceText_SentencePiece* add_pieces(); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText_SentencePiece >& - pieces() const; - - // optional string text = 1; - bool has_text() const; - private: - bool _internal_has_text() const; - public: - void clear_text(); - const std::string& text() const; - void set_text(const std::string& value); - void set_text(std::string&& value); - void set_text(const char* value); - void set_text(const char* value, size_t size); - std::string* mutable_text(); - std::string* release_text(); - void set_allocated_text(std::string* text); - private: - const std::string& _internal_text() const; - void _internal_set_text(const std::string& value); - std::string* _internal_mutable_text(); - public: - - // optional float score = 3; - bool has_score() const; - private: - bool _internal_has_score() const; - public: - void clear_score(); - float score() const; - void set_score(float value); - private: - float _internal_score() const; - void _internal_set_score(float value); - public: - - GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(SentencePieceText) - // @@protoc_insertion_point(class_scope:sentencepiece.SentencePieceText) - private: - class _Internal; - - ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText_SentencePiece > pieces_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr text_; - float score_; - friend struct ::TableStruct_sentencepiece_2eproto; -}; -// ------------------------------------------------------------------- - -class NBestSentencePieceText PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.NBestSentencePieceText) */ { - public: - inline NBestSentencePieceText() : NBestSentencePieceText(nullptr) {} - virtual ~NBestSentencePieceText(); - - NBestSentencePieceText(const NBestSentencePieceText& from); - NBestSentencePieceText(NBestSentencePieceText&& from) noexcept - : NBestSentencePieceText() { - *this = ::std::move(from); - } - - inline NBestSentencePieceText& operator=(const NBestSentencePieceText& from) { - CopyFrom(from); - return *this; - } - inline NBestSentencePieceText& operator=(NBestSentencePieceText&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const NBestSentencePieceText& default_instance(); - - static inline const NBestSentencePieceText* internal_default_instance() { - return reinterpret_cast( - &_NBestSentencePieceText_default_instance_); - } - static constexpr int kIndexInFileMessages = - 2; - - friend void swap(NBestSentencePieceText& a, NBestSentencePieceText& b) { - a.Swap(&b); - } - inline void Swap(NBestSentencePieceText* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(NBestSentencePieceText* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline NBestSentencePieceText* New() const final { - return CreateMaybeMessage(nullptr); - } - - NBestSentencePieceText* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const NBestSentencePieceText& from); - void MergeFrom(const NBestSentencePieceText& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(NBestSentencePieceText* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.NBestSentencePieceText"; - } - protected: - explicit NBestSentencePieceText(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - enum : int { - kNbestsFieldNumber = 1, - }; - // repeated .sentencepiece.SentencePieceText nbests = 1; - int nbests_size() const; - private: - int _internal_nbests_size() const; - public: - void clear_nbests(); - ::sentencepiece::SentencePieceText* mutable_nbests(int index); - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText >* - mutable_nbests(); - private: - const ::sentencepiece::SentencePieceText& _internal_nbests(int index) const; - ::sentencepiece::SentencePieceText* _internal_add_nbests(); - public: - const ::sentencepiece::SentencePieceText& nbests(int index) const; - ::sentencepiece::SentencePieceText* add_nbests(); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText >& - nbests() const; - - // @@protoc_insertion_point(class_scope:sentencepiece.NBestSentencePieceText) - private: - class _Internal; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText > nbests_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - friend struct ::TableStruct_sentencepiece_2eproto; -}; -// =================================================================== - - -// =================================================================== - -#ifdef __GNUC__ - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wstrict-aliasing" -#endif // __GNUC__ -// SentencePieceText_SentencePiece - -// optional string piece = 1; -inline bool SentencePieceText_SentencePiece::_internal_has_piece() const { - bool value = (_has_bits_[0] & 0x00000001u) != 0; - return value; -} -inline bool SentencePieceText_SentencePiece::has_piece() const { - return _internal_has_piece(); -} -inline void SentencePieceText_SentencePiece::clear_piece() { - piece_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000001u; -} -inline const std::string& SentencePieceText_SentencePiece::piece() const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.SentencePiece.piece) - return _internal_piece(); -} -inline void SentencePieceText_SentencePiece::set_piece(const std::string& value) { - _internal_set_piece(value); - // @@protoc_insertion_point(field_set:sentencepiece.SentencePieceText.SentencePiece.piece) -} -inline std::string* SentencePieceText_SentencePiece::mutable_piece() { - // @@protoc_insertion_point(field_mutable:sentencepiece.SentencePieceText.SentencePiece.piece) - return _internal_mutable_piece(); -} -inline const std::string& SentencePieceText_SentencePiece::_internal_piece() const { - return piece_.Get(); -} -inline void SentencePieceText_SentencePiece::_internal_set_piece(const std::string& value) { - _has_bits_[0] |= 0x00000001u; - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void SentencePieceText_SentencePiece::set_piece(std::string&& value) { - _has_bits_[0] |= 0x00000001u; - piece_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.SentencePieceText.SentencePiece.piece) -} -inline void SentencePieceText_SentencePiece::set_piece(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000001u; - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.SentencePieceText.SentencePiece.piece) -} -inline void SentencePieceText_SentencePiece::set_piece(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000001u; - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.SentencePieceText.SentencePiece.piece) -} -inline std::string* SentencePieceText_SentencePiece::_internal_mutable_piece() { - _has_bits_[0] |= 0x00000001u; - return piece_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* SentencePieceText_SentencePiece::release_piece() { - // @@protoc_insertion_point(field_release:sentencepiece.SentencePieceText.SentencePiece.piece) - if (!_internal_has_piece()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000001u; - return piece_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void SentencePieceText_SentencePiece::set_allocated_piece(std::string* piece) { - if (piece != nullptr) { - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - piece_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), piece, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.SentencePieceText.SentencePiece.piece) -} - -// optional uint32 id = 2; -inline bool SentencePieceText_SentencePiece::_internal_has_id() const { - bool value = (_has_bits_[0] & 0x00000004u) != 0; - return value; -} -inline bool SentencePieceText_SentencePiece::has_id() const { - return _internal_has_id(); -} -inline void SentencePieceText_SentencePiece::clear_id() { - id_ = 0u; - _has_bits_[0] &= ~0x00000004u; -} -inline ::PROTOBUF_NAMESPACE_ID::uint32 SentencePieceText_SentencePiece::_internal_id() const { - return id_; -} -inline ::PROTOBUF_NAMESPACE_ID::uint32 SentencePieceText_SentencePiece::id() const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.SentencePiece.id) - return _internal_id(); -} -inline void SentencePieceText_SentencePiece::_internal_set_id(::PROTOBUF_NAMESPACE_ID::uint32 value) { - _has_bits_[0] |= 0x00000004u; - id_ = value; -} -inline void SentencePieceText_SentencePiece::set_id(::PROTOBUF_NAMESPACE_ID::uint32 value) { - _internal_set_id(value); - // @@protoc_insertion_point(field_set:sentencepiece.SentencePieceText.SentencePiece.id) -} - -// optional string surface = 3; -inline bool SentencePieceText_SentencePiece::_internal_has_surface() const { - bool value = (_has_bits_[0] & 0x00000002u) != 0; - return value; -} -inline bool SentencePieceText_SentencePiece::has_surface() const { - return _internal_has_surface(); -} -inline void SentencePieceText_SentencePiece::clear_surface() { - surface_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000002u; -} -inline const std::string& SentencePieceText_SentencePiece::surface() const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.SentencePiece.surface) - return _internal_surface(); -} -inline void SentencePieceText_SentencePiece::set_surface(const std::string& value) { - _internal_set_surface(value); - // @@protoc_insertion_point(field_set:sentencepiece.SentencePieceText.SentencePiece.surface) -} -inline std::string* SentencePieceText_SentencePiece::mutable_surface() { - // @@protoc_insertion_point(field_mutable:sentencepiece.SentencePieceText.SentencePiece.surface) - return _internal_mutable_surface(); -} -inline const std::string& SentencePieceText_SentencePiece::_internal_surface() const { - return surface_.Get(); -} -inline void SentencePieceText_SentencePiece::_internal_set_surface(const std::string& value) { - _has_bits_[0] |= 0x00000002u; - surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void SentencePieceText_SentencePiece::set_surface(std::string&& value) { - _has_bits_[0] |= 0x00000002u; - surface_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.SentencePieceText.SentencePiece.surface) -} -inline void SentencePieceText_SentencePiece::set_surface(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000002u; - surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.SentencePieceText.SentencePiece.surface) -} -inline void SentencePieceText_SentencePiece::set_surface(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000002u; - surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.SentencePieceText.SentencePiece.surface) -} -inline std::string* SentencePieceText_SentencePiece::_internal_mutable_surface() { - _has_bits_[0] |= 0x00000002u; - return surface_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* SentencePieceText_SentencePiece::release_surface() { - // @@protoc_insertion_point(field_release:sentencepiece.SentencePieceText.SentencePiece.surface) - if (!_internal_has_surface()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000002u; - return surface_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void SentencePieceText_SentencePiece::set_allocated_surface(std::string* surface) { - if (surface != nullptr) { - _has_bits_[0] |= 0x00000002u; - } else { - _has_bits_[0] &= ~0x00000002u; - } - surface_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), surface, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.SentencePieceText.SentencePiece.surface) -} - -// optional uint32 begin = 4; -inline bool SentencePieceText_SentencePiece::_internal_has_begin() const { - bool value = (_has_bits_[0] & 0x00000008u) != 0; - return value; -} -inline bool SentencePieceText_SentencePiece::has_begin() const { - return _internal_has_begin(); -} -inline void SentencePieceText_SentencePiece::clear_begin() { - begin_ = 0u; - _has_bits_[0] &= ~0x00000008u; -} -inline ::PROTOBUF_NAMESPACE_ID::uint32 SentencePieceText_SentencePiece::_internal_begin() const { - return begin_; -} -inline ::PROTOBUF_NAMESPACE_ID::uint32 SentencePieceText_SentencePiece::begin() const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.SentencePiece.begin) - return _internal_begin(); -} -inline void SentencePieceText_SentencePiece::_internal_set_begin(::PROTOBUF_NAMESPACE_ID::uint32 value) { - _has_bits_[0] |= 0x00000008u; - begin_ = value; -} -inline void SentencePieceText_SentencePiece::set_begin(::PROTOBUF_NAMESPACE_ID::uint32 value) { - _internal_set_begin(value); - // @@protoc_insertion_point(field_set:sentencepiece.SentencePieceText.SentencePiece.begin) -} - -// optional uint32 end = 5; -inline bool SentencePieceText_SentencePiece::_internal_has_end() const { - bool value = (_has_bits_[0] & 0x00000010u) != 0; - return value; -} -inline bool SentencePieceText_SentencePiece::has_end() const { - return _internal_has_end(); -} -inline void SentencePieceText_SentencePiece::clear_end() { - end_ = 0u; - _has_bits_[0] &= ~0x00000010u; -} -inline ::PROTOBUF_NAMESPACE_ID::uint32 SentencePieceText_SentencePiece::_internal_end() const { - return end_; -} -inline ::PROTOBUF_NAMESPACE_ID::uint32 SentencePieceText_SentencePiece::end() const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.SentencePiece.end) - return _internal_end(); -} -inline void SentencePieceText_SentencePiece::_internal_set_end(::PROTOBUF_NAMESPACE_ID::uint32 value) { - _has_bits_[0] |= 0x00000010u; - end_ = value; -} -inline void SentencePieceText_SentencePiece::set_end(::PROTOBUF_NAMESPACE_ID::uint32 value) { - _internal_set_end(value); - // @@protoc_insertion_point(field_set:sentencepiece.SentencePieceText.SentencePiece.end) -} - -// ------------------------------------------------------------------- - -// SentencePieceText - -// optional string text = 1; -inline bool SentencePieceText::_internal_has_text() const { - bool value = (_has_bits_[0] & 0x00000001u) != 0; - return value; -} -inline bool SentencePieceText::has_text() const { - return _internal_has_text(); -} -inline void SentencePieceText::clear_text() { - text_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000001u; -} -inline const std::string& SentencePieceText::text() const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.text) - return _internal_text(); -} -inline void SentencePieceText::set_text(const std::string& value) { - _internal_set_text(value); - // @@protoc_insertion_point(field_set:sentencepiece.SentencePieceText.text) -} -inline std::string* SentencePieceText::mutable_text() { - // @@protoc_insertion_point(field_mutable:sentencepiece.SentencePieceText.text) - return _internal_mutable_text(); -} -inline const std::string& SentencePieceText::_internal_text() const { - return text_.Get(); -} -inline void SentencePieceText::_internal_set_text(const std::string& value) { - _has_bits_[0] |= 0x00000001u; - text_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void SentencePieceText::set_text(std::string&& value) { - _has_bits_[0] |= 0x00000001u; - text_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.SentencePieceText.text) -} -inline void SentencePieceText::set_text(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000001u; - text_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.SentencePieceText.text) -} -inline void SentencePieceText::set_text(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000001u; - text_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.SentencePieceText.text) -} -inline std::string* SentencePieceText::_internal_mutable_text() { - _has_bits_[0] |= 0x00000001u; - return text_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* SentencePieceText::release_text() { - // @@protoc_insertion_point(field_release:sentencepiece.SentencePieceText.text) - if (!_internal_has_text()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000001u; - return text_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void SentencePieceText::set_allocated_text(std::string* text) { - if (text != nullptr) { - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - text_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), text, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.SentencePieceText.text) -} - -// repeated .sentencepiece.SentencePieceText.SentencePiece pieces = 2; -inline int SentencePieceText::_internal_pieces_size() const { - return pieces_.size(); -} -inline int SentencePieceText::pieces_size() const { - return _internal_pieces_size(); -} -inline void SentencePieceText::clear_pieces() { - pieces_.Clear(); -} -inline ::sentencepiece::SentencePieceText_SentencePiece* SentencePieceText::mutable_pieces(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.SentencePieceText.pieces) - return pieces_.Mutable(index); -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText_SentencePiece >* -SentencePieceText::mutable_pieces() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.SentencePieceText.pieces) - return &pieces_; -} -inline const ::sentencepiece::SentencePieceText_SentencePiece& SentencePieceText::_internal_pieces(int index) const { - return pieces_.Get(index); -} -inline const ::sentencepiece::SentencePieceText_SentencePiece& SentencePieceText::pieces(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.pieces) - return _internal_pieces(index); -} -inline ::sentencepiece::SentencePieceText_SentencePiece* SentencePieceText::_internal_add_pieces() { - return pieces_.Add(); -} -inline ::sentencepiece::SentencePieceText_SentencePiece* SentencePieceText::add_pieces() { - // @@protoc_insertion_point(field_add:sentencepiece.SentencePieceText.pieces) - return _internal_add_pieces(); -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText_SentencePiece >& -SentencePieceText::pieces() const { - // @@protoc_insertion_point(field_list:sentencepiece.SentencePieceText.pieces) - return pieces_; -} - -// optional float score = 3; -inline bool SentencePieceText::_internal_has_score() const { - bool value = (_has_bits_[0] & 0x00000002u) != 0; - return value; -} -inline bool SentencePieceText::has_score() const { - return _internal_has_score(); -} -inline void SentencePieceText::clear_score() { - score_ = 0; - _has_bits_[0] &= ~0x00000002u; -} -inline float SentencePieceText::_internal_score() const { - return score_; -} -inline float SentencePieceText::score() const { - // @@protoc_insertion_point(field_get:sentencepiece.SentencePieceText.score) - return _internal_score(); -} -inline void SentencePieceText::_internal_set_score(float value) { - _has_bits_[0] |= 0x00000002u; - score_ = value; -} -inline void SentencePieceText::set_score(float value) { - _internal_set_score(value); - // @@protoc_insertion_point(field_set:sentencepiece.SentencePieceText.score) -} - -// ------------------------------------------------------------------- - -// NBestSentencePieceText - -// repeated .sentencepiece.SentencePieceText nbests = 1; -inline int NBestSentencePieceText::_internal_nbests_size() const { - return nbests_.size(); -} -inline int NBestSentencePieceText::nbests_size() const { - return _internal_nbests_size(); -} -inline void NBestSentencePieceText::clear_nbests() { - nbests_.Clear(); -} -inline ::sentencepiece::SentencePieceText* NBestSentencePieceText::mutable_nbests(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.NBestSentencePieceText.nbests) - return nbests_.Mutable(index); -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText >* -NBestSentencePieceText::mutable_nbests() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.NBestSentencePieceText.nbests) - return &nbests_; -} -inline const ::sentencepiece::SentencePieceText& NBestSentencePieceText::_internal_nbests(int index) const { - return nbests_.Get(index); -} -inline const ::sentencepiece::SentencePieceText& NBestSentencePieceText::nbests(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.NBestSentencePieceText.nbests) - return _internal_nbests(index); -} -inline ::sentencepiece::SentencePieceText* NBestSentencePieceText::_internal_add_nbests() { - return nbests_.Add(); -} -inline ::sentencepiece::SentencePieceText* NBestSentencePieceText::add_nbests() { - // @@protoc_insertion_point(field_add:sentencepiece.NBestSentencePieceText.nbests) - return _internal_add_nbests(); -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SentencePieceText >& -NBestSentencePieceText::nbests() const { - // @@protoc_insertion_point(field_list:sentencepiece.NBestSentencePieceText.nbests) - return nbests_; -} - -#ifdef __GNUC__ - #pragma GCC diagnostic pop -#endif // __GNUC__ -// ------------------------------------------------------------------- - -// ------------------------------------------------------------------- - - -// @@protoc_insertion_point(namespace_scope) - -} // namespace sentencepiece - -// @@protoc_insertion_point(global_scope) - -#include -#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_sentencepiece_2eproto diff --git a/src/builtin_pb/sentencepiece_model.pb.cc b/src/builtin_pb/sentencepiece_model.pb.cc deleted file mode 100644 index 2951889db..000000000 --- a/src/builtin_pb/sentencepiece_model.pb.cc +++ /dev/null @@ -1,3499 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: sentencepiece_model.proto - -#include "sentencepiece_model.pb.h" - -#include - -#include -#include -#include -#include -// @@protoc_insertion_point(includes) -#include -extern PROTOBUF_INTERNAL_EXPORT_sentencepiece_5fmodel_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ModelProto_SentencePiece_sentencepiece_5fmodel_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_sentencepiece_5fmodel_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_NormalizerSpec_sentencepiece_5fmodel_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_sentencepiece_5fmodel_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_SelfTestData_sentencepiece_5fmodel_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_sentencepiece_5fmodel_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SelfTestData_Sample_sentencepiece_5fmodel_2eproto; -extern PROTOBUF_INTERNAL_EXPORT_sentencepiece_5fmodel_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_TrainerSpec_sentencepiece_5fmodel_2eproto; -namespace sentencepiece { -class TrainerSpecDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _TrainerSpec_default_instance_; -class NormalizerSpecDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _NormalizerSpec_default_instance_; -class SelfTestData_SampleDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _SelfTestData_Sample_default_instance_; -class SelfTestDataDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _SelfTestData_default_instance_; -class ModelProto_SentencePieceDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _ModelProto_SentencePiece_default_instance_; -class ModelProtoDefaultTypeInternal { - public: - ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; -} _ModelProto_default_instance_; -} // namespace sentencepiece -static void InitDefaultsscc_info_ModelProto_sentencepiece_5fmodel_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_ModelProto_default_instance_; - new (ptr) ::sentencepiece::ModelProto(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<4> scc_info_ModelProto_sentencepiece_5fmodel_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 4, 0, InitDefaultsscc_info_ModelProto_sentencepiece_5fmodel_2eproto}, { - &scc_info_ModelProto_SentencePiece_sentencepiece_5fmodel_2eproto.base, - &scc_info_TrainerSpec_sentencepiece_5fmodel_2eproto.base, - &scc_info_NormalizerSpec_sentencepiece_5fmodel_2eproto.base, - &scc_info_SelfTestData_sentencepiece_5fmodel_2eproto.base,}}; - -static void InitDefaultsscc_info_ModelProto_SentencePiece_sentencepiece_5fmodel_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_ModelProto_SentencePiece_default_instance_; - new (ptr) ::sentencepiece::ModelProto_SentencePiece(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ModelProto_SentencePiece_sentencepiece_5fmodel_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_ModelProto_SentencePiece_sentencepiece_5fmodel_2eproto}, {}}; - -static void InitDefaultsscc_info_NormalizerSpec_sentencepiece_5fmodel_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_NormalizerSpec_default_instance_; - new (ptr) ::sentencepiece::NormalizerSpec(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_NormalizerSpec_sentencepiece_5fmodel_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_NormalizerSpec_sentencepiece_5fmodel_2eproto}, {}}; - -static void InitDefaultsscc_info_SelfTestData_sentencepiece_5fmodel_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_SelfTestData_default_instance_; - new (ptr) ::sentencepiece::SelfTestData(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<1> scc_info_SelfTestData_sentencepiece_5fmodel_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 1, 0, InitDefaultsscc_info_SelfTestData_sentencepiece_5fmodel_2eproto}, { - &scc_info_SelfTestData_Sample_sentencepiece_5fmodel_2eproto.base,}}; - -static void InitDefaultsscc_info_SelfTestData_Sample_sentencepiece_5fmodel_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_SelfTestData_Sample_default_instance_; - new (ptr) ::sentencepiece::SelfTestData_Sample(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_SelfTestData_Sample_sentencepiece_5fmodel_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_SelfTestData_Sample_sentencepiece_5fmodel_2eproto}, {}}; - -static void InitDefaultsscc_info_TrainerSpec_sentencepiece_5fmodel_2eproto() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - { - void* ptr = &::sentencepiece::_TrainerSpec_default_instance_; - new (ptr) ::sentencepiece::TrainerSpec(); - ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); - } -} - -::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_TrainerSpec_sentencepiece_5fmodel_2eproto = - {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_TrainerSpec_sentencepiece_5fmodel_2eproto}, {}}; - -namespace sentencepiece { -bool TrainerSpec_ModelType_IsValid(int value) { - switch (value) { - case 1: - case 2: - case 3: - case 4: - return true; - default: - return false; - } -} - -static ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed TrainerSpec_ModelType_strings[4] = {}; - -static const char TrainerSpec_ModelType_names[] = - "BPE" - "CHAR" - "UNIGRAM" - "WORD"; - -static const ::PROTOBUF_NAMESPACE_ID::internal::EnumEntry TrainerSpec_ModelType_entries[] = { - { {TrainerSpec_ModelType_names + 0, 3}, 2 }, - { {TrainerSpec_ModelType_names + 3, 4}, 4 }, - { {TrainerSpec_ModelType_names + 7, 7}, 1 }, - { {TrainerSpec_ModelType_names + 14, 4}, 3 }, -}; - -static const int TrainerSpec_ModelType_entries_by_number[] = { - 2, // 1 -> UNIGRAM - 0, // 2 -> BPE - 3, // 3 -> WORD - 1, // 4 -> CHAR -}; - -const std::string& TrainerSpec_ModelType_Name( - TrainerSpec_ModelType value) { - static const bool dummy = - ::PROTOBUF_NAMESPACE_ID::internal::InitializeEnumStrings( - TrainerSpec_ModelType_entries, - TrainerSpec_ModelType_entries_by_number, - 4, TrainerSpec_ModelType_strings); - (void) dummy; - int idx = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumName( - TrainerSpec_ModelType_entries, - TrainerSpec_ModelType_entries_by_number, - 4, value); - return idx == -1 ? ::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString() : - TrainerSpec_ModelType_strings[idx].get(); -} -bool TrainerSpec_ModelType_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, TrainerSpec_ModelType* value) { - int int_value; - bool success = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumValue( - TrainerSpec_ModelType_entries, 4, name, &int_value); - if (success) { - *value = static_cast(int_value); - } - return success; -} -#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) -constexpr TrainerSpec_ModelType TrainerSpec::UNIGRAM; -constexpr TrainerSpec_ModelType TrainerSpec::BPE; -constexpr TrainerSpec_ModelType TrainerSpec::WORD; -constexpr TrainerSpec_ModelType TrainerSpec::CHAR; -constexpr TrainerSpec_ModelType TrainerSpec::ModelType_MIN; -constexpr TrainerSpec_ModelType TrainerSpec::ModelType_MAX; -constexpr int TrainerSpec::ModelType_ARRAYSIZE; -#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) -bool ModelProto_SentencePiece_Type_IsValid(int value) { - switch (value) { - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - return true; - default: - return false; - } -} - -static ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed ModelProto_SentencePiece_Type_strings[6] = {}; - -static const char ModelProto_SentencePiece_Type_names[] = - "BYTE" - "CONTROL" - "NORMAL" - "UNKNOWN" - "UNUSED" - "USER_DEFINED"; - -static const ::PROTOBUF_NAMESPACE_ID::internal::EnumEntry ModelProto_SentencePiece_Type_entries[] = { - { {ModelProto_SentencePiece_Type_names + 0, 4}, 6 }, - { {ModelProto_SentencePiece_Type_names + 4, 7}, 3 }, - { {ModelProto_SentencePiece_Type_names + 11, 6}, 1 }, - { {ModelProto_SentencePiece_Type_names + 17, 7}, 2 }, - { {ModelProto_SentencePiece_Type_names + 24, 6}, 5 }, - { {ModelProto_SentencePiece_Type_names + 30, 12}, 4 }, -}; - -static const int ModelProto_SentencePiece_Type_entries_by_number[] = { - 2, // 1 -> NORMAL - 3, // 2 -> UNKNOWN - 1, // 3 -> CONTROL - 5, // 4 -> USER_DEFINED - 4, // 5 -> UNUSED - 0, // 6 -> BYTE -}; - -const std::string& ModelProto_SentencePiece_Type_Name( - ModelProto_SentencePiece_Type value) { - static const bool dummy = - ::PROTOBUF_NAMESPACE_ID::internal::InitializeEnumStrings( - ModelProto_SentencePiece_Type_entries, - ModelProto_SentencePiece_Type_entries_by_number, - 6, ModelProto_SentencePiece_Type_strings); - (void) dummy; - int idx = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumName( - ModelProto_SentencePiece_Type_entries, - ModelProto_SentencePiece_Type_entries_by_number, - 6, value); - return idx == -1 ? ::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString() : - ModelProto_SentencePiece_Type_strings[idx].get(); -} -bool ModelProto_SentencePiece_Type_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, ModelProto_SentencePiece_Type* value) { - int int_value; - bool success = ::PROTOBUF_NAMESPACE_ID::internal::LookUpEnumValue( - ModelProto_SentencePiece_Type_entries, 6, name, &int_value); - if (success) { - *value = static_cast(int_value); - } - return success; -} -#if (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::NORMAL; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::UNKNOWN; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::CONTROL; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::USER_DEFINED; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::BYTE; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::UNUSED; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::Type_MIN; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece::Type_MAX; -constexpr int ModelProto_SentencePiece::Type_ARRAYSIZE; -#endif // (__cplusplus < 201703) && (!defined(_MSC_VER) || _MSC_VER >= 1900) - -// =================================================================== - -class TrainerSpec::_Internal { - public: - using HasBits = decltype(std::declval()._has_bits_); - static void set_has_input_format(HasBits* has_bits) { - (*has_bits)[0] |= 2u; - } - static void set_has_model_prefix(HasBits* has_bits) { - (*has_bits)[0] |= 1u; - } - static void set_has_model_type(HasBits* has_bits) { - (*has_bits)[0] |= 16777216u; - } - static void set_has_vocab_size(HasBits* has_bits) { - (*has_bits)[0] |= 33554432u; - } - static void set_has_self_test_sample_size(HasBits* has_bits) { - (*has_bits)[0] |= 1024u; - } - static void set_has_enable_differential_privacy(HasBits* has_bits) { - (*has_bits)[0] |= 16384u; - } - static void set_has_differential_privacy_noise_level(HasBits* has_bits) { - (*has_bits)[0] |= 4194304u; - } - static void set_has_differential_privacy_clipping_threshold(HasBits* has_bits) { - (*has_bits)[0] |= 8388608u; - } - static void set_has_character_coverage(HasBits* has_bits) { - (*has_bits)[0] |= 67108864u; - } - static void set_has_input_sentence_size(HasBits* has_bits) { - (*has_bits)[0] |= 4096u; - } - static void set_has_shuffle_input_sentence(HasBits* has_bits) { - (*has_bits)[1] |= 2u; - } - static void set_has_mining_sentence_size(HasBits* has_bits) { - (*has_bits)[0] |= 2048u; - } - static void set_has_training_sentence_size(HasBits* has_bits) { - (*has_bits)[0] |= 8192u; - } - static void set_has_seed_sentencepiece_size(HasBits* has_bits) { - (*has_bits)[0] |= 134217728u; - } - static void set_has_shrinking_factor(HasBits* has_bits) { - (*has_bits)[0] |= 268435456u; - } - static void set_has_max_sentence_length(HasBits* has_bits) { - (*has_bits)[0] |= 2147483648u; - } - static void set_has_num_threads(HasBits* has_bits) { - (*has_bits)[0] |= 536870912u; - } - static void set_has_num_sub_iterations(HasBits* has_bits) { - (*has_bits)[0] |= 1073741824u; - } - static void set_has_max_sentencepiece_length(HasBits* has_bits) { - (*has_bits)[1] |= 1u; - } - static void set_has_split_by_unicode_script(HasBits* has_bits) { - (*has_bits)[1] |= 4u; - } - static void set_has_split_by_number(HasBits* has_bits) { - (*has_bits)[1] |= 8u; - } - static void set_has_split_by_whitespace(HasBits* has_bits) { - (*has_bits)[1] |= 16u; - } - static void set_has_treat_whitespace_as_suffix(HasBits* has_bits) { - (*has_bits)[0] |= 32768u; - } - static void set_has_allow_whitespace_only_pieces(HasBits* has_bits) { - (*has_bits)[0] |= 65536u; - } - static void set_has_split_digits(HasBits* has_bits) { - (*has_bits)[0] |= 131072u; - } - static void set_has_pretokenization_delimiter(HasBits* has_bits) { - (*has_bits)[0] |= 256u; - } - static void set_has_required_chars(HasBits* has_bits) { - (*has_bits)[0] |= 4u; - } - static void set_has_byte_fallback(HasBits* has_bits) { - (*has_bits)[0] |= 262144u; - } - static void set_has_vocabulary_output_piece_score(HasBits* has_bits) { - (*has_bits)[1] |= 32u; - } - static void set_has_hard_vocab_limit(HasBits* has_bits) { - (*has_bits)[1] |= 64u; - } - static void set_has_use_all_vocab(HasBits* has_bits) { - (*has_bits)[0] |= 524288u; - } - static void set_has_unk_id(HasBits* has_bits) { - (*has_bits)[0] |= 2097152u; - } - static void set_has_bos_id(HasBits* has_bits) { - (*has_bits)[1] |= 128u; - } - static void set_has_eos_id(HasBits* has_bits) { - (*has_bits)[1] |= 256u; - } - static void set_has_pad_id(HasBits* has_bits) { - (*has_bits)[1] |= 512u; - } - static void set_has_unk_piece(HasBits* has_bits) { - (*has_bits)[0] |= 16u; - } - static void set_has_bos_piece(HasBits* has_bits) { - (*has_bits)[0] |= 32u; - } - static void set_has_eos_piece(HasBits* has_bits) { - (*has_bits)[0] |= 64u; - } - static void set_has_pad_piece(HasBits* has_bits) { - (*has_bits)[0] |= 128u; - } - static void set_has_unk_surface(HasBits* has_bits) { - (*has_bits)[0] |= 8u; - } - static void set_has_train_extremely_large_corpus(HasBits* has_bits) { - (*has_bits)[0] |= 1048576u; - } - static void set_has_seed_sentencepieces_file(HasBits* has_bits) { - (*has_bits)[0] |= 512u; - } -}; - -const ::PROTOBUF_NAMESPACE_ID::internal::LazyString TrainerSpec::_i_give_permission_to_break_this_code_default_unk_piece_{{{"", 5}}, {nullptr}}; -const ::PROTOBUF_NAMESPACE_ID::internal::LazyString TrainerSpec::_i_give_permission_to_break_this_code_default_bos_piece_{{{"", 3}}, {nullptr}}; -const ::PROTOBUF_NAMESPACE_ID::internal::LazyString TrainerSpec::_i_give_permission_to_break_this_code_default_eos_piece_{{{"", 4}}, {nullptr}}; -const ::PROTOBUF_NAMESPACE_ID::internal::LazyString TrainerSpec::_i_give_permission_to_break_this_code_default_pad_piece_{{{"", 5}}, {nullptr}}; -const ::PROTOBUF_NAMESPACE_ID::internal::LazyString TrainerSpec::_i_give_permission_to_break_this_code_default_unk_surface_{{{" \342\201\207 ", 5}}, {nullptr}}; -TrainerSpec::TrainerSpec(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - _extensions_(arena), - input_(arena), - accept_language_(arena), - control_symbols_(arena), - user_defined_symbols_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.TrainerSpec) -} -TrainerSpec::TrainerSpec(const TrainerSpec& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - _has_bits_(from._has_bits_), - input_(from.input_), - accept_language_(from.accept_language_), - control_symbols_(from.control_symbols_), - user_defined_symbols_(from.user_defined_symbols_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - _extensions_.MergeFrom(from._extensions_); - model_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_model_prefix()) { - model_prefix_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_model_prefix(), - GetArena()); - } - input_format_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_input_format()) { - input_format_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_input_format(), - GetArena()); - } - required_chars_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_required_chars()) { - required_chars_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_required_chars(), - GetArena()); - } - unk_surface_.UnsafeSetDefault(nullptr); - if (from._internal_has_unk_surface()) { - unk_surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, from._internal_unk_surface(), - GetArena()); - } - unk_piece_.UnsafeSetDefault(nullptr); - if (from._internal_has_unk_piece()) { - unk_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, from._internal_unk_piece(), - GetArena()); - } - bos_piece_.UnsafeSetDefault(nullptr); - if (from._internal_has_bos_piece()) { - bos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, from._internal_bos_piece(), - GetArena()); - } - eos_piece_.UnsafeSetDefault(nullptr); - if (from._internal_has_eos_piece()) { - eos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, from._internal_eos_piece(), - GetArena()); - } - pad_piece_.UnsafeSetDefault(nullptr); - if (from._internal_has_pad_piece()) { - pad_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, from._internal_pad_piece(), - GetArena()); - } - pretokenization_delimiter_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_pretokenization_delimiter()) { - pretokenization_delimiter_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_pretokenization_delimiter(), - GetArena()); - } - seed_sentencepieces_file_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_seed_sentencepieces_file()) { - seed_sentencepieces_file_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_seed_sentencepieces_file(), - GetArena()); - } - ::memcpy(&self_test_sample_size_, &from.self_test_sample_size_, - static_cast(reinterpret_cast(&pad_id_) - - reinterpret_cast(&self_test_sample_size_)) + sizeof(pad_id_)); - // @@protoc_insertion_point(copy_constructor:sentencepiece.TrainerSpec) -} - -void TrainerSpec::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_TrainerSpec_sentencepiece_5fmodel_2eproto.base); - model_prefix_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - input_format_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - required_chars_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - unk_surface_.UnsafeSetDefault(nullptr); - unk_piece_.UnsafeSetDefault(nullptr); - bos_piece_.UnsafeSetDefault(nullptr); - eos_piece_.UnsafeSetDefault(nullptr); - pad_piece_.UnsafeSetDefault(nullptr); - pretokenization_delimiter_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - seed_sentencepieces_file_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - ::memset(reinterpret_cast(this) + static_cast( - reinterpret_cast(&self_test_sample_size_) - reinterpret_cast(this)), - 0, static_cast(reinterpret_cast(&differential_privacy_clipping_threshold_) - - reinterpret_cast(&self_test_sample_size_)) + sizeof(differential_privacy_clipping_threshold_)); - model_type_ = 1; - vocab_size_ = 8000; - character_coverage_ = 0.9995f; - seed_sentencepiece_size_ = 1000000; - shrinking_factor_ = 0.75f; - num_threads_ = 16; - num_sub_iterations_ = 2; - max_sentence_length_ = 4192; - max_sentencepiece_length_ = 16; - shuffle_input_sentence_ = true; - split_by_unicode_script_ = true; - split_by_number_ = true; - split_by_whitespace_ = true; - vocabulary_output_piece_score_ = true; - hard_vocab_limit_ = true; - bos_id_ = 1; - eos_id_ = 2; - pad_id_ = -1; -} - -TrainerSpec::~TrainerSpec() { - // @@protoc_insertion_point(destructor:sentencepiece.TrainerSpec) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void TrainerSpec::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); - model_prefix_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - input_format_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - required_chars_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - unk_surface_.DestroyNoArena(nullptr); - unk_piece_.DestroyNoArena(nullptr); - bos_piece_.DestroyNoArena(nullptr); - eos_piece_.DestroyNoArena(nullptr); - pad_piece_.DestroyNoArena(nullptr); - pretokenization_delimiter_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - seed_sentencepieces_file_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); -} - -void TrainerSpec::ArenaDtor(void* object) { - TrainerSpec* _this = reinterpret_cast< TrainerSpec* >(object); - (void)_this; -} -void TrainerSpec::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void TrainerSpec::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const TrainerSpec& TrainerSpec::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_TrainerSpec_sentencepiece_5fmodel_2eproto.base); - return *internal_default_instance(); -} - - -void TrainerSpec::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.TrainerSpec) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _extensions_.Clear(); - input_.Clear(); - accept_language_.Clear(); - control_symbols_.Clear(); - user_defined_symbols_.Clear(); - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x000000ffu) { - if (cached_has_bits & 0x00000001u) { - model_prefix_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000002u) { - input_format_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000004u) { - required_chars_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000008u) { - unk_surface_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_unk_surface_, GetArena()); - } - if (cached_has_bits & 0x00000010u) { - unk_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_unk_piece_, GetArena()); - } - if (cached_has_bits & 0x00000020u) { - bos_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_bos_piece_, GetArena()); - } - if (cached_has_bits & 0x00000040u) { - eos_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_eos_piece_, GetArena()); - } - if (cached_has_bits & 0x00000080u) { - pad_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_pad_piece_, GetArena()); - } - } - if (cached_has_bits & 0x00000300u) { - if (cached_has_bits & 0x00000100u) { - pretokenization_delimiter_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000200u) { - seed_sentencepieces_file_.ClearNonDefaultToEmpty(); - } - } - if (cached_has_bits & 0x0000fc00u) { - ::memset(&self_test_sample_size_, 0, static_cast( - reinterpret_cast(&treat_whitespace_as_suffix_) - - reinterpret_cast(&self_test_sample_size_)) + sizeof(treat_whitespace_as_suffix_)); - } - if (cached_has_bits & 0x00ff0000u) { - ::memset(&allow_whitespace_only_pieces_, 0, static_cast( - reinterpret_cast(&differential_privacy_clipping_threshold_) - - reinterpret_cast(&allow_whitespace_only_pieces_)) + sizeof(differential_privacy_clipping_threshold_)); - } - if (cached_has_bits & 0xff000000u) { - model_type_ = 1; - vocab_size_ = 8000; - character_coverage_ = 0.9995f; - seed_sentencepiece_size_ = 1000000; - shrinking_factor_ = 0.75f; - num_threads_ = 16; - num_sub_iterations_ = 2; - max_sentence_length_ = 4192; - } - cached_has_bits = _has_bits_[1]; - if (cached_has_bits & 0x000000ffu) { - max_sentencepiece_length_ = 16; - shuffle_input_sentence_ = true; - split_by_unicode_script_ = true; - split_by_number_ = true; - split_by_whitespace_ = true; - vocabulary_output_piece_score_ = true; - hard_vocab_limit_ = true; - bos_id_ = 1; - } - if (cached_has_bits & 0x00000300u) { - eos_id_ = 2; - pad_id_ = -1; - } - _has_bits_.Clear(); - _internal_metadata_.Clear(); -} - -const char* TrainerSpec::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // repeated string input = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - ptr -= 1; - do { - ptr += 1; - auto str = _internal_add_input(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<10>(ptr)); - } else goto handle_unusual; - continue; - // optional string model_prefix = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { - auto str = _internal_mutable_model_prefix(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional .sentencepiece.TrainerSpec.ModelType model_type = 3 [default = UNIGRAM]; - case 3: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { - ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - if (PROTOBUF_PREDICT_TRUE(::sentencepiece::TrainerSpec_ModelType_IsValid(val))) { - _internal_set_model_type(static_cast<::sentencepiece::TrainerSpec_ModelType>(val)); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(3, val, mutable_unknown_fields()); - } - } else goto handle_unusual; - continue; - // optional int32 vocab_size = 4 [default = 8000]; - case 4: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 32)) { - _Internal::set_has_vocab_size(&_has_bits_); - vocab_size_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // repeated string accept_language = 5; - case 5: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { - ptr -= 1; - do { - ptr += 1; - auto str = _internal_add_accept_language(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<42>(ptr)); - } else goto handle_unusual; - continue; - // optional int32 self_test_sample_size = 6 [default = 0]; - case 6: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 48)) { - _Internal::set_has_self_test_sample_size(&_has_bits_); - self_test_sample_size_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string input_format = 7; - case 7: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 58)) { - auto str = _internal_mutable_input_format(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional float character_coverage = 10 [default = 0.9995]; - case 10: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 85)) { - _Internal::set_has_character_coverage(&_has_bits_); - character_coverage_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad(ptr); - ptr += sizeof(float); - } else goto handle_unusual; - continue; - // optional uint64 input_sentence_size = 11 [default = 0]; - case 11: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 88)) { - _Internal::set_has_input_sentence_size(&_has_bits_); - input_sentence_size_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 mining_sentence_size = 12 [deprecated = true]; - case 12: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 96)) { - _Internal::set_has_mining_sentence_size(&_has_bits_); - mining_sentence_size_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 training_sentence_size = 13 [deprecated = true]; - case 13: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 104)) { - _Internal::set_has_training_sentence_size(&_has_bits_); - training_sentence_size_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 seed_sentencepiece_size = 14 [default = 1000000]; - case 14: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 112)) { - _Internal::set_has_seed_sentencepiece_size(&_has_bits_); - seed_sentencepiece_size_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional float shrinking_factor = 15 [default = 0.75]; - case 15: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 125)) { - _Internal::set_has_shrinking_factor(&_has_bits_); - shrinking_factor_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad(ptr); - ptr += sizeof(float); - } else goto handle_unusual; - continue; - // optional int32 num_threads = 16 [default = 16]; - case 16: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 128)) { - _Internal::set_has_num_threads(&_has_bits_); - num_threads_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 num_sub_iterations = 17 [default = 2]; - case 17: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 136)) { - _Internal::set_has_num_sub_iterations(&_has_bits_); - num_sub_iterations_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 max_sentence_length = 18 [default = 4192]; - case 18: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 144)) { - _Internal::set_has_max_sentence_length(&_has_bits_); - max_sentence_length_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool shuffle_input_sentence = 19 [default = true]; - case 19: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 152)) { - _Internal::set_has_shuffle_input_sentence(&_has_bits_); - shuffle_input_sentence_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 max_sentencepiece_length = 20 [default = 16]; - case 20: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 160)) { - _Internal::set_has_max_sentencepiece_length(&_has_bits_); - max_sentencepiece_length_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool split_by_unicode_script = 21 [default = true]; - case 21: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 168)) { - _Internal::set_has_split_by_unicode_script(&_has_bits_); - split_by_unicode_script_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool split_by_whitespace = 22 [default = true]; - case 22: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 176)) { - _Internal::set_has_split_by_whitespace(&_has_bits_); - split_by_whitespace_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool split_by_number = 23 [default = true]; - case 23: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 184)) { - _Internal::set_has_split_by_number(&_has_bits_); - split_by_number_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool treat_whitespace_as_suffix = 24 [default = false]; - case 24: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 192)) { - _Internal::set_has_treat_whitespace_as_suffix(&_has_bits_); - treat_whitespace_as_suffix_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool split_digits = 25 [default = false]; - case 25: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 200)) { - _Internal::set_has_split_digits(&_has_bits_); - split_digits_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool allow_whitespace_only_pieces = 26 [default = false]; - case 26: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 208)) { - _Internal::set_has_allow_whitespace_only_pieces(&_has_bits_); - allow_whitespace_only_pieces_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // repeated string control_symbols = 30; - case 30: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 242)) { - ptr -= 2; - do { - ptr += 2; - auto str = _internal_add_control_symbols(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<242>(ptr)); - } else goto handle_unusual; - continue; - // repeated string user_defined_symbols = 31; - case 31: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 250)) { - ptr -= 2; - do { - ptr += 2; - auto str = _internal_add_user_defined_symbols(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<250>(ptr)); - } else goto handle_unusual; - continue; - // optional bool vocabulary_output_piece_score = 32 [default = true]; - case 32: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 0)) { - _Internal::set_has_vocabulary_output_piece_score(&_has_bits_); - vocabulary_output_piece_score_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool hard_vocab_limit = 33 [default = true]; - case 33: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 8)) { - _Internal::set_has_hard_vocab_limit(&_has_bits_); - hard_vocab_limit_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool use_all_vocab = 34 [default = false]; - case 34: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { - _Internal::set_has_use_all_vocab(&_has_bits_); - use_all_vocab_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool byte_fallback = 35 [default = false]; - case 35: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { - _Internal::set_has_byte_fallback(&_has_bits_); - byte_fallback_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string required_chars = 36; - case 36: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { - auto str = _internal_mutable_required_chars(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 unk_id = 40 [default = 0]; - case 40: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 64)) { - _Internal::set_has_unk_id(&_has_bits_); - unk_id_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 bos_id = 41 [default = 1]; - case 41: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 72)) { - _Internal::set_has_bos_id(&_has_bits_); - bos_id_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 eos_id = 42 [default = 2]; - case 42: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 80)) { - _Internal::set_has_eos_id(&_has_bits_); - eos_id_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional int32 pad_id = 43 [default = -1]; - case 43: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 88)) { - _Internal::set_has_pad_id(&_has_bits_); - pad_id_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string unk_surface = 44 [default = " \342\201\207 "]; - case 44: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 98)) { - auto str = _internal_mutable_unk_surface(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string unk_piece = 45 [default = ""]; - case 45: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 106)) { - auto str = _internal_mutable_unk_piece(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string bos_piece = 46 [default = ""]; - case 46: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 114)) { - auto str = _internal_mutable_bos_piece(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string eos_piece = 47 [default = ""]; - case 47: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 122)) { - auto str = _internal_mutable_eos_piece(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string pad_piece = 48 [default = ""]; - case 48: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 130)) { - auto str = _internal_mutable_pad_piece(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool train_extremely_large_corpus = 49 [default = false]; - case 49: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 136)) { - _Internal::set_has_train_extremely_large_corpus(&_has_bits_); - train_extremely_large_corpus_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool enable_differential_privacy = 50 [default = false]; - case 50: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 144)) { - _Internal::set_has_enable_differential_privacy(&_has_bits_); - enable_differential_privacy_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional float differential_privacy_noise_level = 51 [default = 0]; - case 51: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 157)) { - _Internal::set_has_differential_privacy_noise_level(&_has_bits_); - differential_privacy_noise_level_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad(ptr); - ptr += sizeof(float); - } else goto handle_unusual; - continue; - // optional uint64 differential_privacy_clipping_threshold = 52 [default = 0]; - case 52: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 160)) { - _Internal::set_has_differential_privacy_clipping_threshold(&_has_bits_); - differential_privacy_clipping_threshold_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string pretokenization_delimiter = 53 [default = ""]; - case 53: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 170)) { - auto str = _internal_mutable_pretokenization_delimiter(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string seed_sentencepieces_file = 54 [default = ""]; - case 54: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 178)) { - auto str = _internal_mutable_seed_sentencepieces_file(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - if ((1600u <= tag)) { - ptr = _extensions_.ParseField(tag, ptr, - internal_default_instance(), &_internal_metadata_, ctx); - CHK_(ptr != nullptr); - continue; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* TrainerSpec::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.TrainerSpec) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated string input = 1; - for (int i = 0, n = this->_internal_input_size(); i < n; i++) { - const auto& s = this->_internal_input(i); - target = stream->WriteString(1, s, target); - } - - cached_has_bits = _has_bits_[0]; - // optional string model_prefix = 2; - if (cached_has_bits & 0x00000001u) { - target = stream->WriteStringMaybeAliased( - 2, this->_internal_model_prefix(), target); - } - - // optional .sentencepiece.TrainerSpec.ModelType model_type = 3 [default = UNIGRAM]; - if (cached_has_bits & 0x01000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( - 3, this->_internal_model_type(), target); - } - - // optional int32 vocab_size = 4 [default = 8000]; - if (cached_has_bits & 0x02000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(4, this->_internal_vocab_size(), target); - } - - // repeated string accept_language = 5; - for (int i = 0, n = this->_internal_accept_language_size(); i < n; i++) { - const auto& s = this->_internal_accept_language(i); - target = stream->WriteString(5, s, target); - } - - // optional int32 self_test_sample_size = 6 [default = 0]; - if (cached_has_bits & 0x00000400u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(6, this->_internal_self_test_sample_size(), target); - } - - // optional string input_format = 7; - if (cached_has_bits & 0x00000002u) { - target = stream->WriteStringMaybeAliased( - 7, this->_internal_input_format(), target); - } - - // optional float character_coverage = 10 [default = 0.9995]; - if (cached_has_bits & 0x04000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteFloatToArray(10, this->_internal_character_coverage(), target); - } - - // optional uint64 input_sentence_size = 11 [default = 0]; - if (cached_has_bits & 0x00001000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt64ToArray(11, this->_internal_input_sentence_size(), target); - } - - // optional int32 mining_sentence_size = 12 [deprecated = true]; - if (cached_has_bits & 0x00000800u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(12, this->_internal_mining_sentence_size(), target); - } - - // optional int32 training_sentence_size = 13 [deprecated = true]; - if (cached_has_bits & 0x00002000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(13, this->_internal_training_sentence_size(), target); - } - - // optional int32 seed_sentencepiece_size = 14 [default = 1000000]; - if (cached_has_bits & 0x08000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(14, this->_internal_seed_sentencepiece_size(), target); - } - - // optional float shrinking_factor = 15 [default = 0.75]; - if (cached_has_bits & 0x10000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteFloatToArray(15, this->_internal_shrinking_factor(), target); - } - - // optional int32 num_threads = 16 [default = 16]; - if (cached_has_bits & 0x20000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(16, this->_internal_num_threads(), target); - } - - // optional int32 num_sub_iterations = 17 [default = 2]; - if (cached_has_bits & 0x40000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(17, this->_internal_num_sub_iterations(), target); - } - - // optional int32 max_sentence_length = 18 [default = 4192]; - if (cached_has_bits & 0x80000000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(18, this->_internal_max_sentence_length(), target); - } - - cached_has_bits = _has_bits_[1]; - // optional bool shuffle_input_sentence = 19 [default = true]; - if (cached_has_bits & 0x00000002u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(19, this->_internal_shuffle_input_sentence(), target); - } - - // optional int32 max_sentencepiece_length = 20 [default = 16]; - if (cached_has_bits & 0x00000001u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(20, this->_internal_max_sentencepiece_length(), target); - } - - // optional bool split_by_unicode_script = 21 [default = true]; - if (cached_has_bits & 0x00000004u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(21, this->_internal_split_by_unicode_script(), target); - } - - // optional bool split_by_whitespace = 22 [default = true]; - if (cached_has_bits & 0x00000010u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(22, this->_internal_split_by_whitespace(), target); - } - - // optional bool split_by_number = 23 [default = true]; - if (cached_has_bits & 0x00000008u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(23, this->_internal_split_by_number(), target); - } - - cached_has_bits = _has_bits_[0]; - // optional bool treat_whitespace_as_suffix = 24 [default = false]; - if (cached_has_bits & 0x00008000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(24, this->_internal_treat_whitespace_as_suffix(), target); - } - - // optional bool split_digits = 25 [default = false]; - if (cached_has_bits & 0x00020000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(25, this->_internal_split_digits(), target); - } - - // optional bool allow_whitespace_only_pieces = 26 [default = false]; - if (cached_has_bits & 0x00010000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(26, this->_internal_allow_whitespace_only_pieces(), target); - } - - // repeated string control_symbols = 30; - for (int i = 0, n = this->_internal_control_symbols_size(); i < n; i++) { - const auto& s = this->_internal_control_symbols(i); - target = stream->WriteString(30, s, target); - } - - // repeated string user_defined_symbols = 31; - for (int i = 0, n = this->_internal_user_defined_symbols_size(); i < n; i++) { - const auto& s = this->_internal_user_defined_symbols(i); - target = stream->WriteString(31, s, target); - } - - cached_has_bits = _has_bits_[1]; - // optional bool vocabulary_output_piece_score = 32 [default = true]; - if (cached_has_bits & 0x00000020u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(32, this->_internal_vocabulary_output_piece_score(), target); - } - - // optional bool hard_vocab_limit = 33 [default = true]; - if (cached_has_bits & 0x00000040u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(33, this->_internal_hard_vocab_limit(), target); - } - - cached_has_bits = _has_bits_[0]; - // optional bool use_all_vocab = 34 [default = false]; - if (cached_has_bits & 0x00080000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(34, this->_internal_use_all_vocab(), target); - } - - // optional bool byte_fallback = 35 [default = false]; - if (cached_has_bits & 0x00040000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(35, this->_internal_byte_fallback(), target); - } - - // optional string required_chars = 36; - if (cached_has_bits & 0x00000004u) { - target = stream->WriteStringMaybeAliased( - 36, this->_internal_required_chars(), target); - } - - // optional int32 unk_id = 40 [default = 0]; - if (cached_has_bits & 0x00200000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(40, this->_internal_unk_id(), target); - } - - cached_has_bits = _has_bits_[1]; - // optional int32 bos_id = 41 [default = 1]; - if (cached_has_bits & 0x00000080u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(41, this->_internal_bos_id(), target); - } - - // optional int32 eos_id = 42 [default = 2]; - if (cached_has_bits & 0x00000100u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(42, this->_internal_eos_id(), target); - } - - // optional int32 pad_id = 43 [default = -1]; - if (cached_has_bits & 0x00000200u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteInt32ToArray(43, this->_internal_pad_id(), target); - } - - cached_has_bits = _has_bits_[0]; - // optional string unk_surface = 44 [default = " \342\201\207 "]; - if (cached_has_bits & 0x00000008u) { - target = stream->WriteStringMaybeAliased( - 44, this->_internal_unk_surface(), target); - } - - // optional string unk_piece = 45 [default = ""]; - if (cached_has_bits & 0x00000010u) { - target = stream->WriteStringMaybeAliased( - 45, this->_internal_unk_piece(), target); - } - - // optional string bos_piece = 46 [default = ""]; - if (cached_has_bits & 0x00000020u) { - target = stream->WriteStringMaybeAliased( - 46, this->_internal_bos_piece(), target); - } - - // optional string eos_piece = 47 [default = ""]; - if (cached_has_bits & 0x00000040u) { - target = stream->WriteStringMaybeAliased( - 47, this->_internal_eos_piece(), target); - } - - // optional string pad_piece = 48 [default = ""]; - if (cached_has_bits & 0x00000080u) { - target = stream->WriteStringMaybeAliased( - 48, this->_internal_pad_piece(), target); - } - - // optional bool train_extremely_large_corpus = 49 [default = false]; - if (cached_has_bits & 0x00100000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(49, this->_internal_train_extremely_large_corpus(), target); - } - - // optional bool enable_differential_privacy = 50 [default = false]; - if (cached_has_bits & 0x00004000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(50, this->_internal_enable_differential_privacy(), target); - } - - // optional float differential_privacy_noise_level = 51 [default = 0]; - if (cached_has_bits & 0x00400000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteFloatToArray(51, this->_internal_differential_privacy_noise_level(), target); - } - - // optional uint64 differential_privacy_clipping_threshold = 52 [default = 0]; - if (cached_has_bits & 0x00800000u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt64ToArray(52, this->_internal_differential_privacy_clipping_threshold(), target); - } - - // optional string pretokenization_delimiter = 53 [default = ""]; - if (cached_has_bits & 0x00000100u) { - target = stream->WriteStringMaybeAliased( - 53, this->_internal_pretokenization_delimiter(), target); - } - - // optional string seed_sentencepieces_file = 54 [default = ""]; - if (cached_has_bits & 0x00000200u) { - target = stream->WriteStringMaybeAliased( - 54, this->_internal_seed_sentencepieces_file(), target); - } - - // Extension range [200, 536870912) - target = _extensions_._InternalSerialize( - 200, 536870912, target, stream); - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.TrainerSpec) - return target; -} - -size_t TrainerSpec::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.TrainerSpec) - size_t total_size = 0; - - total_size += _extensions_.ByteSize(); - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - // repeated string input = 1; - total_size += 1 * - ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(input_.size()); - for (int i = 0, n = input_.size(); i < n; i++) { - total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - input_.Get(i)); - } - - // repeated string accept_language = 5; - total_size += 1 * - ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(accept_language_.size()); - for (int i = 0, n = accept_language_.size(); i < n; i++) { - total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - accept_language_.Get(i)); - } - - // repeated string control_symbols = 30; - total_size += 2 * - ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(control_symbols_.size()); - for (int i = 0, n = control_symbols_.size(); i < n; i++) { - total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - control_symbols_.Get(i)); - } - - // repeated string user_defined_symbols = 31; - total_size += 2 * - ::PROTOBUF_NAMESPACE_ID::internal::FromIntSize(user_defined_symbols_.size()); - for (int i = 0, n = user_defined_symbols_.size(); i < n; i++) { - total_size += ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - user_defined_symbols_.Get(i)); - } - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x000000ffu) { - // optional string model_prefix = 2; - if (cached_has_bits & 0x00000001u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_model_prefix()); - } - - // optional string input_format = 7; - if (cached_has_bits & 0x00000002u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_input_format()); - } - - // optional string required_chars = 36; - if (cached_has_bits & 0x00000004u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_required_chars()); - } - - // optional string unk_surface = 44 [default = " \342\201\207 "]; - if (cached_has_bits & 0x00000008u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_unk_surface()); - } - - // optional string unk_piece = 45 [default = ""]; - if (cached_has_bits & 0x00000010u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_unk_piece()); - } - - // optional string bos_piece = 46 [default = ""]; - if (cached_has_bits & 0x00000020u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_bos_piece()); - } - - // optional string eos_piece = 47 [default = ""]; - if (cached_has_bits & 0x00000040u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_eos_piece()); - } - - // optional string pad_piece = 48 [default = ""]; - if (cached_has_bits & 0x00000080u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_pad_piece()); - } - - } - if (cached_has_bits & 0x0000ff00u) { - // optional string pretokenization_delimiter = 53 [default = ""]; - if (cached_has_bits & 0x00000100u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_pretokenization_delimiter()); - } - - // optional string seed_sentencepieces_file = 54 [default = ""]; - if (cached_has_bits & 0x00000200u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_seed_sentencepieces_file()); - } - - // optional int32 self_test_sample_size = 6 [default = 0]; - if (cached_has_bits & 0x00000400u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_self_test_sample_size()); - } - - // optional int32 mining_sentence_size = 12 [deprecated = true]; - if (cached_has_bits & 0x00000800u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_mining_sentence_size()); - } - - // optional uint64 input_sentence_size = 11 [default = 0]; - if (cached_has_bits & 0x00001000u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt64Size( - this->_internal_input_sentence_size()); - } - - // optional int32 training_sentence_size = 13 [deprecated = true]; - if (cached_has_bits & 0x00002000u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_training_sentence_size()); - } - - // optional bool enable_differential_privacy = 50 [default = false]; - if (cached_has_bits & 0x00004000u) { - total_size += 2 + 1; - } - - // optional bool treat_whitespace_as_suffix = 24 [default = false]; - if (cached_has_bits & 0x00008000u) { - total_size += 2 + 1; - } - - } - if (cached_has_bits & 0x00ff0000u) { - // optional bool allow_whitespace_only_pieces = 26 [default = false]; - if (cached_has_bits & 0x00010000u) { - total_size += 2 + 1; - } - - // optional bool split_digits = 25 [default = false]; - if (cached_has_bits & 0x00020000u) { - total_size += 2 + 1; - } - - // optional bool byte_fallback = 35 [default = false]; - if (cached_has_bits & 0x00040000u) { - total_size += 2 + 1; - } - - // optional bool use_all_vocab = 34 [default = false]; - if (cached_has_bits & 0x00080000u) { - total_size += 2 + 1; - } - - // optional bool train_extremely_large_corpus = 49 [default = false]; - if (cached_has_bits & 0x00100000u) { - total_size += 2 + 1; - } - - // optional int32 unk_id = 40 [default = 0]; - if (cached_has_bits & 0x00200000u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_unk_id()); - } - - // optional float differential_privacy_noise_level = 51 [default = 0]; - if (cached_has_bits & 0x00400000u) { - total_size += 2 + 4; - } - - // optional uint64 differential_privacy_clipping_threshold = 52 [default = 0]; - if (cached_has_bits & 0x00800000u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt64Size( - this->_internal_differential_privacy_clipping_threshold()); - } - - } - if (cached_has_bits & 0xff000000u) { - // optional .sentencepiece.TrainerSpec.ModelType model_type = 3 [default = UNIGRAM]; - if (cached_has_bits & 0x01000000u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->_internal_model_type()); - } - - // optional int32 vocab_size = 4 [default = 8000]; - if (cached_has_bits & 0x02000000u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_vocab_size()); - } - - // optional float character_coverage = 10 [default = 0.9995]; - if (cached_has_bits & 0x04000000u) { - total_size += 1 + 4; - } - - // optional int32 seed_sentencepiece_size = 14 [default = 1000000]; - if (cached_has_bits & 0x08000000u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_seed_sentencepiece_size()); - } - - // optional float shrinking_factor = 15 [default = 0.75]; - if (cached_has_bits & 0x10000000u) { - total_size += 1 + 4; - } - - // optional int32 num_threads = 16 [default = 16]; - if (cached_has_bits & 0x20000000u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_num_threads()); - } - - // optional int32 num_sub_iterations = 17 [default = 2]; - if (cached_has_bits & 0x40000000u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_num_sub_iterations()); - } - - // optional int32 max_sentence_length = 18 [default = 4192]; - if (cached_has_bits & 0x80000000u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_max_sentence_length()); - } - - } - cached_has_bits = _has_bits_[1]; - if (cached_has_bits & 0x000000ffu) { - // optional int32 max_sentencepiece_length = 20 [default = 16]; - if (cached_has_bits & 0x00000001u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_max_sentencepiece_length()); - } - - // optional bool shuffle_input_sentence = 19 [default = true]; - if (cached_has_bits & 0x00000002u) { - total_size += 2 + 1; - } - - // optional bool split_by_unicode_script = 21 [default = true]; - if (cached_has_bits & 0x00000004u) { - total_size += 2 + 1; - } - - // optional bool split_by_number = 23 [default = true]; - if (cached_has_bits & 0x00000008u) { - total_size += 2 + 1; - } - - // optional bool split_by_whitespace = 22 [default = true]; - if (cached_has_bits & 0x00000010u) { - total_size += 2 + 1; - } - - // optional bool vocabulary_output_piece_score = 32 [default = true]; - if (cached_has_bits & 0x00000020u) { - total_size += 2 + 1; - } - - // optional bool hard_vocab_limit = 33 [default = true]; - if (cached_has_bits & 0x00000040u) { - total_size += 2 + 1; - } - - // optional int32 bos_id = 41 [default = 1]; - if (cached_has_bits & 0x00000080u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_bos_id()); - } - - } - if (cached_has_bits & 0x00000300u) { - // optional int32 eos_id = 42 [default = 2]; - if (cached_has_bits & 0x00000100u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_eos_id()); - } - - // optional int32 pad_id = 43 [default = -1]; - if (cached_has_bits & 0x00000200u) { - total_size += 2 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::Int32Size( - this->_internal_pad_id()); - } - - } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void TrainerSpec::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void TrainerSpec::MergeFrom(const TrainerSpec& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.TrainerSpec) - GOOGLE_DCHECK_NE(&from, this); - _extensions_.MergeFrom(from._extensions_); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - input_.MergeFrom(from.input_); - accept_language_.MergeFrom(from.accept_language_); - control_symbols_.MergeFrom(from.control_symbols_); - user_defined_symbols_.MergeFrom(from.user_defined_symbols_); - cached_has_bits = from._has_bits_[0]; - if (cached_has_bits & 0x000000ffu) { - if (cached_has_bits & 0x00000001u) { - _internal_set_model_prefix(from._internal_model_prefix()); - } - if (cached_has_bits & 0x00000002u) { - _internal_set_input_format(from._internal_input_format()); - } - if (cached_has_bits & 0x00000004u) { - _internal_set_required_chars(from._internal_required_chars()); - } - if (cached_has_bits & 0x00000008u) { - _internal_set_unk_surface(from._internal_unk_surface()); - } - if (cached_has_bits & 0x00000010u) { - _internal_set_unk_piece(from._internal_unk_piece()); - } - if (cached_has_bits & 0x00000020u) { - _internal_set_bos_piece(from._internal_bos_piece()); - } - if (cached_has_bits & 0x00000040u) { - _internal_set_eos_piece(from._internal_eos_piece()); - } - if (cached_has_bits & 0x00000080u) { - _internal_set_pad_piece(from._internal_pad_piece()); - } - } - if (cached_has_bits & 0x0000ff00u) { - if (cached_has_bits & 0x00000100u) { - _internal_set_pretokenization_delimiter(from._internal_pretokenization_delimiter()); - } - if (cached_has_bits & 0x00000200u) { - _internal_set_seed_sentencepieces_file(from._internal_seed_sentencepieces_file()); - } - if (cached_has_bits & 0x00000400u) { - self_test_sample_size_ = from.self_test_sample_size_; - } - if (cached_has_bits & 0x00000800u) { - mining_sentence_size_ = from.mining_sentence_size_; - } - if (cached_has_bits & 0x00001000u) { - input_sentence_size_ = from.input_sentence_size_; - } - if (cached_has_bits & 0x00002000u) { - training_sentence_size_ = from.training_sentence_size_; - } - if (cached_has_bits & 0x00004000u) { - enable_differential_privacy_ = from.enable_differential_privacy_; - } - if (cached_has_bits & 0x00008000u) { - treat_whitespace_as_suffix_ = from.treat_whitespace_as_suffix_; - } - _has_bits_[0] |= cached_has_bits; - } - if (cached_has_bits & 0x00ff0000u) { - if (cached_has_bits & 0x00010000u) { - allow_whitespace_only_pieces_ = from.allow_whitespace_only_pieces_; - } - if (cached_has_bits & 0x00020000u) { - split_digits_ = from.split_digits_; - } - if (cached_has_bits & 0x00040000u) { - byte_fallback_ = from.byte_fallback_; - } - if (cached_has_bits & 0x00080000u) { - use_all_vocab_ = from.use_all_vocab_; - } - if (cached_has_bits & 0x00100000u) { - train_extremely_large_corpus_ = from.train_extremely_large_corpus_; - } - if (cached_has_bits & 0x00200000u) { - unk_id_ = from.unk_id_; - } - if (cached_has_bits & 0x00400000u) { - differential_privacy_noise_level_ = from.differential_privacy_noise_level_; - } - if (cached_has_bits & 0x00800000u) { - differential_privacy_clipping_threshold_ = from.differential_privacy_clipping_threshold_; - } - _has_bits_[0] |= cached_has_bits; - } - if (cached_has_bits & 0xff000000u) { - if (cached_has_bits & 0x01000000u) { - model_type_ = from.model_type_; - } - if (cached_has_bits & 0x02000000u) { - vocab_size_ = from.vocab_size_; - } - if (cached_has_bits & 0x04000000u) { - character_coverage_ = from.character_coverage_; - } - if (cached_has_bits & 0x08000000u) { - seed_sentencepiece_size_ = from.seed_sentencepiece_size_; - } - if (cached_has_bits & 0x10000000u) { - shrinking_factor_ = from.shrinking_factor_; - } - if (cached_has_bits & 0x20000000u) { - num_threads_ = from.num_threads_; - } - if (cached_has_bits & 0x40000000u) { - num_sub_iterations_ = from.num_sub_iterations_; - } - if (cached_has_bits & 0x80000000u) { - max_sentence_length_ = from.max_sentence_length_; - } - _has_bits_[0] |= cached_has_bits; - } - cached_has_bits = from._has_bits_[1]; - if (cached_has_bits & 0x000000ffu) { - if (cached_has_bits & 0x00000001u) { - max_sentencepiece_length_ = from.max_sentencepiece_length_; - } - if (cached_has_bits & 0x00000002u) { - shuffle_input_sentence_ = from.shuffle_input_sentence_; - } - if (cached_has_bits & 0x00000004u) { - split_by_unicode_script_ = from.split_by_unicode_script_; - } - if (cached_has_bits & 0x00000008u) { - split_by_number_ = from.split_by_number_; - } - if (cached_has_bits & 0x00000010u) { - split_by_whitespace_ = from.split_by_whitespace_; - } - if (cached_has_bits & 0x00000020u) { - vocabulary_output_piece_score_ = from.vocabulary_output_piece_score_; - } - if (cached_has_bits & 0x00000040u) { - hard_vocab_limit_ = from.hard_vocab_limit_; - } - if (cached_has_bits & 0x00000080u) { - bos_id_ = from.bos_id_; - } - _has_bits_[1] |= cached_has_bits; - } - if (cached_has_bits & 0x00000300u) { - if (cached_has_bits & 0x00000100u) { - eos_id_ = from.eos_id_; - } - if (cached_has_bits & 0x00000200u) { - pad_id_ = from.pad_id_; - } - _has_bits_[1] |= cached_has_bits; - } -} - -void TrainerSpec::CopyFrom(const TrainerSpec& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.TrainerSpec) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool TrainerSpec::IsInitialized() const { - if (!_extensions_.IsInitialized()) { - return false; - } - - return true; -} - -void TrainerSpec::InternalSwap(TrainerSpec* other) { - using std::swap; - _extensions_.Swap(&other->_extensions_); - _internal_metadata_.Swap(&other->_internal_metadata_); - swap(_has_bits_[0], other->_has_bits_[0]); - swap(_has_bits_[1], other->_has_bits_[1]); - input_.InternalSwap(&other->input_); - accept_language_.InternalSwap(&other->accept_language_); - control_symbols_.InternalSwap(&other->control_symbols_); - user_defined_symbols_.InternalSwap(&other->user_defined_symbols_); - model_prefix_.Swap(&other->model_prefix_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - input_format_.Swap(&other->input_format_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - required_chars_.Swap(&other->required_chars_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - unk_surface_.Swap(&other->unk_surface_, nullptr, GetArena()); - unk_piece_.Swap(&other->unk_piece_, nullptr, GetArena()); - bos_piece_.Swap(&other->bos_piece_, nullptr, GetArena()); - eos_piece_.Swap(&other->eos_piece_, nullptr, GetArena()); - pad_piece_.Swap(&other->pad_piece_, nullptr, GetArena()); - pretokenization_delimiter_.Swap(&other->pretokenization_delimiter_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - seed_sentencepieces_file_.Swap(&other->seed_sentencepieces_file_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - ::PROTOBUF_NAMESPACE_ID::internal::memswap< - PROTOBUF_FIELD_OFFSET(TrainerSpec, differential_privacy_clipping_threshold_) - + sizeof(TrainerSpec::differential_privacy_clipping_threshold_) - - PROTOBUF_FIELD_OFFSET(TrainerSpec, self_test_sample_size_)>( - reinterpret_cast(&self_test_sample_size_), - reinterpret_cast(&other->self_test_sample_size_)); - swap(model_type_, other->model_type_); - swap(vocab_size_, other->vocab_size_); - swap(character_coverage_, other->character_coverage_); - swap(seed_sentencepiece_size_, other->seed_sentencepiece_size_); - swap(shrinking_factor_, other->shrinking_factor_); - swap(num_threads_, other->num_threads_); - swap(num_sub_iterations_, other->num_sub_iterations_); - swap(max_sentence_length_, other->max_sentence_length_); - swap(max_sentencepiece_length_, other->max_sentencepiece_length_); - swap(shuffle_input_sentence_, other->shuffle_input_sentence_); - swap(split_by_unicode_script_, other->split_by_unicode_script_); - swap(split_by_number_, other->split_by_number_); - swap(split_by_whitespace_, other->split_by_whitespace_); - swap(vocabulary_output_piece_score_, other->vocabulary_output_piece_score_); - swap(hard_vocab_limit_, other->hard_vocab_limit_); - swap(bos_id_, other->bos_id_); - swap(eos_id_, other->eos_id_); - swap(pad_id_, other->pad_id_); -} - -std::string TrainerSpec::GetTypeName() const { - return "sentencepiece.TrainerSpec"; -} - - -// =================================================================== - -class NormalizerSpec::_Internal { - public: - using HasBits = decltype(std::declval()._has_bits_); - static void set_has_name(HasBits* has_bits) { - (*has_bits)[0] |= 1u; - } - static void set_has_precompiled_charsmap(HasBits* has_bits) { - (*has_bits)[0] |= 2u; - } - static void set_has_add_dummy_prefix(HasBits* has_bits) { - (*has_bits)[0] |= 8u; - } - static void set_has_remove_extra_whitespaces(HasBits* has_bits) { - (*has_bits)[0] |= 16u; - } - static void set_has_escape_whitespaces(HasBits* has_bits) { - (*has_bits)[0] |= 32u; - } - static void set_has_normalization_rule_tsv(HasBits* has_bits) { - (*has_bits)[0] |= 4u; - } -}; - -NormalizerSpec::NormalizerSpec(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - _extensions_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.NormalizerSpec) -} -NormalizerSpec::NormalizerSpec(const NormalizerSpec& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - _has_bits_(from._has_bits_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - _extensions_.MergeFrom(from._extensions_); - name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_name()) { - name_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_name(), - GetArena()); - } - precompiled_charsmap_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_precompiled_charsmap()) { - precompiled_charsmap_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_precompiled_charsmap(), - GetArena()); - } - normalization_rule_tsv_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_normalization_rule_tsv()) { - normalization_rule_tsv_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_normalization_rule_tsv(), - GetArena()); - } - ::memcpy(&add_dummy_prefix_, &from.add_dummy_prefix_, - static_cast(reinterpret_cast(&escape_whitespaces_) - - reinterpret_cast(&add_dummy_prefix_)) + sizeof(escape_whitespaces_)); - // @@protoc_insertion_point(copy_constructor:sentencepiece.NormalizerSpec) -} - -void NormalizerSpec::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_NormalizerSpec_sentencepiece_5fmodel_2eproto.base); - name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - precompiled_charsmap_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - normalization_rule_tsv_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - add_dummy_prefix_ = true; - remove_extra_whitespaces_ = true; - escape_whitespaces_ = true; -} - -NormalizerSpec::~NormalizerSpec() { - // @@protoc_insertion_point(destructor:sentencepiece.NormalizerSpec) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void NormalizerSpec::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); - name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - precompiled_charsmap_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - normalization_rule_tsv_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); -} - -void NormalizerSpec::ArenaDtor(void* object) { - NormalizerSpec* _this = reinterpret_cast< NormalizerSpec* >(object); - (void)_this; -} -void NormalizerSpec::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void NormalizerSpec::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const NormalizerSpec& NormalizerSpec::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_NormalizerSpec_sentencepiece_5fmodel_2eproto.base); - return *internal_default_instance(); -} - - -void NormalizerSpec::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.NormalizerSpec) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _extensions_.Clear(); - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x0000003fu) { - if (cached_has_bits & 0x00000001u) { - name_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000002u) { - precompiled_charsmap_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000004u) { - normalization_rule_tsv_.ClearNonDefaultToEmpty(); - } - add_dummy_prefix_ = true; - remove_extra_whitespaces_ = true; - escape_whitespaces_ = true; - } - _has_bits_.Clear(); - _internal_metadata_.Clear(); -} - -const char* NormalizerSpec::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - _Internal::HasBits has_bits{}; - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // optional string name = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - auto str = _internal_mutable_name(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bytes precompiled_charsmap = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { - auto str = _internal_mutable_precompiled_charsmap(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool add_dummy_prefix = 3 [default = true]; - case 3: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { - _Internal::set_has_add_dummy_prefix(&has_bits); - add_dummy_prefix_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool remove_extra_whitespaces = 4 [default = true]; - case 4: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 32)) { - _Internal::set_has_remove_extra_whitespaces(&has_bits); - remove_extra_whitespaces_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional bool escape_whitespaces = 5 [default = true]; - case 5: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 40)) { - _Internal::set_has_escape_whitespaces(&has_bits); - escape_whitespaces_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string normalization_rule_tsv = 6; - case 6: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 50)) { - auto str = _internal_mutable_normalization_rule_tsv(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - if ((1600u <= tag)) { - ptr = _extensions_.ParseField(tag, ptr, - internal_default_instance(), &_internal_metadata_, ctx); - CHK_(ptr != nullptr); - continue; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - _has_bits_.Or(has_bits); - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* NormalizerSpec::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.NormalizerSpec) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - target = stream->WriteStringMaybeAliased( - 1, this->_internal_name(), target); - } - - // optional bytes precompiled_charsmap = 2; - if (cached_has_bits & 0x00000002u) { - target = stream->WriteBytesMaybeAliased( - 2, this->_internal_precompiled_charsmap(), target); - } - - // optional bool add_dummy_prefix = 3 [default = true]; - if (cached_has_bits & 0x00000008u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(3, this->_internal_add_dummy_prefix(), target); - } - - // optional bool remove_extra_whitespaces = 4 [default = true]; - if (cached_has_bits & 0x00000010u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(4, this->_internal_remove_extra_whitespaces(), target); - } - - // optional bool escape_whitespaces = 5 [default = true]; - if (cached_has_bits & 0x00000020u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteBoolToArray(5, this->_internal_escape_whitespaces(), target); - } - - // optional string normalization_rule_tsv = 6; - if (cached_has_bits & 0x00000004u) { - target = stream->WriteStringMaybeAliased( - 6, this->_internal_normalization_rule_tsv(), target); - } - - // Extension range [200, 536870912) - target = _extensions_._InternalSerialize( - 200, 536870912, target, stream); - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.NormalizerSpec) - return target; -} - -size_t NormalizerSpec::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.NormalizerSpec) - size_t total_size = 0; - - total_size += _extensions_.ByteSize(); - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x0000003fu) { - // optional string name = 1; - if (cached_has_bits & 0x00000001u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_name()); - } - - // optional bytes precompiled_charsmap = 2; - if (cached_has_bits & 0x00000002u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize( - this->_internal_precompiled_charsmap()); - } - - // optional string normalization_rule_tsv = 6; - if (cached_has_bits & 0x00000004u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_normalization_rule_tsv()); - } - - // optional bool add_dummy_prefix = 3 [default = true]; - if (cached_has_bits & 0x00000008u) { - total_size += 1 + 1; - } - - // optional bool remove_extra_whitespaces = 4 [default = true]; - if (cached_has_bits & 0x00000010u) { - total_size += 1 + 1; - } - - // optional bool escape_whitespaces = 5 [default = true]; - if (cached_has_bits & 0x00000020u) { - total_size += 1 + 1; - } - - } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void NormalizerSpec::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void NormalizerSpec::MergeFrom(const NormalizerSpec& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.NormalizerSpec) - GOOGLE_DCHECK_NE(&from, this); - _extensions_.MergeFrom(from._extensions_); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = from._has_bits_[0]; - if (cached_has_bits & 0x0000003fu) { - if (cached_has_bits & 0x00000001u) { - _internal_set_name(from._internal_name()); - } - if (cached_has_bits & 0x00000002u) { - _internal_set_precompiled_charsmap(from._internal_precompiled_charsmap()); - } - if (cached_has_bits & 0x00000004u) { - _internal_set_normalization_rule_tsv(from._internal_normalization_rule_tsv()); - } - if (cached_has_bits & 0x00000008u) { - add_dummy_prefix_ = from.add_dummy_prefix_; - } - if (cached_has_bits & 0x00000010u) { - remove_extra_whitespaces_ = from.remove_extra_whitespaces_; - } - if (cached_has_bits & 0x00000020u) { - escape_whitespaces_ = from.escape_whitespaces_; - } - _has_bits_[0] |= cached_has_bits; - } -} - -void NormalizerSpec::CopyFrom(const NormalizerSpec& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.NormalizerSpec) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool NormalizerSpec::IsInitialized() const { - if (!_extensions_.IsInitialized()) { - return false; - } - - return true; -} - -void NormalizerSpec::InternalSwap(NormalizerSpec* other) { - using std::swap; - _extensions_.Swap(&other->_extensions_); - _internal_metadata_.Swap(&other->_internal_metadata_); - swap(_has_bits_[0], other->_has_bits_[0]); - name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - precompiled_charsmap_.Swap(&other->precompiled_charsmap_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - normalization_rule_tsv_.Swap(&other->normalization_rule_tsv_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - swap(add_dummy_prefix_, other->add_dummy_prefix_); - swap(remove_extra_whitespaces_, other->remove_extra_whitespaces_); - swap(escape_whitespaces_, other->escape_whitespaces_); -} - -std::string NormalizerSpec::GetTypeName() const { - return "sentencepiece.NormalizerSpec"; -} - - -// =================================================================== - -class SelfTestData_Sample::_Internal { - public: - using HasBits = decltype(std::declval()._has_bits_); - static void set_has_input(HasBits* has_bits) { - (*has_bits)[0] |= 1u; - } - static void set_has_expected(HasBits* has_bits) { - (*has_bits)[0] |= 2u; - } -}; - -SelfTestData_Sample::SelfTestData_Sample(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.SelfTestData.Sample) -} -SelfTestData_Sample::SelfTestData_Sample(const SelfTestData_Sample& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - _has_bits_(from._has_bits_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - input_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_input()) { - input_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_input(), - GetArena()); - } - expected_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_expected()) { - expected_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_expected(), - GetArena()); - } - // @@protoc_insertion_point(copy_constructor:sentencepiece.SelfTestData.Sample) -} - -void SelfTestData_Sample::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SelfTestData_Sample_sentencepiece_5fmodel_2eproto.base); - input_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - expected_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); -} - -SelfTestData_Sample::~SelfTestData_Sample() { - // @@protoc_insertion_point(destructor:sentencepiece.SelfTestData.Sample) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void SelfTestData_Sample::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); - input_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - expected_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); -} - -void SelfTestData_Sample::ArenaDtor(void* object) { - SelfTestData_Sample* _this = reinterpret_cast< SelfTestData_Sample* >(object); - (void)_this; -} -void SelfTestData_Sample::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void SelfTestData_Sample::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const SelfTestData_Sample& SelfTestData_Sample::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_SelfTestData_Sample_sentencepiece_5fmodel_2eproto.base); - return *internal_default_instance(); -} - - -void SelfTestData_Sample::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.SelfTestData.Sample) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x00000003u) { - if (cached_has_bits & 0x00000001u) { - input_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000002u) { - expected_.ClearNonDefaultToEmpty(); - } - } - _has_bits_.Clear(); - _internal_metadata_.Clear(); -} - -const char* SelfTestData_Sample::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - _Internal::HasBits has_bits{}; - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // optional string input = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - auto str = _internal_mutable_input(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional string expected = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { - auto str = _internal_mutable_expected(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - _has_bits_.Or(has_bits); - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* SelfTestData_Sample::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.SelfTestData.Sample) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string input = 1; - if (cached_has_bits & 0x00000001u) { - target = stream->WriteStringMaybeAliased( - 1, this->_internal_input(), target); - } - - // optional string expected = 2; - if (cached_has_bits & 0x00000002u) { - target = stream->WriteStringMaybeAliased( - 2, this->_internal_expected(), target); - } - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.SelfTestData.Sample) - return target; -} - -size_t SelfTestData_Sample::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.SelfTestData.Sample) - size_t total_size = 0; - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x00000003u) { - // optional string input = 1; - if (cached_has_bits & 0x00000001u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_input()); - } - - // optional string expected = 2; - if (cached_has_bits & 0x00000002u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_expected()); - } - - } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void SelfTestData_Sample::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void SelfTestData_Sample::MergeFrom(const SelfTestData_Sample& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.SelfTestData.Sample) - GOOGLE_DCHECK_NE(&from, this); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = from._has_bits_[0]; - if (cached_has_bits & 0x00000003u) { - if (cached_has_bits & 0x00000001u) { - _internal_set_input(from._internal_input()); - } - if (cached_has_bits & 0x00000002u) { - _internal_set_expected(from._internal_expected()); - } - } -} - -void SelfTestData_Sample::CopyFrom(const SelfTestData_Sample& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.SelfTestData.Sample) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool SelfTestData_Sample::IsInitialized() const { - return true; -} - -void SelfTestData_Sample::InternalSwap(SelfTestData_Sample* other) { - using std::swap; - _internal_metadata_.Swap(&other->_internal_metadata_); - swap(_has_bits_[0], other->_has_bits_[0]); - input_.Swap(&other->input_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - expected_.Swap(&other->expected_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} - -std::string SelfTestData_Sample::GetTypeName() const { - return "sentencepiece.SelfTestData.Sample"; -} - - -// =================================================================== - -class SelfTestData::_Internal { - public: -}; - -SelfTestData::SelfTestData(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - _extensions_(arena), - samples_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.SelfTestData) -} -SelfTestData::SelfTestData(const SelfTestData& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - samples_(from.samples_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - _extensions_.MergeFrom(from._extensions_); - // @@protoc_insertion_point(copy_constructor:sentencepiece.SelfTestData) -} - -void SelfTestData::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_SelfTestData_sentencepiece_5fmodel_2eproto.base); -} - -SelfTestData::~SelfTestData() { - // @@protoc_insertion_point(destructor:sentencepiece.SelfTestData) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void SelfTestData::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); -} - -void SelfTestData::ArenaDtor(void* object) { - SelfTestData* _this = reinterpret_cast< SelfTestData* >(object); - (void)_this; -} -void SelfTestData::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void SelfTestData::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const SelfTestData& SelfTestData::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_SelfTestData_sentencepiece_5fmodel_2eproto.base); - return *internal_default_instance(); -} - - -void SelfTestData::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.SelfTestData) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _extensions_.Clear(); - samples_.Clear(); - _internal_metadata_.Clear(); -} - -const char* SelfTestData::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // repeated .sentencepiece.SelfTestData.Sample samples = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - ptr -= 1; - do { - ptr += 1; - ptr = ctx->ParseMessage(_internal_add_samples(), ptr); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<10>(ptr)); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - if ((1600u <= tag)) { - ptr = _extensions_.ParseField(tag, ptr, - internal_default_instance(), &_internal_metadata_, ctx); - CHK_(ptr != nullptr); - continue; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* SelfTestData::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.SelfTestData) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .sentencepiece.SelfTestData.Sample samples = 1; - for (unsigned int i = 0, - n = static_cast(this->_internal_samples_size()); i < n; i++) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage(1, this->_internal_samples(i), target, stream); - } - - // Extension range [200, 536870912) - target = _extensions_._InternalSerialize( - 200, 536870912, target, stream); - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.SelfTestData) - return target; -} - -size_t SelfTestData::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.SelfTestData) - size_t total_size = 0; - - total_size += _extensions_.ByteSize(); - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - // repeated .sentencepiece.SelfTestData.Sample samples = 1; - total_size += 1UL * this->_internal_samples_size(); - for (const auto& msg : this->samples_) { - total_size += - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize(msg); - } - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void SelfTestData::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void SelfTestData::MergeFrom(const SelfTestData& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.SelfTestData) - GOOGLE_DCHECK_NE(&from, this); - _extensions_.MergeFrom(from._extensions_); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - samples_.MergeFrom(from.samples_); -} - -void SelfTestData::CopyFrom(const SelfTestData& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.SelfTestData) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool SelfTestData::IsInitialized() const { - if (!_extensions_.IsInitialized()) { - return false; - } - - return true; -} - -void SelfTestData::InternalSwap(SelfTestData* other) { - using std::swap; - _extensions_.Swap(&other->_extensions_); - _internal_metadata_.Swap(&other->_internal_metadata_); - samples_.InternalSwap(&other->samples_); -} - -std::string SelfTestData::GetTypeName() const { - return "sentencepiece.SelfTestData"; -} - - -// =================================================================== - -class ModelProto_SentencePiece::_Internal { - public: - using HasBits = decltype(std::declval()._has_bits_); - static void set_has_piece(HasBits* has_bits) { - (*has_bits)[0] |= 1u; - } - static void set_has_score(HasBits* has_bits) { - (*has_bits)[0] |= 2u; - } - static void set_has_type(HasBits* has_bits) { - (*has_bits)[0] |= 4u; - } -}; - -ModelProto_SentencePiece::ModelProto_SentencePiece(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - _extensions_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.ModelProto.SentencePiece) -} -ModelProto_SentencePiece::ModelProto_SentencePiece(const ModelProto_SentencePiece& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - _has_bits_(from._has_bits_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - _extensions_.MergeFrom(from._extensions_); - piece_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - if (from._internal_has_piece()) { - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, from._internal_piece(), - GetArena()); - } - ::memcpy(&score_, &from.score_, - static_cast(reinterpret_cast(&type_) - - reinterpret_cast(&score_)) + sizeof(type_)); - // @@protoc_insertion_point(copy_constructor:sentencepiece.ModelProto.SentencePiece) -} - -void ModelProto_SentencePiece::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ModelProto_SentencePiece_sentencepiece_5fmodel_2eproto.base); - piece_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); - score_ = 0; - type_ = 1; -} - -ModelProto_SentencePiece::~ModelProto_SentencePiece() { - // @@protoc_insertion_point(destructor:sentencepiece.ModelProto.SentencePiece) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void ModelProto_SentencePiece::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); - piece_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); -} - -void ModelProto_SentencePiece::ArenaDtor(void* object) { - ModelProto_SentencePiece* _this = reinterpret_cast< ModelProto_SentencePiece* >(object); - (void)_this; -} -void ModelProto_SentencePiece::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void ModelProto_SentencePiece::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const ModelProto_SentencePiece& ModelProto_SentencePiece::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ModelProto_SentencePiece_sentencepiece_5fmodel_2eproto.base); - return *internal_default_instance(); -} - - -void ModelProto_SentencePiece::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.ModelProto.SentencePiece) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _extensions_.Clear(); - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x00000001u) { - piece_.ClearNonDefaultToEmpty(); - } - if (cached_has_bits & 0x00000006u) { - score_ = 0; - type_ = 1; - } - _has_bits_.Clear(); - _internal_metadata_.Clear(); -} - -const char* ModelProto_SentencePiece::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - _Internal::HasBits has_bits{}; - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // optional string piece = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - auto str = _internal_mutable_piece(); - ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional float score = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 21)) { - _Internal::set_has_score(&has_bits); - score_ = ::PROTOBUF_NAMESPACE_ID::internal::UnalignedLoad(ptr); - ptr += sizeof(float); - } else goto handle_unusual; - continue; - // optional .sentencepiece.ModelProto.SentencePiece.Type type = 3 [default = NORMAL]; - case 3: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 24)) { - ::PROTOBUF_NAMESPACE_ID::uint64 val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); - CHK_(ptr); - if (PROTOBUF_PREDICT_TRUE(::sentencepiece::ModelProto_SentencePiece_Type_IsValid(val))) { - _internal_set_type(static_cast<::sentencepiece::ModelProto_SentencePiece_Type>(val)); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::WriteVarint(3, val, mutable_unknown_fields()); - } - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - if ((1600u <= tag)) { - ptr = _extensions_.ParseField(tag, ptr, - internal_default_instance(), &_internal_metadata_, ctx); - CHK_(ptr != nullptr); - continue; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - _has_bits_.Or(has_bits); - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* ModelProto_SentencePiece::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.ModelProto.SentencePiece) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - // optional string piece = 1; - if (cached_has_bits & 0x00000001u) { - target = stream->WriteStringMaybeAliased( - 1, this->_internal_piece(), target); - } - - // optional float score = 2; - if (cached_has_bits & 0x00000002u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteFloatToArray(2, this->_internal_score(), target); - } - - // optional .sentencepiece.ModelProto.SentencePiece.Type type = 3 [default = NORMAL]; - if (cached_has_bits & 0x00000004u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteEnumToArray( - 3, this->_internal_type(), target); - } - - // Extension range [200, 536870912) - target = _extensions_._InternalSerialize( - 200, 536870912, target, stream); - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.ModelProto.SentencePiece) - return target; -} - -size_t ModelProto_SentencePiece::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.ModelProto.SentencePiece) - size_t total_size = 0; - - total_size += _extensions_.ByteSize(); - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x00000007u) { - // optional string piece = 1; - if (cached_has_bits & 0x00000001u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_piece()); - } - - // optional float score = 2; - if (cached_has_bits & 0x00000002u) { - total_size += 1 + 4; - } - - // optional .sentencepiece.ModelProto.SentencePiece.Type type = 3 [default = NORMAL]; - if (cached_has_bits & 0x00000004u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::EnumSize(this->_internal_type()); - } - - } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void ModelProto_SentencePiece::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void ModelProto_SentencePiece::MergeFrom(const ModelProto_SentencePiece& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.ModelProto.SentencePiece) - GOOGLE_DCHECK_NE(&from, this); - _extensions_.MergeFrom(from._extensions_); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - cached_has_bits = from._has_bits_[0]; - if (cached_has_bits & 0x00000007u) { - if (cached_has_bits & 0x00000001u) { - _internal_set_piece(from._internal_piece()); - } - if (cached_has_bits & 0x00000002u) { - score_ = from.score_; - } - if (cached_has_bits & 0x00000004u) { - type_ = from.type_; - } - _has_bits_[0] |= cached_has_bits; - } -} - -void ModelProto_SentencePiece::CopyFrom(const ModelProto_SentencePiece& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.ModelProto.SentencePiece) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ModelProto_SentencePiece::IsInitialized() const { - if (!_extensions_.IsInitialized()) { - return false; - } - - return true; -} - -void ModelProto_SentencePiece::InternalSwap(ModelProto_SentencePiece* other) { - using std::swap; - _extensions_.Swap(&other->_extensions_); - _internal_metadata_.Swap(&other->_internal_metadata_); - swap(_has_bits_[0], other->_has_bits_[0]); - piece_.Swap(&other->piece_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); - swap(score_, other->score_); - swap(type_, other->type_); -} - -std::string ModelProto_SentencePiece::GetTypeName() const { - return "sentencepiece.ModelProto.SentencePiece"; -} - - -// =================================================================== - -class ModelProto::_Internal { - public: - using HasBits = decltype(std::declval()._has_bits_); - static const ::sentencepiece::TrainerSpec& trainer_spec(const ModelProto* msg); - static void set_has_trainer_spec(HasBits* has_bits) { - (*has_bits)[0] |= 1u; - } - static const ::sentencepiece::NormalizerSpec& normalizer_spec(const ModelProto* msg); - static void set_has_normalizer_spec(HasBits* has_bits) { - (*has_bits)[0] |= 2u; - } - static const ::sentencepiece::SelfTestData& self_test_data(const ModelProto* msg); - static void set_has_self_test_data(HasBits* has_bits) { - (*has_bits)[0] |= 4u; - } - static const ::sentencepiece::NormalizerSpec& denormalizer_spec(const ModelProto* msg); - static void set_has_denormalizer_spec(HasBits* has_bits) { - (*has_bits)[0] |= 8u; - } -}; - -const ::sentencepiece::TrainerSpec& -ModelProto::_Internal::trainer_spec(const ModelProto* msg) { - return *msg->trainer_spec_; -} -const ::sentencepiece::NormalizerSpec& -ModelProto::_Internal::normalizer_spec(const ModelProto* msg) { - return *msg->normalizer_spec_; -} -const ::sentencepiece::SelfTestData& -ModelProto::_Internal::self_test_data(const ModelProto* msg) { - return *msg->self_test_data_; -} -const ::sentencepiece::NormalizerSpec& -ModelProto::_Internal::denormalizer_spec(const ModelProto* msg) { - return *msg->denormalizer_spec_; -} -ModelProto::ModelProto(::PROTOBUF_NAMESPACE_ID::Arena* arena) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena), - _extensions_(arena), - pieces_(arena) { - SharedCtor(); - RegisterArenaDtor(arena); - // @@protoc_insertion_point(arena_constructor:sentencepiece.ModelProto) -} -ModelProto::ModelProto(const ModelProto& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(), - _has_bits_(from._has_bits_), - pieces_(from.pieces_) { - _internal_metadata_.MergeFrom(from._internal_metadata_); - _extensions_.MergeFrom(from._extensions_); - if (from._internal_has_trainer_spec()) { - trainer_spec_ = new ::sentencepiece::TrainerSpec(*from.trainer_spec_); - } else { - trainer_spec_ = nullptr; - } - if (from._internal_has_normalizer_spec()) { - normalizer_spec_ = new ::sentencepiece::NormalizerSpec(*from.normalizer_spec_); - } else { - normalizer_spec_ = nullptr; - } - if (from._internal_has_self_test_data()) { - self_test_data_ = new ::sentencepiece::SelfTestData(*from.self_test_data_); - } else { - self_test_data_ = nullptr; - } - if (from._internal_has_denormalizer_spec()) { - denormalizer_spec_ = new ::sentencepiece::NormalizerSpec(*from.denormalizer_spec_); - } else { - denormalizer_spec_ = nullptr; - } - // @@protoc_insertion_point(copy_constructor:sentencepiece.ModelProto) -} - -void ModelProto::SharedCtor() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ModelProto_sentencepiece_5fmodel_2eproto.base); - ::memset(reinterpret_cast(this) + static_cast( - reinterpret_cast(&trainer_spec_) - reinterpret_cast(this)), - 0, static_cast(reinterpret_cast(&denormalizer_spec_) - - reinterpret_cast(&trainer_spec_)) + sizeof(denormalizer_spec_)); -} - -ModelProto::~ModelProto() { - // @@protoc_insertion_point(destructor:sentencepiece.ModelProto) - SharedDtor(); - _internal_metadata_.Delete(); -} - -void ModelProto::SharedDtor() { - GOOGLE_DCHECK(GetArena() == nullptr); - if (this != internal_default_instance()) delete trainer_spec_; - if (this != internal_default_instance()) delete normalizer_spec_; - if (this != internal_default_instance()) delete self_test_data_; - if (this != internal_default_instance()) delete denormalizer_spec_; -} - -void ModelProto::ArenaDtor(void* object) { - ModelProto* _this = reinterpret_cast< ModelProto* >(object); - (void)_this; -} -void ModelProto::RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena*) { -} -void ModelProto::SetCachedSize(int size) const { - _cached_size_.Set(size); -} -const ModelProto& ModelProto::default_instance() { - ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ModelProto_sentencepiece_5fmodel_2eproto.base); - return *internal_default_instance(); -} - - -void ModelProto::Clear() { -// @@protoc_insertion_point(message_clear_start:sentencepiece.ModelProto) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _extensions_.Clear(); - pieces_.Clear(); - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x0000000fu) { - if (cached_has_bits & 0x00000001u) { - GOOGLE_DCHECK(trainer_spec_ != nullptr); - trainer_spec_->Clear(); - } - if (cached_has_bits & 0x00000002u) { - GOOGLE_DCHECK(normalizer_spec_ != nullptr); - normalizer_spec_->Clear(); - } - if (cached_has_bits & 0x00000004u) { - GOOGLE_DCHECK(self_test_data_ != nullptr); - self_test_data_->Clear(); - } - if (cached_has_bits & 0x00000008u) { - GOOGLE_DCHECK(denormalizer_spec_ != nullptr); - denormalizer_spec_->Clear(); - } - } - _has_bits_.Clear(); - _internal_metadata_.Clear(); -} - -const char* ModelProto::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { -#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - _Internal::HasBits has_bits{}; - while (!ctx->Done(&ptr)) { - ::PROTOBUF_NAMESPACE_ID::uint32 tag; - ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); - CHK_(ptr); - switch (tag >> 3) { - // repeated .sentencepiece.ModelProto.SentencePiece pieces = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { - ptr -= 1; - do { - ptr += 1; - ptr = ctx->ParseMessage(_internal_add_pieces(), ptr); - CHK_(ptr); - if (!ctx->DataAvailable(ptr)) break; - } while (::PROTOBUF_NAMESPACE_ID::internal::ExpectTag<10>(ptr)); - } else goto handle_unusual; - continue; - // optional .sentencepiece.TrainerSpec trainer_spec = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 18)) { - ptr = ctx->ParseMessage(_internal_mutable_trainer_spec(), ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional .sentencepiece.NormalizerSpec normalizer_spec = 3; - case 3: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 26)) { - ptr = ctx->ParseMessage(_internal_mutable_normalizer_spec(), ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional .sentencepiece.SelfTestData self_test_data = 4; - case 4: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 34)) { - ptr = ctx->ParseMessage(_internal_mutable_self_test_data(), ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - // optional .sentencepiece.NormalizerSpec denormalizer_spec = 5; - case 5: - if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 42)) { - ptr = ctx->ParseMessage(_internal_mutable_denormalizer_spec(), ptr); - CHK_(ptr); - } else goto handle_unusual; - continue; - default: { - handle_unusual: - if ((tag & 7) == 4 || tag == 0) { - ctx->SetLastTag(tag); - goto success; - } - if ((1600u <= tag)) { - ptr = _extensions_.ParseField(tag, ptr, - internal_default_instance(), &_internal_metadata_, ctx); - CHK_(ptr != nullptr); - continue; - } - ptr = UnknownFieldParse(tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - continue; - } - } // switch - } // while -success: - _has_bits_.Or(has_bits); - return ptr; -failure: - ptr = nullptr; - goto success; -#undef CHK_ -} - -::PROTOBUF_NAMESPACE_ID::uint8* ModelProto::_InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:sentencepiece.ModelProto) - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - // repeated .sentencepiece.ModelProto.SentencePiece pieces = 1; - for (unsigned int i = 0, - n = static_cast(this->_internal_pieces_size()); i < n; i++) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage(1, this->_internal_pieces(i), target, stream); - } - - cached_has_bits = _has_bits_[0]; - // optional .sentencepiece.TrainerSpec trainer_spec = 2; - if (cached_has_bits & 0x00000001u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage( - 2, _Internal::trainer_spec(this), target, stream); - } - - // optional .sentencepiece.NormalizerSpec normalizer_spec = 3; - if (cached_has_bits & 0x00000002u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage( - 3, _Internal::normalizer_spec(this), target, stream); - } - - // optional .sentencepiece.SelfTestData self_test_data = 4; - if (cached_has_bits & 0x00000004u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage( - 4, _Internal::self_test_data(this), target, stream); - } - - // optional .sentencepiece.NormalizerSpec denormalizer_spec = 5; - if (cached_has_bits & 0x00000008u) { - target = stream->EnsureSpace(target); - target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite:: - InternalWriteMessage( - 5, _Internal::denormalizer_spec(this), target, stream); - } - - // Extension range [200, 536870912) - target = _extensions_._InternalSerialize( - 200, 536870912, target, stream); - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:sentencepiece.ModelProto) - return target; -} - -size_t ModelProto::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:sentencepiece.ModelProto) - size_t total_size = 0; - - total_size += _extensions_.ByteSize(); - - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - // repeated .sentencepiece.ModelProto.SentencePiece pieces = 1; - total_size += 1UL * this->_internal_pieces_size(); - for (const auto& msg : this->pieces_) { - total_size += - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize(msg); - } - - cached_has_bits = _has_bits_[0]; - if (cached_has_bits & 0x0000000fu) { - // optional .sentencepiece.TrainerSpec trainer_spec = 2; - if (cached_has_bits & 0x00000001u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( - *trainer_spec_); - } - - // optional .sentencepiece.NormalizerSpec normalizer_spec = 3; - if (cached_has_bits & 0x00000002u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( - *normalizer_spec_); - } - - // optional .sentencepiece.SelfTestData self_test_data = 4; - if (cached_has_bits & 0x00000004u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( - *self_test_data_); - } - - // optional .sentencepiece.NormalizerSpec denormalizer_spec = 5; - if (cached_has_bits & 0x00000008u) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::MessageSize( - *denormalizer_spec_); - } - - } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; -} - -void ModelProto::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::PROTOBUF_NAMESPACE_ID::internal::DownCast( - &from)); -} - -void ModelProto::MergeFrom(const ModelProto& from) { -// @@protoc_insertion_point(class_specific_merge_from_start:sentencepiece.ModelProto) - GOOGLE_DCHECK_NE(&from, this); - _extensions_.MergeFrom(from._extensions_); - _internal_metadata_.MergeFrom(from._internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; - (void) cached_has_bits; - - pieces_.MergeFrom(from.pieces_); - cached_has_bits = from._has_bits_[0]; - if (cached_has_bits & 0x0000000fu) { - if (cached_has_bits & 0x00000001u) { - _internal_mutable_trainer_spec()->::sentencepiece::TrainerSpec::MergeFrom(from._internal_trainer_spec()); - } - if (cached_has_bits & 0x00000002u) { - _internal_mutable_normalizer_spec()->::sentencepiece::NormalizerSpec::MergeFrom(from._internal_normalizer_spec()); - } - if (cached_has_bits & 0x00000004u) { - _internal_mutable_self_test_data()->::sentencepiece::SelfTestData::MergeFrom(from._internal_self_test_data()); - } - if (cached_has_bits & 0x00000008u) { - _internal_mutable_denormalizer_spec()->::sentencepiece::NormalizerSpec::MergeFrom(from._internal_denormalizer_spec()); - } - } -} - -void ModelProto::CopyFrom(const ModelProto& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:sentencepiece.ModelProto) - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ModelProto::IsInitialized() const { - if (!_extensions_.IsInitialized()) { - return false; - } - - if (!::PROTOBUF_NAMESPACE_ID::internal::AllAreInitialized(pieces_)) return false; - if (_internal_has_trainer_spec()) { - if (!trainer_spec_->IsInitialized()) return false; - } - if (_internal_has_normalizer_spec()) { - if (!normalizer_spec_->IsInitialized()) return false; - } - if (_internal_has_self_test_data()) { - if (!self_test_data_->IsInitialized()) return false; - } - if (_internal_has_denormalizer_spec()) { - if (!denormalizer_spec_->IsInitialized()) return false; - } - return true; -} - -void ModelProto::InternalSwap(ModelProto* other) { - using std::swap; - _extensions_.Swap(&other->_extensions_); - _internal_metadata_.Swap(&other->_internal_metadata_); - swap(_has_bits_[0], other->_has_bits_[0]); - pieces_.InternalSwap(&other->pieces_); - ::PROTOBUF_NAMESPACE_ID::internal::memswap< - PROTOBUF_FIELD_OFFSET(ModelProto, denormalizer_spec_) - + sizeof(ModelProto::denormalizer_spec_) - - PROTOBUF_FIELD_OFFSET(ModelProto, trainer_spec_)>( - reinterpret_cast(&trainer_spec_), - reinterpret_cast(&other->trainer_spec_)); -} - -std::string ModelProto::GetTypeName() const { - return "sentencepiece.ModelProto"; -} - - -// @@protoc_insertion_point(namespace_scope) -} // namespace sentencepiece -PROTOBUF_NAMESPACE_OPEN -template<> PROTOBUF_NOINLINE ::sentencepiece::TrainerSpec* Arena::CreateMaybeMessage< ::sentencepiece::TrainerSpec >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::TrainerSpec >(arena); -} -template<> PROTOBUF_NOINLINE ::sentencepiece::NormalizerSpec* Arena::CreateMaybeMessage< ::sentencepiece::NormalizerSpec >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::NormalizerSpec >(arena); -} -template<> PROTOBUF_NOINLINE ::sentencepiece::SelfTestData_Sample* Arena::CreateMaybeMessage< ::sentencepiece::SelfTestData_Sample >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::SelfTestData_Sample >(arena); -} -template<> PROTOBUF_NOINLINE ::sentencepiece::SelfTestData* Arena::CreateMaybeMessage< ::sentencepiece::SelfTestData >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::SelfTestData >(arena); -} -template<> PROTOBUF_NOINLINE ::sentencepiece::ModelProto_SentencePiece* Arena::CreateMaybeMessage< ::sentencepiece::ModelProto_SentencePiece >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::ModelProto_SentencePiece >(arena); -} -template<> PROTOBUF_NOINLINE ::sentencepiece::ModelProto* Arena::CreateMaybeMessage< ::sentencepiece::ModelProto >(Arena* arena) { - return Arena::CreateMessageInternal< ::sentencepiece::ModelProto >(arena); -} -PROTOBUF_NAMESPACE_CLOSE - -// @@protoc_insertion_point(global_scope) -#include diff --git a/src/builtin_pb/sentencepiece_model.pb.h b/src/builtin_pb/sentencepiece_model.pb.h deleted file mode 100644 index 3c7388136..000000000 --- a/src/builtin_pb/sentencepiece_model.pb.h +++ /dev/null @@ -1,5030 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: sentencepiece_model.proto - -#ifndef GOOGLE_PROTOBUF_INCLUDED_sentencepiece_5fmodel_2eproto -#define GOOGLE_PROTOBUF_INCLUDED_sentencepiece_5fmodel_2eproto - -#include -#include - -#include -#if PROTOBUF_VERSION < 3014000 -#error This file was generated by a newer version of protoc which is -#error incompatible with your Protocol Buffer headers. Please update -#error your headers. -#endif -#if 3014000 < PROTOBUF_MIN_PROTOC_VERSION -#error This file was generated by an older version of protoc which is -#error incompatible with your Protocol Buffer headers. Please -#error regenerate this file with a newer version of protoc. -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include // IWYU pragma: export -#include // IWYU pragma: export -#include -// @@protoc_insertion_point(includes) -#include -#define PROTOBUF_INTERNAL_EXPORT_sentencepiece_5fmodel_2eproto -PROTOBUF_NAMESPACE_OPEN -namespace internal { -class AnyMetadata; -} // namespace internal -PROTOBUF_NAMESPACE_CLOSE - -// Internal implementation detail -- do not use these members. -struct TableStruct_sentencepiece_5fmodel_2eproto { - static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::AuxiliaryParseTableField aux[] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[6] - PROTOBUF_SECTION_VARIABLE(protodesc_cold); - static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; - static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; - static const ::PROTOBUF_NAMESPACE_ID::uint32 offsets[]; -}; -namespace sentencepiece { -class ModelProto; -class ModelProtoDefaultTypeInternal; -extern ModelProtoDefaultTypeInternal _ModelProto_default_instance_; -class ModelProto_SentencePiece; -class ModelProto_SentencePieceDefaultTypeInternal; -extern ModelProto_SentencePieceDefaultTypeInternal _ModelProto_SentencePiece_default_instance_; -class NormalizerSpec; -class NormalizerSpecDefaultTypeInternal; -extern NormalizerSpecDefaultTypeInternal _NormalizerSpec_default_instance_; -class SelfTestData; -class SelfTestDataDefaultTypeInternal; -extern SelfTestDataDefaultTypeInternal _SelfTestData_default_instance_; -class SelfTestData_Sample; -class SelfTestData_SampleDefaultTypeInternal; -extern SelfTestData_SampleDefaultTypeInternal _SelfTestData_Sample_default_instance_; -class TrainerSpec; -class TrainerSpecDefaultTypeInternal; -extern TrainerSpecDefaultTypeInternal _TrainerSpec_default_instance_; -} // namespace sentencepiece -PROTOBUF_NAMESPACE_OPEN -template<> ::sentencepiece::ModelProto* Arena::CreateMaybeMessage<::sentencepiece::ModelProto>(Arena*); -template<> ::sentencepiece::ModelProto_SentencePiece* Arena::CreateMaybeMessage<::sentencepiece::ModelProto_SentencePiece>(Arena*); -template<> ::sentencepiece::NormalizerSpec* Arena::CreateMaybeMessage<::sentencepiece::NormalizerSpec>(Arena*); -template<> ::sentencepiece::SelfTestData* Arena::CreateMaybeMessage<::sentencepiece::SelfTestData>(Arena*); -template<> ::sentencepiece::SelfTestData_Sample* Arena::CreateMaybeMessage<::sentencepiece::SelfTestData_Sample>(Arena*); -template<> ::sentencepiece::TrainerSpec* Arena::CreateMaybeMessage<::sentencepiece::TrainerSpec>(Arena*); -PROTOBUF_NAMESPACE_CLOSE -namespace sentencepiece { - -enum TrainerSpec_ModelType : int { - TrainerSpec_ModelType_UNIGRAM = 1, - TrainerSpec_ModelType_BPE = 2, - TrainerSpec_ModelType_WORD = 3, - TrainerSpec_ModelType_CHAR = 4 -}; -bool TrainerSpec_ModelType_IsValid(int value); -constexpr TrainerSpec_ModelType TrainerSpec_ModelType_ModelType_MIN = TrainerSpec_ModelType_UNIGRAM; -constexpr TrainerSpec_ModelType TrainerSpec_ModelType_ModelType_MAX = TrainerSpec_ModelType_CHAR; -constexpr int TrainerSpec_ModelType_ModelType_ARRAYSIZE = TrainerSpec_ModelType_ModelType_MAX + 1; - -const std::string& TrainerSpec_ModelType_Name(TrainerSpec_ModelType value); -template -inline const std::string& TrainerSpec_ModelType_Name(T enum_t_value) { - static_assert(::std::is_same::value || - ::std::is_integral::value, - "Incorrect type passed to function TrainerSpec_ModelType_Name."); - return TrainerSpec_ModelType_Name(static_cast(enum_t_value)); -} -bool TrainerSpec_ModelType_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, TrainerSpec_ModelType* value); -enum ModelProto_SentencePiece_Type : int { - ModelProto_SentencePiece_Type_NORMAL = 1, - ModelProto_SentencePiece_Type_UNKNOWN = 2, - ModelProto_SentencePiece_Type_CONTROL = 3, - ModelProto_SentencePiece_Type_USER_DEFINED = 4, - ModelProto_SentencePiece_Type_BYTE = 6, - ModelProto_SentencePiece_Type_UNUSED = 5 -}; -bool ModelProto_SentencePiece_Type_IsValid(int value); -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece_Type_Type_MIN = ModelProto_SentencePiece_Type_NORMAL; -constexpr ModelProto_SentencePiece_Type ModelProto_SentencePiece_Type_Type_MAX = ModelProto_SentencePiece_Type_BYTE; -constexpr int ModelProto_SentencePiece_Type_Type_ARRAYSIZE = ModelProto_SentencePiece_Type_Type_MAX + 1; - -const std::string& ModelProto_SentencePiece_Type_Name(ModelProto_SentencePiece_Type value); -template -inline const std::string& ModelProto_SentencePiece_Type_Name(T enum_t_value) { - static_assert(::std::is_same::value || - ::std::is_integral::value, - "Incorrect type passed to function ModelProto_SentencePiece_Type_Name."); - return ModelProto_SentencePiece_Type_Name(static_cast(enum_t_value)); -} -bool ModelProto_SentencePiece_Type_Parse( - ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, ModelProto_SentencePiece_Type* value); -// =================================================================== - -class TrainerSpec PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.TrainerSpec) */ { - public: - inline TrainerSpec() : TrainerSpec(nullptr) {} - virtual ~TrainerSpec(); - - TrainerSpec(const TrainerSpec& from); - TrainerSpec(TrainerSpec&& from) noexcept - : TrainerSpec() { - *this = ::std::move(from); - } - - inline TrainerSpec& operator=(const TrainerSpec& from) { - CopyFrom(from); - return *this; - } - inline TrainerSpec& operator=(TrainerSpec&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const TrainerSpec& default_instance(); - - static inline const TrainerSpec* internal_default_instance() { - return reinterpret_cast( - &_TrainerSpec_default_instance_); - } - static constexpr int kIndexInFileMessages = - 0; - - friend void swap(TrainerSpec& a, TrainerSpec& b) { - a.Swap(&b); - } - inline void Swap(TrainerSpec* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(TrainerSpec* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline TrainerSpec* New() const final { - return CreateMaybeMessage(nullptr); - } - - TrainerSpec* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const TrainerSpec& from); - void MergeFrom(const TrainerSpec& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(TrainerSpec* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.TrainerSpec"; - } - protected: - explicit TrainerSpec(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - typedef TrainerSpec_ModelType ModelType; - static constexpr ModelType UNIGRAM = - TrainerSpec_ModelType_UNIGRAM; - static constexpr ModelType BPE = - TrainerSpec_ModelType_BPE; - static constexpr ModelType WORD = - TrainerSpec_ModelType_WORD; - static constexpr ModelType CHAR = - TrainerSpec_ModelType_CHAR; - static inline bool ModelType_IsValid(int value) { - return TrainerSpec_ModelType_IsValid(value); - } - static constexpr ModelType ModelType_MIN = - TrainerSpec_ModelType_ModelType_MIN; - static constexpr ModelType ModelType_MAX = - TrainerSpec_ModelType_ModelType_MAX; - static constexpr int ModelType_ARRAYSIZE = - TrainerSpec_ModelType_ModelType_ARRAYSIZE; - template - static inline const std::string& ModelType_Name(T enum_t_value) { - static_assert(::std::is_same::value || - ::std::is_integral::value, - "Incorrect type passed to function ModelType_Name."); - return TrainerSpec_ModelType_Name(enum_t_value); - } - static inline bool ModelType_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, - ModelType* value) { - return TrainerSpec_ModelType_Parse(name, value); - } - - // accessors ------------------------------------------------------- - - enum : int { - kInputFieldNumber = 1, - kAcceptLanguageFieldNumber = 5, - kControlSymbolsFieldNumber = 30, - kUserDefinedSymbolsFieldNumber = 31, - kModelPrefixFieldNumber = 2, - kInputFormatFieldNumber = 7, - kRequiredCharsFieldNumber = 36, - kUnkSurfaceFieldNumber = 44, - kUnkPieceFieldNumber = 45, - kBosPieceFieldNumber = 46, - kEosPieceFieldNumber = 47, - kPadPieceFieldNumber = 48, - kPretokenizationDelimiterFieldNumber = 53, - kSeedSentencepiecesFileFieldNumber = 54, - kSelfTestSampleSizeFieldNumber = 6, - kMiningSentenceSizeFieldNumber = 12, - kInputSentenceSizeFieldNumber = 11, - kTrainingSentenceSizeFieldNumber = 13, - kEnableDifferentialPrivacyFieldNumber = 50, - kTreatWhitespaceAsSuffixFieldNumber = 24, - kAllowWhitespaceOnlyPiecesFieldNumber = 26, - kSplitDigitsFieldNumber = 25, - kByteFallbackFieldNumber = 35, - kUseAllVocabFieldNumber = 34, - kTrainExtremelyLargeCorpusFieldNumber = 49, - kUnkIdFieldNumber = 40, - kDifferentialPrivacyNoiseLevelFieldNumber = 51, - kDifferentialPrivacyClippingThresholdFieldNumber = 52, - kModelTypeFieldNumber = 3, - kVocabSizeFieldNumber = 4, - kCharacterCoverageFieldNumber = 10, - kSeedSentencepieceSizeFieldNumber = 14, - kShrinkingFactorFieldNumber = 15, - kNumThreadsFieldNumber = 16, - kNumSubIterationsFieldNumber = 17, - kMaxSentenceLengthFieldNumber = 18, - kMaxSentencepieceLengthFieldNumber = 20, - kShuffleInputSentenceFieldNumber = 19, - kSplitByUnicodeScriptFieldNumber = 21, - kSplitByNumberFieldNumber = 23, - kSplitByWhitespaceFieldNumber = 22, - kVocabularyOutputPieceScoreFieldNumber = 32, - kHardVocabLimitFieldNumber = 33, - kBosIdFieldNumber = 41, - kEosIdFieldNumber = 42, - kPadIdFieldNumber = 43, - }; - // repeated string input = 1; - int input_size() const; - private: - int _internal_input_size() const; - public: - void clear_input(); - const std::string& input(int index) const; - std::string* mutable_input(int index); - void set_input(int index, const std::string& value); - void set_input(int index, std::string&& value); - void set_input(int index, const char* value); - void set_input(int index, const char* value, size_t size); - std::string* add_input(); - void add_input(const std::string& value); - void add_input(std::string&& value); - void add_input(const char* value); - void add_input(const char* value, size_t size); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& input() const; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_input(); - private: - const std::string& _internal_input(int index) const; - std::string* _internal_add_input(); - public: - - // repeated string accept_language = 5; - int accept_language_size() const; - private: - int _internal_accept_language_size() const; - public: - void clear_accept_language(); - const std::string& accept_language(int index) const; - std::string* mutable_accept_language(int index); - void set_accept_language(int index, const std::string& value); - void set_accept_language(int index, std::string&& value); - void set_accept_language(int index, const char* value); - void set_accept_language(int index, const char* value, size_t size); - std::string* add_accept_language(); - void add_accept_language(const std::string& value); - void add_accept_language(std::string&& value); - void add_accept_language(const char* value); - void add_accept_language(const char* value, size_t size); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& accept_language() const; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_accept_language(); - private: - const std::string& _internal_accept_language(int index) const; - std::string* _internal_add_accept_language(); - public: - - // repeated string control_symbols = 30; - int control_symbols_size() const; - private: - int _internal_control_symbols_size() const; - public: - void clear_control_symbols(); - const std::string& control_symbols(int index) const; - std::string* mutable_control_symbols(int index); - void set_control_symbols(int index, const std::string& value); - void set_control_symbols(int index, std::string&& value); - void set_control_symbols(int index, const char* value); - void set_control_symbols(int index, const char* value, size_t size); - std::string* add_control_symbols(); - void add_control_symbols(const std::string& value); - void add_control_symbols(std::string&& value); - void add_control_symbols(const char* value); - void add_control_symbols(const char* value, size_t size); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& control_symbols() const; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_control_symbols(); - private: - const std::string& _internal_control_symbols(int index) const; - std::string* _internal_add_control_symbols(); - public: - - // repeated string user_defined_symbols = 31; - int user_defined_symbols_size() const; - private: - int _internal_user_defined_symbols_size() const; - public: - void clear_user_defined_symbols(); - const std::string& user_defined_symbols(int index) const; - std::string* mutable_user_defined_symbols(int index); - void set_user_defined_symbols(int index, const std::string& value); - void set_user_defined_symbols(int index, std::string&& value); - void set_user_defined_symbols(int index, const char* value); - void set_user_defined_symbols(int index, const char* value, size_t size); - std::string* add_user_defined_symbols(); - void add_user_defined_symbols(const std::string& value); - void add_user_defined_symbols(std::string&& value); - void add_user_defined_symbols(const char* value); - void add_user_defined_symbols(const char* value, size_t size); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& user_defined_symbols() const; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* mutable_user_defined_symbols(); - private: - const std::string& _internal_user_defined_symbols(int index) const; - std::string* _internal_add_user_defined_symbols(); - public: - - // optional string model_prefix = 2; - bool has_model_prefix() const; - private: - bool _internal_has_model_prefix() const; - public: - void clear_model_prefix(); - const std::string& model_prefix() const; - void set_model_prefix(const std::string& value); - void set_model_prefix(std::string&& value); - void set_model_prefix(const char* value); - void set_model_prefix(const char* value, size_t size); - std::string* mutable_model_prefix(); - std::string* release_model_prefix(); - void set_allocated_model_prefix(std::string* model_prefix); - private: - const std::string& _internal_model_prefix() const; - void _internal_set_model_prefix(const std::string& value); - std::string* _internal_mutable_model_prefix(); - public: - - // optional string input_format = 7; - bool has_input_format() const; - private: - bool _internal_has_input_format() const; - public: - void clear_input_format(); - const std::string& input_format() const; - void set_input_format(const std::string& value); - void set_input_format(std::string&& value); - void set_input_format(const char* value); - void set_input_format(const char* value, size_t size); - std::string* mutable_input_format(); - std::string* release_input_format(); - void set_allocated_input_format(std::string* input_format); - private: - const std::string& _internal_input_format() const; - void _internal_set_input_format(const std::string& value); - std::string* _internal_mutable_input_format(); - public: - - // optional string required_chars = 36; - bool has_required_chars() const; - private: - bool _internal_has_required_chars() const; - public: - void clear_required_chars(); - const std::string& required_chars() const; - void set_required_chars(const std::string& value); - void set_required_chars(std::string&& value); - void set_required_chars(const char* value); - void set_required_chars(const char* value, size_t size); - std::string* mutable_required_chars(); - std::string* release_required_chars(); - void set_allocated_required_chars(std::string* required_chars); - private: - const std::string& _internal_required_chars() const; - void _internal_set_required_chars(const std::string& value); - std::string* _internal_mutable_required_chars(); - public: - - // optional string unk_surface = 44 [default = " \342\201\207 "]; - bool has_unk_surface() const; - private: - bool _internal_has_unk_surface() const; - public: - void clear_unk_surface(); - const std::string& unk_surface() const; - void set_unk_surface(const std::string& value); - void set_unk_surface(std::string&& value); - void set_unk_surface(const char* value); - void set_unk_surface(const char* value, size_t size); - std::string* mutable_unk_surface(); - std::string* release_unk_surface(); - void set_allocated_unk_surface(std::string* unk_surface); - private: - const std::string& _internal_unk_surface() const; - void _internal_set_unk_surface(const std::string& value); - std::string* _internal_mutable_unk_surface(); - public: - - // optional string unk_piece = 45 [default = ""]; - bool has_unk_piece() const; - private: - bool _internal_has_unk_piece() const; - public: - void clear_unk_piece(); - const std::string& unk_piece() const; - void set_unk_piece(const std::string& value); - void set_unk_piece(std::string&& value); - void set_unk_piece(const char* value); - void set_unk_piece(const char* value, size_t size); - std::string* mutable_unk_piece(); - std::string* release_unk_piece(); - void set_allocated_unk_piece(std::string* unk_piece); - private: - const std::string& _internal_unk_piece() const; - void _internal_set_unk_piece(const std::string& value); - std::string* _internal_mutable_unk_piece(); - public: - - // optional string bos_piece = 46 [default = ""]; - bool has_bos_piece() const; - private: - bool _internal_has_bos_piece() const; - public: - void clear_bos_piece(); - const std::string& bos_piece() const; - void set_bos_piece(const std::string& value); - void set_bos_piece(std::string&& value); - void set_bos_piece(const char* value); - void set_bos_piece(const char* value, size_t size); - std::string* mutable_bos_piece(); - std::string* release_bos_piece(); - void set_allocated_bos_piece(std::string* bos_piece); - private: - const std::string& _internal_bos_piece() const; - void _internal_set_bos_piece(const std::string& value); - std::string* _internal_mutable_bos_piece(); - public: - - // optional string eos_piece = 47 [default = ""]; - bool has_eos_piece() const; - private: - bool _internal_has_eos_piece() const; - public: - void clear_eos_piece(); - const std::string& eos_piece() const; - void set_eos_piece(const std::string& value); - void set_eos_piece(std::string&& value); - void set_eos_piece(const char* value); - void set_eos_piece(const char* value, size_t size); - std::string* mutable_eos_piece(); - std::string* release_eos_piece(); - void set_allocated_eos_piece(std::string* eos_piece); - private: - const std::string& _internal_eos_piece() const; - void _internal_set_eos_piece(const std::string& value); - std::string* _internal_mutable_eos_piece(); - public: - - // optional string pad_piece = 48 [default = ""]; - bool has_pad_piece() const; - private: - bool _internal_has_pad_piece() const; - public: - void clear_pad_piece(); - const std::string& pad_piece() const; - void set_pad_piece(const std::string& value); - void set_pad_piece(std::string&& value); - void set_pad_piece(const char* value); - void set_pad_piece(const char* value, size_t size); - std::string* mutable_pad_piece(); - std::string* release_pad_piece(); - void set_allocated_pad_piece(std::string* pad_piece); - private: - const std::string& _internal_pad_piece() const; - void _internal_set_pad_piece(const std::string& value); - std::string* _internal_mutable_pad_piece(); - public: - - // optional string pretokenization_delimiter = 53 [default = ""]; - bool has_pretokenization_delimiter() const; - private: - bool _internal_has_pretokenization_delimiter() const; - public: - void clear_pretokenization_delimiter(); - const std::string& pretokenization_delimiter() const; - void set_pretokenization_delimiter(const std::string& value); - void set_pretokenization_delimiter(std::string&& value); - void set_pretokenization_delimiter(const char* value); - void set_pretokenization_delimiter(const char* value, size_t size); - std::string* mutable_pretokenization_delimiter(); - std::string* release_pretokenization_delimiter(); - void set_allocated_pretokenization_delimiter(std::string* pretokenization_delimiter); - private: - const std::string& _internal_pretokenization_delimiter() const; - void _internal_set_pretokenization_delimiter(const std::string& value); - std::string* _internal_mutable_pretokenization_delimiter(); - public: - - // optional string seed_sentencepieces_file = 54 [default = ""]; - bool has_seed_sentencepieces_file() const; - private: - bool _internal_has_seed_sentencepieces_file() const; - public: - void clear_seed_sentencepieces_file(); - const std::string& seed_sentencepieces_file() const; - void set_seed_sentencepieces_file(const std::string& value); - void set_seed_sentencepieces_file(std::string&& value); - void set_seed_sentencepieces_file(const char* value); - void set_seed_sentencepieces_file(const char* value, size_t size); - std::string* mutable_seed_sentencepieces_file(); - std::string* release_seed_sentencepieces_file(); - void set_allocated_seed_sentencepieces_file(std::string* seed_sentencepieces_file); - private: - const std::string& _internal_seed_sentencepieces_file() const; - void _internal_set_seed_sentencepieces_file(const std::string& value); - std::string* _internal_mutable_seed_sentencepieces_file(); - public: - - // optional int32 self_test_sample_size = 6 [default = 0]; - bool has_self_test_sample_size() const; - private: - bool _internal_has_self_test_sample_size() const; - public: - void clear_self_test_sample_size(); - ::PROTOBUF_NAMESPACE_ID::int32 self_test_sample_size() const; - void set_self_test_sample_size(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_self_test_sample_size() const; - void _internal_set_self_test_sample_size(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional int32 mining_sentence_size = 12 [deprecated = true]; - PROTOBUF_DEPRECATED bool has_mining_sentence_size() const; - private: - bool _internal_has_mining_sentence_size() const; - public: - PROTOBUF_DEPRECATED void clear_mining_sentence_size(); - PROTOBUF_DEPRECATED ::PROTOBUF_NAMESPACE_ID::int32 mining_sentence_size() const; - PROTOBUF_DEPRECATED void set_mining_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_mining_sentence_size() const; - void _internal_set_mining_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional uint64 input_sentence_size = 11 [default = 0]; - bool has_input_sentence_size() const; - private: - bool _internal_has_input_sentence_size() const; - public: - void clear_input_sentence_size(); - ::PROTOBUF_NAMESPACE_ID::uint64 input_sentence_size() const; - void set_input_sentence_size(::PROTOBUF_NAMESPACE_ID::uint64 value); - private: - ::PROTOBUF_NAMESPACE_ID::uint64 _internal_input_sentence_size() const; - void _internal_set_input_sentence_size(::PROTOBUF_NAMESPACE_ID::uint64 value); - public: - - // optional int32 training_sentence_size = 13 [deprecated = true]; - PROTOBUF_DEPRECATED bool has_training_sentence_size() const; - private: - bool _internal_has_training_sentence_size() const; - public: - PROTOBUF_DEPRECATED void clear_training_sentence_size(); - PROTOBUF_DEPRECATED ::PROTOBUF_NAMESPACE_ID::int32 training_sentence_size() const; - PROTOBUF_DEPRECATED void set_training_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_training_sentence_size() const; - void _internal_set_training_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional bool enable_differential_privacy = 50 [default = false]; - bool has_enable_differential_privacy() const; - private: - bool _internal_has_enable_differential_privacy() const; - public: - void clear_enable_differential_privacy(); - bool enable_differential_privacy() const; - void set_enable_differential_privacy(bool value); - private: - bool _internal_enable_differential_privacy() const; - void _internal_set_enable_differential_privacy(bool value); - public: - - // optional bool treat_whitespace_as_suffix = 24 [default = false]; - bool has_treat_whitespace_as_suffix() const; - private: - bool _internal_has_treat_whitespace_as_suffix() const; - public: - void clear_treat_whitespace_as_suffix(); - bool treat_whitespace_as_suffix() const; - void set_treat_whitespace_as_suffix(bool value); - private: - bool _internal_treat_whitespace_as_suffix() const; - void _internal_set_treat_whitespace_as_suffix(bool value); - public: - - // optional bool allow_whitespace_only_pieces = 26 [default = false]; - bool has_allow_whitespace_only_pieces() const; - private: - bool _internal_has_allow_whitespace_only_pieces() const; - public: - void clear_allow_whitespace_only_pieces(); - bool allow_whitespace_only_pieces() const; - void set_allow_whitespace_only_pieces(bool value); - private: - bool _internal_allow_whitespace_only_pieces() const; - void _internal_set_allow_whitespace_only_pieces(bool value); - public: - - // optional bool split_digits = 25 [default = false]; - bool has_split_digits() const; - private: - bool _internal_has_split_digits() const; - public: - void clear_split_digits(); - bool split_digits() const; - void set_split_digits(bool value); - private: - bool _internal_split_digits() const; - void _internal_set_split_digits(bool value); - public: - - // optional bool byte_fallback = 35 [default = false]; - bool has_byte_fallback() const; - private: - bool _internal_has_byte_fallback() const; - public: - void clear_byte_fallback(); - bool byte_fallback() const; - void set_byte_fallback(bool value); - private: - bool _internal_byte_fallback() const; - void _internal_set_byte_fallback(bool value); - public: - - // optional bool use_all_vocab = 34 [default = false]; - bool has_use_all_vocab() const; - private: - bool _internal_has_use_all_vocab() const; - public: - void clear_use_all_vocab(); - bool use_all_vocab() const; - void set_use_all_vocab(bool value); - private: - bool _internal_use_all_vocab() const; - void _internal_set_use_all_vocab(bool value); - public: - - // optional bool train_extremely_large_corpus = 49 [default = false]; - bool has_train_extremely_large_corpus() const; - private: - bool _internal_has_train_extremely_large_corpus() const; - public: - void clear_train_extremely_large_corpus(); - bool train_extremely_large_corpus() const; - void set_train_extremely_large_corpus(bool value); - private: - bool _internal_train_extremely_large_corpus() const; - void _internal_set_train_extremely_large_corpus(bool value); - public: - - // optional int32 unk_id = 40 [default = 0]; - bool has_unk_id() const; - private: - bool _internal_has_unk_id() const; - public: - void clear_unk_id(); - ::PROTOBUF_NAMESPACE_ID::int32 unk_id() const; - void set_unk_id(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_unk_id() const; - void _internal_set_unk_id(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional float differential_privacy_noise_level = 51 [default = 0]; - bool has_differential_privacy_noise_level() const; - private: - bool _internal_has_differential_privacy_noise_level() const; - public: - void clear_differential_privacy_noise_level(); - float differential_privacy_noise_level() const; - void set_differential_privacy_noise_level(float value); - private: - float _internal_differential_privacy_noise_level() const; - void _internal_set_differential_privacy_noise_level(float value); - public: - - // optional uint64 differential_privacy_clipping_threshold = 52 [default = 0]; - bool has_differential_privacy_clipping_threshold() const; - private: - bool _internal_has_differential_privacy_clipping_threshold() const; - public: - void clear_differential_privacy_clipping_threshold(); - ::PROTOBUF_NAMESPACE_ID::uint64 differential_privacy_clipping_threshold() const; - void set_differential_privacy_clipping_threshold(::PROTOBUF_NAMESPACE_ID::uint64 value); - private: - ::PROTOBUF_NAMESPACE_ID::uint64 _internal_differential_privacy_clipping_threshold() const; - void _internal_set_differential_privacy_clipping_threshold(::PROTOBUF_NAMESPACE_ID::uint64 value); - public: - - // optional .sentencepiece.TrainerSpec.ModelType model_type = 3 [default = UNIGRAM]; - bool has_model_type() const; - private: - bool _internal_has_model_type() const; - public: - void clear_model_type(); - ::sentencepiece::TrainerSpec_ModelType model_type() const; - void set_model_type(::sentencepiece::TrainerSpec_ModelType value); - private: - ::sentencepiece::TrainerSpec_ModelType _internal_model_type() const; - void _internal_set_model_type(::sentencepiece::TrainerSpec_ModelType value); - public: - - // optional int32 vocab_size = 4 [default = 8000]; - bool has_vocab_size() const; - private: - bool _internal_has_vocab_size() const; - public: - void clear_vocab_size(); - ::PROTOBUF_NAMESPACE_ID::int32 vocab_size() const; - void set_vocab_size(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_vocab_size() const; - void _internal_set_vocab_size(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional float character_coverage = 10 [default = 0.9995]; - bool has_character_coverage() const; - private: - bool _internal_has_character_coverage() const; - public: - void clear_character_coverage(); - float character_coverage() const; - void set_character_coverage(float value); - private: - float _internal_character_coverage() const; - void _internal_set_character_coverage(float value); - public: - - // optional int32 seed_sentencepiece_size = 14 [default = 1000000]; - bool has_seed_sentencepiece_size() const; - private: - bool _internal_has_seed_sentencepiece_size() const; - public: - void clear_seed_sentencepiece_size(); - ::PROTOBUF_NAMESPACE_ID::int32 seed_sentencepiece_size() const; - void set_seed_sentencepiece_size(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_seed_sentencepiece_size() const; - void _internal_set_seed_sentencepiece_size(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional float shrinking_factor = 15 [default = 0.75]; - bool has_shrinking_factor() const; - private: - bool _internal_has_shrinking_factor() const; - public: - void clear_shrinking_factor(); - float shrinking_factor() const; - void set_shrinking_factor(float value); - private: - float _internal_shrinking_factor() const; - void _internal_set_shrinking_factor(float value); - public: - - // optional int32 num_threads = 16 [default = 16]; - bool has_num_threads() const; - private: - bool _internal_has_num_threads() const; - public: - void clear_num_threads(); - ::PROTOBUF_NAMESPACE_ID::int32 num_threads() const; - void set_num_threads(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_num_threads() const; - void _internal_set_num_threads(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional int32 num_sub_iterations = 17 [default = 2]; - bool has_num_sub_iterations() const; - private: - bool _internal_has_num_sub_iterations() const; - public: - void clear_num_sub_iterations(); - ::PROTOBUF_NAMESPACE_ID::int32 num_sub_iterations() const; - void set_num_sub_iterations(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_num_sub_iterations() const; - void _internal_set_num_sub_iterations(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional int32 max_sentence_length = 18 [default = 4192]; - bool has_max_sentence_length() const; - private: - bool _internal_has_max_sentence_length() const; - public: - void clear_max_sentence_length(); - ::PROTOBUF_NAMESPACE_ID::int32 max_sentence_length() const; - void set_max_sentence_length(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_max_sentence_length() const; - void _internal_set_max_sentence_length(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional int32 max_sentencepiece_length = 20 [default = 16]; - bool has_max_sentencepiece_length() const; - private: - bool _internal_has_max_sentencepiece_length() const; - public: - void clear_max_sentencepiece_length(); - ::PROTOBUF_NAMESPACE_ID::int32 max_sentencepiece_length() const; - void set_max_sentencepiece_length(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_max_sentencepiece_length() const; - void _internal_set_max_sentencepiece_length(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional bool shuffle_input_sentence = 19 [default = true]; - bool has_shuffle_input_sentence() const; - private: - bool _internal_has_shuffle_input_sentence() const; - public: - void clear_shuffle_input_sentence(); - bool shuffle_input_sentence() const; - void set_shuffle_input_sentence(bool value); - private: - bool _internal_shuffle_input_sentence() const; - void _internal_set_shuffle_input_sentence(bool value); - public: - - // optional bool split_by_unicode_script = 21 [default = true]; - bool has_split_by_unicode_script() const; - private: - bool _internal_has_split_by_unicode_script() const; - public: - void clear_split_by_unicode_script(); - bool split_by_unicode_script() const; - void set_split_by_unicode_script(bool value); - private: - bool _internal_split_by_unicode_script() const; - void _internal_set_split_by_unicode_script(bool value); - public: - - // optional bool split_by_number = 23 [default = true]; - bool has_split_by_number() const; - private: - bool _internal_has_split_by_number() const; - public: - void clear_split_by_number(); - bool split_by_number() const; - void set_split_by_number(bool value); - private: - bool _internal_split_by_number() const; - void _internal_set_split_by_number(bool value); - public: - - // optional bool split_by_whitespace = 22 [default = true]; - bool has_split_by_whitespace() const; - private: - bool _internal_has_split_by_whitespace() const; - public: - void clear_split_by_whitespace(); - bool split_by_whitespace() const; - void set_split_by_whitespace(bool value); - private: - bool _internal_split_by_whitespace() const; - void _internal_set_split_by_whitespace(bool value); - public: - - // optional bool vocabulary_output_piece_score = 32 [default = true]; - bool has_vocabulary_output_piece_score() const; - private: - bool _internal_has_vocabulary_output_piece_score() const; - public: - void clear_vocabulary_output_piece_score(); - bool vocabulary_output_piece_score() const; - void set_vocabulary_output_piece_score(bool value); - private: - bool _internal_vocabulary_output_piece_score() const; - void _internal_set_vocabulary_output_piece_score(bool value); - public: - - // optional bool hard_vocab_limit = 33 [default = true]; - bool has_hard_vocab_limit() const; - private: - bool _internal_has_hard_vocab_limit() const; - public: - void clear_hard_vocab_limit(); - bool hard_vocab_limit() const; - void set_hard_vocab_limit(bool value); - private: - bool _internal_hard_vocab_limit() const; - void _internal_set_hard_vocab_limit(bool value); - public: - - // optional int32 bos_id = 41 [default = 1]; - bool has_bos_id() const; - private: - bool _internal_has_bos_id() const; - public: - void clear_bos_id(); - ::PROTOBUF_NAMESPACE_ID::int32 bos_id() const; - void set_bos_id(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_bos_id() const; - void _internal_set_bos_id(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional int32 eos_id = 42 [default = 2]; - bool has_eos_id() const; - private: - bool _internal_has_eos_id() const; - public: - void clear_eos_id(); - ::PROTOBUF_NAMESPACE_ID::int32 eos_id() const; - void set_eos_id(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_eos_id() const; - void _internal_set_eos_id(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - // optional int32 pad_id = 43 [default = -1]; - bool has_pad_id() const; - private: - bool _internal_has_pad_id() const; - public: - void clear_pad_id(); - ::PROTOBUF_NAMESPACE_ID::int32 pad_id() const; - void set_pad_id(::PROTOBUF_NAMESPACE_ID::int32 value); - private: - ::PROTOBUF_NAMESPACE_ID::int32 _internal_pad_id() const; - void _internal_set_pad_id(::PROTOBUF_NAMESPACE_ID::int32 value); - public: - - GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(TrainerSpec) - // @@protoc_insertion_point(class_scope:sentencepiece.TrainerSpec) - private: - class _Internal; - - ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::HasBits<2> _has_bits_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField input_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField accept_language_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField control_symbols_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField user_defined_symbols_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr model_prefix_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr input_format_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr required_chars_; - static const ::PROTOBUF_NAMESPACE_ID::internal::LazyString _i_give_permission_to_break_this_code_default_unk_surface_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr unk_surface_; - static const ::PROTOBUF_NAMESPACE_ID::internal::LazyString _i_give_permission_to_break_this_code_default_unk_piece_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr unk_piece_; - static const ::PROTOBUF_NAMESPACE_ID::internal::LazyString _i_give_permission_to_break_this_code_default_bos_piece_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr bos_piece_; - static const ::PROTOBUF_NAMESPACE_ID::internal::LazyString _i_give_permission_to_break_this_code_default_eos_piece_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr eos_piece_; - static const ::PROTOBUF_NAMESPACE_ID::internal::LazyString _i_give_permission_to_break_this_code_default_pad_piece_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr pad_piece_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr pretokenization_delimiter_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr seed_sentencepieces_file_; - ::PROTOBUF_NAMESPACE_ID::int32 self_test_sample_size_; - ::PROTOBUF_NAMESPACE_ID::int32 mining_sentence_size_; - ::PROTOBUF_NAMESPACE_ID::uint64 input_sentence_size_; - ::PROTOBUF_NAMESPACE_ID::int32 training_sentence_size_; - bool enable_differential_privacy_; - bool treat_whitespace_as_suffix_; - bool allow_whitespace_only_pieces_; - bool split_digits_; - bool byte_fallback_; - bool use_all_vocab_; - bool train_extremely_large_corpus_; - ::PROTOBUF_NAMESPACE_ID::int32 unk_id_; - float differential_privacy_noise_level_; - ::PROTOBUF_NAMESPACE_ID::uint64 differential_privacy_clipping_threshold_; - int model_type_; - ::PROTOBUF_NAMESPACE_ID::int32 vocab_size_; - float character_coverage_; - ::PROTOBUF_NAMESPACE_ID::int32 seed_sentencepiece_size_; - float shrinking_factor_; - ::PROTOBUF_NAMESPACE_ID::int32 num_threads_; - ::PROTOBUF_NAMESPACE_ID::int32 num_sub_iterations_; - ::PROTOBUF_NAMESPACE_ID::int32 max_sentence_length_; - ::PROTOBUF_NAMESPACE_ID::int32 max_sentencepiece_length_; - bool shuffle_input_sentence_; - bool split_by_unicode_script_; - bool split_by_number_; - bool split_by_whitespace_; - bool vocabulary_output_piece_score_; - bool hard_vocab_limit_; - ::PROTOBUF_NAMESPACE_ID::int32 bos_id_; - ::PROTOBUF_NAMESPACE_ID::int32 eos_id_; - ::PROTOBUF_NAMESPACE_ID::int32 pad_id_; - friend struct ::TableStruct_sentencepiece_5fmodel_2eproto; -}; -// ------------------------------------------------------------------- - -class NormalizerSpec PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.NormalizerSpec) */ { - public: - inline NormalizerSpec() : NormalizerSpec(nullptr) {} - virtual ~NormalizerSpec(); - - NormalizerSpec(const NormalizerSpec& from); - NormalizerSpec(NormalizerSpec&& from) noexcept - : NormalizerSpec() { - *this = ::std::move(from); - } - - inline NormalizerSpec& operator=(const NormalizerSpec& from) { - CopyFrom(from); - return *this; - } - inline NormalizerSpec& operator=(NormalizerSpec&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const NormalizerSpec& default_instance(); - - static inline const NormalizerSpec* internal_default_instance() { - return reinterpret_cast( - &_NormalizerSpec_default_instance_); - } - static constexpr int kIndexInFileMessages = - 1; - - friend void swap(NormalizerSpec& a, NormalizerSpec& b) { - a.Swap(&b); - } - inline void Swap(NormalizerSpec* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(NormalizerSpec* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline NormalizerSpec* New() const final { - return CreateMaybeMessage(nullptr); - } - - NormalizerSpec* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const NormalizerSpec& from); - void MergeFrom(const NormalizerSpec& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(NormalizerSpec* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.NormalizerSpec"; - } - protected: - explicit NormalizerSpec(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - enum : int { - kNameFieldNumber = 1, - kPrecompiledCharsmapFieldNumber = 2, - kNormalizationRuleTsvFieldNumber = 6, - kAddDummyPrefixFieldNumber = 3, - kRemoveExtraWhitespacesFieldNumber = 4, - kEscapeWhitespacesFieldNumber = 5, - }; - // optional string name = 1; - bool has_name() const; - private: - bool _internal_has_name() const; - public: - void clear_name(); - const std::string& name() const; - void set_name(const std::string& value); - void set_name(std::string&& value); - void set_name(const char* value); - void set_name(const char* value, size_t size); - std::string* mutable_name(); - std::string* release_name(); - void set_allocated_name(std::string* name); - private: - const std::string& _internal_name() const; - void _internal_set_name(const std::string& value); - std::string* _internal_mutable_name(); - public: - - // optional bytes precompiled_charsmap = 2; - bool has_precompiled_charsmap() const; - private: - bool _internal_has_precompiled_charsmap() const; - public: - void clear_precompiled_charsmap(); - const std::string& precompiled_charsmap() const; - void set_precompiled_charsmap(const std::string& value); - void set_precompiled_charsmap(std::string&& value); - void set_precompiled_charsmap(const char* value); - void set_precompiled_charsmap(const void* value, size_t size); - std::string* mutable_precompiled_charsmap(); - std::string* release_precompiled_charsmap(); - void set_allocated_precompiled_charsmap(std::string* precompiled_charsmap); - private: - const std::string& _internal_precompiled_charsmap() const; - void _internal_set_precompiled_charsmap(const std::string& value); - std::string* _internal_mutable_precompiled_charsmap(); - public: - - // optional string normalization_rule_tsv = 6; - bool has_normalization_rule_tsv() const; - private: - bool _internal_has_normalization_rule_tsv() const; - public: - void clear_normalization_rule_tsv(); - const std::string& normalization_rule_tsv() const; - void set_normalization_rule_tsv(const std::string& value); - void set_normalization_rule_tsv(std::string&& value); - void set_normalization_rule_tsv(const char* value); - void set_normalization_rule_tsv(const char* value, size_t size); - std::string* mutable_normalization_rule_tsv(); - std::string* release_normalization_rule_tsv(); - void set_allocated_normalization_rule_tsv(std::string* normalization_rule_tsv); - private: - const std::string& _internal_normalization_rule_tsv() const; - void _internal_set_normalization_rule_tsv(const std::string& value); - std::string* _internal_mutable_normalization_rule_tsv(); - public: - - // optional bool add_dummy_prefix = 3 [default = true]; - bool has_add_dummy_prefix() const; - private: - bool _internal_has_add_dummy_prefix() const; - public: - void clear_add_dummy_prefix(); - bool add_dummy_prefix() const; - void set_add_dummy_prefix(bool value); - private: - bool _internal_add_dummy_prefix() const; - void _internal_set_add_dummy_prefix(bool value); - public: - - // optional bool remove_extra_whitespaces = 4 [default = true]; - bool has_remove_extra_whitespaces() const; - private: - bool _internal_has_remove_extra_whitespaces() const; - public: - void clear_remove_extra_whitespaces(); - bool remove_extra_whitespaces() const; - void set_remove_extra_whitespaces(bool value); - private: - bool _internal_remove_extra_whitespaces() const; - void _internal_set_remove_extra_whitespaces(bool value); - public: - - // optional bool escape_whitespaces = 5 [default = true]; - bool has_escape_whitespaces() const; - private: - bool _internal_has_escape_whitespaces() const; - public: - void clear_escape_whitespaces(); - bool escape_whitespaces() const; - void set_escape_whitespaces(bool value); - private: - bool _internal_escape_whitespaces() const; - void _internal_set_escape_whitespaces(bool value); - public: - - GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(NormalizerSpec) - // @@protoc_insertion_point(class_scope:sentencepiece.NormalizerSpec) - private: - class _Internal; - - ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr precompiled_charsmap_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr normalization_rule_tsv_; - bool add_dummy_prefix_; - bool remove_extra_whitespaces_; - bool escape_whitespaces_; - friend struct ::TableStruct_sentencepiece_5fmodel_2eproto; -}; -// ------------------------------------------------------------------- - -class SelfTestData_Sample PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.SelfTestData.Sample) */ { - public: - inline SelfTestData_Sample() : SelfTestData_Sample(nullptr) {} - virtual ~SelfTestData_Sample(); - - SelfTestData_Sample(const SelfTestData_Sample& from); - SelfTestData_Sample(SelfTestData_Sample&& from) noexcept - : SelfTestData_Sample() { - *this = ::std::move(from); - } - - inline SelfTestData_Sample& operator=(const SelfTestData_Sample& from) { - CopyFrom(from); - return *this; - } - inline SelfTestData_Sample& operator=(SelfTestData_Sample&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const SelfTestData_Sample& default_instance(); - - static inline const SelfTestData_Sample* internal_default_instance() { - return reinterpret_cast( - &_SelfTestData_Sample_default_instance_); - } - static constexpr int kIndexInFileMessages = - 2; - - friend void swap(SelfTestData_Sample& a, SelfTestData_Sample& b) { - a.Swap(&b); - } - inline void Swap(SelfTestData_Sample* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(SelfTestData_Sample* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline SelfTestData_Sample* New() const final { - return CreateMaybeMessage(nullptr); - } - - SelfTestData_Sample* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const SelfTestData_Sample& from); - void MergeFrom(const SelfTestData_Sample& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(SelfTestData_Sample* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.SelfTestData.Sample"; - } - protected: - explicit SelfTestData_Sample(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - enum : int { - kInputFieldNumber = 1, - kExpectedFieldNumber = 2, - }; - // optional string input = 1; - bool has_input() const; - private: - bool _internal_has_input() const; - public: - void clear_input(); - const std::string& input() const; - void set_input(const std::string& value); - void set_input(std::string&& value); - void set_input(const char* value); - void set_input(const char* value, size_t size); - std::string* mutable_input(); - std::string* release_input(); - void set_allocated_input(std::string* input); - private: - const std::string& _internal_input() const; - void _internal_set_input(const std::string& value); - std::string* _internal_mutable_input(); - public: - - // optional string expected = 2; - bool has_expected() const; - private: - bool _internal_has_expected() const; - public: - void clear_expected(); - const std::string& expected() const; - void set_expected(const std::string& value); - void set_expected(std::string&& value); - void set_expected(const char* value); - void set_expected(const char* value, size_t size); - std::string* mutable_expected(); - std::string* release_expected(); - void set_allocated_expected(std::string* expected); - private: - const std::string& _internal_expected() const; - void _internal_set_expected(const std::string& value); - std::string* _internal_mutable_expected(); - public: - - // @@protoc_insertion_point(class_scope:sentencepiece.SelfTestData.Sample) - private: - class _Internal; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr input_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr expected_; - friend struct ::TableStruct_sentencepiece_5fmodel_2eproto; -}; -// ------------------------------------------------------------------- - -class SelfTestData PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.SelfTestData) */ { - public: - inline SelfTestData() : SelfTestData(nullptr) {} - virtual ~SelfTestData(); - - SelfTestData(const SelfTestData& from); - SelfTestData(SelfTestData&& from) noexcept - : SelfTestData() { - *this = ::std::move(from); - } - - inline SelfTestData& operator=(const SelfTestData& from) { - CopyFrom(from); - return *this; - } - inline SelfTestData& operator=(SelfTestData&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const SelfTestData& default_instance(); - - static inline const SelfTestData* internal_default_instance() { - return reinterpret_cast( - &_SelfTestData_default_instance_); - } - static constexpr int kIndexInFileMessages = - 3; - - friend void swap(SelfTestData& a, SelfTestData& b) { - a.Swap(&b); - } - inline void Swap(SelfTestData* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(SelfTestData* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline SelfTestData* New() const final { - return CreateMaybeMessage(nullptr); - } - - SelfTestData* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const SelfTestData& from); - void MergeFrom(const SelfTestData& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(SelfTestData* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.SelfTestData"; - } - protected: - explicit SelfTestData(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - typedef SelfTestData_Sample Sample; - - // accessors ------------------------------------------------------- - - enum : int { - kSamplesFieldNumber = 1, - }; - // repeated .sentencepiece.SelfTestData.Sample samples = 1; - int samples_size() const; - private: - int _internal_samples_size() const; - public: - void clear_samples(); - ::sentencepiece::SelfTestData_Sample* mutable_samples(int index); - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SelfTestData_Sample >* - mutable_samples(); - private: - const ::sentencepiece::SelfTestData_Sample& _internal_samples(int index) const; - ::sentencepiece::SelfTestData_Sample* _internal_add_samples(); - public: - const ::sentencepiece::SelfTestData_Sample& samples(int index) const; - ::sentencepiece::SelfTestData_Sample* add_samples(); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SelfTestData_Sample >& - samples() const; - - GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(SelfTestData) - // @@protoc_insertion_point(class_scope:sentencepiece.SelfTestData) - private: - class _Internal; - - ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SelfTestData_Sample > samples_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - friend struct ::TableStruct_sentencepiece_5fmodel_2eproto; -}; -// ------------------------------------------------------------------- - -class ModelProto_SentencePiece PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.ModelProto.SentencePiece) */ { - public: - inline ModelProto_SentencePiece() : ModelProto_SentencePiece(nullptr) {} - virtual ~ModelProto_SentencePiece(); - - ModelProto_SentencePiece(const ModelProto_SentencePiece& from); - ModelProto_SentencePiece(ModelProto_SentencePiece&& from) noexcept - : ModelProto_SentencePiece() { - *this = ::std::move(from); - } - - inline ModelProto_SentencePiece& operator=(const ModelProto_SentencePiece& from) { - CopyFrom(from); - return *this; - } - inline ModelProto_SentencePiece& operator=(ModelProto_SentencePiece&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const ModelProto_SentencePiece& default_instance(); - - static inline const ModelProto_SentencePiece* internal_default_instance() { - return reinterpret_cast( - &_ModelProto_SentencePiece_default_instance_); - } - static constexpr int kIndexInFileMessages = - 4; - - friend void swap(ModelProto_SentencePiece& a, ModelProto_SentencePiece& b) { - a.Swap(&b); - } - inline void Swap(ModelProto_SentencePiece* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(ModelProto_SentencePiece* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline ModelProto_SentencePiece* New() const final { - return CreateMaybeMessage(nullptr); - } - - ModelProto_SentencePiece* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const ModelProto_SentencePiece& from); - void MergeFrom(const ModelProto_SentencePiece& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(ModelProto_SentencePiece* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.ModelProto.SentencePiece"; - } - protected: - explicit ModelProto_SentencePiece(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - typedef ModelProto_SentencePiece_Type Type; - static constexpr Type NORMAL = - ModelProto_SentencePiece_Type_NORMAL; - static constexpr Type UNKNOWN = - ModelProto_SentencePiece_Type_UNKNOWN; - static constexpr Type CONTROL = - ModelProto_SentencePiece_Type_CONTROL; - static constexpr Type USER_DEFINED = - ModelProto_SentencePiece_Type_USER_DEFINED; - static constexpr Type BYTE = - ModelProto_SentencePiece_Type_BYTE; - static constexpr Type UNUSED = - ModelProto_SentencePiece_Type_UNUSED; - static inline bool Type_IsValid(int value) { - return ModelProto_SentencePiece_Type_IsValid(value); - } - static constexpr Type Type_MIN = - ModelProto_SentencePiece_Type_Type_MIN; - static constexpr Type Type_MAX = - ModelProto_SentencePiece_Type_Type_MAX; - static constexpr int Type_ARRAYSIZE = - ModelProto_SentencePiece_Type_Type_ARRAYSIZE; - template - static inline const std::string& Type_Name(T enum_t_value) { - static_assert(::std::is_same::value || - ::std::is_integral::value, - "Incorrect type passed to function Type_Name."); - return ModelProto_SentencePiece_Type_Name(enum_t_value); - } - static inline bool Type_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name, - Type* value) { - return ModelProto_SentencePiece_Type_Parse(name, value); - } - - // accessors ------------------------------------------------------- - - enum : int { - kPieceFieldNumber = 1, - kScoreFieldNumber = 2, - kTypeFieldNumber = 3, - }; - // optional string piece = 1; - bool has_piece() const; - private: - bool _internal_has_piece() const; - public: - void clear_piece(); - const std::string& piece() const; - void set_piece(const std::string& value); - void set_piece(std::string&& value); - void set_piece(const char* value); - void set_piece(const char* value, size_t size); - std::string* mutable_piece(); - std::string* release_piece(); - void set_allocated_piece(std::string* piece); - private: - const std::string& _internal_piece() const; - void _internal_set_piece(const std::string& value); - std::string* _internal_mutable_piece(); - public: - - // optional float score = 2; - bool has_score() const; - private: - bool _internal_has_score() const; - public: - void clear_score(); - float score() const; - void set_score(float value); - private: - float _internal_score() const; - void _internal_set_score(float value); - public: - - // optional .sentencepiece.ModelProto.SentencePiece.Type type = 3 [default = NORMAL]; - bool has_type() const; - private: - bool _internal_has_type() const; - public: - void clear_type(); - ::sentencepiece::ModelProto_SentencePiece_Type type() const; - void set_type(::sentencepiece::ModelProto_SentencePiece_Type value); - private: - ::sentencepiece::ModelProto_SentencePiece_Type _internal_type() const; - void _internal_set_type(::sentencepiece::ModelProto_SentencePiece_Type value); - public: - - GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(ModelProto_SentencePiece) - // @@protoc_insertion_point(class_scope:sentencepiece.ModelProto.SentencePiece) - private: - class _Internal; - - ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr piece_; - float score_; - int type_; - friend struct ::TableStruct_sentencepiece_5fmodel_2eproto; -}; -// ------------------------------------------------------------------- - -class ModelProto PROTOBUF_FINAL : - public ::PROTOBUF_NAMESPACE_ID::MessageLite /* @@protoc_insertion_point(class_definition:sentencepiece.ModelProto) */ { - public: - inline ModelProto() : ModelProto(nullptr) {} - virtual ~ModelProto(); - - ModelProto(const ModelProto& from); - ModelProto(ModelProto&& from) noexcept - : ModelProto() { - *this = ::std::move(from); - } - - inline ModelProto& operator=(const ModelProto& from) { - CopyFrom(from); - return *this; - } - inline ModelProto& operator=(ModelProto&& from) noexcept { - if (GetArena() == from.GetArena()) { - if (this != &from) InternalSwap(&from); - } else { - CopyFrom(from); - } - return *this; - } - - inline const std::string& unknown_fields() const { - return _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString); - } - inline std::string* mutable_unknown_fields() { - return _internal_metadata_.mutable_unknown_fields(); - } - - static const ModelProto& default_instance(); - - static inline const ModelProto* internal_default_instance() { - return reinterpret_cast( - &_ModelProto_default_instance_); - } - static constexpr int kIndexInFileMessages = - 5; - - friend void swap(ModelProto& a, ModelProto& b) { - a.Swap(&b); - } - inline void Swap(ModelProto* other) { - if (other == this) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other); - } - } - void UnsafeArenaSwap(ModelProto* other) { - if (other == this) return; - GOOGLE_DCHECK(GetArena() == other->GetArena()); - InternalSwap(other); - } - - // implements Message ---------------------------------------------- - - inline ModelProto* New() const final { - return CreateMaybeMessage(nullptr); - } - - ModelProto* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { - return CreateMaybeMessage(arena); - } - void CheckTypeAndMergeFrom(const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) - final; - void CopyFrom(const ModelProto& from); - void MergeFrom(const ModelProto& from); - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; - - size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; - ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( - ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; - void DiscardUnknownFields(); - int GetCachedSize() const final { return _cached_size_.Get(); } - - private: - inline void SharedCtor(); - inline void SharedDtor(); - void SetCachedSize(int size) const; - void InternalSwap(ModelProto* other); - friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; - static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { - return "sentencepiece.ModelProto"; - } - protected: - explicit ModelProto(::PROTOBUF_NAMESPACE_ID::Arena* arena); - private: - static void ArenaDtor(void* object); - inline void RegisterArenaDtor(::PROTOBUF_NAMESPACE_ID::Arena* arena); - public: - - std::string GetTypeName() const final; - - // nested types ---------------------------------------------------- - - typedef ModelProto_SentencePiece SentencePiece; - - // accessors ------------------------------------------------------- - - enum : int { - kPiecesFieldNumber = 1, - kTrainerSpecFieldNumber = 2, - kNormalizerSpecFieldNumber = 3, - kSelfTestDataFieldNumber = 4, - kDenormalizerSpecFieldNumber = 5, - }; - // repeated .sentencepiece.ModelProto.SentencePiece pieces = 1; - int pieces_size() const; - private: - int _internal_pieces_size() const; - public: - void clear_pieces(); - ::sentencepiece::ModelProto_SentencePiece* mutable_pieces(int index); - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::ModelProto_SentencePiece >* - mutable_pieces(); - private: - const ::sentencepiece::ModelProto_SentencePiece& _internal_pieces(int index) const; - ::sentencepiece::ModelProto_SentencePiece* _internal_add_pieces(); - public: - const ::sentencepiece::ModelProto_SentencePiece& pieces(int index) const; - ::sentencepiece::ModelProto_SentencePiece* add_pieces(); - const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::ModelProto_SentencePiece >& - pieces() const; - - // optional .sentencepiece.TrainerSpec trainer_spec = 2; - bool has_trainer_spec() const; - private: - bool _internal_has_trainer_spec() const; - public: - void clear_trainer_spec(); - const ::sentencepiece::TrainerSpec& trainer_spec() const; - ::sentencepiece::TrainerSpec* release_trainer_spec(); - ::sentencepiece::TrainerSpec* mutable_trainer_spec(); - void set_allocated_trainer_spec(::sentencepiece::TrainerSpec* trainer_spec); - private: - const ::sentencepiece::TrainerSpec& _internal_trainer_spec() const; - ::sentencepiece::TrainerSpec* _internal_mutable_trainer_spec(); - public: - void unsafe_arena_set_allocated_trainer_spec( - ::sentencepiece::TrainerSpec* trainer_spec); - ::sentencepiece::TrainerSpec* unsafe_arena_release_trainer_spec(); - - // optional .sentencepiece.NormalizerSpec normalizer_spec = 3; - bool has_normalizer_spec() const; - private: - bool _internal_has_normalizer_spec() const; - public: - void clear_normalizer_spec(); - const ::sentencepiece::NormalizerSpec& normalizer_spec() const; - ::sentencepiece::NormalizerSpec* release_normalizer_spec(); - ::sentencepiece::NormalizerSpec* mutable_normalizer_spec(); - void set_allocated_normalizer_spec(::sentencepiece::NormalizerSpec* normalizer_spec); - private: - const ::sentencepiece::NormalizerSpec& _internal_normalizer_spec() const; - ::sentencepiece::NormalizerSpec* _internal_mutable_normalizer_spec(); - public: - void unsafe_arena_set_allocated_normalizer_spec( - ::sentencepiece::NormalizerSpec* normalizer_spec); - ::sentencepiece::NormalizerSpec* unsafe_arena_release_normalizer_spec(); - - // optional .sentencepiece.SelfTestData self_test_data = 4; - bool has_self_test_data() const; - private: - bool _internal_has_self_test_data() const; - public: - void clear_self_test_data(); - const ::sentencepiece::SelfTestData& self_test_data() const; - ::sentencepiece::SelfTestData* release_self_test_data(); - ::sentencepiece::SelfTestData* mutable_self_test_data(); - void set_allocated_self_test_data(::sentencepiece::SelfTestData* self_test_data); - private: - const ::sentencepiece::SelfTestData& _internal_self_test_data() const; - ::sentencepiece::SelfTestData* _internal_mutable_self_test_data(); - public: - void unsafe_arena_set_allocated_self_test_data( - ::sentencepiece::SelfTestData* self_test_data); - ::sentencepiece::SelfTestData* unsafe_arena_release_self_test_data(); - - // optional .sentencepiece.NormalizerSpec denormalizer_spec = 5; - bool has_denormalizer_spec() const; - private: - bool _internal_has_denormalizer_spec() const; - public: - void clear_denormalizer_spec(); - const ::sentencepiece::NormalizerSpec& denormalizer_spec() const; - ::sentencepiece::NormalizerSpec* release_denormalizer_spec(); - ::sentencepiece::NormalizerSpec* mutable_denormalizer_spec(); - void set_allocated_denormalizer_spec(::sentencepiece::NormalizerSpec* denormalizer_spec); - private: - const ::sentencepiece::NormalizerSpec& _internal_denormalizer_spec() const; - ::sentencepiece::NormalizerSpec* _internal_mutable_denormalizer_spec(); - public: - void unsafe_arena_set_allocated_denormalizer_spec( - ::sentencepiece::NormalizerSpec* denormalizer_spec); - ::sentencepiece::NormalizerSpec* unsafe_arena_release_denormalizer_spec(); - - GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(ModelProto) - // @@protoc_insertion_point(class_scope:sentencepiece.ModelProto) - private: - class _Internal; - - ::PROTOBUF_NAMESPACE_ID::internal::ExtensionSet _extensions_; - - template friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - ::PROTOBUF_NAMESPACE_ID::internal::HasBits<1> _has_bits_; - mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; - ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::ModelProto_SentencePiece > pieces_; - ::sentencepiece::TrainerSpec* trainer_spec_; - ::sentencepiece::NormalizerSpec* normalizer_spec_; - ::sentencepiece::SelfTestData* self_test_data_; - ::sentencepiece::NormalizerSpec* denormalizer_spec_; - friend struct ::TableStruct_sentencepiece_5fmodel_2eproto; -}; -// =================================================================== - - -// =================================================================== - -#ifdef __GNUC__ - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wstrict-aliasing" -#endif // __GNUC__ -// TrainerSpec - -// repeated string input = 1; -inline int TrainerSpec::_internal_input_size() const { - return input_.size(); -} -inline int TrainerSpec::input_size() const { - return _internal_input_size(); -} -inline void TrainerSpec::clear_input() { - input_.Clear(); -} -inline std::string* TrainerSpec::add_input() { - // @@protoc_insertion_point(field_add_mutable:sentencepiece.TrainerSpec.input) - return _internal_add_input(); -} -inline const std::string& TrainerSpec::_internal_input(int index) const { - return input_.Get(index); -} -inline const std::string& TrainerSpec::input(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.input) - return _internal_input(index); -} -inline std::string* TrainerSpec::mutable_input(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.input) - return input_.Mutable(index); -} -inline void TrainerSpec::set_input(int index, const std::string& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.input) - input_.Mutable(index)->assign(value); -} -inline void TrainerSpec::set_input(int index, std::string&& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.input) - input_.Mutable(index)->assign(std::move(value)); -} -inline void TrainerSpec::set_input(int index, const char* value) { - GOOGLE_DCHECK(value != nullptr); - input_.Mutable(index)->assign(value); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.input) -} -inline void TrainerSpec::set_input(int index, const char* value, size_t size) { - input_.Mutable(index)->assign( - reinterpret_cast(value), size); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.input) -} -inline std::string* TrainerSpec::_internal_add_input() { - return input_.Add(); -} -inline void TrainerSpec::add_input(const std::string& value) { - input_.Add()->assign(value); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.input) -} -inline void TrainerSpec::add_input(std::string&& value) { - input_.Add(std::move(value)); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.input) -} -inline void TrainerSpec::add_input(const char* value) { - GOOGLE_DCHECK(value != nullptr); - input_.Add()->assign(value); - // @@protoc_insertion_point(field_add_char:sentencepiece.TrainerSpec.input) -} -inline void TrainerSpec::add_input(const char* value, size_t size) { - input_.Add()->assign(reinterpret_cast(value), size); - // @@protoc_insertion_point(field_add_pointer:sentencepiece.TrainerSpec.input) -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& -TrainerSpec::input() const { - // @@protoc_insertion_point(field_list:sentencepiece.TrainerSpec.input) - return input_; -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* -TrainerSpec::mutable_input() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.TrainerSpec.input) - return &input_; -} - -// optional string input_format = 7; -inline bool TrainerSpec::_internal_has_input_format() const { - bool value = (_has_bits_[0] & 0x00000002u) != 0; - return value; -} -inline bool TrainerSpec::has_input_format() const { - return _internal_has_input_format(); -} -inline void TrainerSpec::clear_input_format() { - input_format_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000002u; -} -inline const std::string& TrainerSpec::input_format() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.input_format) - return _internal_input_format(); -} -inline void TrainerSpec::set_input_format(const std::string& value) { - _internal_set_input_format(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.input_format) -} -inline std::string* TrainerSpec::mutable_input_format() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.input_format) - return _internal_mutable_input_format(); -} -inline const std::string& TrainerSpec::_internal_input_format() const { - return input_format_.Get(); -} -inline void TrainerSpec::_internal_set_input_format(const std::string& value) { - _has_bits_[0] |= 0x00000002u; - input_format_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_input_format(std::string&& value) { - _has_bits_[0] |= 0x00000002u; - input_format_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.input_format) -} -inline void TrainerSpec::set_input_format(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000002u; - input_format_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.input_format) -} -inline void TrainerSpec::set_input_format(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000002u; - input_format_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.input_format) -} -inline std::string* TrainerSpec::_internal_mutable_input_format() { - _has_bits_[0] |= 0x00000002u; - return input_format_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* TrainerSpec::release_input_format() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.input_format) - if (!_internal_has_input_format()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000002u; - return input_format_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void TrainerSpec::set_allocated_input_format(std::string* input_format) { - if (input_format != nullptr) { - _has_bits_[0] |= 0x00000002u; - } else { - _has_bits_[0] &= ~0x00000002u; - } - input_format_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), input_format, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.input_format) -} - -// optional string model_prefix = 2; -inline bool TrainerSpec::_internal_has_model_prefix() const { - bool value = (_has_bits_[0] & 0x00000001u) != 0; - return value; -} -inline bool TrainerSpec::has_model_prefix() const { - return _internal_has_model_prefix(); -} -inline void TrainerSpec::clear_model_prefix() { - model_prefix_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000001u; -} -inline const std::string& TrainerSpec::model_prefix() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.model_prefix) - return _internal_model_prefix(); -} -inline void TrainerSpec::set_model_prefix(const std::string& value) { - _internal_set_model_prefix(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.model_prefix) -} -inline std::string* TrainerSpec::mutable_model_prefix() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.model_prefix) - return _internal_mutable_model_prefix(); -} -inline const std::string& TrainerSpec::_internal_model_prefix() const { - return model_prefix_.Get(); -} -inline void TrainerSpec::_internal_set_model_prefix(const std::string& value) { - _has_bits_[0] |= 0x00000001u; - model_prefix_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_model_prefix(std::string&& value) { - _has_bits_[0] |= 0x00000001u; - model_prefix_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.model_prefix) -} -inline void TrainerSpec::set_model_prefix(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000001u; - model_prefix_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.model_prefix) -} -inline void TrainerSpec::set_model_prefix(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000001u; - model_prefix_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.model_prefix) -} -inline std::string* TrainerSpec::_internal_mutable_model_prefix() { - _has_bits_[0] |= 0x00000001u; - return model_prefix_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* TrainerSpec::release_model_prefix() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.model_prefix) - if (!_internal_has_model_prefix()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000001u; - return model_prefix_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void TrainerSpec::set_allocated_model_prefix(std::string* model_prefix) { - if (model_prefix != nullptr) { - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - model_prefix_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), model_prefix, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.model_prefix) -} - -// optional .sentencepiece.TrainerSpec.ModelType model_type = 3 [default = UNIGRAM]; -inline bool TrainerSpec::_internal_has_model_type() const { - bool value = (_has_bits_[0] & 0x01000000u) != 0; - return value; -} -inline bool TrainerSpec::has_model_type() const { - return _internal_has_model_type(); -} -inline void TrainerSpec::clear_model_type() { - model_type_ = 1; - _has_bits_[0] &= ~0x01000000u; -} -inline ::sentencepiece::TrainerSpec_ModelType TrainerSpec::_internal_model_type() const { - return static_cast< ::sentencepiece::TrainerSpec_ModelType >(model_type_); -} -inline ::sentencepiece::TrainerSpec_ModelType TrainerSpec::model_type() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.model_type) - return _internal_model_type(); -} -inline void TrainerSpec::_internal_set_model_type(::sentencepiece::TrainerSpec_ModelType value) { - assert(::sentencepiece::TrainerSpec_ModelType_IsValid(value)); - _has_bits_[0] |= 0x01000000u; - model_type_ = value; -} -inline void TrainerSpec::set_model_type(::sentencepiece::TrainerSpec_ModelType value) { - _internal_set_model_type(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.model_type) -} - -// optional int32 vocab_size = 4 [default = 8000]; -inline bool TrainerSpec::_internal_has_vocab_size() const { - bool value = (_has_bits_[0] & 0x02000000u) != 0; - return value; -} -inline bool TrainerSpec::has_vocab_size() const { - return _internal_has_vocab_size(); -} -inline void TrainerSpec::clear_vocab_size() { - vocab_size_ = 8000; - _has_bits_[0] &= ~0x02000000u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_vocab_size() const { - return vocab_size_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::vocab_size() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.vocab_size) - return _internal_vocab_size(); -} -inline void TrainerSpec::_internal_set_vocab_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x02000000u; - vocab_size_ = value; -} -inline void TrainerSpec::set_vocab_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_vocab_size(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.vocab_size) -} - -// repeated string accept_language = 5; -inline int TrainerSpec::_internal_accept_language_size() const { - return accept_language_.size(); -} -inline int TrainerSpec::accept_language_size() const { - return _internal_accept_language_size(); -} -inline void TrainerSpec::clear_accept_language() { - accept_language_.Clear(); -} -inline std::string* TrainerSpec::add_accept_language() { - // @@protoc_insertion_point(field_add_mutable:sentencepiece.TrainerSpec.accept_language) - return _internal_add_accept_language(); -} -inline const std::string& TrainerSpec::_internal_accept_language(int index) const { - return accept_language_.Get(index); -} -inline const std::string& TrainerSpec::accept_language(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.accept_language) - return _internal_accept_language(index); -} -inline std::string* TrainerSpec::mutable_accept_language(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.accept_language) - return accept_language_.Mutable(index); -} -inline void TrainerSpec::set_accept_language(int index, const std::string& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.accept_language) - accept_language_.Mutable(index)->assign(value); -} -inline void TrainerSpec::set_accept_language(int index, std::string&& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.accept_language) - accept_language_.Mutable(index)->assign(std::move(value)); -} -inline void TrainerSpec::set_accept_language(int index, const char* value) { - GOOGLE_DCHECK(value != nullptr); - accept_language_.Mutable(index)->assign(value); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.accept_language) -} -inline void TrainerSpec::set_accept_language(int index, const char* value, size_t size) { - accept_language_.Mutable(index)->assign( - reinterpret_cast(value), size); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.accept_language) -} -inline std::string* TrainerSpec::_internal_add_accept_language() { - return accept_language_.Add(); -} -inline void TrainerSpec::add_accept_language(const std::string& value) { - accept_language_.Add()->assign(value); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.accept_language) -} -inline void TrainerSpec::add_accept_language(std::string&& value) { - accept_language_.Add(std::move(value)); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.accept_language) -} -inline void TrainerSpec::add_accept_language(const char* value) { - GOOGLE_DCHECK(value != nullptr); - accept_language_.Add()->assign(value); - // @@protoc_insertion_point(field_add_char:sentencepiece.TrainerSpec.accept_language) -} -inline void TrainerSpec::add_accept_language(const char* value, size_t size) { - accept_language_.Add()->assign(reinterpret_cast(value), size); - // @@protoc_insertion_point(field_add_pointer:sentencepiece.TrainerSpec.accept_language) -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& -TrainerSpec::accept_language() const { - // @@protoc_insertion_point(field_list:sentencepiece.TrainerSpec.accept_language) - return accept_language_; -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* -TrainerSpec::mutable_accept_language() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.TrainerSpec.accept_language) - return &accept_language_; -} - -// optional int32 self_test_sample_size = 6 [default = 0]; -inline bool TrainerSpec::_internal_has_self_test_sample_size() const { - bool value = (_has_bits_[0] & 0x00000400u) != 0; - return value; -} -inline bool TrainerSpec::has_self_test_sample_size() const { - return _internal_has_self_test_sample_size(); -} -inline void TrainerSpec::clear_self_test_sample_size() { - self_test_sample_size_ = 0; - _has_bits_[0] &= ~0x00000400u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_self_test_sample_size() const { - return self_test_sample_size_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::self_test_sample_size() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.self_test_sample_size) - return _internal_self_test_sample_size(); -} -inline void TrainerSpec::_internal_set_self_test_sample_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x00000400u; - self_test_sample_size_ = value; -} -inline void TrainerSpec::set_self_test_sample_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_self_test_sample_size(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.self_test_sample_size) -} - -// optional bool enable_differential_privacy = 50 [default = false]; -inline bool TrainerSpec::_internal_has_enable_differential_privacy() const { - bool value = (_has_bits_[0] & 0x00004000u) != 0; - return value; -} -inline bool TrainerSpec::has_enable_differential_privacy() const { - return _internal_has_enable_differential_privacy(); -} -inline void TrainerSpec::clear_enable_differential_privacy() { - enable_differential_privacy_ = false; - _has_bits_[0] &= ~0x00004000u; -} -inline bool TrainerSpec::_internal_enable_differential_privacy() const { - return enable_differential_privacy_; -} -inline bool TrainerSpec::enable_differential_privacy() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.enable_differential_privacy) - return _internal_enable_differential_privacy(); -} -inline void TrainerSpec::_internal_set_enable_differential_privacy(bool value) { - _has_bits_[0] |= 0x00004000u; - enable_differential_privacy_ = value; -} -inline void TrainerSpec::set_enable_differential_privacy(bool value) { - _internal_set_enable_differential_privacy(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.enable_differential_privacy) -} - -// optional float differential_privacy_noise_level = 51 [default = 0]; -inline bool TrainerSpec::_internal_has_differential_privacy_noise_level() const { - bool value = (_has_bits_[0] & 0x00400000u) != 0; - return value; -} -inline bool TrainerSpec::has_differential_privacy_noise_level() const { - return _internal_has_differential_privacy_noise_level(); -} -inline void TrainerSpec::clear_differential_privacy_noise_level() { - differential_privacy_noise_level_ = 0; - _has_bits_[0] &= ~0x00400000u; -} -inline float TrainerSpec::_internal_differential_privacy_noise_level() const { - return differential_privacy_noise_level_; -} -inline float TrainerSpec::differential_privacy_noise_level() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.differential_privacy_noise_level) - return _internal_differential_privacy_noise_level(); -} -inline void TrainerSpec::_internal_set_differential_privacy_noise_level(float value) { - _has_bits_[0] |= 0x00400000u; - differential_privacy_noise_level_ = value; -} -inline void TrainerSpec::set_differential_privacy_noise_level(float value) { - _internal_set_differential_privacy_noise_level(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.differential_privacy_noise_level) -} - -// optional uint64 differential_privacy_clipping_threshold = 52 [default = 0]; -inline bool TrainerSpec::_internal_has_differential_privacy_clipping_threshold() const { - bool value = (_has_bits_[0] & 0x00800000u) != 0; - return value; -} -inline bool TrainerSpec::has_differential_privacy_clipping_threshold() const { - return _internal_has_differential_privacy_clipping_threshold(); -} -inline void TrainerSpec::clear_differential_privacy_clipping_threshold() { - differential_privacy_clipping_threshold_ = PROTOBUF_ULONGLONG(0); - _has_bits_[0] &= ~0x00800000u; -} -inline ::PROTOBUF_NAMESPACE_ID::uint64 TrainerSpec::_internal_differential_privacy_clipping_threshold() const { - return differential_privacy_clipping_threshold_; -} -inline ::PROTOBUF_NAMESPACE_ID::uint64 TrainerSpec::differential_privacy_clipping_threshold() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.differential_privacy_clipping_threshold) - return _internal_differential_privacy_clipping_threshold(); -} -inline void TrainerSpec::_internal_set_differential_privacy_clipping_threshold(::PROTOBUF_NAMESPACE_ID::uint64 value) { - _has_bits_[0] |= 0x00800000u; - differential_privacy_clipping_threshold_ = value; -} -inline void TrainerSpec::set_differential_privacy_clipping_threshold(::PROTOBUF_NAMESPACE_ID::uint64 value) { - _internal_set_differential_privacy_clipping_threshold(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.differential_privacy_clipping_threshold) -} - -// optional float character_coverage = 10 [default = 0.9995]; -inline bool TrainerSpec::_internal_has_character_coverage() const { - bool value = (_has_bits_[0] & 0x04000000u) != 0; - return value; -} -inline bool TrainerSpec::has_character_coverage() const { - return _internal_has_character_coverage(); -} -inline void TrainerSpec::clear_character_coverage() { - character_coverage_ = 0.9995f; - _has_bits_[0] &= ~0x04000000u; -} -inline float TrainerSpec::_internal_character_coverage() const { - return character_coverage_; -} -inline float TrainerSpec::character_coverage() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.character_coverage) - return _internal_character_coverage(); -} -inline void TrainerSpec::_internal_set_character_coverage(float value) { - _has_bits_[0] |= 0x04000000u; - character_coverage_ = value; -} -inline void TrainerSpec::set_character_coverage(float value) { - _internal_set_character_coverage(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.character_coverage) -} - -// optional uint64 input_sentence_size = 11 [default = 0]; -inline bool TrainerSpec::_internal_has_input_sentence_size() const { - bool value = (_has_bits_[0] & 0x00001000u) != 0; - return value; -} -inline bool TrainerSpec::has_input_sentence_size() const { - return _internal_has_input_sentence_size(); -} -inline void TrainerSpec::clear_input_sentence_size() { - input_sentence_size_ = PROTOBUF_ULONGLONG(0); - _has_bits_[0] &= ~0x00001000u; -} -inline ::PROTOBUF_NAMESPACE_ID::uint64 TrainerSpec::_internal_input_sentence_size() const { - return input_sentence_size_; -} -inline ::PROTOBUF_NAMESPACE_ID::uint64 TrainerSpec::input_sentence_size() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.input_sentence_size) - return _internal_input_sentence_size(); -} -inline void TrainerSpec::_internal_set_input_sentence_size(::PROTOBUF_NAMESPACE_ID::uint64 value) { - _has_bits_[0] |= 0x00001000u; - input_sentence_size_ = value; -} -inline void TrainerSpec::set_input_sentence_size(::PROTOBUF_NAMESPACE_ID::uint64 value) { - _internal_set_input_sentence_size(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.input_sentence_size) -} - -// optional bool shuffle_input_sentence = 19 [default = true]; -inline bool TrainerSpec::_internal_has_shuffle_input_sentence() const { - bool value = (_has_bits_[1] & 0x00000002u) != 0; - return value; -} -inline bool TrainerSpec::has_shuffle_input_sentence() const { - return _internal_has_shuffle_input_sentence(); -} -inline void TrainerSpec::clear_shuffle_input_sentence() { - shuffle_input_sentence_ = true; - _has_bits_[1] &= ~0x00000002u; -} -inline bool TrainerSpec::_internal_shuffle_input_sentence() const { - return shuffle_input_sentence_; -} -inline bool TrainerSpec::shuffle_input_sentence() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.shuffle_input_sentence) - return _internal_shuffle_input_sentence(); -} -inline void TrainerSpec::_internal_set_shuffle_input_sentence(bool value) { - _has_bits_[1] |= 0x00000002u; - shuffle_input_sentence_ = value; -} -inline void TrainerSpec::set_shuffle_input_sentence(bool value) { - _internal_set_shuffle_input_sentence(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.shuffle_input_sentence) -} - -// optional int32 mining_sentence_size = 12 [deprecated = true]; -inline bool TrainerSpec::_internal_has_mining_sentence_size() const { - bool value = (_has_bits_[0] & 0x00000800u) != 0; - return value; -} -inline bool TrainerSpec::has_mining_sentence_size() const { - return _internal_has_mining_sentence_size(); -} -inline void TrainerSpec::clear_mining_sentence_size() { - mining_sentence_size_ = 0; - _has_bits_[0] &= ~0x00000800u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_mining_sentence_size() const { - return mining_sentence_size_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::mining_sentence_size() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.mining_sentence_size) - return _internal_mining_sentence_size(); -} -inline void TrainerSpec::_internal_set_mining_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x00000800u; - mining_sentence_size_ = value; -} -inline void TrainerSpec::set_mining_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_mining_sentence_size(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.mining_sentence_size) -} - -// optional int32 training_sentence_size = 13 [deprecated = true]; -inline bool TrainerSpec::_internal_has_training_sentence_size() const { - bool value = (_has_bits_[0] & 0x00002000u) != 0; - return value; -} -inline bool TrainerSpec::has_training_sentence_size() const { - return _internal_has_training_sentence_size(); -} -inline void TrainerSpec::clear_training_sentence_size() { - training_sentence_size_ = 0; - _has_bits_[0] &= ~0x00002000u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_training_sentence_size() const { - return training_sentence_size_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::training_sentence_size() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.training_sentence_size) - return _internal_training_sentence_size(); -} -inline void TrainerSpec::_internal_set_training_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x00002000u; - training_sentence_size_ = value; -} -inline void TrainerSpec::set_training_sentence_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_training_sentence_size(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.training_sentence_size) -} - -// optional int32 seed_sentencepiece_size = 14 [default = 1000000]; -inline bool TrainerSpec::_internal_has_seed_sentencepiece_size() const { - bool value = (_has_bits_[0] & 0x08000000u) != 0; - return value; -} -inline bool TrainerSpec::has_seed_sentencepiece_size() const { - return _internal_has_seed_sentencepiece_size(); -} -inline void TrainerSpec::clear_seed_sentencepiece_size() { - seed_sentencepiece_size_ = 1000000; - _has_bits_[0] &= ~0x08000000u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_seed_sentencepiece_size() const { - return seed_sentencepiece_size_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::seed_sentencepiece_size() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.seed_sentencepiece_size) - return _internal_seed_sentencepiece_size(); -} -inline void TrainerSpec::_internal_set_seed_sentencepiece_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x08000000u; - seed_sentencepiece_size_ = value; -} -inline void TrainerSpec::set_seed_sentencepiece_size(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_seed_sentencepiece_size(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.seed_sentencepiece_size) -} - -// optional float shrinking_factor = 15 [default = 0.75]; -inline bool TrainerSpec::_internal_has_shrinking_factor() const { - bool value = (_has_bits_[0] & 0x10000000u) != 0; - return value; -} -inline bool TrainerSpec::has_shrinking_factor() const { - return _internal_has_shrinking_factor(); -} -inline void TrainerSpec::clear_shrinking_factor() { - shrinking_factor_ = 0.75f; - _has_bits_[0] &= ~0x10000000u; -} -inline float TrainerSpec::_internal_shrinking_factor() const { - return shrinking_factor_; -} -inline float TrainerSpec::shrinking_factor() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.shrinking_factor) - return _internal_shrinking_factor(); -} -inline void TrainerSpec::_internal_set_shrinking_factor(float value) { - _has_bits_[0] |= 0x10000000u; - shrinking_factor_ = value; -} -inline void TrainerSpec::set_shrinking_factor(float value) { - _internal_set_shrinking_factor(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.shrinking_factor) -} - -// optional int32 max_sentence_length = 18 [default = 4192]; -inline bool TrainerSpec::_internal_has_max_sentence_length() const { - bool value = (_has_bits_[0] & 0x80000000u) != 0; - return value; -} -inline bool TrainerSpec::has_max_sentence_length() const { - return _internal_has_max_sentence_length(); -} -inline void TrainerSpec::clear_max_sentence_length() { - max_sentence_length_ = 4192; - _has_bits_[0] &= ~0x80000000u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_max_sentence_length() const { - return max_sentence_length_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::max_sentence_length() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.max_sentence_length) - return _internal_max_sentence_length(); -} -inline void TrainerSpec::_internal_set_max_sentence_length(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x80000000u; - max_sentence_length_ = value; -} -inline void TrainerSpec::set_max_sentence_length(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_max_sentence_length(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.max_sentence_length) -} - -// optional int32 num_threads = 16 [default = 16]; -inline bool TrainerSpec::_internal_has_num_threads() const { - bool value = (_has_bits_[0] & 0x20000000u) != 0; - return value; -} -inline bool TrainerSpec::has_num_threads() const { - return _internal_has_num_threads(); -} -inline void TrainerSpec::clear_num_threads() { - num_threads_ = 16; - _has_bits_[0] &= ~0x20000000u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_num_threads() const { - return num_threads_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::num_threads() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.num_threads) - return _internal_num_threads(); -} -inline void TrainerSpec::_internal_set_num_threads(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x20000000u; - num_threads_ = value; -} -inline void TrainerSpec::set_num_threads(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_num_threads(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.num_threads) -} - -// optional int32 num_sub_iterations = 17 [default = 2]; -inline bool TrainerSpec::_internal_has_num_sub_iterations() const { - bool value = (_has_bits_[0] & 0x40000000u) != 0; - return value; -} -inline bool TrainerSpec::has_num_sub_iterations() const { - return _internal_has_num_sub_iterations(); -} -inline void TrainerSpec::clear_num_sub_iterations() { - num_sub_iterations_ = 2; - _has_bits_[0] &= ~0x40000000u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_num_sub_iterations() const { - return num_sub_iterations_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::num_sub_iterations() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.num_sub_iterations) - return _internal_num_sub_iterations(); -} -inline void TrainerSpec::_internal_set_num_sub_iterations(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x40000000u; - num_sub_iterations_ = value; -} -inline void TrainerSpec::set_num_sub_iterations(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_num_sub_iterations(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.num_sub_iterations) -} - -// optional int32 max_sentencepiece_length = 20 [default = 16]; -inline bool TrainerSpec::_internal_has_max_sentencepiece_length() const { - bool value = (_has_bits_[1] & 0x00000001u) != 0; - return value; -} -inline bool TrainerSpec::has_max_sentencepiece_length() const { - return _internal_has_max_sentencepiece_length(); -} -inline void TrainerSpec::clear_max_sentencepiece_length() { - max_sentencepiece_length_ = 16; - _has_bits_[1] &= ~0x00000001u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_max_sentencepiece_length() const { - return max_sentencepiece_length_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::max_sentencepiece_length() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.max_sentencepiece_length) - return _internal_max_sentencepiece_length(); -} -inline void TrainerSpec::_internal_set_max_sentencepiece_length(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[1] |= 0x00000001u; - max_sentencepiece_length_ = value; -} -inline void TrainerSpec::set_max_sentencepiece_length(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_max_sentencepiece_length(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.max_sentencepiece_length) -} - -// optional bool split_by_unicode_script = 21 [default = true]; -inline bool TrainerSpec::_internal_has_split_by_unicode_script() const { - bool value = (_has_bits_[1] & 0x00000004u) != 0; - return value; -} -inline bool TrainerSpec::has_split_by_unicode_script() const { - return _internal_has_split_by_unicode_script(); -} -inline void TrainerSpec::clear_split_by_unicode_script() { - split_by_unicode_script_ = true; - _has_bits_[1] &= ~0x00000004u; -} -inline bool TrainerSpec::_internal_split_by_unicode_script() const { - return split_by_unicode_script_; -} -inline bool TrainerSpec::split_by_unicode_script() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.split_by_unicode_script) - return _internal_split_by_unicode_script(); -} -inline void TrainerSpec::_internal_set_split_by_unicode_script(bool value) { - _has_bits_[1] |= 0x00000004u; - split_by_unicode_script_ = value; -} -inline void TrainerSpec::set_split_by_unicode_script(bool value) { - _internal_set_split_by_unicode_script(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.split_by_unicode_script) -} - -// optional bool split_by_number = 23 [default = true]; -inline bool TrainerSpec::_internal_has_split_by_number() const { - bool value = (_has_bits_[1] & 0x00000008u) != 0; - return value; -} -inline bool TrainerSpec::has_split_by_number() const { - return _internal_has_split_by_number(); -} -inline void TrainerSpec::clear_split_by_number() { - split_by_number_ = true; - _has_bits_[1] &= ~0x00000008u; -} -inline bool TrainerSpec::_internal_split_by_number() const { - return split_by_number_; -} -inline bool TrainerSpec::split_by_number() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.split_by_number) - return _internal_split_by_number(); -} -inline void TrainerSpec::_internal_set_split_by_number(bool value) { - _has_bits_[1] |= 0x00000008u; - split_by_number_ = value; -} -inline void TrainerSpec::set_split_by_number(bool value) { - _internal_set_split_by_number(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.split_by_number) -} - -// optional bool split_by_whitespace = 22 [default = true]; -inline bool TrainerSpec::_internal_has_split_by_whitespace() const { - bool value = (_has_bits_[1] & 0x00000010u) != 0; - return value; -} -inline bool TrainerSpec::has_split_by_whitespace() const { - return _internal_has_split_by_whitespace(); -} -inline void TrainerSpec::clear_split_by_whitespace() { - split_by_whitespace_ = true; - _has_bits_[1] &= ~0x00000010u; -} -inline bool TrainerSpec::_internal_split_by_whitespace() const { - return split_by_whitespace_; -} -inline bool TrainerSpec::split_by_whitespace() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.split_by_whitespace) - return _internal_split_by_whitespace(); -} -inline void TrainerSpec::_internal_set_split_by_whitespace(bool value) { - _has_bits_[1] |= 0x00000010u; - split_by_whitespace_ = value; -} -inline void TrainerSpec::set_split_by_whitespace(bool value) { - _internal_set_split_by_whitespace(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.split_by_whitespace) -} - -// optional bool treat_whitespace_as_suffix = 24 [default = false]; -inline bool TrainerSpec::_internal_has_treat_whitespace_as_suffix() const { - bool value = (_has_bits_[0] & 0x00008000u) != 0; - return value; -} -inline bool TrainerSpec::has_treat_whitespace_as_suffix() const { - return _internal_has_treat_whitespace_as_suffix(); -} -inline void TrainerSpec::clear_treat_whitespace_as_suffix() { - treat_whitespace_as_suffix_ = false; - _has_bits_[0] &= ~0x00008000u; -} -inline bool TrainerSpec::_internal_treat_whitespace_as_suffix() const { - return treat_whitespace_as_suffix_; -} -inline bool TrainerSpec::treat_whitespace_as_suffix() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.treat_whitespace_as_suffix) - return _internal_treat_whitespace_as_suffix(); -} -inline void TrainerSpec::_internal_set_treat_whitespace_as_suffix(bool value) { - _has_bits_[0] |= 0x00008000u; - treat_whitespace_as_suffix_ = value; -} -inline void TrainerSpec::set_treat_whitespace_as_suffix(bool value) { - _internal_set_treat_whitespace_as_suffix(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.treat_whitespace_as_suffix) -} - -// optional bool allow_whitespace_only_pieces = 26 [default = false]; -inline bool TrainerSpec::_internal_has_allow_whitespace_only_pieces() const { - bool value = (_has_bits_[0] & 0x00010000u) != 0; - return value; -} -inline bool TrainerSpec::has_allow_whitespace_only_pieces() const { - return _internal_has_allow_whitespace_only_pieces(); -} -inline void TrainerSpec::clear_allow_whitespace_only_pieces() { - allow_whitespace_only_pieces_ = false; - _has_bits_[0] &= ~0x00010000u; -} -inline bool TrainerSpec::_internal_allow_whitespace_only_pieces() const { - return allow_whitespace_only_pieces_; -} -inline bool TrainerSpec::allow_whitespace_only_pieces() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.allow_whitespace_only_pieces) - return _internal_allow_whitespace_only_pieces(); -} -inline void TrainerSpec::_internal_set_allow_whitespace_only_pieces(bool value) { - _has_bits_[0] |= 0x00010000u; - allow_whitespace_only_pieces_ = value; -} -inline void TrainerSpec::set_allow_whitespace_only_pieces(bool value) { - _internal_set_allow_whitespace_only_pieces(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.allow_whitespace_only_pieces) -} - -// optional bool split_digits = 25 [default = false]; -inline bool TrainerSpec::_internal_has_split_digits() const { - bool value = (_has_bits_[0] & 0x00020000u) != 0; - return value; -} -inline bool TrainerSpec::has_split_digits() const { - return _internal_has_split_digits(); -} -inline void TrainerSpec::clear_split_digits() { - split_digits_ = false; - _has_bits_[0] &= ~0x00020000u; -} -inline bool TrainerSpec::_internal_split_digits() const { - return split_digits_; -} -inline bool TrainerSpec::split_digits() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.split_digits) - return _internal_split_digits(); -} -inline void TrainerSpec::_internal_set_split_digits(bool value) { - _has_bits_[0] |= 0x00020000u; - split_digits_ = value; -} -inline void TrainerSpec::set_split_digits(bool value) { - _internal_set_split_digits(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.split_digits) -} - -// optional string pretokenization_delimiter = 53 [default = ""]; -inline bool TrainerSpec::_internal_has_pretokenization_delimiter() const { - bool value = (_has_bits_[0] & 0x00000100u) != 0; - return value; -} -inline bool TrainerSpec::has_pretokenization_delimiter() const { - return _internal_has_pretokenization_delimiter(); -} -inline void TrainerSpec::clear_pretokenization_delimiter() { - pretokenization_delimiter_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000100u; -} -inline const std::string& TrainerSpec::pretokenization_delimiter() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.pretokenization_delimiter) - return _internal_pretokenization_delimiter(); -} -inline void TrainerSpec::set_pretokenization_delimiter(const std::string& value) { - _internal_set_pretokenization_delimiter(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.pretokenization_delimiter) -} -inline std::string* TrainerSpec::mutable_pretokenization_delimiter() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.pretokenization_delimiter) - return _internal_mutable_pretokenization_delimiter(); -} -inline const std::string& TrainerSpec::_internal_pretokenization_delimiter() const { - return pretokenization_delimiter_.Get(); -} -inline void TrainerSpec::_internal_set_pretokenization_delimiter(const std::string& value) { - _has_bits_[0] |= 0x00000100u; - pretokenization_delimiter_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_pretokenization_delimiter(std::string&& value) { - _has_bits_[0] |= 0x00000100u; - pretokenization_delimiter_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.pretokenization_delimiter) -} -inline void TrainerSpec::set_pretokenization_delimiter(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000100u; - pretokenization_delimiter_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.pretokenization_delimiter) -} -inline void TrainerSpec::set_pretokenization_delimiter(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000100u; - pretokenization_delimiter_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.pretokenization_delimiter) -} -inline std::string* TrainerSpec::_internal_mutable_pretokenization_delimiter() { - _has_bits_[0] |= 0x00000100u; - return pretokenization_delimiter_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* TrainerSpec::release_pretokenization_delimiter() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.pretokenization_delimiter) - if (!_internal_has_pretokenization_delimiter()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000100u; - return pretokenization_delimiter_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void TrainerSpec::set_allocated_pretokenization_delimiter(std::string* pretokenization_delimiter) { - if (pretokenization_delimiter != nullptr) { - _has_bits_[0] |= 0x00000100u; - } else { - _has_bits_[0] &= ~0x00000100u; - } - pretokenization_delimiter_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), pretokenization_delimiter, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.pretokenization_delimiter) -} - -// repeated string control_symbols = 30; -inline int TrainerSpec::_internal_control_symbols_size() const { - return control_symbols_.size(); -} -inline int TrainerSpec::control_symbols_size() const { - return _internal_control_symbols_size(); -} -inline void TrainerSpec::clear_control_symbols() { - control_symbols_.Clear(); -} -inline std::string* TrainerSpec::add_control_symbols() { - // @@protoc_insertion_point(field_add_mutable:sentencepiece.TrainerSpec.control_symbols) - return _internal_add_control_symbols(); -} -inline const std::string& TrainerSpec::_internal_control_symbols(int index) const { - return control_symbols_.Get(index); -} -inline const std::string& TrainerSpec::control_symbols(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.control_symbols) - return _internal_control_symbols(index); -} -inline std::string* TrainerSpec::mutable_control_symbols(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.control_symbols) - return control_symbols_.Mutable(index); -} -inline void TrainerSpec::set_control_symbols(int index, const std::string& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.control_symbols) - control_symbols_.Mutable(index)->assign(value); -} -inline void TrainerSpec::set_control_symbols(int index, std::string&& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.control_symbols) - control_symbols_.Mutable(index)->assign(std::move(value)); -} -inline void TrainerSpec::set_control_symbols(int index, const char* value) { - GOOGLE_DCHECK(value != nullptr); - control_symbols_.Mutable(index)->assign(value); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.control_symbols) -} -inline void TrainerSpec::set_control_symbols(int index, const char* value, size_t size) { - control_symbols_.Mutable(index)->assign( - reinterpret_cast(value), size); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.control_symbols) -} -inline std::string* TrainerSpec::_internal_add_control_symbols() { - return control_symbols_.Add(); -} -inline void TrainerSpec::add_control_symbols(const std::string& value) { - control_symbols_.Add()->assign(value); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.control_symbols) -} -inline void TrainerSpec::add_control_symbols(std::string&& value) { - control_symbols_.Add(std::move(value)); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.control_symbols) -} -inline void TrainerSpec::add_control_symbols(const char* value) { - GOOGLE_DCHECK(value != nullptr); - control_symbols_.Add()->assign(value); - // @@protoc_insertion_point(field_add_char:sentencepiece.TrainerSpec.control_symbols) -} -inline void TrainerSpec::add_control_symbols(const char* value, size_t size) { - control_symbols_.Add()->assign(reinterpret_cast(value), size); - // @@protoc_insertion_point(field_add_pointer:sentencepiece.TrainerSpec.control_symbols) -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& -TrainerSpec::control_symbols() const { - // @@protoc_insertion_point(field_list:sentencepiece.TrainerSpec.control_symbols) - return control_symbols_; -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* -TrainerSpec::mutable_control_symbols() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.TrainerSpec.control_symbols) - return &control_symbols_; -} - -// repeated string user_defined_symbols = 31; -inline int TrainerSpec::_internal_user_defined_symbols_size() const { - return user_defined_symbols_.size(); -} -inline int TrainerSpec::user_defined_symbols_size() const { - return _internal_user_defined_symbols_size(); -} -inline void TrainerSpec::clear_user_defined_symbols() { - user_defined_symbols_.Clear(); -} -inline std::string* TrainerSpec::add_user_defined_symbols() { - // @@protoc_insertion_point(field_add_mutable:sentencepiece.TrainerSpec.user_defined_symbols) - return _internal_add_user_defined_symbols(); -} -inline const std::string& TrainerSpec::_internal_user_defined_symbols(int index) const { - return user_defined_symbols_.Get(index); -} -inline const std::string& TrainerSpec::user_defined_symbols(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.user_defined_symbols) - return _internal_user_defined_symbols(index); -} -inline std::string* TrainerSpec::mutable_user_defined_symbols(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.user_defined_symbols) - return user_defined_symbols_.Mutable(index); -} -inline void TrainerSpec::set_user_defined_symbols(int index, const std::string& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.user_defined_symbols) - user_defined_symbols_.Mutable(index)->assign(value); -} -inline void TrainerSpec::set_user_defined_symbols(int index, std::string&& value) { - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.user_defined_symbols) - user_defined_symbols_.Mutable(index)->assign(std::move(value)); -} -inline void TrainerSpec::set_user_defined_symbols(int index, const char* value) { - GOOGLE_DCHECK(value != nullptr); - user_defined_symbols_.Mutable(index)->assign(value); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.user_defined_symbols) -} -inline void TrainerSpec::set_user_defined_symbols(int index, const char* value, size_t size) { - user_defined_symbols_.Mutable(index)->assign( - reinterpret_cast(value), size); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.user_defined_symbols) -} -inline std::string* TrainerSpec::_internal_add_user_defined_symbols() { - return user_defined_symbols_.Add(); -} -inline void TrainerSpec::add_user_defined_symbols(const std::string& value) { - user_defined_symbols_.Add()->assign(value); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.user_defined_symbols) -} -inline void TrainerSpec::add_user_defined_symbols(std::string&& value) { - user_defined_symbols_.Add(std::move(value)); - // @@protoc_insertion_point(field_add:sentencepiece.TrainerSpec.user_defined_symbols) -} -inline void TrainerSpec::add_user_defined_symbols(const char* value) { - GOOGLE_DCHECK(value != nullptr); - user_defined_symbols_.Add()->assign(value); - // @@protoc_insertion_point(field_add_char:sentencepiece.TrainerSpec.user_defined_symbols) -} -inline void TrainerSpec::add_user_defined_symbols(const char* value, size_t size) { - user_defined_symbols_.Add()->assign(reinterpret_cast(value), size); - // @@protoc_insertion_point(field_add_pointer:sentencepiece.TrainerSpec.user_defined_symbols) -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField& -TrainerSpec::user_defined_symbols() const { - // @@protoc_insertion_point(field_list:sentencepiece.TrainerSpec.user_defined_symbols) - return user_defined_symbols_; -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField* -TrainerSpec::mutable_user_defined_symbols() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.TrainerSpec.user_defined_symbols) - return &user_defined_symbols_; -} - -// optional string required_chars = 36; -inline bool TrainerSpec::_internal_has_required_chars() const { - bool value = (_has_bits_[0] & 0x00000004u) != 0; - return value; -} -inline bool TrainerSpec::has_required_chars() const { - return _internal_has_required_chars(); -} -inline void TrainerSpec::clear_required_chars() { - required_chars_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000004u; -} -inline const std::string& TrainerSpec::required_chars() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.required_chars) - return _internal_required_chars(); -} -inline void TrainerSpec::set_required_chars(const std::string& value) { - _internal_set_required_chars(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.required_chars) -} -inline std::string* TrainerSpec::mutable_required_chars() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.required_chars) - return _internal_mutable_required_chars(); -} -inline const std::string& TrainerSpec::_internal_required_chars() const { - return required_chars_.Get(); -} -inline void TrainerSpec::_internal_set_required_chars(const std::string& value) { - _has_bits_[0] |= 0x00000004u; - required_chars_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_required_chars(std::string&& value) { - _has_bits_[0] |= 0x00000004u; - required_chars_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.required_chars) -} -inline void TrainerSpec::set_required_chars(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000004u; - required_chars_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.required_chars) -} -inline void TrainerSpec::set_required_chars(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000004u; - required_chars_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.required_chars) -} -inline std::string* TrainerSpec::_internal_mutable_required_chars() { - _has_bits_[0] |= 0x00000004u; - return required_chars_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* TrainerSpec::release_required_chars() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.required_chars) - if (!_internal_has_required_chars()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000004u; - return required_chars_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void TrainerSpec::set_allocated_required_chars(std::string* required_chars) { - if (required_chars != nullptr) { - _has_bits_[0] |= 0x00000004u; - } else { - _has_bits_[0] &= ~0x00000004u; - } - required_chars_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), required_chars, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.required_chars) -} - -// optional bool byte_fallback = 35 [default = false]; -inline bool TrainerSpec::_internal_has_byte_fallback() const { - bool value = (_has_bits_[0] & 0x00040000u) != 0; - return value; -} -inline bool TrainerSpec::has_byte_fallback() const { - return _internal_has_byte_fallback(); -} -inline void TrainerSpec::clear_byte_fallback() { - byte_fallback_ = false; - _has_bits_[0] &= ~0x00040000u; -} -inline bool TrainerSpec::_internal_byte_fallback() const { - return byte_fallback_; -} -inline bool TrainerSpec::byte_fallback() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.byte_fallback) - return _internal_byte_fallback(); -} -inline void TrainerSpec::_internal_set_byte_fallback(bool value) { - _has_bits_[0] |= 0x00040000u; - byte_fallback_ = value; -} -inline void TrainerSpec::set_byte_fallback(bool value) { - _internal_set_byte_fallback(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.byte_fallback) -} - -// optional bool vocabulary_output_piece_score = 32 [default = true]; -inline bool TrainerSpec::_internal_has_vocabulary_output_piece_score() const { - bool value = (_has_bits_[1] & 0x00000020u) != 0; - return value; -} -inline bool TrainerSpec::has_vocabulary_output_piece_score() const { - return _internal_has_vocabulary_output_piece_score(); -} -inline void TrainerSpec::clear_vocabulary_output_piece_score() { - vocabulary_output_piece_score_ = true; - _has_bits_[1] &= ~0x00000020u; -} -inline bool TrainerSpec::_internal_vocabulary_output_piece_score() const { - return vocabulary_output_piece_score_; -} -inline bool TrainerSpec::vocabulary_output_piece_score() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.vocabulary_output_piece_score) - return _internal_vocabulary_output_piece_score(); -} -inline void TrainerSpec::_internal_set_vocabulary_output_piece_score(bool value) { - _has_bits_[1] |= 0x00000020u; - vocabulary_output_piece_score_ = value; -} -inline void TrainerSpec::set_vocabulary_output_piece_score(bool value) { - _internal_set_vocabulary_output_piece_score(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.vocabulary_output_piece_score) -} - -// optional bool hard_vocab_limit = 33 [default = true]; -inline bool TrainerSpec::_internal_has_hard_vocab_limit() const { - bool value = (_has_bits_[1] & 0x00000040u) != 0; - return value; -} -inline bool TrainerSpec::has_hard_vocab_limit() const { - return _internal_has_hard_vocab_limit(); -} -inline void TrainerSpec::clear_hard_vocab_limit() { - hard_vocab_limit_ = true; - _has_bits_[1] &= ~0x00000040u; -} -inline bool TrainerSpec::_internal_hard_vocab_limit() const { - return hard_vocab_limit_; -} -inline bool TrainerSpec::hard_vocab_limit() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.hard_vocab_limit) - return _internal_hard_vocab_limit(); -} -inline void TrainerSpec::_internal_set_hard_vocab_limit(bool value) { - _has_bits_[1] |= 0x00000040u; - hard_vocab_limit_ = value; -} -inline void TrainerSpec::set_hard_vocab_limit(bool value) { - _internal_set_hard_vocab_limit(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.hard_vocab_limit) -} - -// optional bool use_all_vocab = 34 [default = false]; -inline bool TrainerSpec::_internal_has_use_all_vocab() const { - bool value = (_has_bits_[0] & 0x00080000u) != 0; - return value; -} -inline bool TrainerSpec::has_use_all_vocab() const { - return _internal_has_use_all_vocab(); -} -inline void TrainerSpec::clear_use_all_vocab() { - use_all_vocab_ = false; - _has_bits_[0] &= ~0x00080000u; -} -inline bool TrainerSpec::_internal_use_all_vocab() const { - return use_all_vocab_; -} -inline bool TrainerSpec::use_all_vocab() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.use_all_vocab) - return _internal_use_all_vocab(); -} -inline void TrainerSpec::_internal_set_use_all_vocab(bool value) { - _has_bits_[0] |= 0x00080000u; - use_all_vocab_ = value; -} -inline void TrainerSpec::set_use_all_vocab(bool value) { - _internal_set_use_all_vocab(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.use_all_vocab) -} - -// optional int32 unk_id = 40 [default = 0]; -inline bool TrainerSpec::_internal_has_unk_id() const { - bool value = (_has_bits_[0] & 0x00200000u) != 0; - return value; -} -inline bool TrainerSpec::has_unk_id() const { - return _internal_has_unk_id(); -} -inline void TrainerSpec::clear_unk_id() { - unk_id_ = 0; - _has_bits_[0] &= ~0x00200000u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_unk_id() const { - return unk_id_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::unk_id() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.unk_id) - return _internal_unk_id(); -} -inline void TrainerSpec::_internal_set_unk_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[0] |= 0x00200000u; - unk_id_ = value; -} -inline void TrainerSpec::set_unk_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_unk_id(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.unk_id) -} - -// optional int32 bos_id = 41 [default = 1]; -inline bool TrainerSpec::_internal_has_bos_id() const { - bool value = (_has_bits_[1] & 0x00000080u) != 0; - return value; -} -inline bool TrainerSpec::has_bos_id() const { - return _internal_has_bos_id(); -} -inline void TrainerSpec::clear_bos_id() { - bos_id_ = 1; - _has_bits_[1] &= ~0x00000080u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_bos_id() const { - return bos_id_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::bos_id() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.bos_id) - return _internal_bos_id(); -} -inline void TrainerSpec::_internal_set_bos_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[1] |= 0x00000080u; - bos_id_ = value; -} -inline void TrainerSpec::set_bos_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_bos_id(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.bos_id) -} - -// optional int32 eos_id = 42 [default = 2]; -inline bool TrainerSpec::_internal_has_eos_id() const { - bool value = (_has_bits_[1] & 0x00000100u) != 0; - return value; -} -inline bool TrainerSpec::has_eos_id() const { - return _internal_has_eos_id(); -} -inline void TrainerSpec::clear_eos_id() { - eos_id_ = 2; - _has_bits_[1] &= ~0x00000100u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_eos_id() const { - return eos_id_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::eos_id() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.eos_id) - return _internal_eos_id(); -} -inline void TrainerSpec::_internal_set_eos_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[1] |= 0x00000100u; - eos_id_ = value; -} -inline void TrainerSpec::set_eos_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_eos_id(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.eos_id) -} - -// optional int32 pad_id = 43 [default = -1]; -inline bool TrainerSpec::_internal_has_pad_id() const { - bool value = (_has_bits_[1] & 0x00000200u) != 0; - return value; -} -inline bool TrainerSpec::has_pad_id() const { - return _internal_has_pad_id(); -} -inline void TrainerSpec::clear_pad_id() { - pad_id_ = -1; - _has_bits_[1] &= ~0x00000200u; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::_internal_pad_id() const { - return pad_id_; -} -inline ::PROTOBUF_NAMESPACE_ID::int32 TrainerSpec::pad_id() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.pad_id) - return _internal_pad_id(); -} -inline void TrainerSpec::_internal_set_pad_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _has_bits_[1] |= 0x00000200u; - pad_id_ = value; -} -inline void TrainerSpec::set_pad_id(::PROTOBUF_NAMESPACE_ID::int32 value) { - _internal_set_pad_id(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.pad_id) -} - -// optional string unk_piece = 45 [default = ""]; -inline bool TrainerSpec::_internal_has_unk_piece() const { - bool value = (_has_bits_[0] & 0x00000010u) != 0; - return value; -} -inline bool TrainerSpec::has_unk_piece() const { - return _internal_has_unk_piece(); -} -inline void TrainerSpec::clear_unk_piece() { - unk_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_unk_piece_, GetArena()); - _has_bits_[0] &= ~0x00000010u; -} -inline const std::string& TrainerSpec::unk_piece() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.unk_piece) - if (unk_piece_.IsDefault(nullptr)) return _i_give_permission_to_break_this_code_default_unk_piece_.get(); - return _internal_unk_piece(); -} -inline void TrainerSpec::set_unk_piece(const std::string& value) { - _internal_set_unk_piece(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.unk_piece) -} -inline std::string* TrainerSpec::mutable_unk_piece() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.unk_piece) - return _internal_mutable_unk_piece(); -} -inline const std::string& TrainerSpec::_internal_unk_piece() const { - return unk_piece_.Get(); -} -inline void TrainerSpec::_internal_set_unk_piece(const std::string& value) { - _has_bits_[0] |= 0x00000010u; - unk_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_unk_piece(std::string&& value) { - _has_bits_[0] |= 0x00000010u; - unk_piece_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.unk_piece) -} -inline void TrainerSpec::set_unk_piece(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000010u; - unk_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.unk_piece) -} -inline void TrainerSpec::set_unk_piece(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000010u; - unk_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.unk_piece) -} -inline std::string* TrainerSpec::_internal_mutable_unk_piece() { - _has_bits_[0] |= 0x00000010u; - return unk_piece_.Mutable(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_unk_piece_, GetArena()); -} -inline std::string* TrainerSpec::release_unk_piece() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.unk_piece) - if (!_internal_has_unk_piece()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000010u; - return unk_piece_.ReleaseNonDefault(nullptr, GetArena()); -} -inline void TrainerSpec::set_allocated_unk_piece(std::string* unk_piece) { - if (unk_piece != nullptr) { - _has_bits_[0] |= 0x00000010u; - } else { - _has_bits_[0] &= ~0x00000010u; - } - unk_piece_.SetAllocated(nullptr, unk_piece, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.unk_piece) -} - -// optional string bos_piece = 46 [default = ""]; -inline bool TrainerSpec::_internal_has_bos_piece() const { - bool value = (_has_bits_[0] & 0x00000020u) != 0; - return value; -} -inline bool TrainerSpec::has_bos_piece() const { - return _internal_has_bos_piece(); -} -inline void TrainerSpec::clear_bos_piece() { - bos_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_bos_piece_, GetArena()); - _has_bits_[0] &= ~0x00000020u; -} -inline const std::string& TrainerSpec::bos_piece() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.bos_piece) - if (bos_piece_.IsDefault(nullptr)) return _i_give_permission_to_break_this_code_default_bos_piece_.get(); - return _internal_bos_piece(); -} -inline void TrainerSpec::set_bos_piece(const std::string& value) { - _internal_set_bos_piece(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.bos_piece) -} -inline std::string* TrainerSpec::mutable_bos_piece() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.bos_piece) - return _internal_mutable_bos_piece(); -} -inline const std::string& TrainerSpec::_internal_bos_piece() const { - return bos_piece_.Get(); -} -inline void TrainerSpec::_internal_set_bos_piece(const std::string& value) { - _has_bits_[0] |= 0x00000020u; - bos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_bos_piece(std::string&& value) { - _has_bits_[0] |= 0x00000020u; - bos_piece_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.bos_piece) -} -inline void TrainerSpec::set_bos_piece(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000020u; - bos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.bos_piece) -} -inline void TrainerSpec::set_bos_piece(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000020u; - bos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.bos_piece) -} -inline std::string* TrainerSpec::_internal_mutable_bos_piece() { - _has_bits_[0] |= 0x00000020u; - return bos_piece_.Mutable(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_bos_piece_, GetArena()); -} -inline std::string* TrainerSpec::release_bos_piece() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.bos_piece) - if (!_internal_has_bos_piece()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000020u; - return bos_piece_.ReleaseNonDefault(nullptr, GetArena()); -} -inline void TrainerSpec::set_allocated_bos_piece(std::string* bos_piece) { - if (bos_piece != nullptr) { - _has_bits_[0] |= 0x00000020u; - } else { - _has_bits_[0] &= ~0x00000020u; - } - bos_piece_.SetAllocated(nullptr, bos_piece, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.bos_piece) -} - -// optional string eos_piece = 47 [default = ""]; -inline bool TrainerSpec::_internal_has_eos_piece() const { - bool value = (_has_bits_[0] & 0x00000040u) != 0; - return value; -} -inline bool TrainerSpec::has_eos_piece() const { - return _internal_has_eos_piece(); -} -inline void TrainerSpec::clear_eos_piece() { - eos_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_eos_piece_, GetArena()); - _has_bits_[0] &= ~0x00000040u; -} -inline const std::string& TrainerSpec::eos_piece() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.eos_piece) - if (eos_piece_.IsDefault(nullptr)) return _i_give_permission_to_break_this_code_default_eos_piece_.get(); - return _internal_eos_piece(); -} -inline void TrainerSpec::set_eos_piece(const std::string& value) { - _internal_set_eos_piece(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.eos_piece) -} -inline std::string* TrainerSpec::mutable_eos_piece() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.eos_piece) - return _internal_mutable_eos_piece(); -} -inline const std::string& TrainerSpec::_internal_eos_piece() const { - return eos_piece_.Get(); -} -inline void TrainerSpec::_internal_set_eos_piece(const std::string& value) { - _has_bits_[0] |= 0x00000040u; - eos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_eos_piece(std::string&& value) { - _has_bits_[0] |= 0x00000040u; - eos_piece_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.eos_piece) -} -inline void TrainerSpec::set_eos_piece(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000040u; - eos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.eos_piece) -} -inline void TrainerSpec::set_eos_piece(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000040u; - eos_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.eos_piece) -} -inline std::string* TrainerSpec::_internal_mutable_eos_piece() { - _has_bits_[0] |= 0x00000040u; - return eos_piece_.Mutable(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_eos_piece_, GetArena()); -} -inline std::string* TrainerSpec::release_eos_piece() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.eos_piece) - if (!_internal_has_eos_piece()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000040u; - return eos_piece_.ReleaseNonDefault(nullptr, GetArena()); -} -inline void TrainerSpec::set_allocated_eos_piece(std::string* eos_piece) { - if (eos_piece != nullptr) { - _has_bits_[0] |= 0x00000040u; - } else { - _has_bits_[0] &= ~0x00000040u; - } - eos_piece_.SetAllocated(nullptr, eos_piece, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.eos_piece) -} - -// optional string pad_piece = 48 [default = ""]; -inline bool TrainerSpec::_internal_has_pad_piece() const { - bool value = (_has_bits_[0] & 0x00000080u) != 0; - return value; -} -inline bool TrainerSpec::has_pad_piece() const { - return _internal_has_pad_piece(); -} -inline void TrainerSpec::clear_pad_piece() { - pad_piece_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_pad_piece_, GetArena()); - _has_bits_[0] &= ~0x00000080u; -} -inline const std::string& TrainerSpec::pad_piece() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.pad_piece) - if (pad_piece_.IsDefault(nullptr)) return _i_give_permission_to_break_this_code_default_pad_piece_.get(); - return _internal_pad_piece(); -} -inline void TrainerSpec::set_pad_piece(const std::string& value) { - _internal_set_pad_piece(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.pad_piece) -} -inline std::string* TrainerSpec::mutable_pad_piece() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.pad_piece) - return _internal_mutable_pad_piece(); -} -inline const std::string& TrainerSpec::_internal_pad_piece() const { - return pad_piece_.Get(); -} -inline void TrainerSpec::_internal_set_pad_piece(const std::string& value) { - _has_bits_[0] |= 0x00000080u; - pad_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_pad_piece(std::string&& value) { - _has_bits_[0] |= 0x00000080u; - pad_piece_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.pad_piece) -} -inline void TrainerSpec::set_pad_piece(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000080u; - pad_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.pad_piece) -} -inline void TrainerSpec::set_pad_piece(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000080u; - pad_piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.pad_piece) -} -inline std::string* TrainerSpec::_internal_mutable_pad_piece() { - _has_bits_[0] |= 0x00000080u; - return pad_piece_.Mutable(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_pad_piece_, GetArena()); -} -inline std::string* TrainerSpec::release_pad_piece() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.pad_piece) - if (!_internal_has_pad_piece()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000080u; - return pad_piece_.ReleaseNonDefault(nullptr, GetArena()); -} -inline void TrainerSpec::set_allocated_pad_piece(std::string* pad_piece) { - if (pad_piece != nullptr) { - _has_bits_[0] |= 0x00000080u; - } else { - _has_bits_[0] &= ~0x00000080u; - } - pad_piece_.SetAllocated(nullptr, pad_piece, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.pad_piece) -} - -// optional string unk_surface = 44 [default = " \342\201\207 "]; -inline bool TrainerSpec::_internal_has_unk_surface() const { - bool value = (_has_bits_[0] & 0x00000008u) != 0; - return value; -} -inline bool TrainerSpec::has_unk_surface() const { - return _internal_has_unk_surface(); -} -inline void TrainerSpec::clear_unk_surface() { - unk_surface_.ClearToDefault(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_unk_surface_, GetArena()); - _has_bits_[0] &= ~0x00000008u; -} -inline const std::string& TrainerSpec::unk_surface() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.unk_surface) - if (unk_surface_.IsDefault(nullptr)) return _i_give_permission_to_break_this_code_default_unk_surface_.get(); - return _internal_unk_surface(); -} -inline void TrainerSpec::set_unk_surface(const std::string& value) { - _internal_set_unk_surface(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.unk_surface) -} -inline std::string* TrainerSpec::mutable_unk_surface() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.unk_surface) - return _internal_mutable_unk_surface(); -} -inline const std::string& TrainerSpec::_internal_unk_surface() const { - return unk_surface_.Get(); -} -inline void TrainerSpec::_internal_set_unk_surface(const std::string& value) { - _has_bits_[0] |= 0x00000008u; - unk_surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_unk_surface(std::string&& value) { - _has_bits_[0] |= 0x00000008u; - unk_surface_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.unk_surface) -} -inline void TrainerSpec::set_unk_surface(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000008u; - unk_surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.unk_surface) -} -inline void TrainerSpec::set_unk_surface(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000008u; - unk_surface_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::NonEmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.unk_surface) -} -inline std::string* TrainerSpec::_internal_mutable_unk_surface() { - _has_bits_[0] |= 0x00000008u; - return unk_surface_.Mutable(::sentencepiece::TrainerSpec::_i_give_permission_to_break_this_code_default_unk_surface_, GetArena()); -} -inline std::string* TrainerSpec::release_unk_surface() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.unk_surface) - if (!_internal_has_unk_surface()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000008u; - return unk_surface_.ReleaseNonDefault(nullptr, GetArena()); -} -inline void TrainerSpec::set_allocated_unk_surface(std::string* unk_surface) { - if (unk_surface != nullptr) { - _has_bits_[0] |= 0x00000008u; - } else { - _has_bits_[0] &= ~0x00000008u; - } - unk_surface_.SetAllocated(nullptr, unk_surface, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.unk_surface) -} - -// optional bool train_extremely_large_corpus = 49 [default = false]; -inline bool TrainerSpec::_internal_has_train_extremely_large_corpus() const { - bool value = (_has_bits_[0] & 0x00100000u) != 0; - return value; -} -inline bool TrainerSpec::has_train_extremely_large_corpus() const { - return _internal_has_train_extremely_large_corpus(); -} -inline void TrainerSpec::clear_train_extremely_large_corpus() { - train_extremely_large_corpus_ = false; - _has_bits_[0] &= ~0x00100000u; -} -inline bool TrainerSpec::_internal_train_extremely_large_corpus() const { - return train_extremely_large_corpus_; -} -inline bool TrainerSpec::train_extremely_large_corpus() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.train_extremely_large_corpus) - return _internal_train_extremely_large_corpus(); -} -inline void TrainerSpec::_internal_set_train_extremely_large_corpus(bool value) { - _has_bits_[0] |= 0x00100000u; - train_extremely_large_corpus_ = value; -} -inline void TrainerSpec::set_train_extremely_large_corpus(bool value) { - _internal_set_train_extremely_large_corpus(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.train_extremely_large_corpus) -} - -// optional string seed_sentencepieces_file = 54 [default = ""]; -inline bool TrainerSpec::_internal_has_seed_sentencepieces_file() const { - bool value = (_has_bits_[0] & 0x00000200u) != 0; - return value; -} -inline bool TrainerSpec::has_seed_sentencepieces_file() const { - return _internal_has_seed_sentencepieces_file(); -} -inline void TrainerSpec::clear_seed_sentencepieces_file() { - seed_sentencepieces_file_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000200u; -} -inline const std::string& TrainerSpec::seed_sentencepieces_file() const { - // @@protoc_insertion_point(field_get:sentencepiece.TrainerSpec.seed_sentencepieces_file) - return _internal_seed_sentencepieces_file(); -} -inline void TrainerSpec::set_seed_sentencepieces_file(const std::string& value) { - _internal_set_seed_sentencepieces_file(value); - // @@protoc_insertion_point(field_set:sentencepiece.TrainerSpec.seed_sentencepieces_file) -} -inline std::string* TrainerSpec::mutable_seed_sentencepieces_file() { - // @@protoc_insertion_point(field_mutable:sentencepiece.TrainerSpec.seed_sentencepieces_file) - return _internal_mutable_seed_sentencepieces_file(); -} -inline const std::string& TrainerSpec::_internal_seed_sentencepieces_file() const { - return seed_sentencepieces_file_.Get(); -} -inline void TrainerSpec::_internal_set_seed_sentencepieces_file(const std::string& value) { - _has_bits_[0] |= 0x00000200u; - seed_sentencepieces_file_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void TrainerSpec::set_seed_sentencepieces_file(std::string&& value) { - _has_bits_[0] |= 0x00000200u; - seed_sentencepieces_file_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.TrainerSpec.seed_sentencepieces_file) -} -inline void TrainerSpec::set_seed_sentencepieces_file(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000200u; - seed_sentencepieces_file_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.TrainerSpec.seed_sentencepieces_file) -} -inline void TrainerSpec::set_seed_sentencepieces_file(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000200u; - seed_sentencepieces_file_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.TrainerSpec.seed_sentencepieces_file) -} -inline std::string* TrainerSpec::_internal_mutable_seed_sentencepieces_file() { - _has_bits_[0] |= 0x00000200u; - return seed_sentencepieces_file_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* TrainerSpec::release_seed_sentencepieces_file() { - // @@protoc_insertion_point(field_release:sentencepiece.TrainerSpec.seed_sentencepieces_file) - if (!_internal_has_seed_sentencepieces_file()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000200u; - return seed_sentencepieces_file_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void TrainerSpec::set_allocated_seed_sentencepieces_file(std::string* seed_sentencepieces_file) { - if (seed_sentencepieces_file != nullptr) { - _has_bits_[0] |= 0x00000200u; - } else { - _has_bits_[0] &= ~0x00000200u; - } - seed_sentencepieces_file_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), seed_sentencepieces_file, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.TrainerSpec.seed_sentencepieces_file) -} - -// ------------------------------------------------------------------- - -// NormalizerSpec - -// optional string name = 1; -inline bool NormalizerSpec::_internal_has_name() const { - bool value = (_has_bits_[0] & 0x00000001u) != 0; - return value; -} -inline bool NormalizerSpec::has_name() const { - return _internal_has_name(); -} -inline void NormalizerSpec::clear_name() { - name_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000001u; -} -inline const std::string& NormalizerSpec::name() const { - // @@protoc_insertion_point(field_get:sentencepiece.NormalizerSpec.name) - return _internal_name(); -} -inline void NormalizerSpec::set_name(const std::string& value) { - _internal_set_name(value); - // @@protoc_insertion_point(field_set:sentencepiece.NormalizerSpec.name) -} -inline std::string* NormalizerSpec::mutable_name() { - // @@protoc_insertion_point(field_mutable:sentencepiece.NormalizerSpec.name) - return _internal_mutable_name(); -} -inline const std::string& NormalizerSpec::_internal_name() const { - return name_.Get(); -} -inline void NormalizerSpec::_internal_set_name(const std::string& value) { - _has_bits_[0] |= 0x00000001u; - name_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void NormalizerSpec::set_name(std::string&& value) { - _has_bits_[0] |= 0x00000001u; - name_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.NormalizerSpec.name) -} -inline void NormalizerSpec::set_name(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000001u; - name_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.NormalizerSpec.name) -} -inline void NormalizerSpec::set_name(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000001u; - name_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.NormalizerSpec.name) -} -inline std::string* NormalizerSpec::_internal_mutable_name() { - _has_bits_[0] |= 0x00000001u; - return name_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* NormalizerSpec::release_name() { - // @@protoc_insertion_point(field_release:sentencepiece.NormalizerSpec.name) - if (!_internal_has_name()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000001u; - return name_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void NormalizerSpec::set_allocated_name(std::string* name) { - if (name != nullptr) { - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - name_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.NormalizerSpec.name) -} - -// optional bytes precompiled_charsmap = 2; -inline bool NormalizerSpec::_internal_has_precompiled_charsmap() const { - bool value = (_has_bits_[0] & 0x00000002u) != 0; - return value; -} -inline bool NormalizerSpec::has_precompiled_charsmap() const { - return _internal_has_precompiled_charsmap(); -} -inline void NormalizerSpec::clear_precompiled_charsmap() { - precompiled_charsmap_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000002u; -} -inline const std::string& NormalizerSpec::precompiled_charsmap() const { - // @@protoc_insertion_point(field_get:sentencepiece.NormalizerSpec.precompiled_charsmap) - return _internal_precompiled_charsmap(); -} -inline void NormalizerSpec::set_precompiled_charsmap(const std::string& value) { - _internal_set_precompiled_charsmap(value); - // @@protoc_insertion_point(field_set:sentencepiece.NormalizerSpec.precompiled_charsmap) -} -inline std::string* NormalizerSpec::mutable_precompiled_charsmap() { - // @@protoc_insertion_point(field_mutable:sentencepiece.NormalizerSpec.precompiled_charsmap) - return _internal_mutable_precompiled_charsmap(); -} -inline const std::string& NormalizerSpec::_internal_precompiled_charsmap() const { - return precompiled_charsmap_.Get(); -} -inline void NormalizerSpec::_internal_set_precompiled_charsmap(const std::string& value) { - _has_bits_[0] |= 0x00000002u; - precompiled_charsmap_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void NormalizerSpec::set_precompiled_charsmap(std::string&& value) { - _has_bits_[0] |= 0x00000002u; - precompiled_charsmap_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.NormalizerSpec.precompiled_charsmap) -} -inline void NormalizerSpec::set_precompiled_charsmap(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000002u; - precompiled_charsmap_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.NormalizerSpec.precompiled_charsmap) -} -inline void NormalizerSpec::set_precompiled_charsmap(const void* value, - size_t size) { - _has_bits_[0] |= 0x00000002u; - precompiled_charsmap_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.NormalizerSpec.precompiled_charsmap) -} -inline std::string* NormalizerSpec::_internal_mutable_precompiled_charsmap() { - _has_bits_[0] |= 0x00000002u; - return precompiled_charsmap_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* NormalizerSpec::release_precompiled_charsmap() { - // @@protoc_insertion_point(field_release:sentencepiece.NormalizerSpec.precompiled_charsmap) - if (!_internal_has_precompiled_charsmap()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000002u; - return precompiled_charsmap_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void NormalizerSpec::set_allocated_precompiled_charsmap(std::string* precompiled_charsmap) { - if (precompiled_charsmap != nullptr) { - _has_bits_[0] |= 0x00000002u; - } else { - _has_bits_[0] &= ~0x00000002u; - } - precompiled_charsmap_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), precompiled_charsmap, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.NormalizerSpec.precompiled_charsmap) -} - -// optional bool add_dummy_prefix = 3 [default = true]; -inline bool NormalizerSpec::_internal_has_add_dummy_prefix() const { - bool value = (_has_bits_[0] & 0x00000008u) != 0; - return value; -} -inline bool NormalizerSpec::has_add_dummy_prefix() const { - return _internal_has_add_dummy_prefix(); -} -inline void NormalizerSpec::clear_add_dummy_prefix() { - add_dummy_prefix_ = true; - _has_bits_[0] &= ~0x00000008u; -} -inline bool NormalizerSpec::_internal_add_dummy_prefix() const { - return add_dummy_prefix_; -} -inline bool NormalizerSpec::add_dummy_prefix() const { - // @@protoc_insertion_point(field_get:sentencepiece.NormalizerSpec.add_dummy_prefix) - return _internal_add_dummy_prefix(); -} -inline void NormalizerSpec::_internal_set_add_dummy_prefix(bool value) { - _has_bits_[0] |= 0x00000008u; - add_dummy_prefix_ = value; -} -inline void NormalizerSpec::set_add_dummy_prefix(bool value) { - _internal_set_add_dummy_prefix(value); - // @@protoc_insertion_point(field_set:sentencepiece.NormalizerSpec.add_dummy_prefix) -} - -// optional bool remove_extra_whitespaces = 4 [default = true]; -inline bool NormalizerSpec::_internal_has_remove_extra_whitespaces() const { - bool value = (_has_bits_[0] & 0x00000010u) != 0; - return value; -} -inline bool NormalizerSpec::has_remove_extra_whitespaces() const { - return _internal_has_remove_extra_whitespaces(); -} -inline void NormalizerSpec::clear_remove_extra_whitespaces() { - remove_extra_whitespaces_ = true; - _has_bits_[0] &= ~0x00000010u; -} -inline bool NormalizerSpec::_internal_remove_extra_whitespaces() const { - return remove_extra_whitespaces_; -} -inline bool NormalizerSpec::remove_extra_whitespaces() const { - // @@protoc_insertion_point(field_get:sentencepiece.NormalizerSpec.remove_extra_whitespaces) - return _internal_remove_extra_whitespaces(); -} -inline void NormalizerSpec::_internal_set_remove_extra_whitespaces(bool value) { - _has_bits_[0] |= 0x00000010u; - remove_extra_whitespaces_ = value; -} -inline void NormalizerSpec::set_remove_extra_whitespaces(bool value) { - _internal_set_remove_extra_whitespaces(value); - // @@protoc_insertion_point(field_set:sentencepiece.NormalizerSpec.remove_extra_whitespaces) -} - -// optional bool escape_whitespaces = 5 [default = true]; -inline bool NormalizerSpec::_internal_has_escape_whitespaces() const { - bool value = (_has_bits_[0] & 0x00000020u) != 0; - return value; -} -inline bool NormalizerSpec::has_escape_whitespaces() const { - return _internal_has_escape_whitespaces(); -} -inline void NormalizerSpec::clear_escape_whitespaces() { - escape_whitespaces_ = true; - _has_bits_[0] &= ~0x00000020u; -} -inline bool NormalizerSpec::_internal_escape_whitespaces() const { - return escape_whitespaces_; -} -inline bool NormalizerSpec::escape_whitespaces() const { - // @@protoc_insertion_point(field_get:sentencepiece.NormalizerSpec.escape_whitespaces) - return _internal_escape_whitespaces(); -} -inline void NormalizerSpec::_internal_set_escape_whitespaces(bool value) { - _has_bits_[0] |= 0x00000020u; - escape_whitespaces_ = value; -} -inline void NormalizerSpec::set_escape_whitespaces(bool value) { - _internal_set_escape_whitespaces(value); - // @@protoc_insertion_point(field_set:sentencepiece.NormalizerSpec.escape_whitespaces) -} - -// optional string normalization_rule_tsv = 6; -inline bool NormalizerSpec::_internal_has_normalization_rule_tsv() const { - bool value = (_has_bits_[0] & 0x00000004u) != 0; - return value; -} -inline bool NormalizerSpec::has_normalization_rule_tsv() const { - return _internal_has_normalization_rule_tsv(); -} -inline void NormalizerSpec::clear_normalization_rule_tsv() { - normalization_rule_tsv_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000004u; -} -inline const std::string& NormalizerSpec::normalization_rule_tsv() const { - // @@protoc_insertion_point(field_get:sentencepiece.NormalizerSpec.normalization_rule_tsv) - return _internal_normalization_rule_tsv(); -} -inline void NormalizerSpec::set_normalization_rule_tsv(const std::string& value) { - _internal_set_normalization_rule_tsv(value); - // @@protoc_insertion_point(field_set:sentencepiece.NormalizerSpec.normalization_rule_tsv) -} -inline std::string* NormalizerSpec::mutable_normalization_rule_tsv() { - // @@protoc_insertion_point(field_mutable:sentencepiece.NormalizerSpec.normalization_rule_tsv) - return _internal_mutable_normalization_rule_tsv(); -} -inline const std::string& NormalizerSpec::_internal_normalization_rule_tsv() const { - return normalization_rule_tsv_.Get(); -} -inline void NormalizerSpec::_internal_set_normalization_rule_tsv(const std::string& value) { - _has_bits_[0] |= 0x00000004u; - normalization_rule_tsv_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void NormalizerSpec::set_normalization_rule_tsv(std::string&& value) { - _has_bits_[0] |= 0x00000004u; - normalization_rule_tsv_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.NormalizerSpec.normalization_rule_tsv) -} -inline void NormalizerSpec::set_normalization_rule_tsv(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000004u; - normalization_rule_tsv_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.NormalizerSpec.normalization_rule_tsv) -} -inline void NormalizerSpec::set_normalization_rule_tsv(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000004u; - normalization_rule_tsv_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.NormalizerSpec.normalization_rule_tsv) -} -inline std::string* NormalizerSpec::_internal_mutable_normalization_rule_tsv() { - _has_bits_[0] |= 0x00000004u; - return normalization_rule_tsv_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* NormalizerSpec::release_normalization_rule_tsv() { - // @@protoc_insertion_point(field_release:sentencepiece.NormalizerSpec.normalization_rule_tsv) - if (!_internal_has_normalization_rule_tsv()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000004u; - return normalization_rule_tsv_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void NormalizerSpec::set_allocated_normalization_rule_tsv(std::string* normalization_rule_tsv) { - if (normalization_rule_tsv != nullptr) { - _has_bits_[0] |= 0x00000004u; - } else { - _has_bits_[0] &= ~0x00000004u; - } - normalization_rule_tsv_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), normalization_rule_tsv, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.NormalizerSpec.normalization_rule_tsv) -} - -// ------------------------------------------------------------------- - -// SelfTestData_Sample - -// optional string input = 1; -inline bool SelfTestData_Sample::_internal_has_input() const { - bool value = (_has_bits_[0] & 0x00000001u) != 0; - return value; -} -inline bool SelfTestData_Sample::has_input() const { - return _internal_has_input(); -} -inline void SelfTestData_Sample::clear_input() { - input_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000001u; -} -inline const std::string& SelfTestData_Sample::input() const { - // @@protoc_insertion_point(field_get:sentencepiece.SelfTestData.Sample.input) - return _internal_input(); -} -inline void SelfTestData_Sample::set_input(const std::string& value) { - _internal_set_input(value); - // @@protoc_insertion_point(field_set:sentencepiece.SelfTestData.Sample.input) -} -inline std::string* SelfTestData_Sample::mutable_input() { - // @@protoc_insertion_point(field_mutable:sentencepiece.SelfTestData.Sample.input) - return _internal_mutable_input(); -} -inline const std::string& SelfTestData_Sample::_internal_input() const { - return input_.Get(); -} -inline void SelfTestData_Sample::_internal_set_input(const std::string& value) { - _has_bits_[0] |= 0x00000001u; - input_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void SelfTestData_Sample::set_input(std::string&& value) { - _has_bits_[0] |= 0x00000001u; - input_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.SelfTestData.Sample.input) -} -inline void SelfTestData_Sample::set_input(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000001u; - input_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.SelfTestData.Sample.input) -} -inline void SelfTestData_Sample::set_input(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000001u; - input_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.SelfTestData.Sample.input) -} -inline std::string* SelfTestData_Sample::_internal_mutable_input() { - _has_bits_[0] |= 0x00000001u; - return input_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* SelfTestData_Sample::release_input() { - // @@protoc_insertion_point(field_release:sentencepiece.SelfTestData.Sample.input) - if (!_internal_has_input()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000001u; - return input_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void SelfTestData_Sample::set_allocated_input(std::string* input) { - if (input != nullptr) { - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - input_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), input, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.SelfTestData.Sample.input) -} - -// optional string expected = 2; -inline bool SelfTestData_Sample::_internal_has_expected() const { - bool value = (_has_bits_[0] & 0x00000002u) != 0; - return value; -} -inline bool SelfTestData_Sample::has_expected() const { - return _internal_has_expected(); -} -inline void SelfTestData_Sample::clear_expected() { - expected_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000002u; -} -inline const std::string& SelfTestData_Sample::expected() const { - // @@protoc_insertion_point(field_get:sentencepiece.SelfTestData.Sample.expected) - return _internal_expected(); -} -inline void SelfTestData_Sample::set_expected(const std::string& value) { - _internal_set_expected(value); - // @@protoc_insertion_point(field_set:sentencepiece.SelfTestData.Sample.expected) -} -inline std::string* SelfTestData_Sample::mutable_expected() { - // @@protoc_insertion_point(field_mutable:sentencepiece.SelfTestData.Sample.expected) - return _internal_mutable_expected(); -} -inline const std::string& SelfTestData_Sample::_internal_expected() const { - return expected_.Get(); -} -inline void SelfTestData_Sample::_internal_set_expected(const std::string& value) { - _has_bits_[0] |= 0x00000002u; - expected_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void SelfTestData_Sample::set_expected(std::string&& value) { - _has_bits_[0] |= 0x00000002u; - expected_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.SelfTestData.Sample.expected) -} -inline void SelfTestData_Sample::set_expected(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000002u; - expected_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.SelfTestData.Sample.expected) -} -inline void SelfTestData_Sample::set_expected(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000002u; - expected_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.SelfTestData.Sample.expected) -} -inline std::string* SelfTestData_Sample::_internal_mutable_expected() { - _has_bits_[0] |= 0x00000002u; - return expected_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* SelfTestData_Sample::release_expected() { - // @@protoc_insertion_point(field_release:sentencepiece.SelfTestData.Sample.expected) - if (!_internal_has_expected()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000002u; - return expected_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void SelfTestData_Sample::set_allocated_expected(std::string* expected) { - if (expected != nullptr) { - _has_bits_[0] |= 0x00000002u; - } else { - _has_bits_[0] &= ~0x00000002u; - } - expected_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), expected, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.SelfTestData.Sample.expected) -} - -// ------------------------------------------------------------------- - -// SelfTestData - -// repeated .sentencepiece.SelfTestData.Sample samples = 1; -inline int SelfTestData::_internal_samples_size() const { - return samples_.size(); -} -inline int SelfTestData::samples_size() const { - return _internal_samples_size(); -} -inline void SelfTestData::clear_samples() { - samples_.Clear(); -} -inline ::sentencepiece::SelfTestData_Sample* SelfTestData::mutable_samples(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.SelfTestData.samples) - return samples_.Mutable(index); -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SelfTestData_Sample >* -SelfTestData::mutable_samples() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.SelfTestData.samples) - return &samples_; -} -inline const ::sentencepiece::SelfTestData_Sample& SelfTestData::_internal_samples(int index) const { - return samples_.Get(index); -} -inline const ::sentencepiece::SelfTestData_Sample& SelfTestData::samples(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.SelfTestData.samples) - return _internal_samples(index); -} -inline ::sentencepiece::SelfTestData_Sample* SelfTestData::_internal_add_samples() { - return samples_.Add(); -} -inline ::sentencepiece::SelfTestData_Sample* SelfTestData::add_samples() { - // @@protoc_insertion_point(field_add:sentencepiece.SelfTestData.samples) - return _internal_add_samples(); -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::SelfTestData_Sample >& -SelfTestData::samples() const { - // @@protoc_insertion_point(field_list:sentencepiece.SelfTestData.samples) - return samples_; -} - -// ------------------------------------------------------------------- - -// ModelProto_SentencePiece - -// optional string piece = 1; -inline bool ModelProto_SentencePiece::_internal_has_piece() const { - bool value = (_has_bits_[0] & 0x00000001u) != 0; - return value; -} -inline bool ModelProto_SentencePiece::has_piece() const { - return _internal_has_piece(); -} -inline void ModelProto_SentencePiece::clear_piece() { - piece_.ClearToEmpty(); - _has_bits_[0] &= ~0x00000001u; -} -inline const std::string& ModelProto_SentencePiece::piece() const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.SentencePiece.piece) - return _internal_piece(); -} -inline void ModelProto_SentencePiece::set_piece(const std::string& value) { - _internal_set_piece(value); - // @@protoc_insertion_point(field_set:sentencepiece.ModelProto.SentencePiece.piece) -} -inline std::string* ModelProto_SentencePiece::mutable_piece() { - // @@protoc_insertion_point(field_mutable:sentencepiece.ModelProto.SentencePiece.piece) - return _internal_mutable_piece(); -} -inline const std::string& ModelProto_SentencePiece::_internal_piece() const { - return piece_.Get(); -} -inline void ModelProto_SentencePiece::_internal_set_piece(const std::string& value) { - _has_bits_[0] |= 0x00000001u; - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, value, GetArena()); -} -inline void ModelProto_SentencePiece::set_piece(std::string&& value) { - _has_bits_[0] |= 0x00000001u; - piece_.Set( - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::move(value), GetArena()); - // @@protoc_insertion_point(field_set_rvalue:sentencepiece.ModelProto.SentencePiece.piece) -} -inline void ModelProto_SentencePiece::set_piece(const char* value) { - GOOGLE_DCHECK(value != nullptr); - _has_bits_[0] |= 0x00000001u; - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string(value), GetArena()); - // @@protoc_insertion_point(field_set_char:sentencepiece.ModelProto.SentencePiece.piece) -} -inline void ModelProto_SentencePiece::set_piece(const char* value, - size_t size) { - _has_bits_[0] |= 0x00000001u; - piece_.Set(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, ::std::string( - reinterpret_cast(value), size), GetArena()); - // @@protoc_insertion_point(field_set_pointer:sentencepiece.ModelProto.SentencePiece.piece) -} -inline std::string* ModelProto_SentencePiece::_internal_mutable_piece() { - _has_bits_[0] |= 0x00000001u; - return piece_.Mutable(::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::EmptyDefault{}, GetArena()); -} -inline std::string* ModelProto_SentencePiece::release_piece() { - // @@protoc_insertion_point(field_release:sentencepiece.ModelProto.SentencePiece.piece) - if (!_internal_has_piece()) { - return nullptr; - } - _has_bits_[0] &= ~0x00000001u; - return piece_.ReleaseNonDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), GetArena()); -} -inline void ModelProto_SentencePiece::set_allocated_piece(std::string* piece) { - if (piece != nullptr) { - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - piece_.SetAllocated(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), piece, - GetArena()); - // @@protoc_insertion_point(field_set_allocated:sentencepiece.ModelProto.SentencePiece.piece) -} - -// optional float score = 2; -inline bool ModelProto_SentencePiece::_internal_has_score() const { - bool value = (_has_bits_[0] & 0x00000002u) != 0; - return value; -} -inline bool ModelProto_SentencePiece::has_score() const { - return _internal_has_score(); -} -inline void ModelProto_SentencePiece::clear_score() { - score_ = 0; - _has_bits_[0] &= ~0x00000002u; -} -inline float ModelProto_SentencePiece::_internal_score() const { - return score_; -} -inline float ModelProto_SentencePiece::score() const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.SentencePiece.score) - return _internal_score(); -} -inline void ModelProto_SentencePiece::_internal_set_score(float value) { - _has_bits_[0] |= 0x00000002u; - score_ = value; -} -inline void ModelProto_SentencePiece::set_score(float value) { - _internal_set_score(value); - // @@protoc_insertion_point(field_set:sentencepiece.ModelProto.SentencePiece.score) -} - -// optional .sentencepiece.ModelProto.SentencePiece.Type type = 3 [default = NORMAL]; -inline bool ModelProto_SentencePiece::_internal_has_type() const { - bool value = (_has_bits_[0] & 0x00000004u) != 0; - return value; -} -inline bool ModelProto_SentencePiece::has_type() const { - return _internal_has_type(); -} -inline void ModelProto_SentencePiece::clear_type() { - type_ = 1; - _has_bits_[0] &= ~0x00000004u; -} -inline ::sentencepiece::ModelProto_SentencePiece_Type ModelProto_SentencePiece::_internal_type() const { - return static_cast< ::sentencepiece::ModelProto_SentencePiece_Type >(type_); -} -inline ::sentencepiece::ModelProto_SentencePiece_Type ModelProto_SentencePiece::type() const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.SentencePiece.type) - return _internal_type(); -} -inline void ModelProto_SentencePiece::_internal_set_type(::sentencepiece::ModelProto_SentencePiece_Type value) { - assert(::sentencepiece::ModelProto_SentencePiece_Type_IsValid(value)); - _has_bits_[0] |= 0x00000004u; - type_ = value; -} -inline void ModelProto_SentencePiece::set_type(::sentencepiece::ModelProto_SentencePiece_Type value) { - _internal_set_type(value); - // @@protoc_insertion_point(field_set:sentencepiece.ModelProto.SentencePiece.type) -} - -// ------------------------------------------------------------------- - -// ModelProto - -// repeated .sentencepiece.ModelProto.SentencePiece pieces = 1; -inline int ModelProto::_internal_pieces_size() const { - return pieces_.size(); -} -inline int ModelProto::pieces_size() const { - return _internal_pieces_size(); -} -inline void ModelProto::clear_pieces() { - pieces_.Clear(); -} -inline ::sentencepiece::ModelProto_SentencePiece* ModelProto::mutable_pieces(int index) { - // @@protoc_insertion_point(field_mutable:sentencepiece.ModelProto.pieces) - return pieces_.Mutable(index); -} -inline ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::ModelProto_SentencePiece >* -ModelProto::mutable_pieces() { - // @@protoc_insertion_point(field_mutable_list:sentencepiece.ModelProto.pieces) - return &pieces_; -} -inline const ::sentencepiece::ModelProto_SentencePiece& ModelProto::_internal_pieces(int index) const { - return pieces_.Get(index); -} -inline const ::sentencepiece::ModelProto_SentencePiece& ModelProto::pieces(int index) const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.pieces) - return _internal_pieces(index); -} -inline ::sentencepiece::ModelProto_SentencePiece* ModelProto::_internal_add_pieces() { - return pieces_.Add(); -} -inline ::sentencepiece::ModelProto_SentencePiece* ModelProto::add_pieces() { - // @@protoc_insertion_point(field_add:sentencepiece.ModelProto.pieces) - return _internal_add_pieces(); -} -inline const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::sentencepiece::ModelProto_SentencePiece >& -ModelProto::pieces() const { - // @@protoc_insertion_point(field_list:sentencepiece.ModelProto.pieces) - return pieces_; -} - -// optional .sentencepiece.TrainerSpec trainer_spec = 2; -inline bool ModelProto::_internal_has_trainer_spec() const { - bool value = (_has_bits_[0] & 0x00000001u) != 0; - PROTOBUF_ASSUME(!value || trainer_spec_ != nullptr); - return value; -} -inline bool ModelProto::has_trainer_spec() const { - return _internal_has_trainer_spec(); -} -inline void ModelProto::clear_trainer_spec() { - if (trainer_spec_ != nullptr) trainer_spec_->Clear(); - _has_bits_[0] &= ~0x00000001u; -} -inline const ::sentencepiece::TrainerSpec& ModelProto::_internal_trainer_spec() const { - const ::sentencepiece::TrainerSpec* p = trainer_spec_; - return p != nullptr ? *p : reinterpret_cast( - ::sentencepiece::_TrainerSpec_default_instance_); -} -inline const ::sentencepiece::TrainerSpec& ModelProto::trainer_spec() const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.trainer_spec) - return _internal_trainer_spec(); -} -inline void ModelProto::unsafe_arena_set_allocated_trainer_spec( - ::sentencepiece::TrainerSpec* trainer_spec) { - if (GetArena() == nullptr) { - delete reinterpret_cast<::PROTOBUF_NAMESPACE_ID::MessageLite*>(trainer_spec_); - } - trainer_spec_ = trainer_spec; - if (trainer_spec) { - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - // @@protoc_insertion_point(field_unsafe_arena_set_allocated:sentencepiece.ModelProto.trainer_spec) -} -inline ::sentencepiece::TrainerSpec* ModelProto::release_trainer_spec() { - _has_bits_[0] &= ~0x00000001u; - ::sentencepiece::TrainerSpec* temp = trainer_spec_; - trainer_spec_ = nullptr; - if (GetArena() != nullptr) { - temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); - } - return temp; -} -inline ::sentencepiece::TrainerSpec* ModelProto::unsafe_arena_release_trainer_spec() { - // @@protoc_insertion_point(field_release:sentencepiece.ModelProto.trainer_spec) - _has_bits_[0] &= ~0x00000001u; - ::sentencepiece::TrainerSpec* temp = trainer_spec_; - trainer_spec_ = nullptr; - return temp; -} -inline ::sentencepiece::TrainerSpec* ModelProto::_internal_mutable_trainer_spec() { - _has_bits_[0] |= 0x00000001u; - if (trainer_spec_ == nullptr) { - auto* p = CreateMaybeMessage<::sentencepiece::TrainerSpec>(GetArena()); - trainer_spec_ = p; - } - return trainer_spec_; -} -inline ::sentencepiece::TrainerSpec* ModelProto::mutable_trainer_spec() { - // @@protoc_insertion_point(field_mutable:sentencepiece.ModelProto.trainer_spec) - return _internal_mutable_trainer_spec(); -} -inline void ModelProto::set_allocated_trainer_spec(::sentencepiece::TrainerSpec* trainer_spec) { - ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArena(); - if (message_arena == nullptr) { - delete trainer_spec_; - } - if (trainer_spec) { - ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = - ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(trainer_spec); - if (message_arena != submessage_arena) { - trainer_spec = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( - message_arena, trainer_spec, submessage_arena); - } - _has_bits_[0] |= 0x00000001u; - } else { - _has_bits_[0] &= ~0x00000001u; - } - trainer_spec_ = trainer_spec; - // @@protoc_insertion_point(field_set_allocated:sentencepiece.ModelProto.trainer_spec) -} - -// optional .sentencepiece.NormalizerSpec normalizer_spec = 3; -inline bool ModelProto::_internal_has_normalizer_spec() const { - bool value = (_has_bits_[0] & 0x00000002u) != 0; - PROTOBUF_ASSUME(!value || normalizer_spec_ != nullptr); - return value; -} -inline bool ModelProto::has_normalizer_spec() const { - return _internal_has_normalizer_spec(); -} -inline void ModelProto::clear_normalizer_spec() { - if (normalizer_spec_ != nullptr) normalizer_spec_->Clear(); - _has_bits_[0] &= ~0x00000002u; -} -inline const ::sentencepiece::NormalizerSpec& ModelProto::_internal_normalizer_spec() const { - const ::sentencepiece::NormalizerSpec* p = normalizer_spec_; - return p != nullptr ? *p : reinterpret_cast( - ::sentencepiece::_NormalizerSpec_default_instance_); -} -inline const ::sentencepiece::NormalizerSpec& ModelProto::normalizer_spec() const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.normalizer_spec) - return _internal_normalizer_spec(); -} -inline void ModelProto::unsafe_arena_set_allocated_normalizer_spec( - ::sentencepiece::NormalizerSpec* normalizer_spec) { - if (GetArena() == nullptr) { - delete reinterpret_cast<::PROTOBUF_NAMESPACE_ID::MessageLite*>(normalizer_spec_); - } - normalizer_spec_ = normalizer_spec; - if (normalizer_spec) { - _has_bits_[0] |= 0x00000002u; - } else { - _has_bits_[0] &= ~0x00000002u; - } - // @@protoc_insertion_point(field_unsafe_arena_set_allocated:sentencepiece.ModelProto.normalizer_spec) -} -inline ::sentencepiece::NormalizerSpec* ModelProto::release_normalizer_spec() { - _has_bits_[0] &= ~0x00000002u; - ::sentencepiece::NormalizerSpec* temp = normalizer_spec_; - normalizer_spec_ = nullptr; - if (GetArena() != nullptr) { - temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); - } - return temp; -} -inline ::sentencepiece::NormalizerSpec* ModelProto::unsafe_arena_release_normalizer_spec() { - // @@protoc_insertion_point(field_release:sentencepiece.ModelProto.normalizer_spec) - _has_bits_[0] &= ~0x00000002u; - ::sentencepiece::NormalizerSpec* temp = normalizer_spec_; - normalizer_spec_ = nullptr; - return temp; -} -inline ::sentencepiece::NormalizerSpec* ModelProto::_internal_mutable_normalizer_spec() { - _has_bits_[0] |= 0x00000002u; - if (normalizer_spec_ == nullptr) { - auto* p = CreateMaybeMessage<::sentencepiece::NormalizerSpec>(GetArena()); - normalizer_spec_ = p; - } - return normalizer_spec_; -} -inline ::sentencepiece::NormalizerSpec* ModelProto::mutable_normalizer_spec() { - // @@protoc_insertion_point(field_mutable:sentencepiece.ModelProto.normalizer_spec) - return _internal_mutable_normalizer_spec(); -} -inline void ModelProto::set_allocated_normalizer_spec(::sentencepiece::NormalizerSpec* normalizer_spec) { - ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArena(); - if (message_arena == nullptr) { - delete normalizer_spec_; - } - if (normalizer_spec) { - ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = - ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(normalizer_spec); - if (message_arena != submessage_arena) { - normalizer_spec = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( - message_arena, normalizer_spec, submessage_arena); - } - _has_bits_[0] |= 0x00000002u; - } else { - _has_bits_[0] &= ~0x00000002u; - } - normalizer_spec_ = normalizer_spec; - // @@protoc_insertion_point(field_set_allocated:sentencepiece.ModelProto.normalizer_spec) -} - -// optional .sentencepiece.SelfTestData self_test_data = 4; -inline bool ModelProto::_internal_has_self_test_data() const { - bool value = (_has_bits_[0] & 0x00000004u) != 0; - PROTOBUF_ASSUME(!value || self_test_data_ != nullptr); - return value; -} -inline bool ModelProto::has_self_test_data() const { - return _internal_has_self_test_data(); -} -inline void ModelProto::clear_self_test_data() { - if (self_test_data_ != nullptr) self_test_data_->Clear(); - _has_bits_[0] &= ~0x00000004u; -} -inline const ::sentencepiece::SelfTestData& ModelProto::_internal_self_test_data() const { - const ::sentencepiece::SelfTestData* p = self_test_data_; - return p != nullptr ? *p : reinterpret_cast( - ::sentencepiece::_SelfTestData_default_instance_); -} -inline const ::sentencepiece::SelfTestData& ModelProto::self_test_data() const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.self_test_data) - return _internal_self_test_data(); -} -inline void ModelProto::unsafe_arena_set_allocated_self_test_data( - ::sentencepiece::SelfTestData* self_test_data) { - if (GetArena() == nullptr) { - delete reinterpret_cast<::PROTOBUF_NAMESPACE_ID::MessageLite*>(self_test_data_); - } - self_test_data_ = self_test_data; - if (self_test_data) { - _has_bits_[0] |= 0x00000004u; - } else { - _has_bits_[0] &= ~0x00000004u; - } - // @@protoc_insertion_point(field_unsafe_arena_set_allocated:sentencepiece.ModelProto.self_test_data) -} -inline ::sentencepiece::SelfTestData* ModelProto::release_self_test_data() { - _has_bits_[0] &= ~0x00000004u; - ::sentencepiece::SelfTestData* temp = self_test_data_; - self_test_data_ = nullptr; - if (GetArena() != nullptr) { - temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); - } - return temp; -} -inline ::sentencepiece::SelfTestData* ModelProto::unsafe_arena_release_self_test_data() { - // @@protoc_insertion_point(field_release:sentencepiece.ModelProto.self_test_data) - _has_bits_[0] &= ~0x00000004u; - ::sentencepiece::SelfTestData* temp = self_test_data_; - self_test_data_ = nullptr; - return temp; -} -inline ::sentencepiece::SelfTestData* ModelProto::_internal_mutable_self_test_data() { - _has_bits_[0] |= 0x00000004u; - if (self_test_data_ == nullptr) { - auto* p = CreateMaybeMessage<::sentencepiece::SelfTestData>(GetArena()); - self_test_data_ = p; - } - return self_test_data_; -} -inline ::sentencepiece::SelfTestData* ModelProto::mutable_self_test_data() { - // @@protoc_insertion_point(field_mutable:sentencepiece.ModelProto.self_test_data) - return _internal_mutable_self_test_data(); -} -inline void ModelProto::set_allocated_self_test_data(::sentencepiece::SelfTestData* self_test_data) { - ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArena(); - if (message_arena == nullptr) { - delete self_test_data_; - } - if (self_test_data) { - ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = - ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(self_test_data); - if (message_arena != submessage_arena) { - self_test_data = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( - message_arena, self_test_data, submessage_arena); - } - _has_bits_[0] |= 0x00000004u; - } else { - _has_bits_[0] &= ~0x00000004u; - } - self_test_data_ = self_test_data; - // @@protoc_insertion_point(field_set_allocated:sentencepiece.ModelProto.self_test_data) -} - -// optional .sentencepiece.NormalizerSpec denormalizer_spec = 5; -inline bool ModelProto::_internal_has_denormalizer_spec() const { - bool value = (_has_bits_[0] & 0x00000008u) != 0; - PROTOBUF_ASSUME(!value || denormalizer_spec_ != nullptr); - return value; -} -inline bool ModelProto::has_denormalizer_spec() const { - return _internal_has_denormalizer_spec(); -} -inline void ModelProto::clear_denormalizer_spec() { - if (denormalizer_spec_ != nullptr) denormalizer_spec_->Clear(); - _has_bits_[0] &= ~0x00000008u; -} -inline const ::sentencepiece::NormalizerSpec& ModelProto::_internal_denormalizer_spec() const { - const ::sentencepiece::NormalizerSpec* p = denormalizer_spec_; - return p != nullptr ? *p : reinterpret_cast( - ::sentencepiece::_NormalizerSpec_default_instance_); -} -inline const ::sentencepiece::NormalizerSpec& ModelProto::denormalizer_spec() const { - // @@protoc_insertion_point(field_get:sentencepiece.ModelProto.denormalizer_spec) - return _internal_denormalizer_spec(); -} -inline void ModelProto::unsafe_arena_set_allocated_denormalizer_spec( - ::sentencepiece::NormalizerSpec* denormalizer_spec) { - if (GetArena() == nullptr) { - delete reinterpret_cast<::PROTOBUF_NAMESPACE_ID::MessageLite*>(denormalizer_spec_); - } - denormalizer_spec_ = denormalizer_spec; - if (denormalizer_spec) { - _has_bits_[0] |= 0x00000008u; - } else { - _has_bits_[0] &= ~0x00000008u; - } - // @@protoc_insertion_point(field_unsafe_arena_set_allocated:sentencepiece.ModelProto.denormalizer_spec) -} -inline ::sentencepiece::NormalizerSpec* ModelProto::release_denormalizer_spec() { - _has_bits_[0] &= ~0x00000008u; - ::sentencepiece::NormalizerSpec* temp = denormalizer_spec_; - denormalizer_spec_ = nullptr; - if (GetArena() != nullptr) { - temp = ::PROTOBUF_NAMESPACE_ID::internal::DuplicateIfNonNull(temp); - } - return temp; -} -inline ::sentencepiece::NormalizerSpec* ModelProto::unsafe_arena_release_denormalizer_spec() { - // @@protoc_insertion_point(field_release:sentencepiece.ModelProto.denormalizer_spec) - _has_bits_[0] &= ~0x00000008u; - ::sentencepiece::NormalizerSpec* temp = denormalizer_spec_; - denormalizer_spec_ = nullptr; - return temp; -} -inline ::sentencepiece::NormalizerSpec* ModelProto::_internal_mutable_denormalizer_spec() { - _has_bits_[0] |= 0x00000008u; - if (denormalizer_spec_ == nullptr) { - auto* p = CreateMaybeMessage<::sentencepiece::NormalizerSpec>(GetArena()); - denormalizer_spec_ = p; - } - return denormalizer_spec_; -} -inline ::sentencepiece::NormalizerSpec* ModelProto::mutable_denormalizer_spec() { - // @@protoc_insertion_point(field_mutable:sentencepiece.ModelProto.denormalizer_spec) - return _internal_mutable_denormalizer_spec(); -} -inline void ModelProto::set_allocated_denormalizer_spec(::sentencepiece::NormalizerSpec* denormalizer_spec) { - ::PROTOBUF_NAMESPACE_ID::Arena* message_arena = GetArena(); - if (message_arena == nullptr) { - delete denormalizer_spec_; - } - if (denormalizer_spec) { - ::PROTOBUF_NAMESPACE_ID::Arena* submessage_arena = - ::PROTOBUF_NAMESPACE_ID::Arena::GetArena(denormalizer_spec); - if (message_arena != submessage_arena) { - denormalizer_spec = ::PROTOBUF_NAMESPACE_ID::internal::GetOwnedMessage( - message_arena, denormalizer_spec, submessage_arena); - } - _has_bits_[0] |= 0x00000008u; - } else { - _has_bits_[0] &= ~0x00000008u; - } - denormalizer_spec_ = denormalizer_spec; - // @@protoc_insertion_point(field_set_allocated:sentencepiece.ModelProto.denormalizer_spec) -} - -#ifdef __GNUC__ - #pragma GCC diagnostic pop -#endif // __GNUC__ -// ------------------------------------------------------------------- - -// ------------------------------------------------------------------- - -// ------------------------------------------------------------------- - -// ------------------------------------------------------------------- - -// ------------------------------------------------------------------- - - -// @@protoc_insertion_point(namespace_scope) - -} // namespace sentencepiece - -PROTOBUF_NAMESPACE_OPEN - -template <> struct is_proto_enum< ::sentencepiece::TrainerSpec_ModelType> : ::std::true_type {}; -template <> struct is_proto_enum< ::sentencepiece::ModelProto_SentencePiece_Type> : ::std::true_type {}; - -PROTOBUF_NAMESPACE_CLOSE - -// @@protoc_insertion_point(global_scope) - -#include -#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_sentencepiece_5fmodel_2eproto diff --git a/src/builtin_upb/README.txt b/src/builtin_upb/README.txt new file mode 100644 index 000000000..19c533f87 --- /dev/null +++ b/src/builtin_upb/README.txt @@ -0,0 +1,20 @@ +How to generate upb stub files: + +1. Install protobuf compiler (protoc) and upb compiler (upbc). + See: https://github.com/protocolbuffers/upb + +2. Run the following commands to generate stub files from .proto files: + + # From this directory: + protoc -I../ --upb_out=. ../sentencepiece.proto + protoc -I../ --upb_out=. ../sentencepiece_model.proto + + # If you need upbdefs (not used by default, but kept for compatibility): + # protoc -I../ --upbdefs_out=. ../sentencepiece.proto + # protoc -I../ --upbdefs_out=. ../sentencepiece_model.proto + + (Note: depending on the upb version, you may need to specify the plugin path: + --plugin=protoc-gen-upb=/path/to/upbc) + +These generated files (*.upb.h, *.upb.c) are checked into VCS +to allow building without upb compiler dependency. diff --git a/src/builtin_upb/sentencepiece.pb.h b/src/builtin_upb/sentencepiece.pb.h new file mode 100644 index 000000000..5eff1b333 --- /dev/null +++ b/src/builtin_upb/sentencepiece.pb.h @@ -0,0 +1,20 @@ +// Copyright 2016 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef UPB_COMPAT_HDRS_SENTENCEPIECE_PB_H_ +#define UPB_COMPAT_HDRS_SENTENCEPIECE_PB_H_ + +#include "upb_wrapper.h" + +#endif // UPB_COMPAT_HDRS_SENTENCEPIECE_PB_H_ diff --git a/src/builtin_upb/sentencepiece.upb.h b/src/builtin_upb/sentencepiece.upb.h new file mode 100644 index 000000000..79fe9a6b5 --- /dev/null +++ b/src/builtin_upb/sentencepiece.upb.h @@ -0,0 +1,457 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#ifndef SENTENCEPIECE_PROTO_UPB_H__UPB_H_ +#define SENTENCEPIECE_PROTO_UPB_H__UPB_H_ + +#include "upb.h" +#include "sentencepiece.upb_minitable.h" + +#ifdef __cplusplus +extern "C" { +#endif +typedef struct sentencepiece_SentencePieceText { + upb_Message UPB_PRIVATE(base); +} sentencepiece_SentencePieceText; + +typedef struct sentencepiece_SentencePieceText_SentencePiece { + upb_Message UPB_PRIVATE(base); +} sentencepiece_SentencePieceText_SentencePiece; + +typedef struct sentencepiece_NBestSentencePieceText { + upb_Message UPB_PRIVATE(base); +} sentencepiece_NBestSentencePieceText; + + + + +/* sentencepiece.SentencePieceText */ +UPB_INLINE sentencepiece_SentencePieceText* sentencepiece_SentencePieceText_new(upb_Arena* arena) { + return (sentencepiece_SentencePieceText*)_upb_Message_New(&sentencepiece__SentencePieceText_msg_init, arena); +} +UPB_INLINE sentencepiece_SentencePieceText* sentencepiece_SentencePieceText_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_SentencePieceText* ret = sentencepiece_SentencePieceText_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SentencePieceText_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_SentencePieceText* sentencepiece_SentencePieceText_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_SentencePieceText* ret = sentencepiece_SentencePieceText_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SentencePieceText_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_SentencePieceText_serialize(const sentencepiece_SentencePieceText* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SentencePieceText_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_SentencePieceText_serialize_ex(const sentencepiece_SentencePieceText* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SentencePieceText_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_SentencePieceText_clear_text(sentencepiece_SentencePieceText* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_SentencePieceText_text(const sentencepiece_SentencePieceText* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SentencePieceText_has_text(const sentencepiece_SentencePieceText* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_SentencePieceText_clear_pieces(sentencepiece_SentencePieceText* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_SentencePieceText_SentencePiece* const* sentencepiece_SentencePieceText_pieces(const sentencepiece_SentencePieceText* msg, + size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText__SentencePiece_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const sentencepiece_SentencePieceText_SentencePiece* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_SentencePieceText_pieces_upb_array( + const sentencepiece_SentencePieceText* msg, size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText__SentencePiece_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_SentencePieceText_pieces_mutable_upb_array( + sentencepiece_SentencePieceText* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText__SentencePiece_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void sentencepiece_SentencePieceText_clear_score(sentencepiece_SentencePieceText* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE float sentencepiece_SentencePieceText_score(const sentencepiece_SentencePieceText* msg) { + float default_val = 0; + float ret; + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SentencePieceText_has_score(const sentencepiece_SentencePieceText* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void sentencepiece_SentencePieceText_set_text(sentencepiece_SentencePieceText* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE sentencepiece_SentencePieceText_SentencePiece** sentencepiece_SentencePieceText_mutable_pieces(sentencepiece_SentencePieceText* msg, + size_t* size) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText__SentencePiece_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (sentencepiece_SentencePieceText_SentencePiece**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE sentencepiece_SentencePieceText_SentencePiece** sentencepiece_SentencePieceText_resize_pieces(sentencepiece_SentencePieceText* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText__SentencePiece_msg_init); + return (sentencepiece_SentencePieceText_SentencePiece**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct sentencepiece_SentencePieceText_SentencePiece* sentencepiece_SentencePieceText_add_pieces( + sentencepiece_SentencePieceText* msg, upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText__SentencePiece_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct sentencepiece_SentencePieceText_SentencePiece* sub = + (struct sentencepiece_SentencePieceText_SentencePiece*)_upb_Message_New(&sentencepiece__SentencePieceText__SentencePiece_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void sentencepiece_SentencePieceText_set_score(sentencepiece_SentencePieceText* msg, float value) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* sentencepiece.SentencePieceText.SentencePiece */ +UPB_INLINE sentencepiece_SentencePieceText_SentencePiece* sentencepiece_SentencePieceText_SentencePiece_new(upb_Arena* arena) { + return (sentencepiece_SentencePieceText_SentencePiece*)_upb_Message_New(&sentencepiece__SentencePieceText__SentencePiece_msg_init, arena); +} +UPB_INLINE sentencepiece_SentencePieceText_SentencePiece* sentencepiece_SentencePieceText_SentencePiece_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_SentencePieceText_SentencePiece* ret = sentencepiece_SentencePieceText_SentencePiece_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SentencePieceText__SentencePiece_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_SentencePieceText_SentencePiece* sentencepiece_SentencePieceText_SentencePiece_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_SentencePieceText_SentencePiece* ret = sentencepiece_SentencePieceText_SentencePiece_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SentencePieceText__SentencePiece_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_SentencePieceText_SentencePiece_serialize(const sentencepiece_SentencePieceText_SentencePiece* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SentencePieceText__SentencePiece_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_SentencePieceText_SentencePiece_serialize_ex(const sentencepiece_SentencePieceText_SentencePiece* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SentencePieceText__SentencePiece_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_clear_piece(sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {1, 24, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_SentencePieceText_SentencePiece_piece(const sentencepiece_SentencePieceText_SentencePiece* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, 24, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SentencePieceText_SentencePiece_has_piece(const sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {1, 24, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_clear_id(sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE uint32_t sentencepiece_SentencePieceText_SentencePiece_id(const sentencepiece_SentencePieceText_SentencePiece* msg) { + uint32_t default_val = (uint32_t)0u; + uint32_t ret; + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SentencePieceText_SentencePiece_has_id(const sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_clear_surface(sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 40), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_SentencePieceText_SentencePiece_surface(const sentencepiece_SentencePieceText_SentencePiece* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {3, UPB_SIZE(32, 40), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SentencePieceText_SentencePiece_has_surface(const sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 40), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_clear_begin(sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {4, 16, 67, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE uint32_t sentencepiece_SentencePieceText_SentencePiece_begin(const sentencepiece_SentencePieceText_SentencePiece* msg) { + uint32_t default_val = (uint32_t)0u; + uint32_t ret; + const upb_MiniTableField field = {4, 16, 67, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SentencePieceText_SentencePiece_has_begin(const sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {4, 16, 67, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_clear_end(sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {5, 20, 68, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE uint32_t sentencepiece_SentencePieceText_SentencePiece_end(const sentencepiece_SentencePieceText_SentencePiece* msg) { + uint32_t default_val = (uint32_t)0u; + uint32_t ret; + const upb_MiniTableField field = {5, 20, 68, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SentencePieceText_SentencePiece_has_end(const sentencepiece_SentencePieceText_SentencePiece* msg) { + const upb_MiniTableField field = {5, 20, 68, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_set_piece(sentencepiece_SentencePieceText_SentencePiece* msg, upb_StringView value) { + const upb_MiniTableField field = {1, 24, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_set_id(sentencepiece_SentencePieceText_SentencePiece* msg, uint32_t value) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_set_surface(sentencepiece_SentencePieceText_SentencePiece* msg, upb_StringView value) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 40), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_set_begin(sentencepiece_SentencePieceText_SentencePiece* msg, uint32_t value) { + const upb_MiniTableField field = {4, 16, 67, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_SentencePieceText_SentencePiece_set_end(sentencepiece_SentencePieceText_SentencePiece* msg, uint32_t value) { + const upb_MiniTableField field = {5, 20, 68, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* sentencepiece.NBestSentencePieceText */ +UPB_INLINE sentencepiece_NBestSentencePieceText* sentencepiece_NBestSentencePieceText_new(upb_Arena* arena) { + return (sentencepiece_NBestSentencePieceText*)_upb_Message_New(&sentencepiece__NBestSentencePieceText_msg_init, arena); +} +UPB_INLINE sentencepiece_NBestSentencePieceText* sentencepiece_NBestSentencePieceText_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_NBestSentencePieceText* ret = sentencepiece_NBestSentencePieceText_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__NBestSentencePieceText_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_NBestSentencePieceText* sentencepiece_NBestSentencePieceText_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_NBestSentencePieceText* ret = sentencepiece_NBestSentencePieceText_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__NBestSentencePieceText_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_NBestSentencePieceText_serialize(const sentencepiece_NBestSentencePieceText* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__NBestSentencePieceText_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_NBestSentencePieceText_serialize_ex(const sentencepiece_NBestSentencePieceText* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__NBestSentencePieceText_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_NBestSentencePieceText_clear_nbests(sentencepiece_NBestSentencePieceText* msg) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_SentencePieceText* const* sentencepiece_NBestSentencePieceText_nbests(const sentencepiece_NBestSentencePieceText* msg, + size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const sentencepiece_SentencePieceText* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_NBestSentencePieceText_nbests_upb_array( + const sentencepiece_NBestSentencePieceText* msg, size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_NBestSentencePieceText_nbests_mutable_upb_array( + sentencepiece_NBestSentencePieceText* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE sentencepiece_SentencePieceText** sentencepiece_NBestSentencePieceText_mutable_nbests(sentencepiece_NBestSentencePieceText* msg, + size_t* size) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (sentencepiece_SentencePieceText**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE sentencepiece_SentencePieceText** sentencepiece_NBestSentencePieceText_resize_nbests(sentencepiece_NBestSentencePieceText* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText_msg_init); + return (sentencepiece_SentencePieceText**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct sentencepiece_SentencePieceText* sentencepiece_NBestSentencePieceText_add_nbests( + sentencepiece_NBestSentencePieceText* msg, upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SentencePieceText_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct sentencepiece_SentencePieceText* sub = + (struct sentencepiece_SentencePieceText*)_upb_Message_New(&sentencepiece__SentencePieceText_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +#ifdef __cplusplus + } /* extern "C" */ +#endif + +#endif /* SENTENCEPIECE_PROTO_UPB_H__UPB_H_ */ diff --git a/src/builtin_upb/sentencepiece.upb_minitable.c b/src/builtin_upb/sentencepiece.upb_minitable.c new file mode 100644 index 000000000..f2ae43597 --- /dev/null +++ b/src/builtin_upb/sentencepiece.upb_minitable.c @@ -0,0 +1,106 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#include +#include "upb.h" +#include "sentencepiece.upb_minitable.h" + +extern const UPB_PRIVATE(upb_GeneratedExtensionListEntry)* UPB_PRIVATE(upb_generated_extension_list); +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[1]; +} sentencepiece__SentencePieceText_msg_init_Fields; + +static const sentencepiece__SentencePieceText_msg_init_Fields sentencepiece_SentencePieceText__fields = {{ + {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &sentencepiece__SentencePieceText__SentencePiece_msg_init}, +}}; + +const upb_MiniTable sentencepiece__SentencePieceText_msg_init = { + &sentencepiece_SentencePieceText__fields.fields[0], + UPB_SIZE(32, 40), 3, kUpb_ExtMode_Extendable, 3, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.SentencePieceText", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c00000100001d, &upb_DecodeFast_Fixed32_Scalar_Tag1Byte}, + }) +}; + +typedef struct { + upb_MiniTableField fields[5]; +} sentencepiece__SentencePieceText__SentencePiece_msg_init_Fields; + +static const sentencepiece__SentencePieceText__SentencePiece_msg_init_Fields sentencepiece_SentencePieceText_SentencePiece__fields = {{ + {1, 24, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, 12, 65, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(32, 40), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, 16, 67, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, 20, 68, kUpb_NoSub, 13, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable sentencepiece__SentencePieceText__SentencePiece_msg_init = { + &sentencepiece_SentencePieceText_SentencePiece__fields.fields[0], + UPB_SIZE(40, 56), 5, kUpb_ExtMode_Extendable, 5, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.SentencePieceText.SentencePiece", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001800000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x000c000001000010, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x002800000200001a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0010000003000020, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0014000004000028, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[1]; + upb_MiniTableSubInternal subs[1]; +} sentencepiece__NBestSentencePieceText_msg_init_Fields; + +static const sentencepiece__NBestSentencePieceText_msg_init_Fields sentencepiece_NBestSentencePieceText__fields = {{ + {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &sentencepiece__SentencePieceText_msg_init}, +}}; + +const upb_MiniTable sentencepiece__NBestSentencePieceText_msg_init = { + &sentencepiece_NBestSentencePieceText__fields.fields[0], + 16, 1, kUpb_ExtMode_NonExtendable, 1, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.NBestSentencePieceText", +#endif +}; + +static const upb_MiniTable *messages_layout[3] = { + &sentencepiece__SentencePieceText_msg_init, + &sentencepiece__SentencePieceText__SentencePiece_msg_init, + &sentencepiece__NBestSentencePieceText_msg_init, +}; + +const upb_MiniTableFile sentencepiece_proto_upb_file_layout = { + messages_layout, + NULL, + NULL, + 3, + 0, + 0, +}; + diff --git a/src/builtin_upb/sentencepiece.upb_minitable.h b/src/builtin_upb/sentencepiece.upb_minitable.h new file mode 100644 index 000000000..1e98c5a9f --- /dev/null +++ b/src/builtin_upb/sentencepiece.upb_minitable.h @@ -0,0 +1,28 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#ifndef SENTENCEPIECE_PROTO_UPB_H__UPB_MINITABLE_H_ +#define SENTENCEPIECE_PROTO_UPB_H__UPB_MINITABLE_H_ + +#include "upb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern const upb_MiniTable sentencepiece__SentencePieceText_msg_init; +extern const upb_MiniTable sentencepiece__SentencePieceText__SentencePiece_msg_init; +extern const upb_MiniTable sentencepiece__NBestSentencePieceText_msg_init; + +extern const upb_MiniTableFile sentencepiece_proto_upb_file_layout; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* SENTENCEPIECE_PROTO_UPB_H__UPB_MINITABLE_H_ */ diff --git a/src/builtin_upb/sentencepiece.upbdefs.c b/src/builtin_upb/sentencepiece.upbdefs.c new file mode 100644 index 000000000..2c7002f21 --- /dev/null +++ b/src/builtin_upb/sentencepiece.upbdefs.c @@ -0,0 +1,60 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + + +#include "upb/reflection/def.h" +#include "sentencepiece.upbdefs.h" +#include "sentencepiece.upb_minitable.h" + +static const char descriptor[404] = { + '\n', '\023', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', + 'e', 'c', 'e', '.', 'p', 'r', 'o', 't', 'o', '\022', '\r', 's', + 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', 'c', 'e', + '\"', '\225', '\002', '\n', '\021', 'S', 'e', 'n', 't', 'e', 'n', 'c', + 'e', 'P', 'i', 'e', 'c', 'e', 'T', 'e', 'x', 't', '\022', '\022', + '\n', '\004', 't', 'e', 'x', 't', '\030', '\001', ' ', '\001', '(', '\t', + 'R', '\004', 't', 'e', 'x', 't', '\022', 'F', '\n', '\006', 'p', 'i', + 'e', 'c', 'e', 's', '\030', '\002', ' ', '\003', '(', '\013', '2', '.', + '.', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', + 'c', 'e', '.', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'P', + 'i', 'e', 'c', 'e', 'T', 'e', 'x', 't', '.', 'S', 'e', 'n', + 't', 'e', 'n', 'c', 'e', 'P', 'i', 'e', 'c', 'e', 'R', '\006', + 'p', 'i', 'e', 'c', 'e', 's', '\022', '\024', '\n', '\005', 's', 'c', + 'o', 'r', 'e', '\030', '\003', ' ', '\001', '(', '\002', 'R', '\005', 's', + 'c', 'o', 'r', 'e', '\032', '\202', '\001', '\n', '\r', 'S', 'e', 'n', + 't', 'e', 'n', 'c', 'e', 'P', 'i', 'e', 'c', 'e', '\022', '\024', + '\n', '\005', 'p', 'i', 'e', 'c', 'e', '\030', '\001', ' ', '\001', '(', + '\t', 'R', '\005', 'p', 'i', 'e', 'c', 'e', '\022', '\016', '\n', '\002', + 'i', 'd', '\030', '\002', ' ', '\001', '(', '\r', 'R', '\002', 'i', 'd', + '\022', '\030', '\n', '\007', 's', 'u', 'r', 'f', 'a', 'c', 'e', '\030', + '\003', ' ', '\001', '(', '\t', 'R', '\007', 's', 'u', 'r', 'f', 'a', + 'c', 'e', '\022', '\024', '\n', '\005', 'b', 'e', 'g', 'i', 'n', '\030', + '\004', ' ', '\001', '(', '\r', 'R', '\005', 'b', 'e', 'g', 'i', 'n', + '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\005', ' ', '\001', '(', + '\r', 'R', '\003', 'e', 'n', 'd', '*', '\t', '\010', '\310', '\001', '\020', + '\200', '\200', '\200', '\200', '\002', '*', '\t', '\010', '\310', '\001', '\020', '\200', + '\200', '\200', '\200', '\002', '\"', 'R', '\n', '\026', 'N', 'B', 'e', 's', + 't', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'P', 'i', 'e', + 'c', 'e', 'T', 'e', 'x', 't', '\022', '8', '\n', '\006', 'n', 'b', + 'e', 's', 't', 's', '\030', '\001', ' ', '\003', '(', '\013', '2', ' ', + '.', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', + 'c', 'e', '.', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'P', + 'i', 'e', 'c', 'e', 'T', 'e', 'x', 't', 'R', '\006', 'n', 'b', + 'e', 's', 't', 's', 'B', '\002', 'H', '\003', +}; + +static _upb_DefPool_Init *deps[1] = { + NULL, +}; + +_upb_DefPool_Init sentencepiece_proto_upbdefinit = { + deps, + &sentencepiece_proto_upb_file_layout, + "sentencepiece.proto", + UPB_STRINGVIEW_INIT(descriptor, sizeof(descriptor)), +}; diff --git a/src/builtin_upb/sentencepiece.upbdefs.h b/src/builtin_upb/sentencepiece.upbdefs.h new file mode 100644 index 000000000..ff1ec519d --- /dev/null +++ b/src/builtin_upb/sentencepiece.upbdefs.h @@ -0,0 +1,47 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + + + +#ifndef SENTENCEPIECE_PROTO_UPB_H__UPBDEFS_H_ +#define SENTENCEPIECE_PROTO_UPB_H__UPBDEFS_H_ + +#include "upb/reflection/def.h" +#include "upb/reflection/internal/def_pool.h" + +#include "upb/port/def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +extern _upb_DefPool_Init sentencepiece_proto_upbdefinit; + +UPB_INLINE const upb_MessageDef *sentencepiece_SentencePieceText_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.SentencePieceText"); +} + +UPB_INLINE const upb_MessageDef *sentencepiece_SentencePieceText_SentencePiece_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.SentencePieceText.SentencePiece"); +} + +UPB_INLINE const upb_MessageDef *sentencepiece_NBestSentencePieceText_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.NBestSentencePieceText"); +} + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port/undef.inc" + +#endif /* SENTENCEPIECE_PROTO_UPB_H__UPBDEFS_H_ */ diff --git a/src/builtin_upb/sentencepiece_model.pb.h b/src/builtin_upb/sentencepiece_model.pb.h new file mode 100644 index 000000000..fb45c12a3 --- /dev/null +++ b/src/builtin_upb/sentencepiece_model.pb.h @@ -0,0 +1,20 @@ +// Copyright 2016 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef UPB_COMPAT_HDRS_SENTENCEPIECE_MODEL_PB_H_ +#define UPB_COMPAT_HDRS_SENTENCEPIECE_MODEL_PB_H_ + +#include "upb_wrapper.h" + +#endif // UPB_COMPAT_HDRS_SENTENCEPIECE_MODEL_PB_H_ diff --git a/src/builtin_upb/sentencepiece_model.upb.h b/src/builtin_upb/sentencepiece_model.upb.h new file mode 100644 index 000000000..f81d43ac8 --- /dev/null +++ b/src/builtin_upb/sentencepiece_model.upb.h @@ -0,0 +1,1933 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece_model.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#ifndef SENTENCEPIECE_MODEL_PROTO_UPB_H__UPB_H_ +#define SENTENCEPIECE_MODEL_PROTO_UPB_H__UPB_H_ + +#include "upb.h" +#include "sentencepiece_model.upb_minitable.h" + +#ifdef __cplusplus +extern "C" { +#endif +typedef struct sentencepiece_TrainerSpec { + upb_Message UPB_PRIVATE(base); +} sentencepiece_TrainerSpec; + +typedef struct sentencepiece_NormalizerSpec { + upb_Message UPB_PRIVATE(base); +} sentencepiece_NormalizerSpec; + +typedef struct sentencepiece_SelfTestData { + upb_Message UPB_PRIVATE(base); +} sentencepiece_SelfTestData; + +typedef struct sentencepiece_SelfTestData_Sample { + upb_Message UPB_PRIVATE(base); +} sentencepiece_SelfTestData_Sample; + +typedef struct sentencepiece_ModelProto { + upb_Message UPB_PRIVATE(base); +} sentencepiece_ModelProto; + +typedef struct sentencepiece_ModelProto_SentencePiece { + upb_Message UPB_PRIVATE(base); +} sentencepiece_ModelProto_SentencePiece; + + +typedef enum { + sentencepiece_ModelProto_SentencePiece_NORMAL = 1, + sentencepiece_ModelProto_SentencePiece_UNKNOWN = 2, + sentencepiece_ModelProto_SentencePiece_CONTROL = 3, + sentencepiece_ModelProto_SentencePiece_USER_DEFINED = 4, + sentencepiece_ModelProto_SentencePiece_UNUSED = 5, + sentencepiece_ModelProto_SentencePiece_BYTE = 6 +} sentencepiece_ModelProto_SentencePiece_Type; + +typedef enum { + sentencepiece_TrainerSpec_UNIGRAM = 1, + sentencepiece_TrainerSpec_BPE = 2, + sentencepiece_TrainerSpec_WORD = 3, + sentencepiece_TrainerSpec_CHAR = 4 +} sentencepiece_TrainerSpec_ModelType; + + + +/* sentencepiece.TrainerSpec */ +UPB_INLINE sentencepiece_TrainerSpec* sentencepiece_TrainerSpec_new(upb_Arena* arena) { + return (sentencepiece_TrainerSpec*)_upb_Message_New(&sentencepiece__TrainerSpec_msg_init, arena); +} +UPB_INLINE sentencepiece_TrainerSpec* sentencepiece_TrainerSpec_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_TrainerSpec* ret = sentencepiece_TrainerSpec_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__TrainerSpec_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_TrainerSpec* sentencepiece_TrainerSpec_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_TrainerSpec* ret = sentencepiece_TrainerSpec_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__TrainerSpec_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_TrainerSpec_serialize(const sentencepiece_TrainerSpec* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__TrainerSpec_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_TrainerSpec_serialize_ex(const sentencepiece_TrainerSpec* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__TrainerSpec_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_input(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* sentencepiece_TrainerSpec_input(const sentencepiece_TrainerSpec* msg, + size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_TrainerSpec_input_upb_array( + const sentencepiece_TrainerSpec* msg, size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_TrainerSpec_input_mutable_upb_array( + sentencepiece_TrainerSpec* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_model_prefix(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(112, 96), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_model_prefix(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, UPB_SIZE(112, 96), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_model_prefix(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(112, 96), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_model_type(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 28), 65, 132, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_model_type(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = 1; + int32_t ret; + const upb_MiniTableField field = {3, UPB_SIZE(32, 28), 65, 132, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_model_type(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 28), 65, 132, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_vocab_size(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(36, 32), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_vocab_size(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)8000; + int32_t ret; + const upb_MiniTableField field = {4, UPB_SIZE(36, 32), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_vocab_size(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(36, 32), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_accept_language(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* sentencepiece_TrainerSpec_accept_language(const sentencepiece_TrainerSpec* msg, + size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_TrainerSpec_accept_language_upb_array( + const sentencepiece_TrainerSpec* msg, size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_TrainerSpec_accept_language_mutable_upb_array( + sentencepiece_TrainerSpec* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_self_test_sample_size(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(44, 36), 67, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_self_test_sample_size(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {6, UPB_SIZE(44, 36), 67, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_self_test_sample_size(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(44, 36), 67, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_input_format(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(120, 112), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_input_format(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {7, UPB_SIZE(120, 112), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_input_format(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(120, 112), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_character_coverage(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {10, UPB_SIZE(48, 40), 69, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE float sentencepiece_TrainerSpec_character_coverage(const sentencepiece_TrainerSpec* msg) { + float default_val = 0.9995; + float ret; + const upb_MiniTableField field = {10, UPB_SIZE(48, 40), 69, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_character_coverage(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {10, UPB_SIZE(48, 40), 69, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_input_sentence_size(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {11, UPB_SIZE(192, 272), 70, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE uint64_t sentencepiece_TrainerSpec_input_sentence_size(const sentencepiece_TrainerSpec* msg) { + uint64_t default_val = (uint64_t)0ull; + uint64_t ret; + const upb_MiniTableField field = {11, UPB_SIZE(192, 272), 70, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_input_sentence_size(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {11, UPB_SIZE(192, 272), 70, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_mining_sentence_size(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {12, UPB_SIZE(52, 44), 71, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_mining_sentence_size(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {12, UPB_SIZE(52, 44), 71, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_mining_sentence_size(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {12, UPB_SIZE(52, 44), 71, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_training_sentence_size(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {13, UPB_SIZE(56, 48), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_training_sentence_size(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {13, UPB_SIZE(56, 48), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_training_sentence_size(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {13, UPB_SIZE(56, 48), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_seed_sentencepiece_size(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {14, UPB_SIZE(60, 52), 73, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_seed_sentencepiece_size(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)1000000; + int32_t ret; + const upb_MiniTableField field = {14, UPB_SIZE(60, 52), 73, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_seed_sentencepiece_size(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {14, UPB_SIZE(60, 52), 73, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_shrinking_factor(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {15, UPB_SIZE(64, 56), 74, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE float sentencepiece_TrainerSpec_shrinking_factor(const sentencepiece_TrainerSpec* msg) { + float default_val = 0.75; + float ret; + const upb_MiniTableField field = {15, UPB_SIZE(64, 56), 74, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_shrinking_factor(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {15, UPB_SIZE(64, 56), 74, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_num_threads(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {16, UPB_SIZE(68, 60), 75, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_num_threads(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)16; + int32_t ret; + const upb_MiniTableField field = {16, UPB_SIZE(68, 60), 75, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_num_threads(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {16, UPB_SIZE(68, 60), 75, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_num_sub_iterations(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {17, UPB_SIZE(72, 64), 76, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_num_sub_iterations(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)2; + int32_t ret; + const upb_MiniTableField field = {17, UPB_SIZE(72, 64), 76, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_num_sub_iterations(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {17, UPB_SIZE(72, 64), 76, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_max_sentence_length(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {18, UPB_SIZE(76, 68), 77, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_max_sentence_length(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)4192; + int32_t ret; + const upb_MiniTableField field = {18, UPB_SIZE(76, 68), 77, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_max_sentence_length(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {18, UPB_SIZE(76, 68), 77, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_shuffle_input_sentence(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {19, 14, 78, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_shuffle_input_sentence(const sentencepiece_TrainerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {19, 14, 78, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_shuffle_input_sentence(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {19, 14, 78, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_max_sentencepiece_length(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {20, UPB_SIZE(80, 72), 79, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_max_sentencepiece_length(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)16; + int32_t ret; + const upb_MiniTableField field = {20, UPB_SIZE(80, 72), 79, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_max_sentencepiece_length(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {20, UPB_SIZE(80, 72), 79, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_split_by_unicode_script(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {21, 15, 80, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_split_by_unicode_script(const sentencepiece_TrainerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {21, 15, 80, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_split_by_unicode_script(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {21, 15, 80, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_split_by_whitespace(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {22, 16, 81, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_split_by_whitespace(const sentencepiece_TrainerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {22, 16, 81, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_split_by_whitespace(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {22, 16, 81, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_split_by_number(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {23, 17, 82, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_split_by_number(const sentencepiece_TrainerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {23, 17, 82, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_split_by_number(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {23, 17, 82, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_treat_whitespace_as_suffix(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {24, 18, 83, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_treat_whitespace_as_suffix(const sentencepiece_TrainerSpec* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {24, 18, 83, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_treat_whitespace_as_suffix(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {24, 18, 83, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_split_digits(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {25, 19, 84, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_split_digits(const sentencepiece_TrainerSpec* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {25, 19, 84, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_split_digits(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {25, 19, 84, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_allow_whitespace_only_pieces(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {26, 20, 85, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_allow_whitespace_only_pieces(const sentencepiece_TrainerSpec* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {26, 20, 85, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_allow_whitespace_only_pieces(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {26, 20, 85, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_control_symbols(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* sentencepiece_TrainerSpec_control_symbols(const sentencepiece_TrainerSpec* msg, + size_t* size) { + const upb_MiniTableField field = {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_TrainerSpec_control_symbols_upb_array( + const sentencepiece_TrainerSpec* msg, size_t* size) { + const upb_MiniTableField field = {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_TrainerSpec_control_symbols_mutable_upb_array( + sentencepiece_TrainerSpec* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_user_defined_symbols(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* sentencepiece_TrainerSpec_user_defined_symbols(const sentencepiece_TrainerSpec* msg, + size_t* size) { + const upb_MiniTableField field = {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_TrainerSpec_user_defined_symbols_upb_array( + const sentencepiece_TrainerSpec* msg, size_t* size) { + const upb_MiniTableField field = {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_TrainerSpec_user_defined_symbols_mutable_upb_array( + sentencepiece_TrainerSpec* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_vocabulary_output_piece_score(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {32, 21, 86, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_vocabulary_output_piece_score(const sentencepiece_TrainerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {32, 21, 86, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_vocabulary_output_piece_score(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {32, 21, 86, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_hard_vocab_limit(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {33, 22, 87, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_hard_vocab_limit(const sentencepiece_TrainerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {33, 22, 87, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_hard_vocab_limit(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {33, 22, 87, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_use_all_vocab(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {34, 23, 88, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_use_all_vocab(const sentencepiece_TrainerSpec* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {34, 23, 88, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_use_all_vocab(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {34, 23, 88, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_byte_fallback(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {35, 24, 89, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_byte_fallback(const sentencepiece_TrainerSpec* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {35, 24, 89, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_byte_fallback(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {35, 24, 89, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_required_chars(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {36, 128, 90, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_required_chars(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {36, 128, 90, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_required_chars(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {36, 128, 90, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_unk_id(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {40, UPB_SIZE(92, 76), 91, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_unk_id(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {40, UPB_SIZE(92, 76), 91, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_unk_id(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {40, UPB_SIZE(92, 76), 91, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_bos_id(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {41, UPB_SIZE(96, 80), 92, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_bos_id(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)1; + int32_t ret; + const upb_MiniTableField field = {41, UPB_SIZE(96, 80), 92, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_bos_id(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {41, UPB_SIZE(96, 80), 92, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_eos_id(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {42, UPB_SIZE(100, 84), 93, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_eos_id(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)2; + int32_t ret; + const upb_MiniTableField field = {42, UPB_SIZE(100, 84), 93, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_eos_id(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {42, UPB_SIZE(100, 84), 93, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_pad_id(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {43, UPB_SIZE(104, 88), 94, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_TrainerSpec_pad_id(const sentencepiece_TrainerSpec* msg) { + int32_t default_val = (int32_t)-1; + int32_t ret; + const upb_MiniTableField field = {43, UPB_SIZE(104, 88), 94, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_pad_id(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {43, UPB_SIZE(104, 88), 94, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_unk_surface(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {44, UPB_SIZE(136, 144), 95, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_unk_surface(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(" \342\201\207 "); + upb_StringView ret; + const upb_MiniTableField field = {44, UPB_SIZE(136, 144), 95, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_unk_surface(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {44, UPB_SIZE(136, 144), 95, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_unk_piece(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {45, UPB_SIZE(144, 160), 96, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_unk_piece(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {45, UPB_SIZE(144, 160), 96, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_unk_piece(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {45, UPB_SIZE(144, 160), 96, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_bos_piece(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {46, UPB_SIZE(152, 176), 97, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_bos_piece(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {46, UPB_SIZE(152, 176), 97, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_bos_piece(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {46, UPB_SIZE(152, 176), 97, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_eos_piece(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {47, UPB_SIZE(160, 192), 98, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_eos_piece(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {47, UPB_SIZE(160, 192), 98, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_eos_piece(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {47, UPB_SIZE(160, 192), 98, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_pad_piece(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {48, UPB_SIZE(168, 208), 99, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_pad_piece(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {48, UPB_SIZE(168, 208), 99, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_pad_piece(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {48, UPB_SIZE(168, 208), 99, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_train_extremely_large_corpus(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {49, 25, 100, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_train_extremely_large_corpus(const sentencepiece_TrainerSpec* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {49, 25, 100, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_train_extremely_large_corpus(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {49, 25, 100, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_enable_differential_privacy(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {50, 26, 101, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_TrainerSpec_enable_differential_privacy(const sentencepiece_TrainerSpec* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {50, 26, 101, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_enable_differential_privacy(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {50, 26, 101, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_differential_privacy_noise_level(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {51, UPB_SIZE(108, 92), 102, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE float sentencepiece_TrainerSpec_differential_privacy_noise_level(const sentencepiece_TrainerSpec* msg) { + float default_val = 0; + float ret; + const upb_MiniTableField field = {51, UPB_SIZE(108, 92), 102, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_differential_privacy_noise_level(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {51, UPB_SIZE(108, 92), 102, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_differential_privacy_clipping_threshold(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {52, UPB_SIZE(200, 296), 103, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE uint64_t sentencepiece_TrainerSpec_differential_privacy_clipping_threshold(const sentencepiece_TrainerSpec* msg) { + uint64_t default_val = (uint64_t)0ull; + uint64_t ret; + const upb_MiniTableField field = {52, UPB_SIZE(200, 296), 103, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_differential_privacy_clipping_threshold(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {52, UPB_SIZE(200, 296), 103, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_pretokenization_delimiter(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {53, UPB_SIZE(176, 224), 104, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_pretokenization_delimiter(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {53, UPB_SIZE(176, 224), 104, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_pretokenization_delimiter(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {53, UPB_SIZE(176, 224), 104, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_TrainerSpec_clear_seed_sentencepieces_file(sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {54, UPB_SIZE(184, 240), 105, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_TrainerSpec_seed_sentencepieces_file(const sentencepiece_TrainerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {54, UPB_SIZE(184, 240), 105, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_TrainerSpec_has_seed_sentencepieces_file(const sentencepiece_TrainerSpec* msg) { + const upb_MiniTableField field = {54, UPB_SIZE(184, 240), 105, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_mutable_input(sentencepiece_TrainerSpec* msg, + size_t* size) { + upb_MiniTableField field = {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_resize_input(sentencepiece_TrainerSpec* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool sentencepiece_TrainerSpec_add_input(sentencepiece_TrainerSpec* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void sentencepiece_TrainerSpec_set_model_prefix(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {2, UPB_SIZE(112, 96), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_model_type(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 28), 65, 132, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_vocab_size(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {4, UPB_SIZE(36, 32), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_mutable_accept_language(sentencepiece_TrainerSpec* msg, + size_t* size) { + upb_MiniTableField field = {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_resize_accept_language(sentencepiece_TrainerSpec* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool sentencepiece_TrainerSpec_add_accept_language(sentencepiece_TrainerSpec* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void sentencepiece_TrainerSpec_set_self_test_sample_size(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {6, UPB_SIZE(44, 36), 67, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_input_format(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {7, UPB_SIZE(120, 112), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_character_coverage(sentencepiece_TrainerSpec* msg, float value) { + const upb_MiniTableField field = {10, UPB_SIZE(48, 40), 69, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_input_sentence_size(sentencepiece_TrainerSpec* msg, uint64_t value) { + const upb_MiniTableField field = {11, UPB_SIZE(192, 272), 70, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_mining_sentence_size(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {12, UPB_SIZE(52, 44), 71, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_training_sentence_size(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {13, UPB_SIZE(56, 48), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_seed_sentencepiece_size(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {14, UPB_SIZE(60, 52), 73, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_shrinking_factor(sentencepiece_TrainerSpec* msg, float value) { + const upb_MiniTableField field = {15, UPB_SIZE(64, 56), 74, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_num_threads(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {16, UPB_SIZE(68, 60), 75, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_num_sub_iterations(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {17, UPB_SIZE(72, 64), 76, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_max_sentence_length(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {18, UPB_SIZE(76, 68), 77, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_shuffle_input_sentence(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {19, 14, 78, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_max_sentencepiece_length(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {20, UPB_SIZE(80, 72), 79, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_split_by_unicode_script(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {21, 15, 80, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_split_by_whitespace(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {22, 16, 81, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_split_by_number(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {23, 17, 82, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_treat_whitespace_as_suffix(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {24, 18, 83, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_split_digits(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {25, 19, 84, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_allow_whitespace_only_pieces(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {26, 20, 85, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_mutable_control_symbols(sentencepiece_TrainerSpec* msg, + size_t* size) { + upb_MiniTableField field = {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_resize_control_symbols(sentencepiece_TrainerSpec* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool sentencepiece_TrainerSpec_add_control_symbols(sentencepiece_TrainerSpec* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_mutable_user_defined_symbols(sentencepiece_TrainerSpec* msg, + size_t* size) { + upb_MiniTableField field = {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* sentencepiece_TrainerSpec_resize_user_defined_symbols(sentencepiece_TrainerSpec* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool sentencepiece_TrainerSpec_add_user_defined_symbols(sentencepiece_TrainerSpec* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void sentencepiece_TrainerSpec_set_vocabulary_output_piece_score(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {32, 21, 86, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_hard_vocab_limit(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {33, 22, 87, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_use_all_vocab(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {34, 23, 88, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_byte_fallback(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {35, 24, 89, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_required_chars(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {36, 128, 90, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_unk_id(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {40, UPB_SIZE(92, 76), 91, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_bos_id(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {41, UPB_SIZE(96, 80), 92, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_eos_id(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {42, UPB_SIZE(100, 84), 93, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_pad_id(sentencepiece_TrainerSpec* msg, int32_t value) { + const upb_MiniTableField field = {43, UPB_SIZE(104, 88), 94, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_unk_surface(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {44, UPB_SIZE(136, 144), 95, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_unk_piece(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {45, UPB_SIZE(144, 160), 96, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_bos_piece(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {46, UPB_SIZE(152, 176), 97, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_eos_piece(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {47, UPB_SIZE(160, 192), 98, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_pad_piece(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {48, UPB_SIZE(168, 208), 99, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_train_extremely_large_corpus(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {49, 25, 100, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_enable_differential_privacy(sentencepiece_TrainerSpec* msg, bool value) { + const upb_MiniTableField field = {50, 26, 101, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_differential_privacy_noise_level(sentencepiece_TrainerSpec* msg, float value) { + const upb_MiniTableField field = {51, UPB_SIZE(108, 92), 102, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_differential_privacy_clipping_threshold(sentencepiece_TrainerSpec* msg, uint64_t value) { + const upb_MiniTableField field = {52, UPB_SIZE(200, 296), 103, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_pretokenization_delimiter(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {53, UPB_SIZE(176, 224), 104, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_TrainerSpec_set_seed_sentencepieces_file(sentencepiece_TrainerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {54, UPB_SIZE(184, 240), 105, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* sentencepiece.NormalizerSpec */ +UPB_INLINE sentencepiece_NormalizerSpec* sentencepiece_NormalizerSpec_new(upb_Arena* arena) { + return (sentencepiece_NormalizerSpec*)_upb_Message_New(&sentencepiece__NormalizerSpec_msg_init, arena); +} +UPB_INLINE sentencepiece_NormalizerSpec* sentencepiece_NormalizerSpec_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_NormalizerSpec* ret = sentencepiece_NormalizerSpec_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__NormalizerSpec_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_NormalizerSpec* sentencepiece_NormalizerSpec_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_NormalizerSpec* ret = sentencepiece_NormalizerSpec_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__NormalizerSpec_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_NormalizerSpec_serialize(const sentencepiece_NormalizerSpec* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__NormalizerSpec_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_NormalizerSpec_serialize_ex(const sentencepiece_NormalizerSpec* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__NormalizerSpec_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_NormalizerSpec_clear_name(sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_NormalizerSpec_name(const sentencepiece_NormalizerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_NormalizerSpec_has_name(const sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_NormalizerSpec_clear_precompiled_charsmap(sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_NormalizerSpec_precompiled_charsmap(const sentencepiece_NormalizerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_NormalizerSpec_has_precompiled_charsmap(const sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_NormalizerSpec_clear_add_dummy_prefix(sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {3, 9, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_NormalizerSpec_add_dummy_prefix(const sentencepiece_NormalizerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {3, 9, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_NormalizerSpec_has_add_dummy_prefix(const sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {3, 9, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_NormalizerSpec_clear_remove_extra_whitespaces(sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {4, 10, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_NormalizerSpec_remove_extra_whitespaces(const sentencepiece_NormalizerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {4, 10, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_NormalizerSpec_has_remove_extra_whitespaces(const sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {4, 10, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_NormalizerSpec_clear_escape_whitespaces(sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {5, 11, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool sentencepiece_NormalizerSpec_escape_whitespaces(const sentencepiece_NormalizerSpec* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {5, 11, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_NormalizerSpec_has_escape_whitespaces(const sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {5, 11, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_NormalizerSpec_clear_normalization_rule_tsv(sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_NormalizerSpec_normalization_rule_tsv(const sentencepiece_NormalizerSpec* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {6, UPB_SIZE(28, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_NormalizerSpec_has_normalization_rule_tsv(const sentencepiece_NormalizerSpec* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void sentencepiece_NormalizerSpec_set_name(sentencepiece_NormalizerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_NormalizerSpec_set_precompiled_charsmap(sentencepiece_NormalizerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_NormalizerSpec_set_add_dummy_prefix(sentencepiece_NormalizerSpec* msg, bool value) { + const upb_MiniTableField field = {3, 9, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_NormalizerSpec_set_remove_extra_whitespaces(sentencepiece_NormalizerSpec* msg, bool value) { + const upb_MiniTableField field = {4, 10, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_NormalizerSpec_set_escape_whitespaces(sentencepiece_NormalizerSpec* msg, bool value) { + const upb_MiniTableField field = {5, 11, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_NormalizerSpec_set_normalization_rule_tsv(sentencepiece_NormalizerSpec* msg, upb_StringView value) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* sentencepiece.SelfTestData */ +UPB_INLINE sentencepiece_SelfTestData* sentencepiece_SelfTestData_new(upb_Arena* arena) { + return (sentencepiece_SelfTestData*)_upb_Message_New(&sentencepiece__SelfTestData_msg_init, arena); +} +UPB_INLINE sentencepiece_SelfTestData* sentencepiece_SelfTestData_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_SelfTestData* ret = sentencepiece_SelfTestData_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SelfTestData_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_SelfTestData* sentencepiece_SelfTestData_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_SelfTestData* ret = sentencepiece_SelfTestData_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SelfTestData_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_SelfTestData_serialize(const sentencepiece_SelfTestData* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SelfTestData_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_SelfTestData_serialize_ex(const sentencepiece_SelfTestData* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SelfTestData_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_SelfTestData_clear_samples(sentencepiece_SelfTestData* msg) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_SelfTestData_Sample* const* sentencepiece_SelfTestData_samples(const sentencepiece_SelfTestData* msg, + size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData__Sample_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const sentencepiece_SelfTestData_Sample* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_SelfTestData_samples_upb_array( + const sentencepiece_SelfTestData* msg, size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData__Sample_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_SelfTestData_samples_mutable_upb_array( + sentencepiece_SelfTestData* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData__Sample_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE sentencepiece_SelfTestData_Sample** sentencepiece_SelfTestData_mutable_samples(sentencepiece_SelfTestData* msg, + size_t* size) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData__Sample_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (sentencepiece_SelfTestData_Sample**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE sentencepiece_SelfTestData_Sample** sentencepiece_SelfTestData_resize_samples(sentencepiece_SelfTestData* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData__Sample_msg_init); + return (sentencepiece_SelfTestData_Sample**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct sentencepiece_SelfTestData_Sample* sentencepiece_SelfTestData_add_samples( + sentencepiece_SelfTestData* msg, upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData__Sample_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct sentencepiece_SelfTestData_Sample* sub = + (struct sentencepiece_SelfTestData_Sample*)_upb_Message_New(&sentencepiece__SelfTestData__Sample_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* sentencepiece.SelfTestData.Sample */ +UPB_INLINE sentencepiece_SelfTestData_Sample* sentencepiece_SelfTestData_Sample_new(upb_Arena* arena) { + return (sentencepiece_SelfTestData_Sample*)_upb_Message_New(&sentencepiece__SelfTestData__Sample_msg_init, arena); +} +UPB_INLINE sentencepiece_SelfTestData_Sample* sentencepiece_SelfTestData_Sample_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_SelfTestData_Sample* ret = sentencepiece_SelfTestData_Sample_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SelfTestData__Sample_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_SelfTestData_Sample* sentencepiece_SelfTestData_Sample_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_SelfTestData_Sample* ret = sentencepiece_SelfTestData_Sample_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__SelfTestData__Sample_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_SelfTestData_Sample_serialize(const sentencepiece_SelfTestData_Sample* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SelfTestData__Sample_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_SelfTestData_Sample_serialize_ex(const sentencepiece_SelfTestData_Sample* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__SelfTestData__Sample_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_SelfTestData_Sample_clear_input(sentencepiece_SelfTestData_Sample* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_SelfTestData_Sample_input(const sentencepiece_SelfTestData_Sample* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SelfTestData_Sample_has_input(const sentencepiece_SelfTestData_Sample* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_SelfTestData_Sample_clear_expected(sentencepiece_SelfTestData_Sample* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_SelfTestData_Sample_expected(const sentencepiece_SelfTestData_Sample* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_SelfTestData_Sample_has_expected(const sentencepiece_SelfTestData_Sample* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void sentencepiece_SelfTestData_Sample_set_input(sentencepiece_SelfTestData_Sample* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_SelfTestData_Sample_set_expected(sentencepiece_SelfTestData_Sample* msg, upb_StringView value) { + const upb_MiniTableField field = {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* sentencepiece.ModelProto */ +UPB_INLINE sentencepiece_ModelProto* sentencepiece_ModelProto_new(upb_Arena* arena) { + return (sentencepiece_ModelProto*)_upb_Message_New(&sentencepiece__ModelProto_msg_init, arena); +} +UPB_INLINE sentencepiece_ModelProto* sentencepiece_ModelProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_ModelProto* ret = sentencepiece_ModelProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__ModelProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_ModelProto* sentencepiece_ModelProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_ModelProto* ret = sentencepiece_ModelProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__ModelProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_ModelProto_serialize(const sentencepiece_ModelProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__ModelProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_ModelProto_serialize_ex(const sentencepiece_ModelProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__ModelProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_ModelProto_clear_pieces(sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_ModelProto_SentencePiece* const* sentencepiece_ModelProto_pieces(const sentencepiece_ModelProto* msg, + size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__ModelProto__SentencePiece_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const sentencepiece_ModelProto_SentencePiece* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _sentencepiece_ModelProto_pieces_upb_array( + const sentencepiece_ModelProto* msg, size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__ModelProto__SentencePiece_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _sentencepiece_ModelProto_pieces_mutable_upb_array( + sentencepiece_ModelProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__ModelProto__SentencePiece_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void sentencepiece_ModelProto_clear_trainer_spec(sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(16, 24), 64, UPB_SIZE(13, 15), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_TrainerSpec* sentencepiece_ModelProto_trainer_spec(const sentencepiece_ModelProto* msg) { + const sentencepiece_TrainerSpec* default_val = NULL; + const sentencepiece_TrainerSpec* ret; + const upb_MiniTableField field = {2, UPB_SIZE(16, 24), 64, UPB_SIZE(13, 15), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__TrainerSpec_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_ModelProto_has_trainer_spec(const sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(16, 24), 64, UPB_SIZE(13, 15), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_ModelProto_clear_normalizer_spec(sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(20, 32), 65, UPB_SIZE(11, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_NormalizerSpec* sentencepiece_ModelProto_normalizer_spec(const sentencepiece_ModelProto* msg) { + const sentencepiece_NormalizerSpec* default_val = NULL; + const sentencepiece_NormalizerSpec* ret; + const upb_MiniTableField field = {3, UPB_SIZE(20, 32), 65, UPB_SIZE(11, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__NormalizerSpec_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_ModelProto_has_normalizer_spec(const sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(20, 32), 65, UPB_SIZE(11, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_ModelProto_clear_self_test_data(sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(24, 40), 66, UPB_SIZE(9, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_SelfTestData* sentencepiece_ModelProto_self_test_data(const sentencepiece_ModelProto* msg) { + const sentencepiece_SelfTestData* default_val = NULL; + const sentencepiece_SelfTestData* ret; + const upb_MiniTableField field = {4, UPB_SIZE(24, 40), 66, UPB_SIZE(9, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_ModelProto_has_self_test_data(const sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(24, 40), 66, UPB_SIZE(9, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_ModelProto_clear_denormalizer_spec(sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(28, 48), 67, UPB_SIZE(7, 12), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const sentencepiece_NormalizerSpec* sentencepiece_ModelProto_denormalizer_spec(const sentencepiece_ModelProto* msg) { + const sentencepiece_NormalizerSpec* default_val = NULL; + const sentencepiece_NormalizerSpec* ret; + const upb_MiniTableField field = {5, UPB_SIZE(28, 48), 67, UPB_SIZE(7, 12), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__NormalizerSpec_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_ModelProto_has_denormalizer_spec(const sentencepiece_ModelProto* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(28, 48), 67, UPB_SIZE(7, 12), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE sentencepiece_ModelProto_SentencePiece** sentencepiece_ModelProto_mutable_pieces(sentencepiece_ModelProto* msg, + size_t* size) { + upb_MiniTableField field = {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__ModelProto__SentencePiece_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (sentencepiece_ModelProto_SentencePiece**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE sentencepiece_ModelProto_SentencePiece** sentencepiece_ModelProto_resize_pieces(sentencepiece_ModelProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__ModelProto__SentencePiece_msg_init); + return (sentencepiece_ModelProto_SentencePiece**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct sentencepiece_ModelProto_SentencePiece* sentencepiece_ModelProto_add_pieces( + sentencepiece_ModelProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__ModelProto__SentencePiece_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct sentencepiece_ModelProto_SentencePiece* sub = + (struct sentencepiece_ModelProto_SentencePiece*)_upb_Message_New(&sentencepiece__ModelProto__SentencePiece_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void sentencepiece_ModelProto_set_trainer_spec(sentencepiece_ModelProto* msg, sentencepiece_TrainerSpec* value) { + const upb_MiniTableField field = {2, UPB_SIZE(16, 24), 64, UPB_SIZE(13, 15), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__TrainerSpec_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct sentencepiece_TrainerSpec* sentencepiece_ModelProto_mutable_trainer_spec( + sentencepiece_ModelProto* msg, upb_Arena* arena) { + struct sentencepiece_TrainerSpec* sub = (struct sentencepiece_TrainerSpec*)sentencepiece_ModelProto_trainer_spec(msg); + if (sub == NULL) { + sub = (struct sentencepiece_TrainerSpec*)_upb_Message_New(&sentencepiece__TrainerSpec_msg_init, arena); + if (sub) sentencepiece_ModelProto_set_trainer_spec(msg, sub); + } + return sub; +} +UPB_INLINE void sentencepiece_ModelProto_set_normalizer_spec(sentencepiece_ModelProto* msg, sentencepiece_NormalizerSpec* value) { + const upb_MiniTableField field = {3, UPB_SIZE(20, 32), 65, UPB_SIZE(11, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__NormalizerSpec_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct sentencepiece_NormalizerSpec* sentencepiece_ModelProto_mutable_normalizer_spec( + sentencepiece_ModelProto* msg, upb_Arena* arena) { + struct sentencepiece_NormalizerSpec* sub = (struct sentencepiece_NormalizerSpec*)sentencepiece_ModelProto_normalizer_spec(msg); + if (sub == NULL) { + sub = (struct sentencepiece_NormalizerSpec*)_upb_Message_New(&sentencepiece__NormalizerSpec_msg_init, arena); + if (sub) sentencepiece_ModelProto_set_normalizer_spec(msg, sub); + } + return sub; +} +UPB_INLINE void sentencepiece_ModelProto_set_self_test_data(sentencepiece_ModelProto* msg, sentencepiece_SelfTestData* value) { + const upb_MiniTableField field = {4, UPB_SIZE(24, 40), 66, UPB_SIZE(9, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__SelfTestData_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct sentencepiece_SelfTestData* sentencepiece_ModelProto_mutable_self_test_data( + sentencepiece_ModelProto* msg, upb_Arena* arena) { + struct sentencepiece_SelfTestData* sub = (struct sentencepiece_SelfTestData*)sentencepiece_ModelProto_self_test_data(msg); + if (sub == NULL) { + sub = (struct sentencepiece_SelfTestData*)_upb_Message_New(&sentencepiece__SelfTestData_msg_init, arena); + if (sub) sentencepiece_ModelProto_set_self_test_data(msg, sub); + } + return sub; +} +UPB_INLINE void sentencepiece_ModelProto_set_denormalizer_spec(sentencepiece_ModelProto* msg, sentencepiece_NormalizerSpec* value) { + const upb_MiniTableField field = {5, UPB_SIZE(28, 48), 67, UPB_SIZE(7, 12), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&sentencepiece__NormalizerSpec_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct sentencepiece_NormalizerSpec* sentencepiece_ModelProto_mutable_denormalizer_spec( + sentencepiece_ModelProto* msg, upb_Arena* arena) { + struct sentencepiece_NormalizerSpec* sub = (struct sentencepiece_NormalizerSpec*)sentencepiece_ModelProto_denormalizer_spec(msg); + if (sub == NULL) { + sub = (struct sentencepiece_NormalizerSpec*)_upb_Message_New(&sentencepiece__NormalizerSpec_msg_init, arena); + if (sub) sentencepiece_ModelProto_set_denormalizer_spec(msg, sub); + } + return sub; +} + +/* sentencepiece.ModelProto.SentencePiece */ +UPB_INLINE sentencepiece_ModelProto_SentencePiece* sentencepiece_ModelProto_SentencePiece_new(upb_Arena* arena) { + return (sentencepiece_ModelProto_SentencePiece*)_upb_Message_New(&sentencepiece__ModelProto__SentencePiece_msg_init, arena); +} +UPB_INLINE sentencepiece_ModelProto_SentencePiece* sentencepiece_ModelProto_SentencePiece_parse(const char* buf, size_t size, + upb_Arena* arena) { + sentencepiece_ModelProto_SentencePiece* ret = sentencepiece_ModelProto_SentencePiece_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__ModelProto__SentencePiece_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE sentencepiece_ModelProto_SentencePiece* sentencepiece_ModelProto_SentencePiece_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + sentencepiece_ModelProto_SentencePiece* ret = sentencepiece_ModelProto_SentencePiece_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &sentencepiece__ModelProto__SentencePiece_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* sentencepiece_ModelProto_SentencePiece_serialize(const sentencepiece_ModelProto_SentencePiece* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__ModelProto__SentencePiece_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* sentencepiece_ModelProto_SentencePiece_serialize_ex(const sentencepiece_ModelProto_SentencePiece* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &sentencepiece__ModelProto__SentencePiece_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void sentencepiece_ModelProto_SentencePiece_clear_piece(sentencepiece_ModelProto_SentencePiece* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView sentencepiece_ModelProto_SentencePiece_piece(const sentencepiece_ModelProto_SentencePiece* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(20, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_ModelProto_SentencePiece_has_piece(const sentencepiece_ModelProto_SentencePiece* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_ModelProto_SentencePiece_clear_score(sentencepiece_ModelProto_SentencePiece* msg) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE float sentencepiece_ModelProto_SentencePiece_score(const sentencepiece_ModelProto_SentencePiece* msg) { + float default_val = 0; + float ret; + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_ModelProto_SentencePiece_has_score(const sentencepiece_ModelProto_SentencePiece* msg) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void sentencepiece_ModelProto_SentencePiece_clear_type(sentencepiece_ModelProto_SentencePiece* msg) { + const upb_MiniTableField field = {3, 16, 66, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t sentencepiece_ModelProto_SentencePiece_type(const sentencepiece_ModelProto_SentencePiece* msg) { + int32_t default_val = 1; + int32_t ret; + const upb_MiniTableField field = {3, 16, 66, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool sentencepiece_ModelProto_SentencePiece_has_type(const sentencepiece_ModelProto_SentencePiece* msg) { + const upb_MiniTableField field = {3, 16, 66, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void sentencepiece_ModelProto_SentencePiece_set_piece(sentencepiece_ModelProto_SentencePiece* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_ModelProto_SentencePiece_set_score(sentencepiece_ModelProto_SentencePiece* msg, float value) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void sentencepiece_ModelProto_SentencePiece_set_type(sentencepiece_ModelProto_SentencePiece* msg, int32_t value) { + const upb_MiniTableField field = {3, 16, 66, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +#ifdef __cplusplus + } /* extern "C" */ +#endif + +#endif /* SENTENCEPIECE_MODEL_PROTO_UPB_H__UPB_H_ */ diff --git a/src/builtin_upb/sentencepiece_model.upb_minitable.c b/src/builtin_upb/sentencepiece_model.upb_minitable.c new file mode 100644 index 000000000..958c393c4 --- /dev/null +++ b/src/builtin_upb/sentencepiece_model.upb_minitable.c @@ -0,0 +1,284 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece_model.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#include +#include "upb.h" +#include "sentencepiece_model.upb_minitable.h" + +extern const UPB_PRIVATE(upb_GeneratedExtensionListEntry)* UPB_PRIVATE(upb_generated_extension_list); +typedef struct { + upb_MiniTableField fields[46]; + upb_MiniTableSubInternal subs[1]; +} sentencepiece__TrainerSpec_msg_init_Fields; + +static const sentencepiece__TrainerSpec_msg_init_Fields sentencepiece_TrainerSpec__fields = {{ + {1, UPB_SIZE(28, 256), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(112, 96), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(32, 28), 65, 132, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(36, 32), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(40, 264), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(44, 36), 67, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(120, 112), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(48, 40), 69, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {11, UPB_SIZE(192, 272), 70, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {12, UPB_SIZE(52, 44), 71, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {13, UPB_SIZE(56, 48), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {14, UPB_SIZE(60, 52), 73, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {15, UPB_SIZE(64, 56), 74, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {16, UPB_SIZE(68, 60), 75, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {17, UPB_SIZE(72, 64), 76, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {18, UPB_SIZE(76, 68), 77, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {19, 14, 78, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {20, UPB_SIZE(80, 72), 79, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {21, 15, 80, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {22, 16, 81, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {23, 17, 82, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {24, 18, 83, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {25, 19, 84, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {26, 20, 85, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {30, UPB_SIZE(84, 280), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {31, UPB_SIZE(88, 288), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {32, 21, 86, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {33, 22, 87, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {34, 23, 88, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {35, 24, 89, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {36, 128, 90, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {40, UPB_SIZE(92, 76), 91, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {41, UPB_SIZE(96, 80), 92, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {42, UPB_SIZE(100, 84), 93, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {43, UPB_SIZE(104, 88), 94, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {44, UPB_SIZE(136, 144), 95, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {45, UPB_SIZE(144, 160), 96, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {46, UPB_SIZE(152, 176), 97, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {47, UPB_SIZE(160, 192), 98, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {48, UPB_SIZE(168, 208), 99, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {49, 25, 100, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {50, 26, 101, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {51, UPB_SIZE(108, 92), 102, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {52, UPB_SIZE(200, 296), 103, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {53, UPB_SIZE(176, 224), 104, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {54, UPB_SIZE(184, 240), 105, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &sentencepiece__TrainerSpec__ModelType_enum_init}, +}}; + +const upb_MiniTable sentencepiece__TrainerSpec_msg_init = { + &sentencepiece_TrainerSpec__fields.fields[0], + UPB_SIZE(208, 304), 46, kUpb_ExtMode_Extendable, 7, UPB_FASTTABLE_MASK(248), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.TrainerSpec", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x010000003f00000a, &upb_DecodeFast_Bytes_Repeated_Tag1Byte}, + {0x0060000000000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0020000002000020, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x010800003f00002a, &upb_DecodeFast_Bytes_Repeated_Tag1Byte}, + {0x0024000003000030, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x007000000400003a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0028000005000055, &upb_DecodeFast_Fixed32_Scalar_Tag1Byte}, + {0x0110000006000058, &upb_DecodeFast_Varint64_Scalar_Tag1Byte}, + {0x002c000007000060, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0030000008000068, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0034000009000070, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x003800000a00007d, &upb_DecodeFast_Fixed32_Scalar_Tag1Byte}, + {0x003c00000b000180, &upb_DecodeFast_Varint32_Scalar_Tag2Byte}, + {0x004000000c000188, &upb_DecodeFast_Varint32_Scalar_Tag2Byte}, + {0x004400000d000190, &upb_DecodeFast_Varint32_Scalar_Tag2Byte}, + {0x000e00000e000198, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x004800000f0001a0, &upb_DecodeFast_Varint32_Scalar_Tag2Byte}, + {0x000f0000100001a8, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x00100000110001b0, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x00110000120001b8, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x00120000130001c0, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x00130000140001c8, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x00140000150001d0, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x005800001e0002d8, &upb_DecodeFast_Varint32_Scalar_Tag2Byte}, + {0x009000001f0002e2, &upb_DecodeFast_Bytes_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x011800003f0001f2, &upb_DecodeFast_Bytes_Repeated_Tag2Byte}, + {0x012000003f0001fa, &upb_DecodeFast_Bytes_Repeated_Tag2Byte}, + }) +}; + +typedef struct { + upb_MiniTableField fields[6]; +} sentencepiece__NormalizerSpec_msg_init_Fields; + +static const sentencepiece__NormalizerSpec_msg_init_Fields sentencepiece_NormalizerSpec__fields = {{ + {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, 9, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {4, 10, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {5, 11, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(28, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable sentencepiece__NormalizerSpec_msg_init = { + &sentencepiece_NormalizerSpec__fields.fields[0], + UPB_SIZE(40, 64), 6, kUpb_ExtMode_Extendable, 6, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.NormalizerSpec", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0020000001000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0009000002000018, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000a000003000020, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000b000004000028, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0030000005000032, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[1]; + upb_MiniTableSubInternal subs[1]; +} sentencepiece__SelfTestData_msg_init_Fields; + +static const sentencepiece__SelfTestData_msg_init_Fields sentencepiece_SelfTestData__fields = {{ + {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &sentencepiece__SelfTestData__Sample_msg_init}, +}}; + +const upb_MiniTable sentencepiece__SelfTestData_msg_init = { + &sentencepiece_SelfTestData__fields.fields[0], + 16, 1, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.SelfTestData", +#endif +}; + +typedef struct { + upb_MiniTableField fields[2]; +} sentencepiece__SelfTestData__Sample_msg_init_Fields; + +static const sentencepiece__SelfTestData__Sample_msg_init_Fields sentencepiece_SelfTestData_Sample__fields = {{ + {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(20, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable sentencepiece__SelfTestData__Sample_msg_init = { + &sentencepiece_SelfTestData_Sample__fields.fields[0], + UPB_SIZE(32, 48), 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.SelfTestData.Sample", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0020000001000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[5]; + upb_MiniTableSubInternal subs[5]; +} sentencepiece__ModelProto_msg_init_Fields; + +static const sentencepiece__ModelProto_msg_init_Fields sentencepiece_ModelProto__fields = {{ + {1, UPB_SIZE(12, 16), 0, UPB_SIZE(15, 16), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(16, 24), 64, UPB_SIZE(13, 15), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(20, 32), 65, UPB_SIZE(11, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(24, 40), 66, UPB_SIZE(9, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(28, 48), 67, UPB_SIZE(7, 12), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &sentencepiece__ModelProto__SentencePiece_msg_init}, + {.UPB_PRIVATE(submsg) = &sentencepiece__TrainerSpec_msg_init}, + {.UPB_PRIVATE(submsg) = &sentencepiece__NormalizerSpec_msg_init}, + {.UPB_PRIVATE(submsg) = &sentencepiece__SelfTestData_msg_init}, + {.UPB_PRIVATE(submsg) = &sentencepiece__NormalizerSpec_msg_init}, +}}; + +const upb_MiniTable sentencepiece__ModelProto_msg_init = { + &sentencepiece_ModelProto__fields.fields[0], + UPB_SIZE(32, 56), 5, kUpb_ExtMode_Extendable, 5, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.ModelProto", +#endif +}; + +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[1]; +} sentencepiece__ModelProto__SentencePiece_msg_init_Fields; + +static const sentencepiece__ModelProto__SentencePiece_msg_init_Fields sentencepiece_ModelProto_SentencePiece__fields = {{ + {1, UPB_SIZE(20, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, 12, 65, kUpb_NoSub, 2, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, 16, 66, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &sentencepiece__ModelProto__SentencePiece__Type_enum_init}, +}}; + +const upb_MiniTable sentencepiece__ModelProto__SentencePiece_msg_init = { + &sentencepiece_ModelProto_SentencePiece__fields.fields[0], + UPB_SIZE(32, 40), 3, kUpb_ExtMode_Extendable, 3, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "sentencepiece.ModelProto.SentencePiece", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001800000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x000c000001000015, &upb_DecodeFast_Fixed32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +const upb_MiniTableEnum sentencepiece__ModelProto__SentencePiece__Type_enum_init = { + 64, + 0, + { + 0x7e, + 0x0, + }, +}; + +const upb_MiniTableEnum sentencepiece__TrainerSpec__ModelType_enum_init = { + 64, + 0, + { + 0x1e, + 0x0, + }, +}; + +static const upb_MiniTable *messages_layout[6] = { + &sentencepiece__TrainerSpec_msg_init, + &sentencepiece__NormalizerSpec_msg_init, + &sentencepiece__SelfTestData_msg_init, + &sentencepiece__SelfTestData__Sample_msg_init, + &sentencepiece__ModelProto_msg_init, + &sentencepiece__ModelProto__SentencePiece_msg_init, +}; + +static const upb_MiniTableEnum *enums_layout[2] = { + &sentencepiece__ModelProto__SentencePiece__Type_enum_init, + &sentencepiece__TrainerSpec__ModelType_enum_init, +}; + +const upb_MiniTableFile sentencepiece_model_proto_upb_file_layout = { + messages_layout, + enums_layout, + NULL, + 6, + 2, + 0, +}; + + diff --git a/src/builtin_upb/sentencepiece_model.upb_minitable.h b/src/builtin_upb/sentencepiece_model.upb_minitable.h new file mode 100644 index 000000000..110e04c9e --- /dev/null +++ b/src/builtin_upb/sentencepiece_model.upb_minitable.h @@ -0,0 +1,33 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece_model.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#ifndef SENTENCEPIECE_MODEL_PROTO_UPB_H__UPB_MINITABLE_H_ +#define SENTENCEPIECE_MODEL_PROTO_UPB_H__UPB_MINITABLE_H_ + +#include "upb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern const upb_MiniTable sentencepiece__TrainerSpec_msg_init; +extern const upb_MiniTable sentencepiece__NormalizerSpec_msg_init; +extern const upb_MiniTable sentencepiece__SelfTestData_msg_init; +extern const upb_MiniTable sentencepiece__SelfTestData__Sample_msg_init; +extern const upb_MiniTable sentencepiece__ModelProto_msg_init; +extern const upb_MiniTable sentencepiece__ModelProto__SentencePiece_msg_init; + +extern const upb_MiniTableEnum sentencepiece__ModelProto__SentencePiece__Type_enum_init; +extern const upb_MiniTableEnum sentencepiece__TrainerSpec__ModelType_enum_init; +extern const upb_MiniTableFile sentencepiece_model_proto_upb_file_layout; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* SENTENCEPIECE_MODEL_PROTO_UPB_H__UPB_MINITABLE_H_ */ diff --git a/src/builtin_upb/sentencepiece_model.upbdefs.c b/src/builtin_upb/sentencepiece_model.upbdefs.c new file mode 100644 index 000000000..d86e875b4 --- /dev/null +++ b/src/builtin_upb/sentencepiece_model.upbdefs.c @@ -0,0 +1,318 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece_model.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + + +#include "upb/reflection/def.h" +#include "sentencepiece_model.upbdefs.h" +#include "sentencepiece_model.upb_minitable.h" + +static const char descriptor[3500] = { + '\n', '\031', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', + 'e', 'c', 'e', '_', 'm', 'o', 'd', 'e', 'l', '.', 'p', 'r', + 'o', 't', 'o', '\022', '\r', 's', 'e', 'n', 't', 'e', 'n', 'c', + 'e', 'p', 'i', 'e', 'c', 'e', '\"', '\313', '\022', '\n', '\013', 'T', + 'r', 'a', 'i', 'n', 'e', 'r', 'S', 'p', 'e', 'c', '\022', '\024', + '\n', '\005', 'i', 'n', 'p', 'u', 't', '\030', '\001', ' ', '\003', '(', + '\t', 'R', '\005', 'i', 'n', 'p', 'u', 't', '\022', '!', '\n', '\014', + 'i', 'n', 'p', 'u', 't', '_', 'f', 'o', 'r', 'm', 'a', 't', + '\030', '\007', ' ', '\001', '(', '\t', 'R', '\013', 'i', 'n', 'p', 'u', + 't', 'F', 'o', 'r', 'm', 'a', 't', '\022', '!', '\n', '\014', 'm', + 'o', 'd', 'e', 'l', '_', 'p', 'r', 'e', 'f', 'i', 'x', '\030', + '\002', ' ', '\001', '(', '\t', 'R', '\013', 'm', 'o', 'd', 'e', 'l', + 'P', 'r', 'e', 'f', 'i', 'x', '\022', 'L', '\n', '\n', 'm', 'o', + 'd', 'e', 'l', '_', 't', 'y', 'p', 'e', '\030', '\003', ' ', '\001', + '(', '\016', '2', '$', '.', 's', 'e', 'n', 't', 'e', 'n', 'c', + 'e', 'p', 'i', 'e', 'c', 'e', '.', 'T', 'r', 'a', 'i', 'n', + 'e', 'r', 'S', 'p', 'e', 'c', '.', 'M', 'o', 'd', 'e', 'l', + 'T', 'y', 'p', 'e', ':', '\007', 'U', 'N', 'I', 'G', 'R', 'A', + 'M', 'R', '\t', 'm', 'o', 'd', 'e', 'l', 'T', 'y', 'p', 'e', + '\022', '#', '\n', '\n', 'v', 'o', 'c', 'a', 'b', '_', 's', 'i', + 'z', 'e', '\030', '\004', ' ', '\001', '(', '\005', ':', '\004', '8', '0', + '0', '0', 'R', '\t', 'v', 'o', 'c', 'a', 'b', 'S', 'i', 'z', + 'e', '\022', '\'', '\n', '\017', 'a', 'c', 'c', 'e', 'p', 't', '_', + 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e', '\030', '\005', ' ', '\003', + '(', '\t', 'R', '\016', 'a', 'c', 'c', 'e', 'p', 't', 'L', 'a', + 'n', 'g', 'u', 'a', 'g', 'e', '\022', '4', '\n', '\025', 's', 'e', + 'l', 'f', '_', 't', 'e', 's', 't', '_', 's', 'a', 'm', 'p', + 'l', 'e', '_', 's', 'i', 'z', 'e', '\030', '\006', ' ', '\001', '(', + '\005', ':', '\001', '0', 'R', '\022', 's', 'e', 'l', 'f', 'T', 'e', + 's', 't', 'S', 'a', 'm', 'p', 'l', 'e', 'S', 'i', 'z', 'e', + '\022', 'E', '\n', '\033', 'e', 'n', 'a', 'b', 'l', 'e', '_', 'd', + 'i', 'f', 'f', 'e', 'r', 'e', 'n', 't', 'i', 'a', 'l', '_', + 'p', 'r', 'i', 'v', 'a', 'c', 'y', '\030', '2', ' ', '\001', '(', + '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\031', 'e', 'n', + 'a', 'b', 'l', 'e', 'D', 'i', 'f', 'f', 'e', 'r', 'e', 'n', + 't', 'i', 'a', 'l', 'P', 'r', 'i', 'v', 'a', 'c', 'y', '\022', + 'J', '\n', ' ', 'd', 'i', 'f', 'f', 'e', 'r', 'e', 'n', 't', + 'i', 'a', 'l', '_', 'p', 'r', 'i', 'v', 'a', 'c', 'y', '_', + 'n', 'o', 'i', 's', 'e', '_', 'l', 'e', 'v', 'e', 'l', '\030', + '3', ' ', '\001', '(', '\002', ':', '\001', '0', 'R', '\035', 'd', 'i', + 'f', 'f', 'e', 'r', 'e', 'n', 't', 'i', 'a', 'l', 'P', 'r', + 'i', 'v', 'a', 'c', 'y', 'N', 'o', 'i', 's', 'e', 'L', 'e', + 'v', 'e', 'l', '\022', 'X', '\n', '\'', 'd', 'i', 'f', 'f', 'e', + 'r', 'e', 'n', 't', 'i', 'a', 'l', '_', 'p', 'r', 'i', 'v', + 'a', 'c', 'y', '_', 'c', 'l', 'i', 'p', 'p', 'i', 'n', 'g', + '_', 't', 'h', 'r', 'e', 's', 'h', 'o', 'l', 'd', '\030', '4', + ' ', '\001', '(', '\004', ':', '\001', '0', 'R', '$', 'd', 'i', 'f', + 'f', 'e', 'r', 'e', 'n', 't', 'i', 'a', 'l', 'P', 'r', 'i', + 'v', 'a', 'c', 'y', 'C', 'l', 'i', 'p', 'p', 'i', 'n', 'g', + 'T', 'h', 'r', 'e', 's', 'h', 'o', 'l', 'd', '\022', ':', '\n', + '\022', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', '_', 'c', + 'o', 'v', 'e', 'r', 'a', 'g', 'e', '\030', '\n', ' ', '\001', '(', + '\002', ':', '\013', '0', '.', '9', '9', '9', '4', '9', '9', '9', + '7', '7', 'R', '\021', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', + 'r', 'C', 'o', 'v', 'e', 'r', 'a', 'g', 'e', '\022', '1', '\n', + '\023', 'i', 'n', 'p', 'u', 't', '_', 's', 'e', 'n', 't', 'e', + 'n', 'c', 'e', '_', 's', 'i', 'z', 'e', '\030', '\013', ' ', '\001', + '(', '\004', ':', '\001', '0', 'R', '\021', 'i', 'n', 'p', 'u', 't', + 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'S', 'i', 'z', 'e', + '\022', ':', '\n', '\026', 's', 'h', 'u', 'f', 'f', 'l', 'e', '_', + 'i', 'n', 'p', 'u', 't', '_', 's', 'e', 'n', 't', 'e', 'n', + 'c', 'e', '\030', '\023', ' ', '\001', '(', '\010', ':', '\004', 't', 'r', + 'u', 'e', 'R', '\024', 's', 'h', 'u', 'f', 'f', 'l', 'e', 'I', + 'n', 'p', 'u', 't', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', + '\022', '4', '\n', '\024', 'm', 'i', 'n', 'i', 'n', 'g', '_', 's', + 'e', 'n', 't', 'e', 'n', 'c', 'e', '_', 's', 'i', 'z', 'e', + '\030', '\014', ' ', '\001', '(', '\005', 'B', '\002', '\030', '\001', 'R', '\022', + 'm', 'i', 'n', 'i', 'n', 'g', 'S', 'e', 'n', 't', 'e', 'n', + 'c', 'e', 'S', 'i', 'z', 'e', '\022', '8', '\n', '\026', 't', 'r', + 'a', 'i', 'n', 'i', 'n', 'g', '_', 's', 'e', 'n', 't', 'e', + 'n', 'c', 'e', '_', 's', 'i', 'z', 'e', '\030', '\r', ' ', '\001', + '(', '\005', 'B', '\002', '\030', '\001', 'R', '\024', 't', 'r', 'a', 'i', + 'n', 'i', 'n', 'g', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', + 'S', 'i', 'z', 'e', '\022', '?', '\n', '\027', 's', 'e', 'e', 'd', + '_', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', + 'c', 'e', '_', 's', 'i', 'z', 'e', '\030', '\016', ' ', '\001', '(', + '\005', ':', '\007', '1', '0', '0', '0', '0', '0', '0', 'R', '\025', + 's', 'e', 'e', 'd', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', + 'p', 'i', 'e', 'c', 'e', 'S', 'i', 'z', 'e', '\022', '/', '\n', + '\020', 's', 'h', 'r', 'i', 'n', 'k', 'i', 'n', 'g', '_', 'f', + 'a', 'c', 't', 'o', 'r', '\030', '\017', ' ', '\001', '(', '\002', ':', + '\004', '0', '.', '7', '5', 'R', '\017', 's', 'h', 'r', 'i', 'n', + 'k', 'i', 'n', 'g', 'F', 'a', 'c', 't', 'o', 'r', '\022', '4', + '\n', '\023', 'm', 'a', 'x', '_', 's', 'e', 'n', 't', 'e', 'n', + 'c', 'e', '_', 'l', 'e', 'n', 'g', 't', 'h', '\030', '\022', ' ', + '\001', '(', '\005', ':', '\004', '4', '1', '9', '2', 'R', '\021', 'm', + 'a', 'x', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'L', 'e', + 'n', 'g', 't', 'h', '\022', '#', '\n', '\013', 'n', 'u', 'm', '_', + 't', 'h', 'r', 'e', 'a', 'd', 's', '\030', '\020', ' ', '\001', '(', + '\005', ':', '\002', '1', '6', 'R', '\n', 'n', 'u', 'm', 'T', 'h', + 'r', 'e', 'a', 'd', 's', '\022', '/', '\n', '\022', 'n', 'u', 'm', + '_', 's', 'u', 'b', '_', 'i', 't', 'e', 'r', 'a', 't', 'i', + 'o', 'n', 's', '\030', '\021', ' ', '\001', '(', '\005', ':', '\001', '2', + 'R', '\020', 'n', 'u', 'm', 'S', 'u', 'b', 'I', 't', 'e', 'r', + 'a', 't', 'i', 'o', 'n', 's', '\022', '<', '\n', '\030', 'm', 'a', + 'x', '_', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', + 'e', 'c', 'e', '_', 'l', 'e', 'n', 'g', 't', 'h', '\030', '\024', + ' ', '\001', '(', '\005', ':', '\002', '1', '6', 'R', '\026', 'm', 'a', + 'x', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', + 'c', 'e', 'L', 'e', 'n', 'g', 't', 'h', '\022', ';', '\n', '\027', + 's', 'p', 'l', 'i', 't', '_', 'b', 'y', '_', 'u', 'n', 'i', + 'c', 'o', 'd', 'e', '_', 's', 'c', 'r', 'i', 'p', 't', '\030', + '\025', ' ', '\001', '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', + '\024', 's', 'p', 'l', 'i', 't', 'B', 'y', 'U', 'n', 'i', 'c', + 'o', 'd', 'e', 'S', 'c', 'r', 'i', 'p', 't', '\022', ',', '\n', + '\017', 's', 'p', 'l', 'i', 't', '_', 'b', 'y', '_', 'n', 'u', + 'm', 'b', 'e', 'r', '\030', '\027', ' ', '\001', '(', '\010', ':', '\004', + 't', 'r', 'u', 'e', 'R', '\r', 's', 'p', 'l', 'i', 't', 'B', + 'y', 'N', 'u', 'm', 'b', 'e', 'r', '\022', '4', '\n', '\023', 's', + 'p', 'l', 'i', 't', '_', 'b', 'y', '_', 'w', 'h', 'i', 't', + 'e', 's', 'p', 'a', 'c', 'e', '\030', '\026', ' ', '\001', '(', '\010', + ':', '\004', 't', 'r', 'u', 'e', 'R', '\021', 's', 'p', 'l', 'i', + 't', 'B', 'y', 'W', 'h', 'i', 't', 'e', 's', 'p', 'a', 'c', + 'e', '\022', 'B', '\n', '\032', 't', 'r', 'e', 'a', 't', '_', 'w', + 'h', 'i', 't', 'e', 's', 'p', 'a', 'c', 'e', '_', 'a', 's', + '_', 's', 'u', 'f', 'f', 'i', 'x', '\030', '\030', ' ', '\001', '(', + '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\027', 't', 'r', + 'e', 'a', 't', 'W', 'h', 'i', 't', 'e', 's', 'p', 'a', 'c', + 'e', 'A', 's', 'S', 'u', 'f', 'f', 'i', 'x', '\022', 'F', '\n', + '\034', 'a', 'l', 'l', 'o', 'w', '_', 'w', 'h', 'i', 't', 'e', + 's', 'p', 'a', 'c', 'e', '_', 'o', 'n', 'l', 'y', '_', 'p', + 'i', 'e', 'c', 'e', 's', '\030', '\032', ' ', '\001', '(', '\010', ':', + '\005', 'f', 'a', 'l', 's', 'e', 'R', '\031', 'a', 'l', 'l', 'o', + 'w', 'W', 'h', 'i', 't', 'e', 's', 'p', 'a', 'c', 'e', 'O', + 'n', 'l', 'y', 'P', 'i', 'e', 'c', 'e', 's', '\022', '(', '\n', + '\014', 's', 'p', 'l', 'i', 't', '_', 'd', 'i', 'g', 'i', 't', + 's', '\030', '\031', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', + 's', 'e', 'R', '\013', 's', 'p', 'l', 'i', 't', 'D', 'i', 'g', + 'i', 't', 's', '\022', '=', '\n', '\031', 'p', 'r', 'e', 't', 'o', + 'k', 'e', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', '_', 'd', + 'e', 'l', 'i', 'm', 'i', 't', 'e', 'r', '\030', '5', ' ', '\001', + '(', '\t', ':', '\000', 'R', '\030', 'p', 'r', 'e', 't', 'o', 'k', + 'e', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'D', 'e', 'l', + 'i', 'm', 'i', 't', 'e', 'r', '\022', '\'', '\n', '\017', 'c', 'o', + 'n', 't', 'r', 'o', 'l', '_', 's', 'y', 'm', 'b', 'o', 'l', + 's', '\030', '\036', ' ', '\003', '(', '\t', 'R', '\016', 'c', 'o', 'n', + 't', 'r', 'o', 'l', 'S', 'y', 'm', 'b', 'o', 'l', 's', '\022', + '0', '\n', '\024', 'u', 's', 'e', 'r', '_', 'd', 'e', 'f', 'i', + 'n', 'e', 'd', '_', 's', 'y', 'm', 'b', 'o', 'l', 's', '\030', + '\037', ' ', '\003', '(', '\t', 'R', '\022', 'u', 's', 'e', 'r', 'D', + 'e', 'f', 'i', 'n', 'e', 'd', 'S', 'y', 'm', 'b', 'o', 'l', + 's', '\022', '%', '\n', '\016', 'r', 'e', 'q', 'u', 'i', 'r', 'e', + 'd', '_', 'c', 'h', 'a', 'r', 's', '\030', '$', ' ', '\001', '(', + '\t', 'R', '\r', 'r', 'e', 'q', 'u', 'i', 'r', 'e', 'd', 'C', + 'h', 'a', 'r', 's', '\022', '*', '\n', '\r', 'b', 'y', 't', 'e', + '_', 'f', 'a', 'l', 'l', 'b', 'a', 'c', 'k', '\030', '#', ' ', + '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\014', + 'b', 'y', 't', 'e', 'F', 'a', 'l', 'l', 'b', 'a', 'c', 'k', + '\022', 'G', '\n', '\035', 'v', 'o', 'c', 'a', 'b', 'u', 'l', 'a', + 'r', 'y', '_', 'o', 'u', 't', 'p', 'u', 't', '_', 'p', 'i', + 'e', 'c', 'e', '_', 's', 'c', 'o', 'r', 'e', '\030', ' ', ' ', + '\001', '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', '\032', 'v', + 'o', 'c', 'a', 'b', 'u', 'l', 'a', 'r', 'y', 'O', 'u', 't', + 'p', 'u', 't', 'P', 'i', 'e', 'c', 'e', 'S', 'c', 'o', 'r', + 'e', '\022', '.', '\n', '\020', 'h', 'a', 'r', 'd', '_', 'v', 'o', + 'c', 'a', 'b', '_', 'l', 'i', 'm', 'i', 't', '\030', '!', ' ', + '\001', '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', '\016', 'h', + 'a', 'r', 'd', 'V', 'o', 'c', 'a', 'b', 'L', 'i', 'm', 'i', + 't', '\022', ')', '\n', '\r', 'u', 's', 'e', '_', 'a', 'l', 'l', + '_', 'v', 'o', 'c', 'a', 'b', '\030', '\"', ' ', '\001', '(', '\010', + ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\013', 'u', 's', 'e', + 'A', 'l', 'l', 'V', 'o', 'c', 'a', 'b', '\022', '\030', '\n', '\006', + 'u', 'n', 'k', '_', 'i', 'd', '\030', '(', ' ', '\001', '(', '\005', + ':', '\001', '0', 'R', '\005', 'u', 'n', 'k', 'I', 'd', '\022', '\030', + '\n', '\006', 'b', 'o', 's', '_', 'i', 'd', '\030', ')', ' ', '\001', + '(', '\005', ':', '\001', '1', 'R', '\005', 'b', 'o', 's', 'I', 'd', + '\022', '\030', '\n', '\006', 'e', 'o', 's', '_', 'i', 'd', '\030', '*', + ' ', '\001', '(', '\005', ':', '\001', '2', 'R', '\005', 'e', 'o', 's', + 'I', 'd', '\022', '\031', '\n', '\006', 'p', 'a', 'd', '_', 'i', 'd', + '\030', '+', ' ', '\001', '(', '\005', ':', '\002', '-', '1', 'R', '\005', + 'p', 'a', 'd', 'I', 'd', '\022', '\"', '\n', '\t', 'u', 'n', 'k', + '_', 'p', 'i', 'e', 'c', 'e', '\030', '-', ' ', '\001', '(', '\t', + ':', '\005', '<', 'u', 'n', 'k', '>', 'R', '\010', 'u', 'n', 'k', + 'P', 'i', 'e', 'c', 'e', '\022', ' ', '\n', '\t', 'b', 'o', 's', + '_', 'p', 'i', 'e', 'c', 'e', '\030', '.', ' ', '\001', '(', '\t', + ':', '\003', '<', 's', '>', 'R', '\010', 'b', 'o', 's', 'P', 'i', + 'e', 'c', 'e', '\022', '!', '\n', '\t', 'e', 'o', 's', '_', 'p', + 'i', 'e', 'c', 'e', '\030', '/', ' ', '\001', '(', '\t', ':', '\004', + '<', '/', 's', '>', 'R', '\010', 'e', 'o', 's', 'P', 'i', 'e', + 'c', 'e', '\022', '\"', '\n', '\t', 'p', 'a', 'd', '_', 'p', 'i', + 'e', 'c', 'e', '\030', '0', ' ', '\001', '(', '\t', ':', '\005', '<', + 'p', 'a', 'd', '>', 'R', '\010', 'p', 'a', 'd', 'P', 'i', 'e', + 'c', 'e', '\022', '&', '\n', '\013', 'u', 'n', 'k', '_', 's', 'u', + 'r', 'f', 'a', 'c', 'e', '\030', ',', ' ', '\001', '(', '\t', ':', + '\005', ' ', '\342', '\201', '\207', ' ', 'R', '\n', 'u', 'n', 'k', 'S', + 'u', 'r', 'f', 'a', 'c', 'e', '\022', 'F', '\n', '\034', 't', 'r', + 'a', 'i', 'n', '_', 'e', 'x', 't', 'r', 'e', 'm', 'e', 'l', + 'y', '_', 'l', 'a', 'r', 'g', 'e', '_', 'c', 'o', 'r', 'p', + 'u', 's', '\030', '1', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', + 'l', 's', 'e', 'R', '\031', 't', 'r', 'a', 'i', 'n', 'E', 'x', + 't', 'r', 'e', 'm', 'e', 'l', 'y', 'L', 'a', 'r', 'g', 'e', + 'C', 'o', 'r', 'p', 'u', 's', '\022', ':', '\n', '\030', 's', 'e', + 'e', 'd', '_', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', + 'i', 'e', 'c', 'e', 's', '_', 'f', 'i', 'l', 'e', '\030', '6', + ' ', '\001', '(', '\t', ':', '\000', 'R', '\026', 's', 'e', 'e', 'd', + 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', 'c', + 'e', 's', 'F', 'i', 'l', 'e', '\"', '5', '\n', '\t', 'M', 'o', + 'd', 'e', 'l', 'T', 'y', 'p', 'e', '\022', '\013', '\n', '\007', 'U', + 'N', 'I', 'G', 'R', 'A', 'M', '\020', '\001', '\022', '\007', '\n', '\003', + 'B', 'P', 'E', '\020', '\002', '\022', '\010', '\n', '\004', 'W', 'O', 'R', + 'D', '\020', '\003', '\022', '\010', '\n', '\004', 'C', 'H', 'A', 'R', '\020', + '\004', '*', '\t', '\010', '\310', '\001', '\020', '\200', '\200', '\200', '\200', '\002', + '\"', '\275', '\002', '\n', '\016', 'N', 'o', 'r', 'm', 'a', 'l', 'i', + 'z', 'e', 'r', 'S', 'p', 'e', 'c', '\022', '\022', '\n', '\004', 'n', + 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', + 'a', 'm', 'e', '\022', '1', '\n', '\024', 'p', 'r', 'e', 'c', 'o', + 'm', 'p', 'i', 'l', 'e', 'd', '_', 'c', 'h', 'a', 'r', 's', + 'm', 'a', 'p', '\030', '\002', ' ', '\001', '(', '\014', 'R', '\023', 'p', + 'r', 'e', 'c', 'o', 'm', 'p', 'i', 'l', 'e', 'd', 'C', 'h', + 'a', 'r', 's', 'm', 'a', 'p', '\022', '.', '\n', '\020', 'a', 'd', + 'd', '_', 'd', 'u', 'm', 'm', 'y', '_', 'p', 'r', 'e', 'f', + 'i', 'x', '\030', '\003', ' ', '\001', '(', '\010', ':', '\004', 't', 'r', + 'u', 'e', 'R', '\016', 'a', 'd', 'd', 'D', 'u', 'm', 'm', 'y', + 'P', 'r', 'e', 'f', 'i', 'x', '\022', '>', '\n', '\030', 'r', 'e', + 'm', 'o', 'v', 'e', '_', 'e', 'x', 't', 'r', 'a', '_', 'w', + 'h', 'i', 't', 'e', 's', 'p', 'a', 'c', 'e', 's', '\030', '\004', + ' ', '\001', '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', '\026', + 'r', 'e', 'm', 'o', 'v', 'e', 'E', 'x', 't', 'r', 'a', 'W', + 'h', 'i', 't', 'e', 's', 'p', 'a', 'c', 'e', 's', '\022', '3', + '\n', '\022', 'e', 's', 'c', 'a', 'p', 'e', '_', 'w', 'h', 'i', + 't', 'e', 's', 'p', 'a', 'c', 'e', 's', '\030', '\005', ' ', '\001', + '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', '\021', 'e', 's', + 'c', 'a', 'p', 'e', 'W', 'h', 'i', 't', 'e', 's', 'p', 'a', + 'c', 'e', 's', '\022', '4', '\n', '\026', 'n', 'o', 'r', 'm', 'a', + 'l', 'i', 'z', 'a', 't', 'i', 'o', 'n', '_', 'r', 'u', 'l', + 'e', '_', 't', 's', 'v', '\030', '\006', ' ', '\001', '(', '\t', 'R', + '\024', 'n', 'o', 'r', 'm', 'a', 'l', 'i', 'z', 'a', 't', 'i', + 'o', 'n', 'R', 'u', 'l', 'e', 'T', 's', 'v', '*', '\t', '\010', + '\310', '\001', '\020', '\200', '\200', '\200', '\200', '\002', '\"', '\223', '\001', '\n', + '\014', 'S', 'e', 'l', 'f', 'T', 'e', 's', 't', 'D', 'a', 't', + 'a', '\022', '<', '\n', '\007', 's', 'a', 'm', 'p', 'l', 'e', 's', + '\030', '\001', ' ', '\003', '(', '\013', '2', '\"', '.', 's', 'e', 'n', + 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', 'c', 'e', '.', 'S', + 'e', 'l', 'f', 'T', 'e', 's', 't', 'D', 'a', 't', 'a', '.', + 'S', 'a', 'm', 'p', 'l', 'e', 'R', '\007', 's', 'a', 'm', 'p', + 'l', 'e', 's', '\032', ':', '\n', '\006', 'S', 'a', 'm', 'p', 'l', + 'e', '\022', '\024', '\n', '\005', 'i', 'n', 'p', 'u', 't', '\030', '\001', + ' ', '\001', '(', '\t', 'R', '\005', 'i', 'n', 'p', 'u', 't', '\022', + '\032', '\n', '\010', 'e', 'x', 'p', 'e', 'c', 't', 'e', 'd', '\030', + '\002', ' ', '\001', '(', '\t', 'R', '\010', 'e', 'x', 'p', 'e', 'c', + 't', 'e', 'd', '*', '\t', '\010', '\310', '\001', '\020', '\200', '\200', '\200', + '\200', '\002', '\"', '\327', '\004', '\n', '\n', 'M', 'o', 'd', 'e', 'l', + 'P', 'r', 'o', 't', 'o', '\022', '?', '\n', '\006', 'p', 'i', 'e', + 'c', 'e', 's', '\030', '\001', ' ', '\003', '(', '\013', '2', '\'', '.', + 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', 'c', + 'e', '.', 'M', 'o', 'd', 'e', 'l', 'P', 'r', 'o', 't', 'o', + '.', 'S', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'P', 'i', 'e', + 'c', 'e', 'R', '\006', 'p', 'i', 'e', 'c', 'e', 's', '\022', '=', + '\n', '\014', 't', 'r', 'a', 'i', 'n', 'e', 'r', '_', 's', 'p', + 'e', 'c', '\030', '\002', ' ', '\001', '(', '\013', '2', '\032', '.', 's', + 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', 'e', 'c', 'e', + '.', 'T', 'r', 'a', 'i', 'n', 'e', 'r', 'S', 'p', 'e', 'c', + 'R', '\013', 't', 'r', 'a', 'i', 'n', 'e', 'r', 'S', 'p', 'e', + 'c', '\022', 'F', '\n', '\017', 'n', 'o', 'r', 'm', 'a', 'l', 'i', + 'z', 'e', 'r', '_', 's', 'p', 'e', 'c', '\030', '\003', ' ', '\001', + '(', '\013', '2', '\035', '.', 's', 'e', 'n', 't', 'e', 'n', 'c', + 'e', 'p', 'i', 'e', 'c', 'e', '.', 'N', 'o', 'r', 'm', 'a', + 'l', 'i', 'z', 'e', 'r', 'S', 'p', 'e', 'c', 'R', '\016', 'n', + 'o', 'r', 'm', 'a', 'l', 'i', 'z', 'e', 'r', 'S', 'p', 'e', + 'c', '\022', 'A', '\n', '\016', 's', 'e', 'l', 'f', '_', 't', 'e', + 's', 't', '_', 'd', 'a', 't', 'a', '\030', '\004', ' ', '\001', '(', + '\013', '2', '\033', '.', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', + 'p', 'i', 'e', 'c', 'e', '.', 'S', 'e', 'l', 'f', 'T', 'e', + 's', 't', 'D', 'a', 't', 'a', 'R', '\014', 's', 'e', 'l', 'f', + 'T', 'e', 's', 't', 'D', 'a', 't', 'a', '\022', 'J', '\n', '\021', + 'd', 'e', 'n', 'o', 'r', 'm', 'a', 'l', 'i', 'z', 'e', 'r', + '_', 's', 'p', 'e', 'c', '\030', '\005', ' ', '\001', '(', '\013', '2', + '\035', '.', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e', 'p', 'i', + 'e', 'c', 'e', '.', 'N', 'o', 'r', 'm', 'a', 'l', 'i', 'z', + 'e', 'r', 'S', 'p', 'e', 'c', 'R', '\020', 'd', 'e', 'n', 'o', + 'r', 'm', 'a', 'l', 'i', 'z', 'e', 'r', 'S', 'p', 'e', 'c', + '\032', '\346', '\001', '\n', '\r', 'S', 'e', 'n', 't', 'e', 'n', 'c', + 'e', 'P', 'i', 'e', 'c', 'e', '\022', '\024', '\n', '\005', 'p', 'i', + 'e', 'c', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\005', 'p', + 'i', 'e', 'c', 'e', '\022', '\024', '\n', '\005', 's', 'c', 'o', 'r', + 'e', '\030', '\002', ' ', '\001', '(', '\002', 'R', '\005', 's', 'c', 'o', + 'r', 'e', '\022', 'H', '\n', '\004', 't', 'y', 'p', 'e', '\030', '\003', + ' ', '\001', '(', '\016', '2', ',', '.', 's', 'e', 'n', 't', 'e', + 'n', 'c', 'e', 'p', 'i', 'e', 'c', 'e', '.', 'M', 'o', 'd', + 'e', 'l', 'P', 'r', 'o', 't', 'o', '.', 'S', 'e', 'n', 't', + 'e', 'n', 'c', 'e', 'P', 'i', 'e', 'c', 'e', '.', 'T', 'y', + 'p', 'e', ':', '\006', 'N', 'O', 'R', 'M', 'A', 'L', 'R', '\004', + 't', 'y', 'p', 'e', '\"', 'T', '\n', '\004', 'T', 'y', 'p', 'e', + '\022', '\n', '\n', '\006', 'N', 'O', 'R', 'M', 'A', 'L', '\020', '\001', + '\022', '\013', '\n', '\007', 'U', 'N', 'K', 'N', 'O', 'W', 'N', '\020', + '\002', '\022', '\013', '\n', '\007', 'C', 'O', 'N', 'T', 'R', 'O', 'L', + '\020', '\003', '\022', '\020', '\n', '\014', 'U', 'S', 'E', 'R', '_', 'D', + 'E', 'F', 'I', 'N', 'E', 'D', '\020', '\004', '\022', '\010', '\n', '\004', + 'B', 'Y', 'T', 'E', '\020', '\006', '\022', '\n', '\n', '\006', 'U', 'N', + 'U', 'S', 'E', 'D', '\020', '\005', '*', '\t', '\010', '\310', '\001', '\020', + '\200', '\200', '\200', '\200', '\002', '*', '\t', '\010', '\310', '\001', '\020', '\200', + '\200', '\200', '\200', '\002', 'B', '\002', 'H', '\003', +}; + +static _upb_DefPool_Init *deps[1] = { + NULL, +}; + +_upb_DefPool_Init sentencepiece_model_proto_upbdefinit = { + deps, + &sentencepiece_model_proto_upb_file_layout, + "sentencepiece_model.proto", + UPB_STRINGVIEW_INIT(descriptor, sizeof(descriptor)), +}; diff --git a/src/builtin_upb/sentencepiece_model.upbdefs.h b/src/builtin_upb/sentencepiece_model.upbdefs.h new file mode 100644 index 000000000..ab6c4148f --- /dev/null +++ b/src/builtin_upb/sentencepiece_model.upbdefs.h @@ -0,0 +1,62 @@ +/* This file was generated by upb_generator from the input file: + * + * sentencepiece_model.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + + + +#ifndef SENTENCEPIECE_MODEL_PROTO_UPB_H__UPBDEFS_H_ +#define SENTENCEPIECE_MODEL_PROTO_UPB_H__UPBDEFS_H_ + +#include "upb/reflection/def.h" +#include "upb/reflection/internal/def_pool.h" + +#include "upb/port/def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +extern _upb_DefPool_Init sentencepiece_model_proto_upbdefinit; + +UPB_INLINE const upb_MessageDef *sentencepiece_TrainerSpec_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_model_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.TrainerSpec"); +} + +UPB_INLINE const upb_MessageDef *sentencepiece_NormalizerSpec_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_model_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.NormalizerSpec"); +} + +UPB_INLINE const upb_MessageDef *sentencepiece_SelfTestData_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_model_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.SelfTestData"); +} + +UPB_INLINE const upb_MessageDef *sentencepiece_SelfTestData_Sample_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_model_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.SelfTestData.Sample"); +} + +UPB_INLINE const upb_MessageDef *sentencepiece_ModelProto_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_model_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.ModelProto"); +} + +UPB_INLINE const upb_MessageDef *sentencepiece_ModelProto_SentencePiece_getmsgdef(upb_DefPool *s) { + _upb_DefPool_LoadDefInit(s, &sentencepiece_model_proto_upbdefinit); + return upb_DefPool_FindMessageByName(s, "sentencepiece.ModelProto.SentencePiece"); +} + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port/undef.inc" + +#endif /* SENTENCEPIECE_MODEL_PROTO_UPB_H__UPBDEFS_H_ */ diff --git a/src/builtin_upb/upb_wrapper.h b/src/builtin_upb/upb_wrapper.h new file mode 100644 index 000000000..7e964ec05 --- /dev/null +++ b/src/builtin_upb/upb_wrapper.h @@ -0,0 +1,1686 @@ +// Copyright 2016 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef CORE_UPB_WRAPPER_H_ +#define CORE_UPB_WRAPPER_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "third_party/absl/strings/string_view.h" +#include "third_party/absl/types/span.h" + +// Include the upb generated headers +#include "sentencepiece_model.upb.h" +#include "sentencepiece.upb.h" + +namespace sentencepiece { +class ProtoStr : public absl::string_view { + public: + ProtoStr(absl::string_view sv) : absl::string_view(sv) {} + ProtoStr(const char* s) : absl::string_view(s ? s : "") {} + ProtoStr(upb_StringView sv) : absl::string_view(sv.data, sv.size) {} + + operator std::string() const { return std::string(*this); } + bool operator==(const std::string& other) const { return absl::string_view(*this) == other; } + friend bool operator==(const std::string& a, const ProtoStr& b) { return a == absl::string_view(b); } +}; + +class TrainerSpec; +class NormalizerSpec; +class SelfTestData; +class SentencePieceText; +class NBestSentencePieceText; +class ModelProto; +class NBestSentencePieceText_Sub; + +namespace upb { +class ModelProtoWrapper; +} // namespace upb + +class ModelProto_SentencePiece { + public: + enum Type { + NORMAL = sentencepiece_ModelProto_SentencePiece_NORMAL, + UNKNOWN = sentencepiece_ModelProto_SentencePiece_UNKNOWN, + CONTROL = sentencepiece_ModelProto_SentencePiece_CONTROL, + USER_DEFINED = sentencepiece_ModelProto_SentencePiece_USER_DEFINED, + BYTE = sentencepiece_ModelProto_SentencePiece_BYTE, + UNUSED = sentencepiece_ModelProto_SentencePiece_UNUSED, + }; + + ModelProto_SentencePiece(upb::ModelProtoWrapper* parent, int index) + : parent_(parent), index_(index) {} + ModelProto_SentencePiece() : parent_(nullptr), index_(-1) {} + + inline ProtoStr piece() const; + inline float score() const; + inline Type type() const; + inline void set_type(Type type); + inline void set_piece(absl::string_view piece); + inline void set_piece(const char* data, size_t size) { + set_piece(absl::string_view(data, size)); + } + inline void set_score(float score); + private: + upb::ModelProtoWrapper* parent_; + int index_; +}; + +namespace upb { + +#define DEFINE_UPB_SERIALIZATION_METHODS(ClassName, CPrefix) \ + std::string SerializeAsString() const { \ + if (!msg_) return ""; \ + size_t len; \ + upb_Arena* tmp_arena = upb_Arena_New(); \ + char* ptr = CPrefix##_serialize(msg_, tmp_arena, &len); \ + std::string ret; \ + if (ptr) { \ + ret.assign(ptr, len); \ + } \ + upb_Arena_Free(tmp_arena); \ + return ret; \ + } \ + bool ParseFromArray(const void* data, int size) { \ + if (owns_msg_ && arena_) { \ + upb_Arena_Free(arena_); \ + } \ + arena_ = upb_Arena_New(); \ + owns_msg_ = true; \ + msg_ = CPrefix##_parse(reinterpret_cast(data), size, arena_); \ + OnArenaReset(); \ + return msg_ != nullptr; \ + } \ + bool ParseFromString(const std::string& bytes) { \ + return ParseFromArray(bytes.data(), bytes.size()); \ + } \ + void CopyFrom(const ClassName& other) { \ + std::string bytes = other.SerializeAsString(); \ + ParseFromString(bytes); \ + } \ + void Clear() { \ + if (owns_msg_ && arena_) { \ + upb_Arena_Free(arena_); \ + } \ + arena_ = upb_Arena_New(); \ + owns_msg_ = true; \ + msg_ = CPrefix##_new(arena_); \ + OnArenaReset(); \ + } + +class SentencePieceTextWrapper; +} // namespace upb + +class SentencePieceText_SentencePiece { + public: + SentencePieceText_SentencePiece(upb::SentencePieceTextWrapper* parent, int index) + : parent_(parent), index_(index) {} + SentencePieceText_SentencePiece() : parent_(nullptr), index_(-1) {} + + inline absl::string_view piece() const; + inline void set_piece(absl::string_view piece); + inline void set_piece(const char* data, size_t size); + + inline uint32_t id() const; + inline void set_id(uint32_t id); + + inline absl::string_view surface() const; + inline void set_surface(absl::string_view surface); + inline void set_surface(const char* data, size_t size); + + inline uint32_t begin() const; + inline void set_begin(uint32_t begin); + + inline uint32_t end() const; + inline void set_end(uint32_t end); + + friend void swap(SentencePieceText_SentencePiece a, SentencePieceText_SentencePiece b); + + static const SentencePieceText_SentencePiece& default_instance() { + static SentencePieceText_SentencePiece instance(nullptr, -1); + return instance; + } + private: + upb::SentencePieceTextWrapper* parent_; + int index_; +}; + +namespace upb { + +// Forward declarations +class ModelProtoWrapper; +class SentencePieceTextWrapper; +class NBestSentencePieceTextWrapper; +class SelfTestDataWrapper; +class SelfTestData_SampleWrapper; + + + +class RepeatedStringWrapper { + public: + RepeatedStringWrapper(const upb_StringView* arr, size_t size) : arr_(arr), size_(size) {} + + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = ProtoStr; + using difference_type = std::ptrdiff_t; + using pointer = ProtoStr*; + using reference = ProtoStr; + + Iterator(const upb_StringView* arr, size_t index) : arr_(arr), index_(index) {} + Iterator& operator++() { ++index_; return *this; } + Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } + bool operator==(const Iterator& other) const { return index_ == other.index_; } + bool operator!=(const Iterator& other) const { return index_ != other.index_; } + ProtoStr operator*() const { + return ProtoStr(arr_[index_]); + } + private: + const upb_StringView* arr_; + size_t index_; + }; + + Iterator begin() const { return Iterator(arr_, 0); } + Iterator end() const { return Iterator(arr_, size_); } + size_t size() const { return size_; } + bool empty() const { return size_ == 0; } + ProtoStr operator[](size_t index) const { return ProtoStr(arr_[index]); } + + private: + const upb_StringView* arr_; + size_t size_; +}; + +#define DEFINE_UPB_PRIMITIVE_ACCESSOR(FieldName, Type, DefaultVal, UpbPrefix) \ + Type FieldName() const { return msg_ ? UpbPrefix##_##FieldName(msg_) : DefaultVal; } \ + void set_##FieldName(Type val) { \ + if (msg_) { \ + UpbPrefix##_set_##FieldName(msg_, val); \ + } \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + } \ + } + +#define DEFINE_UPB_STRING_ACCESSOR(FieldName, DefaultVal, UpbPrefix) \ + ProtoStr FieldName() const { \ + if (!msg_ || !UpbPrefix##_has_##FieldName(msg_)) return DefaultVal; \ + return UpbPrefix##_##FieldName(msg_); \ + } \ + void set_##FieldName(absl::string_view val) { \ + if (msg_ && arena_) { \ + upb_StringView sv; \ + char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); \ + memcpy(ptr, val.data(), val.size()); \ + sv.data = ptr; \ + sv.size = val.size(); \ + UpbPrefix##_set_##FieldName(msg_, sv); \ + } \ + } \ + void set_##FieldName(const char* data, size_t size) { \ + set_##FieldName(absl::string_view(data, size)); \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + } \ + } + +#define DEFINE_UPB_HAS_FIELD_ACCESSOR(FieldName, UpbPrefix) \ + bool has_##FieldName() const { return msg_ && UpbPrefix##_has_##FieldName(msg_); } + +#define DEFINE_UPB_ENUM_ACCESSOR(FieldName, EnumType, DefaultVal, UpbPrefix) \ + EnumType FieldName() const { \ + return msg_ ? static_cast(UpbPrefix##_##FieldName(msg_)) : DefaultVal; \ + } \ + void set_##FieldName(EnumType type) { \ + if (msg_) { \ + UpbPrefix##_set_##FieldName(msg_, static_cast(type)); \ + } \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + } \ + } + +#define DEFINE_UPB_REPEATED_STRING_ACCESSOR(FieldName, UpbPrefix) \ + int FieldName##_size() const { \ + if (!msg_) return 0; \ + size_t size; \ + UpbPrefix##_##FieldName(msg_, &size); \ + return size; \ + } \ + ProtoStr FieldName(int index) const { \ + if (!msg_) return ""; \ + size_t size; \ + const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ + if (index < 0 || static_cast(index) >= size) return ""; \ + return arr[index]; \ + } \ + RepeatedStringWrapper FieldName() const { \ + if (!msg_) return RepeatedStringWrapper(nullptr, 0); \ + size_t size; \ + const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ + return RepeatedStringWrapper(arr, size); \ + } \ + void add_##FieldName(const std::string& val) { \ + if (msg_ && arena_) { \ + upb_StringView sv; \ + char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); \ + memcpy(ptr, val.data(), val.size()); \ + sv.data = ptr; \ + sv.size = val.size(); \ + UpbPrefix##_add_##FieldName(msg_, sv, arena_); \ + } \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + } \ + } + +class TrainerSpecWrapper { + friend class upb::ModelProtoWrapper; + public: + TrainerSpecWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { + msg_ = sentencepiece_TrainerSpec_new(arena_); + } + explicit TrainerSpecWrapper(const sentencepiece_TrainerSpec* msg, upb_Arena* arena = nullptr) + : msg_(const_cast(msg)), arena_(arena), owns_msg_(false) {} + + TrainerSpecWrapper(const sentencepiece_TrainerSpec* msg, upb_Arena* arena, + std::function on_change) + : msg_(const_cast(msg)), arena_(arena), owns_msg_(false), on_change_(on_change) {} + + TrainerSpecWrapper(const TrainerSpecWrapper& other) : arena_(upb_Arena_New()), owns_msg_(true) { + msg_ = sentencepiece_TrainerSpec_new(arena_); + CopyFrom(other); + } + + TrainerSpecWrapper& operator=(const TrainerSpecWrapper& other) { + if (this != &other) { + if (owns_msg_) { + if (arena_) upb_Arena_Free(arena_); + arena_ = upb_Arena_New(); + owns_msg_ = true; + CopyFrom(other); + } else { + if (other.msg_) { + size_t size = 0; + upb_Arena* tmp_arena = upb_Arena_New(); + char* buf = sentencepiece_TrainerSpec_serialize(other.msg_, tmp_arena, &size); + if (buf) { + msg_ = sentencepiece_TrainerSpec_parse(buf, size, arena_); + if (on_change_) on_change_(msg_); + } + upb_Arena_Free(tmp_arena); + } else { + msg_ = nullptr; + if (on_change_) on_change_(msg_); + } + } + } + return *this; + } + + virtual ~TrainerSpecWrapper() { + if (owns_msg_ && arena_) { + upb_Arena_Free(arena_); + } + } + + DEFINE_UPB_SERIALIZATION_METHODS(TrainerSpecWrapper, sentencepiece_TrainerSpec) + + void OnArenaReset() {} + + enum ModelType { + UNIGRAM = sentencepiece_TrainerSpec_UNIGRAM, + BPE = sentencepiece_TrainerSpec_BPE, + WORD = sentencepiece_TrainerSpec_WORD, + CHAR = sentencepiece_TrainerSpec_CHAR, + }; + + DEFINE_UPB_REPEATED_STRING_ACCESSOR(input, sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(input_format, "", sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(model_prefix, "", sentencepiece_TrainerSpec) + DEFINE_UPB_ENUM_ACCESSOR(model_type, ModelType, UNIGRAM, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(vocab_size, int32_t, 8000, sentencepiece_TrainerSpec) + DEFINE_UPB_REPEATED_STRING_ACCESSOR(accept_language, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(self_test_sample_size, int32_t, 0, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(enable_differential_privacy, bool, false, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(differential_privacy_noise_level, float, 0.0f, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(differential_privacy_clipping_threshold, uint64_t, 0, sentencepiece_TrainerSpec) + + DEFINE_UPB_PRIMITIVE_ACCESSOR(character_coverage, float, 0.9995f, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(input_sentence_size, uint64_t, 0, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(shuffle_input_sentence, bool, true, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(seed_sentencepiece_size, int32_t, 1000000, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(shrinking_factor, float, 0.75f, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(max_sentence_length, int32_t, 4192, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(num_threads, int32_t, 16, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(num_sub_iterations, int32_t, 2, sentencepiece_TrainerSpec) + + DEFINE_UPB_PRIMITIVE_ACCESSOR(max_sentencepiece_length, int32_t, 16, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_unicode_script, bool, true, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_number, bool, true, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_whitespace, bool, true, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(treat_whitespace_as_suffix, bool, false, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(allow_whitespace_only_pieces, bool, false, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_digits, bool, false, sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(pretokenization_delimiter, "", sentencepiece_TrainerSpec) + + DEFINE_UPB_REPEATED_STRING_ACCESSOR(control_symbols, sentencepiece_TrainerSpec) + DEFINE_UPB_REPEATED_STRING_ACCESSOR(user_defined_symbols, sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(required_chars, "", sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(byte_fallback, bool, false, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(vocabulary_output_piece_score, bool, true, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(hard_vocab_limit, bool, true, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(use_all_vocab, bool, false, sentencepiece_TrainerSpec) + + DEFINE_UPB_PRIMITIVE_ACCESSOR(unk_id, int32_t, 0, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(bos_id, int32_t, 1, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(eos_id, int32_t, 2, sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(pad_id, int32_t, -1, sentencepiece_TrainerSpec) + + DEFINE_UPB_STRING_ACCESSOR(unk_piece, "", sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(bos_piece, "", sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(eos_piece, "", sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(pad_piece, "", sentencepiece_TrainerSpec) + + DEFINE_UPB_HAS_FIELD_ACCESSOR(unk_surface, sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(unk_surface, " \xE2\x81\x87 ", sentencepiece_TrainerSpec) + + DEFINE_UPB_PRIMITIVE_ACCESSOR(train_extremely_large_corpus, bool, false, sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(seed_sentencepieces_file, "", sentencepiece_TrainerSpec) + + private: + sentencepiece_TrainerSpec* msg_; + upb_Arena* arena_; + bool owns_msg_ = false; + std::function on_change_; +}; + +class NormalizerSpecWrapper { + friend class upb::ModelProtoWrapper; + public: + NormalizerSpecWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { + msg_ = sentencepiece_NormalizerSpec_new(arena_); + } + explicit NormalizerSpecWrapper(const sentencepiece_NormalizerSpec* msg, upb_Arena* arena = nullptr) + : msg_(const_cast(msg)), arena_(arena), owns_msg_(false) {} + + NormalizerSpecWrapper(const sentencepiece_NormalizerSpec* msg, upb_Arena* arena, + std::function on_change) + : msg_(const_cast(msg)), arena_(arena), owns_msg_(false), on_change_(on_change) {} + + NormalizerSpecWrapper(const NormalizerSpecWrapper& other) : arena_(upb_Arena_New()), owns_msg_(true) { + msg_ = sentencepiece_NormalizerSpec_new(arena_); + CopyFrom(other); + } + + NormalizerSpecWrapper& operator=(const NormalizerSpecWrapper& other) { + if (this != &other) { + if (owns_msg_) { + if (arena_) upb_Arena_Free(arena_); + arena_ = upb_Arena_New(); + owns_msg_ = true; + CopyFrom(other); + } else { + if (other.msg_) { + size_t size = 0; + upb_Arena* tmp_arena = upb_Arena_New(); + char* buf = sentencepiece_NormalizerSpec_serialize(other.msg_, tmp_arena, &size); + if (buf) { + msg_ = sentencepiece_NormalizerSpec_parse(buf, size, arena_); + if (on_change_) on_change_(msg_); + } + upb_Arena_Free(tmp_arena); + } else { + msg_ = nullptr; + if (on_change_) on_change_(msg_); + } + } + } + return *this; + } + + virtual ~NormalizerSpecWrapper() { + if (owns_msg_ && arena_) { + upb_Arena_Free(arena_); + } + } + + DEFINE_UPB_SERIALIZATION_METHODS(NormalizerSpecWrapper, sentencepiece_NormalizerSpec) + + void OnArenaReset() {} + + DEFINE_UPB_STRING_ACCESSOR(name, "", sentencepiece_NormalizerSpec) + DEFINE_UPB_STRING_ACCESSOR(precompiled_charsmap, "", sentencepiece_NormalizerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(add_dummy_prefix, bool, true, sentencepiece_NormalizerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(remove_extra_whitespaces, bool, true, sentencepiece_NormalizerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(escape_whitespaces, bool, true, sentencepiece_NormalizerSpec) + DEFINE_UPB_STRING_ACCESSOR(normalization_rule_tsv, "", sentencepiece_NormalizerSpec) + + private: + sentencepiece_NormalizerSpec* msg_; + upb_Arena* arena_; + bool owns_msg_ = false; + std::function on_change_; +}; + +class SelfTestData_SampleWrapper { + public: + explicit SelfTestData_SampleWrapper(const sentencepiece_SelfTestData_Sample* msg) + : msg_(msg), mutable_msg_(nullptr), arena_(nullptr) {} + SelfTestData_SampleWrapper(sentencepiece_SelfTestData_Sample* msg, upb_Arena* arena) + : msg_(msg), mutable_msg_(msg), arena_(arena) {} + ProtoStr input() const { + if (!msg_ || !sentencepiece_SelfTestData_Sample_has_input(msg_)) return ""; + return sentencepiece_SelfTestData_Sample_input(msg_); + } + ProtoStr expected() const { + if (!msg_ || !sentencepiece_SelfTestData_Sample_has_expected(msg_)) return ""; + return sentencepiece_SelfTestData_Sample_expected(msg_); + } + void set_input(const std::string& val) { + if (mutable_msg_ && arena_) { + upb_StringView sv; + char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); + memcpy(ptr, val.data(), val.size()); + sv.data = ptr; + sv.size = val.size(); + sentencepiece_SelfTestData_Sample_set_input(mutable_msg_, sv); + } + } + void set_expected(const std::string& val) { + if (mutable_msg_ && arena_) { + upb_StringView sv; + char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); + memcpy(ptr, val.data(), val.size()); + sv.data = ptr; + sv.size = val.size(); + sentencepiece_SelfTestData_Sample_set_expected(mutable_msg_, sv); + } + } + private: + const sentencepiece_SelfTestData_Sample* msg_; + sentencepiece_SelfTestData_Sample* mutable_msg_; + upb_Arena* arena_; +}; + +class SelfTestDataWrapper { + public: + explicit SelfTestDataWrapper(const sentencepiece_SelfTestData* msg) + : msg_(msg), mutable_msg_(nullptr), arena_(nullptr) {} + SelfTestDataWrapper(sentencepiece_SelfTestData* msg, upb_Arena* arena) + : msg_(msg), mutable_msg_(msg), arena_(arena) {} + + class SamplesRepeatedWrapper { + public: + SamplesRepeatedWrapper(const sentencepiece_SelfTestData_Sample* const* elements, size_t count) + : elements_(elements), count_(count) {} + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = SelfTestData_SampleWrapper; + using difference_type = std::ptrdiff_t; + using pointer = SelfTestData_SampleWrapper*; + using reference = SelfTestData_SampleWrapper; + + Iterator(const sentencepiece_SelfTestData_Sample* const* elements, size_t index) : elements_(elements), index_(index) {} + Iterator& operator++() { ++index_; return *this; } + Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } + bool operator==(const Iterator& other) const { return index_ == other.index_; } + bool operator!=(const Iterator& other) const { return index_ != other.index_; } + SelfTestData_SampleWrapper operator*() const { + return SelfTestData_SampleWrapper(elements_[index_]); + } + private: + const sentencepiece_SelfTestData_Sample* const* elements_; + size_t index_; + }; + + Iterator begin() const { return Iterator(elements_, 0); } + Iterator end() const { return Iterator(elements_, count_); } + size_t size() const { return count_; } + bool empty() const { return count_ == 0; } + SelfTestData_SampleWrapper operator[](size_t index) const { + return SelfTestData_SampleWrapper(elements_[index]); + } + private: + const sentencepiece_SelfTestData_Sample* const* elements_; + size_t count_; + }; + + SamplesRepeatedWrapper samples() const { + size_t size = 0; + const sentencepiece_SelfTestData_Sample* const* elements = nullptr; + if (msg_) { + elements = sentencepiece_SelfTestData_samples(msg_, &size); + } + return SamplesRepeatedWrapper(elements, size); + } + int samples_size() const { + return samples().size(); + } + SelfTestData_SampleWrapper* add_samples() { + if (mutable_msg_ && arena_) { + sentencepiece_SelfTestData_add_samples(mutable_msg_, arena_); + int new_index = samples_size() - 1; + sample_wrappers_.resize(samples_size()); + size_t size; + sentencepiece_SelfTestData_Sample** samples = sentencepiece_SelfTestData_mutable_samples(mutable_msg_, &size); + sample_wrappers_[new_index] = std::make_unique(samples[new_index], arena_); + return sample_wrappers_[new_index].get(); + } + return nullptr; + } + private: + const sentencepiece_SelfTestData* msg_; + sentencepiece_SelfTestData* mutable_msg_; + upb_Arena* arena_; + mutable std::vector> sample_wrappers_; +}; + +class ModelProtoWrapper { + public: + ModelProtoWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { + msg_ = sentencepiece_ModelProto_new(arena_); + } + + explicit ModelProtoWrapper(const sentencepiece_ModelProto* msg, upb_Arena* arena) + : msg_(const_cast(msg)), arena_(arena), owns_msg_(false) {} + + virtual ~ModelProtoWrapper() { + if (owns_msg_ && arena_) { + upb_Arena_Free(arena_); + } + } + + ModelProtoWrapper(const ModelProtoWrapper& other) : ModelProtoWrapper() { + CopyFrom(other); + } + ModelProtoWrapper& operator=(const ModelProtoWrapper& other) { + if (this != &other) { + Clear(); + CopyFrom(other); + } + return *this; + } + + DEFINE_UPB_SERIALIZATION_METHODS(ModelProtoWrapper, sentencepiece_ModelProto) + + void OnArenaReset() { + piece_wrappers_.clear(); + mutable_pieces_wrapper_.reset(); + mutable_normalizer_spec_.reset(); + mutable_trainer_spec_.reset(); + normalizer_spec_cache_.reset(); + denormalizer_spec_cache_.reset(); + mutable_denormalizer_spec_.reset(); + mutable_self_test_data_.reset(); + } + + bool ParseFromIstream(std::istream* input) { + std::string bytes((std::istreambuf_iterator(*input)), + std::istreambuf_iterator()); + return ParseFromString(bytes); + } + + int pieces_size() const { + if (!msg_) return 0; + size_t size = 0; + sentencepiece_ModelProto_pieces(msg_, &size); + return size; + } + + inline ::sentencepiece::ModelProto_SentencePiece pieces(int index) const { + return ::sentencepiece::ModelProto_SentencePiece(const_cast(this), index); + } + inline ::sentencepiece::ModelProto_SentencePiece pieces(int index) { + return ::sentencepiece::ModelProto_SentencePiece(this, index); + } + + class PiecesRepeatedWrapper { + public: + PiecesRepeatedWrapper(const ModelProtoWrapper* parent) : parent_(parent) {} + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = ModelProto_SentencePiece; + using difference_type = std::ptrdiff_t; + using pointer = ModelProto_SentencePiece*; + using reference = ModelProto_SentencePiece; + + Iterator(const ModelProtoWrapper* parent, int index) : parent_(parent), index_(index) {} + ModelProto_SentencePiece operator*() const { + return ModelProto_SentencePiece(const_cast(parent_), index_); + } + Iterator& operator++() { ++index_; return *this; } + bool operator!=(const Iterator& other) const { return index_ != other.index_; } + private: + const ModelProtoWrapper* parent_; + int index_; + }; + Iterator begin() const { return Iterator(parent_, 0); } + Iterator end() const { return Iterator(parent_, parent_->pieces_size()); } + int size() const { return parent_->pieces_size(); } + private: + const ModelProtoWrapper* parent_; + }; + + PiecesRepeatedWrapper pieces() const { + return PiecesRepeatedWrapper(this); + } + + class MutablePiecesRepeatedWrapper { + public: + MutablePiecesRepeatedWrapper(ModelProtoWrapper* parent) : parent_(parent) {} + class Iterator { + public: + using iterator_category = std::random_access_iterator_tag; + using value_type = ModelProto_SentencePiece; + using difference_type = std::ptrdiff_t; + using pointer = ModelProto_SentencePiece*; + using reference = ModelProto_SentencePiece&; + + Iterator(ModelProtoWrapper* parent, int index) : parent_(parent), index_(index) {} + ModelProto_SentencePiece& operator*() const { + return *(parent_->mutable_pieces(index_)); + } + Iterator& operator++() { ++index_; return *this; } + Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } + Iterator& operator--() { --index_; return *this; } + Iterator operator--(int) { Iterator tmp = *this; --index_; return tmp; } + + Iterator& operator+=(difference_type n) { index_ += n; return *this; } + Iterator& operator-=(difference_type n) { index_ -= n; return *this; } + + friend Iterator operator+(Iterator it, difference_type n) { it += n; return it; } + friend Iterator operator+(difference_type n, Iterator it) { it += n; return it; } + friend Iterator operator-(Iterator it, difference_type n) { it -= n; return it; } + friend difference_type operator-(const Iterator& a, const Iterator& b) { return a.index_ - b.index_; } + + ModelProto_SentencePiece& operator[](difference_type n) const { return *(parent_->mutable_pieces(index_ + n)); } + + bool operator==(const Iterator& other) const { return index_ == other.index_ && parent_ == other.parent_; } + bool operator!=(const Iterator& other) const { return !(*this == other); } + bool operator<(const Iterator& other) const { return index_ < other.index_; } + bool operator>(const Iterator& other) const { return index_ > other.index_; } + bool operator<=(const Iterator& other) const { return index_ <= other.index_; } + bool operator>=(const Iterator& other) const { return index_ >= other.index_; } + private: + ModelProtoWrapper* parent_; + int index_; + }; + Iterator begin() { return Iterator(parent_, 0); } + Iterator end() { return Iterator(parent_, parent_->pieces_size()); } + int size() const { return parent_->pieces_size(); } + ModelProto_SentencePiece* Mutable(int index) { return parent_->mutable_pieces(index); } + private: + ModelProtoWrapper* parent_; + }; + + MutablePiecesRepeatedWrapper* mutable_pieces() { + if (!mutable_pieces_wrapper_) { + mutable_pieces_wrapper_ = std::make_unique(this); + } + return mutable_pieces_wrapper_.get(); + } + + inline ModelProto_SentencePiece* mutable_pieces(int index) { + LazyInitPieceWrappersCache(); + return piece_wrappers_[index].get(); + } + + ProtoStr piece_at(int index) const { + size_t size = 0; + const sentencepiece_ModelProto_SentencePiece* const* pieces = sentencepiece_ModelProto_pieces(msg_, &size); + if (index < 0 || static_cast(index) >= size) return ""; + return ProtoStr(sentencepiece_ModelProto_SentencePiece_piece(pieces[index])); + } + float score_at(int index) const { + size_t size = 0; + const sentencepiece_ModelProto_SentencePiece* const* pieces = sentencepiece_ModelProto_pieces(msg_, &size); + if (index < 0 || static_cast(index) >= size) return 0.0f; + return sentencepiece_ModelProto_SentencePiece_score(pieces[index]); + } + int type_at(int index) const { + size_t size = 0; + const sentencepiece_ModelProto_SentencePiece* const* pieces = sentencepiece_ModelProto_pieces(msg_, &size); + if (index < 0 || static_cast(index) >= size) return 0; + return sentencepiece_ModelProto_SentencePiece_type(pieces[index]); + } + void set_type_at(int index, int type) { + if (msg_) { + size_t size = 0; + sentencepiece_ModelProto_SentencePiece** pieces = sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_SentencePiece_set_type(pieces[index], static_cast(type)); + } + } + void set_piece_at(int index, absl::string_view piece) { + if (msg_) { + size_t size = 0; + sentencepiece_ModelProto_SentencePiece** pieces = sentencepiece_ModelProto_mutable_pieces(msg_, &size); + upb_StringView sv; + char* ptr = (char*)upb_Arena_Malloc(arena_, piece.size()); + memcpy(ptr, piece.data(), piece.size()); + sv.data = ptr; + sv.size = piece.size(); + sentencepiece_ModelProto_SentencePiece_set_piece(pieces[index], sv); + } + } + void set_piece_at(int index, const std::string& piece) { + set_piece_at(index, absl::string_view(piece)); + } + void set_score_at(int index, float score) { + if (msg_) { + size_t size = 0; + sentencepiece_ModelProto_SentencePiece** pieces = sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_SentencePiece_set_score(pieces[index], score); + } + } + + inline bool has_trainer_spec() const { + return msg_ && sentencepiece_ModelProto_has_trainer_spec(msg_); + } + inline TrainerSpec trainer_spec() const; + inline ::sentencepiece::TrainerSpec* mutable_trainer_spec() const; + + inline bool has_normalizer_spec() const { + return msg_ && sentencepiece_ModelProto_has_normalizer_spec(msg_); + } + inline const NormalizerSpec& normalizer_spec() const; + inline ::sentencepiece::NormalizerSpec* mutable_normalizer_spec() const; + + inline bool has_denormalizer_spec() const { + return msg_ && sentencepiece_ModelProto_has_denormalizer_spec(msg_); + } + inline const NormalizerSpec& denormalizer_spec() const; + inline ::sentencepiece::NormalizerSpec* mutable_denormalizer_spec() const; + inline void set_trainer_spec(const TrainerSpec& spec); + inline void set_normalizer_spec(const NormalizerSpec& spec); + inline void set_denormalizer_spec(const NormalizerSpec& spec); + + inline bool has_self_test_data() const { + return msg_ && sentencepiece_ModelProto_has_self_test_data(msg_); + } + inline SelfTestData self_test_data() const; + inline ::sentencepiece::SelfTestData* mutable_self_test_data() const; + + protected: + sentencepiece_ModelProto* msg_; + upb_Arena* arena_; + bool owns_msg_; + + std::vector> piece_wrappers_; + std::unique_ptr mutable_pieces_wrapper_; + mutable std::unique_ptr mutable_normalizer_spec_; + mutable std::unique_ptr mutable_trainer_spec_; + mutable std::unique_ptr normalizer_spec_cache_; + mutable std::unique_ptr denormalizer_spec_cache_; + mutable std::unique_ptr mutable_denormalizer_spec_; + mutable std::unique_ptr mutable_self_test_data_; + + private: + void LazyInitPieceWrappersCache() { + if (piece_wrappers_.size() != pieces_size()) { + piece_wrappers_.resize(pieces_size()); + for (int i = 0; i < pieces_size(); ++i) { + if (!piece_wrappers_[i]) { + piece_wrappers_[i] = std::make_unique(this, i); + } + } + } + } +}; + +class SentencePieceTextWrapper { + public: + SentencePieceTextWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { + msg_ = sentencepiece_SentencePieceText_new(arena_); + } + explicit SentencePieceTextWrapper(std::nullptr_t) : msg_(nullptr), arena_(nullptr), owns_msg_(false) {} + + virtual ~SentencePieceTextWrapper() { + if (owns_msg_ && arena_) { + upb_Arena_Free(arena_); + } + } + + SentencePieceTextWrapper(const SentencePieceTextWrapper& other) : SentencePieceTextWrapper() { + CopyFrom(other); + } + SentencePieceTextWrapper& operator=(const SentencePieceTextWrapper& other) { + if (this != &other) { + Clear(); + CopyFrom(other); + } + return *this; + } + + DEFINE_UPB_SERIALIZATION_METHODS(SentencePieceTextWrapper, sentencepiece_SentencePieceText) + + void OnArenaReset() { + piece_wrappers_.clear(); + mutable_pieces_wrapper_.reset(); + } + + virtual sentencepiece_SentencePieceText* mutable_msg() { + return msg_; + } + virtual const sentencepiece_SentencePieceText* msg() const { + return msg_; + } + virtual upb_Arena* arena() { return arena_; } + virtual const upb_Arena* arena() const { return arena_; } + + void clear_pieces() { + if (mutable_msg()) { + sentencepiece_SentencePieceText_clear_pieces(mutable_msg()); + } + piece_wrappers_.clear(); + } + + absl::string_view text() const { + if (!msg()) { + return ""; + } + upb_StringView sv = sentencepiece_SentencePieceText_text(msg()); + return absl::string_view(sv.data, sv.size); + } + void set_text(absl::string_view text) { + upb_StringView sv; + char* ptr = (char*)upb_Arena_Malloc(arena(), text.size()); + memcpy(ptr, text.data(), text.size()); + sv.data = ptr; + sv.size = text.size(); + sentencepiece_SentencePieceText_set_text(mutable_msg(), sv); + } + void set_text(const char* data, size_t size) { + set_text(absl::string_view(data, size)); + } + + float score() const { return msg() ? sentencepiece_SentencePieceText_score(msg()) : 0.0; } + void set_score(float score) { sentencepiece_SentencePieceText_set_score(mutable_msg(), score); } + + int pieces_size() const { + const auto* m = msg(); + if (!m) return 0; + size_t size = 0; + sentencepiece_SentencePieceText_pieces(m, &size); + return size; + } + + const SentencePieceText_SentencePiece& pieces(int index) const { + LazyInitPieceWrappersCache(); + return *piece_wrappers_[index]; + } + + SentencePieceText_SentencePiece& pieces(int index) { + LazyInitPieceWrappersCache(); + return *piece_wrappers_[index]; + } + + void ReservePieces(int size) { + if (mutable_msg()) { + // Do nothing. upb doesn't have a simple "reserve capacity" API, + // and resize_pieces actually grows the array with uninitialized elements. + } + } + + SentencePieceText_SentencePiece* add_pieces() { + if (!mutable_msg()) return nullptr; + sentencepiece_SentencePieceText_add_pieces(mutable_msg(), arena()); + int index = pieces_size() - 1; + auto wrapper = std::make_unique(this, index); + SentencePieceText_SentencePiece* ptr = wrapper.get(); + piece_wrappers_.push_back(std::move(wrapper)); + return ptr; + } + + class ConstPiecesRepeatedWrapper { + public: + ConstPiecesRepeatedWrapper(const SentencePieceTextWrapper* parent) : parent_(parent) {} + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = SentencePieceText_SentencePiece; + using difference_type = std::ptrdiff_t; + using pointer = SentencePieceText_SentencePiece*; + using reference = SentencePieceText_SentencePiece&; + + Iterator(const SentencePieceTextWrapper* parent, int index) : parent_(parent), index_(index) {} + SentencePieceText_SentencePiece& operator*() const { + return *(const_cast(parent_)->mutable_pieces(index_)); + } + Iterator& operator++() { ++index_; return *this; } + bool operator!=(const Iterator& other) const { return index_ != other.index_; } + private: + const SentencePieceTextWrapper* parent_; + int index_; + }; + Iterator begin() const { return Iterator(parent_, 0); } + Iterator end() const { return Iterator(parent_, parent_->pieces_size()); } + int size() const { return parent_->pieces_size(); } + private: + const SentencePieceTextWrapper* parent_; + }; + + ConstPiecesRepeatedWrapper pieces() const { + return ConstPiecesRepeatedWrapper(this); + } + + class MutablePiecesRepeatedWrapper { + public: + MutablePiecesRepeatedWrapper(SentencePieceTextWrapper* parent) : parent_(parent) {} + class Iterator { + public: + using iterator_category = std::random_access_iterator_tag; + using value_type = SentencePieceText_SentencePiece; + using difference_type = std::ptrdiff_t; + using pointer = SentencePieceText_SentencePiece*; + using reference = SentencePieceText_SentencePiece&; + + Iterator(SentencePieceTextWrapper* parent, int index) : parent_(parent), index_(index) {} + Iterator() : parent_(nullptr), index_(-1) {} + + SentencePieceText_SentencePiece& operator*() const { + return *(parent_->mutable_pieces(index_)); + } + Iterator& operator++() { ++index_; return *this; } + Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } + Iterator& operator--() { --index_; return *this; } + Iterator operator--(int) { Iterator tmp = *this; --index_; return tmp; } + + Iterator& operator+=(difference_type n) { index_ += n; return *this; } + Iterator& operator-=(difference_type n) { index_ -= n; return *this; } + + friend Iterator operator+(Iterator it, difference_type n) { it += n; return it; } + friend Iterator operator+(difference_type n, Iterator it) { it += n; return it; } + friend Iterator operator-(Iterator it, difference_type n) { it -= n; return it; } + friend difference_type operator-(const Iterator& a, const Iterator& b) { return a.index_ - b.index_; } + + SentencePieceText_SentencePiece& operator[](difference_type n) const { return *(parent_->mutable_pieces(index_ + n)); } + + bool operator==(const Iterator& other) const { return index_ == other.index_ && parent_ == other.parent_; } + bool operator!=(const Iterator& other) const { return !(*this == other); } + bool operator<(const Iterator& other) const { return index_ < other.index_; } + bool operator>(const Iterator& other) const { return index_ > other.index_; } + bool operator<=(const Iterator& other) const { return index_ <= other.index_; } + bool operator>=(const Iterator& other) const { return index_ >= other.index_; } + private: + SentencePieceTextWrapper* parent_; + int index_; + }; + Iterator begin() { return Iterator(parent_, 0); } + Iterator end() { return Iterator(parent_, parent_->pieces_size()); } + int size() const { return parent_->pieces_size(); } + + SentencePieceText_SentencePiece* Add() { return parent_->add_pieces(); } + void SwapElements(int i, int j) { parent_->SwapElementsData(i, j); } + void Reserve(int size) { parent_->ReservePieces(size); } + SentencePieceText_SentencePiece* Mutable(int index) { return parent_->mutable_pieces(index); } + private: + SentencePieceTextWrapper* parent_; + }; + + MutablePiecesRepeatedWrapper* mutable_pieces() { + if (!mutable_pieces_wrapper_) { + mutable_pieces_wrapper_ = std::make_unique(this); + } + return mutable_pieces_wrapper_.get(); + } + + SentencePieceText_SentencePiece* mutable_pieces(int index) { + LazyInitPieceWrappersCache(); + return piece_wrappers_[index].get(); + } + + + + absl::string_view piece_at(int index) const { + size_t size = 0; + const auto* pieces = sentencepiece_SentencePieceText_pieces(msg(), &size); + if (index < 0 || index >= size) return ""; + upb_StringView sv = sentencepiece_SentencePieceText_SentencePiece_piece(pieces[index]); + return absl::string_view(sv.data, sv.size); + } + + void set_piece_at(int index, absl::string_view piece) { + size_t size = 0; + auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + upb_StringView sv; + char* ptr = (char*)upb_Arena_Malloc(arena(), piece.size()); + memcpy(ptr, piece.data(), piece.size()); + sv.data = ptr; + sv.size = piece.size(); + sentencepiece_SentencePieceText_SentencePiece_set_piece(pieces[index], sv); + } + + uint32_t id_at(int index) const { + size_t size = 0; + const auto* pieces = sentencepiece_SentencePieceText_pieces(msg(), &size); + if (index < 0 || static_cast(index) >= size) return 0; + return sentencepiece_SentencePieceText_SentencePiece_id(pieces[index]); + } + void set_id_at(int index, uint32_t id) { + size_t size = 0; + auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + sentencepiece_SentencePieceText_SentencePiece_set_id(pieces[index], id); + } + + absl::string_view surface_at(int index) const { + size_t size = 0; + const auto* pieces = sentencepiece_SentencePieceText_pieces(msg(), &size); + if (index < 0 || index >= size) return ""; + upb_StringView sv = sentencepiece_SentencePieceText_SentencePiece_surface(pieces[index]); + return absl::string_view(sv.data, sv.size); + } + void set_surface_at(int index, absl::string_view surface) { + size_t size = 0; + auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + upb_StringView sv; + char* ptr = (char*)upb_Arena_Malloc(arena(), surface.size()); + memcpy(ptr, surface.data(), surface.size()); + sv.data = ptr; + sv.size = surface.size(); + sentencepiece_SentencePieceText_SentencePiece_set_surface(pieces[index], sv); + } + + uint32_t begin_at(int index) const { + size_t size = 0; + const auto* pieces = sentencepiece_SentencePieceText_pieces(msg(), &size); + if (index < 0 || static_cast(index) >= size) return 0; + return sentencepiece_SentencePieceText_SentencePiece_begin(pieces[index]); + } + void set_begin_at(int index, uint32_t begin) { + size_t size = 0; + auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + sentencepiece_SentencePieceText_SentencePiece_set_begin(pieces[index], begin); + } + + uint32_t end_at(int index) const { + size_t size = 0; + const auto* pieces = sentencepiece_SentencePieceText_pieces(msg(), &size); + if (index < 0 || static_cast(index) >= size) return 0; + return sentencepiece_SentencePieceText_SentencePiece_end(pieces[index]); + } + void set_end_at(int index, uint32_t end) { + size_t size = 0; + auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + sentencepiece_SentencePieceText_SentencePiece_set_end(pieces[index], end); + } + + void SwapElementsData(int i, int j) { + size_t size = 0; + auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + std::swap(pieces[i], pieces[j]); + } + + void LazyInitPieceWrappersCache() const { + if (piece_wrappers_.size() != pieces_size()) { + piece_wrappers_.resize(pieces_size()); + for (int i = 0; i < pieces_size(); ++i) { + if (!piece_wrappers_[i]) { + piece_wrappers_[i] = std::make_unique( + const_cast(this), i); + } + } + } + } + + private: + + + + sentencepiece_SentencePieceText* msg_; + bool owns_msg_; + upb_Arena* arena_; + + mutable std::vector> piece_wrappers_; + std::unique_ptr mutable_pieces_wrapper_; +}; + +class NBestSentencePieceTextWrapper { + public: + NBestSentencePieceTextWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { + msg_ = sentencepiece_NBestSentencePieceText_new(arena_); + } + virtual ~NBestSentencePieceTextWrapper() { + if (owns_msg_ && arena_) { + upb_Arena_Free(arena_); + } + } + + NBestSentencePieceTextWrapper(const NBestSentencePieceTextWrapper& other) : NBestSentencePieceTextWrapper() { + CopyFrom(other); + } + NBestSentencePieceTextWrapper& operator=(const NBestSentencePieceTextWrapper& other) { + if (this != &other) { + Clear(); + CopyFrom(other); + } + return *this; + } + + DEFINE_UPB_SERIALIZATION_METHODS(NBestSentencePieceTextWrapper, sentencepiece_NBestSentencePieceText) + + void OnArenaReset() { + nbest_wrappers_.clear(); + } + + virtual sentencepiece_NBestSentencePieceText* mutable_msg() { return msg_; } + virtual const sentencepiece_NBestSentencePieceText* msg() const { return msg_; } + virtual upb_Arena* arena() { return arena_; } + virtual const upb_Arena* arena() const { return arena_; } + + int nbests_size() const { + if (!msg_) return 0; + size_t size = 0; + sentencepiece_NBestSentencePieceText_nbests(msg_, &size); + return size; + } + + void ReserveNbests(int size) { + if (msg_) { + sentencepiece_NBestSentencePieceText_resize_nbests(msg_, size, arena_); + } + } + + inline NBestSentencePieceText_Sub* add_nbests(); + inline const NBestSentencePieceText_Sub& nbests(int index) const; + + class ConstNbestsRepeatedWrapper { + public: + ConstNbestsRepeatedWrapper(const NBestSentencePieceTextWrapper* parent) : parent_(parent) {} + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = NBestSentencePieceText_Sub; + using difference_type = std::ptrdiff_t; + using pointer = const NBestSentencePieceText_Sub*; + using reference = const NBestSentencePieceText_Sub&; + + Iterator(const NBestSentencePieceTextWrapper* parent, int index) : parent_(parent), index_(index) {} + inline const NBestSentencePieceText_Sub& operator*() const; + Iterator& operator++() { ++index_; return *this; } + bool operator!=(const Iterator& other) const { return index_ != other.index_; } + private: + const NBestSentencePieceTextWrapper* parent_; + int index_; + }; + Iterator begin() const { return Iterator(parent_, 0); } + Iterator end() const { return Iterator(parent_, parent_->nbests_size()); } + int size() const { return parent_->nbests_size(); } + private: + const NBestSentencePieceTextWrapper* parent_; + }; + + ConstNbestsRepeatedWrapper nbests() const { + return ConstNbestsRepeatedWrapper(this); + } + + + + void LazyInitNbestWrappersCache() const { + if (nbest_wrappers_.size() != nbests_size()) { + nbest_wrappers_.resize(nbests_size()); + } + for (int i = 0; i < nbests_size(); ++i) { + if (!nbest_wrappers_[i]) { + nbest_wrappers_[i] = std::make_unique( + const_cast(this), i); + } + } + } + + protected: + sentencepiece_NBestSentencePieceText* msg_; + upb_Arena* arena_; + bool owns_msg_; + + mutable std::vector> nbest_wrappers_; + private: +}; + +} // namespace upb + + + +class TrainerSpec : public sentencepiece::upb::TrainerSpecWrapper { + public: + using TrainerSpecWrapper::TrainerSpecWrapper; + using TrainerSpecWrapper::ModelType; + using TrainerSpecWrapper::UNIGRAM; + using TrainerSpecWrapper::BPE; + using TrainerSpecWrapper::WORD; + using TrainerSpecWrapper::CHAR; + + static const TrainerSpec& default_instance() { + static TrainerSpec instance(nullptr); + return instance; + } +}; + +class NormalizerSpec : public sentencepiece::upb::NormalizerSpecWrapper { + public: + using NormalizerSpecWrapper::NormalizerSpecWrapper; + + static const NormalizerSpec& default_instance() { + static NormalizerSpec instance(nullptr); + return instance; + } +}; + +class SelfTestData : public sentencepiece::upb::SelfTestDataWrapper { + public: + using SelfTestDataWrapper::SelfTestDataWrapper; + + static const SelfTestData& default_instance() { + static SelfTestData instance(nullptr); + return instance; + } +}; + +class ModelProto : public sentencepiece::upb::ModelProtoWrapper { + public: + using ModelProtoWrapper::ModelProtoWrapper; + using ModelProtoWrapper::pieces; + using SentencePiece = ModelProto_SentencePiece; + + inline ModelProto_SentencePiece* add_pieces(); + + static const ModelProto& default_instance() { + static ModelProto instance(nullptr, nullptr); + return instance; + } +}; + +class SentencePieceText : public sentencepiece::upb::SentencePieceTextWrapper { + public: + using SentencePieceTextWrapper::SentencePieceTextWrapper; + using SentencePiece = SentencePieceText_SentencePiece; + + static const SentencePieceText& default_instance() { + static SentencePieceText instance(nullptr); + return instance; + } +}; + +class NBestSentencePieceText; + +class NBestSentencePieceText_Sub : public SentencePieceText { + public: + NBestSentencePieceText_Sub(upb::NBestSentencePieceTextWrapper* parent, int index) + : SentencePieceText(nullptr), parent_(parent), index_(index) {} + + inline sentencepiece_SentencePieceText* mutable_msg() override; + inline const sentencepiece_SentencePieceText* msg() const override; + inline upb_Arena* arena() override; + inline const upb_Arena* arena() const override; + private: + upb::NBestSentencePieceTextWrapper* parent_; + int index_; +}; + +class NBestSentencePieceText : public sentencepiece::upb::NBestSentencePieceTextWrapper { + public: + using NBestSentencePieceTextWrapper::NBestSentencePieceTextWrapper; + using NBestSentencePieceTextWrapper::nbests; + using SentencePieceTextWrapperSub = NBestSentencePieceText_Sub; + + inline const NBestSentencePieceText_Sub& nbests(int index) const; + inline NBestSentencePieceText_Sub* add_nbests(); + + class MutableNbestsRepeatedWrapper { + public: + MutableNbestsRepeatedWrapper(NBestSentencePieceText* parent) : parent_(parent) {} + + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = NBestSentencePieceText_Sub; + using difference_type = std::ptrdiff_t; + using pointer = NBestSentencePieceText_Sub*; + using reference = NBestSentencePieceText_Sub&; + + Iterator(NBestSentencePieceText* parent, int index) : parent_(parent), index_(index) {} + inline NBestSentencePieceText_Sub& operator*() const { + return *(parent_->mutable_nbests_at(index_)); + } + Iterator& operator++() { ++index_; return *this; } + bool operator!=(const Iterator& other) const { return index_ != other.index_; } + private: + NBestSentencePieceText* parent_; + int index_; + }; + + Iterator begin() { return Iterator(parent_, 0); } + Iterator end() { return Iterator(parent_, parent_->nbests_size()); } + int size() const { return parent_->nbests_size(); } + + NBestSentencePieceText_Sub* Add() { return parent_->add_nbests(); } + private: + NBestSentencePieceText* parent_; + }; + + MutableNbestsRepeatedWrapper* mutable_nbests() { + if (!mutable_nbests_wrapper_) { + mutable_nbests_wrapper_ = std::make_unique(this); + } + return mutable_nbests_wrapper_.get(); + } + + inline NBestSentencePieceText_Sub* mutable_nbests_at(int index); + + static const NBestSentencePieceText& default_instance() { + static NBestSentencePieceText instance; + return instance; + } + private: + std::unique_ptr mutable_nbests_wrapper_; +}; + + + +inline ProtoStr ModelProto_SentencePiece::piece() const { + return parent_->piece_at(index_); +} +inline float ModelProto_SentencePiece::score() const { + return parent_->score_at(index_); +} +inline ModelProto_SentencePiece::Type ModelProto_SentencePiece::type() const { + return static_cast(parent_->type_at(index_)); +} +inline void ModelProto_SentencePiece::set_type(Type type) { + parent_->set_type_at(index_, type); +} + +inline TrainerSpec upb::ModelProtoWrapper::trainer_spec() const { + return TrainerSpec(msg_ ? sentencepiece_ModelProto_trainer_spec(msg_) : nullptr, arena_); +} +inline const NormalizerSpec& upb::ModelProtoWrapper::normalizer_spec() const { + if (!normalizer_spec_cache_) { + normalizer_spec_cache_ = std::make_unique( + msg_ ? sentencepiece_ModelProto_normalizer_spec(msg_) : nullptr, arena_); + } + return *normalizer_spec_cache_; +} +inline const NormalizerSpec& upb::ModelProtoWrapper::denormalizer_spec() const { + if (!denormalizer_spec_cache_) { + denormalizer_spec_cache_ = std::make_unique( + msg_ ? sentencepiece_ModelProto_denormalizer_spec(msg_) : nullptr, arena_); + } + return *denormalizer_spec_cache_; +} +inline SelfTestData upb::ModelProtoWrapper::self_test_data() const { + return SelfTestData(msg_ ? sentencepiece_ModelProto_self_test_data(msg_) : nullptr); +} + +inline upb_Arena* NBestSentencePieceText_Sub::arena() { return parent_->arena(); } +inline const upb_Arena* NBestSentencePieceText_Sub::arena() const { return parent_->arena(); } + +inline NBestSentencePieceText_Sub* upb::NBestSentencePieceTextWrapper::add_nbests() { + if (!msg_) return nullptr; + sentencepiece_SentencePieceText* sub_msg = + sentencepiece_NBestSentencePieceText_add_nbests(msg_, arena_); + + int index = nbests_size() - 1; + + auto wrapper = std::make_unique( + this, index); + NBestSentencePieceText_Sub* ptr = wrapper.get(); + nbest_wrappers_.push_back(std::move(wrapper)); + return ptr; +} + +inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper::nbests(int index) const { + LazyInitNbestWrappersCache(); + if (index < 0 || static_cast(index) >= nbest_wrappers_.size()) { + static const NBestSentencePieceText_Sub default_sub(nullptr, -1); + return default_sub; + } + return *nbest_wrappers_[index]; +} + +inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper::ConstNbestsRepeatedWrapper::Iterator::operator*() const { + return parent_->nbests(index_); +} + +inline sentencepiece_SentencePieceText* NBestSentencePieceText_Sub::mutable_msg() { + size_t size = 0; + sentencepiece_SentencePieceText** nbests = + sentencepiece_NBestSentencePieceText_mutable_nbests(parent_->mutable_msg(), &size); + return nbests[index_]; +} +inline const sentencepiece_SentencePieceText* NBestSentencePieceText_Sub::msg() const { + size_t size = 0; + const sentencepiece_SentencePieceText* const* nbests = + sentencepiece_NBestSentencePieceText_nbests(parent_->msg(), &size); + return nbests[index_]; +} + +inline absl::string_view SentencePieceText_SentencePiece::piece() const { + if (!parent_) { + return ""; + } + return parent_->piece_at(index_); +} +inline void SentencePieceText_SentencePiece::set_piece(absl::string_view piece) { + if (parent_) { + parent_->set_piece_at(index_, piece); + } +} +inline void SentencePieceText_SentencePiece::set_piece(const char* data, size_t size) { + set_piece(absl::string_view(data, size)); +} + +inline uint32_t SentencePieceText_SentencePiece::id() const { + return parent_ ? parent_->id_at(index_) : 0; +} +inline void SentencePieceText_SentencePiece::set_id(uint32_t id) { + if (parent_) parent_->set_id_at(index_, id); +} + +inline absl::string_view SentencePieceText_SentencePiece::surface() const { + if (!parent_) { + return ""; + } + return parent_->surface_at(index_); +} +inline void SentencePieceText_SentencePiece::set_surface(absl::string_view surface) { + if (parent_) { + parent_->set_surface_at(index_, surface); + } +} +inline void SentencePieceText_SentencePiece::set_surface(const char* data, size_t size) { + set_surface(absl::string_view(data, size)); +} + +inline uint32_t SentencePieceText_SentencePiece::begin() const { + return parent_ ? parent_->begin_at(index_) : 0; +} +inline void SentencePieceText_SentencePiece::set_begin(uint32_t begin) { + if (parent_) parent_->set_begin_at(index_, begin); +} + +inline uint32_t SentencePieceText_SentencePiece::end() const { + return parent_ ? parent_->end_at(index_) : 0; +} +inline void SentencePieceText_SentencePiece::set_end(uint32_t end) { + if (parent_) parent_->set_end_at(index_, end); +} + +inline void swap(SentencePieceText_SentencePiece a, SentencePieceText_SentencePiece b) { + a.parent_->SwapElementsData(a.index_, b.index_); +} + + + +inline const NBestSentencePieceText_Sub& NBestSentencePieceText::nbests(int index) const { + return NBestSentencePieceTextWrapper::nbests(index); +} +inline NBestSentencePieceText_Sub* NBestSentencePieceText::add_nbests() { + return NBestSentencePieceTextWrapper::add_nbests(); +} +inline NBestSentencePieceText_Sub* NBestSentencePieceText::mutable_nbests_at(int index) { + if (nbest_wrappers_.size() != nbests_size()) { + nbest_wrappers_.resize(nbests_size()); + for (int i = 0; i < nbests_size(); ++i) { + if (!nbest_wrappers_[i]) { + nbest_wrappers_[i] = std::make_unique(this, i); + } + } + } + return nbest_wrappers_[index].get(); +} + +inline ::sentencepiece::NormalizerSpec* upb::ModelProtoWrapper::mutable_normalizer_spec() const { + if (!mutable_normalizer_spec_) { + if (msg_) { + sentencepiece_NormalizerSpec* sub_msg = sentencepiece_ModelProto_mutable_normalizer_spec(msg_, arena_); + mutable_normalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>( + sub_msg, arena_, [this](const sentencepiece_NormalizerSpec* new_sub_msg) { + sentencepiece_ModelProto_set_normalizer_spec( + this->msg_, const_cast(new_sub_msg)); + this->normalizer_spec_cache_.reset(); + }); + } else { + mutable_normalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); + } + } + return mutable_normalizer_spec_.get(); +} + +inline ::sentencepiece::NormalizerSpec* upb::ModelProtoWrapper::mutable_denormalizer_spec() const { + if (!mutable_denormalizer_spec_) { + if (msg_) { + sentencepiece_NormalizerSpec* sub_msg = sentencepiece_ModelProto_mutable_denormalizer_spec(msg_, arena_); + mutable_denormalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>( + sub_msg, arena_, [this](const sentencepiece_NormalizerSpec* new_sub_msg) { + sentencepiece_ModelProto_set_denormalizer_spec( + this->msg_, const_cast(new_sub_msg)); + this->denormalizer_spec_cache_.reset(); + }); + } else { + mutable_denormalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); + } + } + return mutable_denormalizer_spec_.get(); +} + +inline ModelProto_SentencePiece* ModelProto::add_pieces() { + if (msg_) { + sentencepiece_ModelProto_add_pieces(msg_, arena_); + int new_index = pieces_size() - 1; + piece_wrappers_.resize(pieces_size()); + piece_wrappers_[new_index] = std::make_unique(this, new_index); + return piece_wrappers_[new_index].get(); + } + return nullptr; +} + +inline ::sentencepiece::TrainerSpec* upb::ModelProtoWrapper::mutable_trainer_spec() const { + if (!mutable_trainer_spec_) { + if (msg_) { + sentencepiece_TrainerSpec* sub_msg = sentencepiece_ModelProto_mutable_trainer_spec(msg_, arena_); + mutable_trainer_spec_ = std::make_unique<::sentencepiece::TrainerSpec>( + sub_msg, arena_, [this](const sentencepiece_TrainerSpec* new_sub_msg) { + sentencepiece_ModelProto_set_trainer_spec( + this->msg_, const_cast(new_sub_msg)); + }); + } else { + mutable_trainer_spec_ = std::make_unique<::sentencepiece::TrainerSpec>(nullptr); + } + } + return mutable_trainer_spec_.get(); +} + +inline ::sentencepiece::SelfTestData* upb::ModelProtoWrapper::mutable_self_test_data() const { + if (!mutable_self_test_data_) { + if (msg_) { + sentencepiece_SelfTestData* sub_msg = sentencepiece_ModelProto_mutable_self_test_data(msg_, arena_); + mutable_self_test_data_ = std::make_unique<::sentencepiece::SelfTestData>(sub_msg, arena_); + } else { + mutable_self_test_data_ = std::make_unique<::sentencepiece::SelfTestData>(nullptr, nullptr); + } + } + return mutable_self_test_data_.get(); +} + +inline void upb::ModelProtoWrapper::set_trainer_spec(const TrainerSpec& spec) { + if (msg_ && spec.msg_) { + size_t size = 0; + upb_Arena* tmp_arena = upb_Arena_New(); + char* buf = sentencepiece_TrainerSpec_serialize(spec.msg_, tmp_arena, &size); + if (buf) { + sentencepiece_TrainerSpec* sub_msg = sentencepiece_TrainerSpec_parse(buf, size, arena_); + sentencepiece_ModelProto_set_trainer_spec(msg_, sub_msg); + mutable_trainer_spec_ = std::make_unique<::sentencepiece::TrainerSpec>(sub_msg, arena_); + } + upb_Arena_Free(tmp_arena); + } +} + +inline void upb::ModelProtoWrapper::set_normalizer_spec(const NormalizerSpec& spec) { + if (msg_ && spec.msg_) { + size_t size = 0; + upb_Arena* tmp_arena = upb_Arena_New(); + char* buf = sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); + if (buf) { + sentencepiece_NormalizerSpec* sub_msg = sentencepiece_NormalizerSpec_parse(buf, size, arena_); + sentencepiece_ModelProto_set_normalizer_spec(msg_, sub_msg); + normalizer_spec_cache_ = std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); + } + upb_Arena_Free(tmp_arena); + } +} + +inline void upb::ModelProtoWrapper::set_denormalizer_spec(const NormalizerSpec& spec) { + if (msg_ && spec.msg_) { + size_t size = 0; + upb_Arena* tmp_arena = upb_Arena_New(); + char* buf = sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); + if (buf) { + sentencepiece_NormalizerSpec* sub_msg = sentencepiece_NormalizerSpec_parse(buf, size, arena_); + sentencepiece_ModelProto_set_denormalizer_spec(msg_, sub_msg); + mutable_denormalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); + } + upb_Arena_Free(tmp_arena); + } +} + +inline void ModelProto_SentencePiece::set_piece(absl::string_view piece) { + parent_->set_piece_at(index_, piece); +} +inline void ModelProto_SentencePiece::set_score(float score) { + parent_->set_score_at(index_, score); +} + +} // namespace sentencepiece + +#endif // CORE_UPB_WRAPPER_H_ diff --git a/src/filesystem.cc b/src/filesystem.cc index 08c265626..7bafe150c 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -25,7 +25,7 @@ #if defined(OS_WIN) && defined(UNICODE) && defined(_UNICODE) #define WPATH(path) (::sentencepiece::util::Utf8ToWide(path).c_str()) #else -#define WPATH(path) (path.data()) +#define WPATH(path) (std::string(path).c_str()) #endif namespace sentencepiece { @@ -44,7 +44,7 @@ class PosixReadableFile : public ReadableFile { } if (!*is_ || ((is_->peek() != 0) && is_->fail())) { status_ = absl::StatusBuilder(absl::StatusCode::kNotFound) - << "\"" << filename.data() << "\": " << util::StrError(errno); + << "\"" << filename << "\": " << util::StrError(errno); } } @@ -85,7 +85,7 @@ class PosixWritableFile : public WritableFile { } if (!*os_) { status_ = absl::StatusBuilder(absl::StatusCode::kPermissionDenied) - << "\"" << filename.data() << "\": " << util::StrError(errno); + << "\"" << filename << "\": " << util::StrError(errno); } } diff --git a/src/init.cc b/src/init.cc index c2c651e20..0501dc81c 100644 --- a/src/init.cc +++ b/src/init.cc @@ -24,11 +24,7 @@ #include "third_party/absl/strings/str_cat.h" #include "util.h" -#ifdef _USE_EXTERNAL_PROTOBUF #include "google/protobuf/message_lite.h" -#else -#include "third_party/protobuf-lite/google/protobuf/message_lite.h" -#endif ABSL_FLAG(bool, quiet, false, "Suppress logging message."); diff --git a/src/model_interface.h b/src/model_interface.h index 39e0ef634..4f8125986 100644 --- a/src/model_interface.h +++ b/src/model_interface.h @@ -149,7 +149,7 @@ class ModelInterface { // Returns the string representation of vocab with `id`. // id must be 0 <= id < GetPieceSize(). - [[nodiscard]] virtual const std::string& IdToPiece(int id) const { + [[nodiscard]] virtual absl::string_view IdToPiece(int id) const { DCHECK_GE(id, 0); DCHECK_LT(id, model_proto_->pieces_size()); return model_proto_->pieces(id).piece(); diff --git a/src/normalizer_test.cc b/src/normalizer_test.cc index 94f6b3488..24b45338e 100644 --- a/src/normalizer_test.cc +++ b/src/normalizer_test.cc @@ -173,9 +173,11 @@ TEST(NormalizeTest, NomalizeWithSpaceContainedRules) { AddRule("d", " F G "); NormalizerSpec spec; + std::string compiled; EXPECT_TRUE( - Builder::CompileCharsMap(charsmap, spec.mutable_precompiled_charsmap()) + Builder::CompileCharsMap(charsmap, &compiled) .ok()); + spec.set_precompiled_charsmap(compiled); // Test default behavior { diff --git a/src/sentencepiece_processor.cc b/src/sentencepiece_processor.cc index 1421c1872..48da90a87 100644 --- a/src/sentencepiece_processor.cc +++ b/src/sentencepiece_processor.cc @@ -47,11 +47,7 @@ #include "unigram_model.h" #include "util.h" -#ifdef _USE_EXTERNAL_PROTOBUF #include "google/protobuf/arena.h" -#else -#include "third_party/protobuf-lite/google/protobuf/arena.h" -#endif using ::google::protobuf::Arena; @@ -128,12 +124,12 @@ ImmutableSentencePieceText_ImmutableSentencePiece:: const SentencePieceText_SentencePiece& sp) : sp_(&sp) {} -const std::string& ImmutableSentencePieceText_ImmutableSentencePiece::piece() +absl::string_view ImmutableSentencePieceText_ImmutableSentencePiece::piece() const { return sp_->piece(); } -const std::string& ImmutableSentencePieceText_ImmutableSentencePiece::surface() +absl::string_view ImmutableSentencePieceText_ImmutableSentencePiece::surface() const { return sp_->surface(); } @@ -169,7 +165,7 @@ ImmutableSentencePieceText::pieces(int index) const { return ImmutableSentencePieceText_ImmutableSentencePiece(spt_->pieces(index)); } -const std::string& ImmutableSentencePieceText::text() const { +absl::string_view ImmutableSentencePieceText::text() const { return spt_->text(); } @@ -352,7 +348,7 @@ absl::Status SentencePieceProcessor::SetVocabulary( continue; } if (vocab.find(piece->piece()) != vocab.end() || - string_util::OneCharLen(piece->piece().c_str()) == + string_util::OneCharLen(piece->piece()) == piece->piece().size()) { piece->set_type(ModelProto::SentencePiece::NORMAL); } else { @@ -607,8 +603,8 @@ absl::Status SentencePieceProcessor::PopulateSentencePieceText( // Create a byte piece const uint8_t b = static_cast(w[i]); SentencePieceText::SentencePiece* sp = spt->add_pieces(); - std::string& piece = *sp->mutable_piece(); - piece = ByteToPiece(b); + std::string piece = ByteToPiece(b); + sp->set_piece(piece); int sp_id = model_->PieceToId(piece); sp->set_id(sp_id); @@ -631,8 +627,14 @@ absl::Status SentencePieceProcessor::PopulateSentencePieceText( // since known pieces never consist of unknown characters. if (is_prev_unk && is_unk) { auto* sp = spt->mutable_pieces(spt->pieces_size() - 1); - sp->mutable_piece()->append(w); - if (!skip_surface) sp->mutable_surface()->append(surface); + std::string piece(sp->piece()); + piece.append(w.data(), w.size()); + sp->set_piece(piece); + if (!skip_surface) { + std::string surf(sp->surface()); + surf.append(surface.data(), surface.size()); + sp->set_surface(surf); + } sp->set_end(orig_end); } else { auto* sp = spt->add_pieces(); @@ -849,19 +851,19 @@ absl::Status SentencePieceProcessor::Decode( spt->mutable_pieces()->Reserve(pieces.size()); for (absl::string_view w : pieces) { auto* sp = spt->add_pieces(); - sp->mutable_piece()->assign(w.data(), w.size()); + sp->set_piece(w.data(), w.size()); sp->set_id(PieceToId(w)); } RETURN_IF_ERROR(ApplyExtraOptions(decode_extra_options_, spt)); - std::string* text = spt->mutable_text(); + std::string decoded_text; auto SetSurface = [&](int index, absl::string_view surface) { auto* sp = spt->mutable_pieces(index); sp->set_surface(surface.data(), surface.size()); - sp->set_begin(text->size()); - sp->set_end(text->size() + surface.size()); - absl::StrAppend(text, surface); + sp->set_begin(decoded_text.size()); + sp->set_end(decoded_text.size() + surface.size()); + absl::StrAppend(&decoded_text, surface); }; auto ProcessBytePieces = [&](int token_index_begin, @@ -928,7 +930,7 @@ absl::Status SentencePieceProcessor::Decode( RETURN_IF_ERROR(ProcessBytePieces(byte_start, i)); // if we have seen a bos_ws token or any non-empty token - if (bos_ws_seen || !text->empty()) is_bos_ws = false; + if (bos_ws_seen || !decoded_text.empty()) is_bos_ws = false; byte_start = i + 1; std::tie(decoded, bos_ws_seen) = @@ -940,8 +942,9 @@ absl::Status SentencePieceProcessor::Decode( RETURN_IF_ERROR(ProcessBytePieces(byte_start, spt->pieces_size())); if (denormalizer_) { - *text = denormalizer_->Normalize(*text); + decoded_text = denormalizer_->Normalize(decoded_text); } + spt->set_text(decoded_text); return absl::OkStatus(); } @@ -1384,9 +1387,8 @@ int SentencePieceProcessor::PieceToId(absl::string_view piece) const { return model_->PieceToId(piece); } -const std::string& SentencePieceProcessor::IdToPiece(int id) const { - static const std::string* kEmptyString = new std::string; - RET_CHECK_OR_RETURN_DEFAULT(*kEmptyString); +absl::string_view SentencePieceProcessor::IdToPiece(int id) const { + RET_CHECK_OR_RETURN_DEFAULT(""); return model_->IdToPiece(id); } diff --git a/src/sentencepiece_processor.h b/src/sentencepiece_processor.h index 2e7338963..cc376a000 100644 --- a/src/sentencepiece_processor.h +++ b/src/sentencepiece_processor.h @@ -158,8 +158,8 @@ class ImmutableSentencePieceText_ImmutableSentencePiece { ImmutableSentencePieceText_ImmutableSentencePiece(); ~ImmutableSentencePieceText_ImmutableSentencePiece() = default; - [[nodiscard]] const std::string& piece() const; - [[nodiscard]] const std::string& surface() const; + [[nodiscard]] absl::string_view piece() const; + [[nodiscard]] absl::string_view surface() const; [[nodiscard]] uint32_t id() const; [[nodiscard]] uint32_t begin() const; [[nodiscard]] uint32_t end() const; @@ -184,7 +184,7 @@ class ImmutableSentencePieceText { [[nodiscard]] ImmutableSentencePieceText_ImmutableSentencePiece pieces( int index) const; - [[nodiscard]] const std::string& text() const; + [[nodiscard]] absl::string_view text() const; [[nodiscard]] float score() const; [[nodiscard]] util::bytes SerializeAsString() const; @@ -754,7 +754,7 @@ class SentencePieceProcessor { [[nodiscard]] virtual int PieceToId(absl::string_view piece) const; // Returns the string representation of vocab with `id`. - [[nodiscard]] virtual const std::string& IdToPiece(int id) const; + [[nodiscard]] virtual absl::string_view IdToPiece(int id) const; // Returns the string representation of vocab with `id`. // Returns false when id is out of range. diff --git a/src/sentencepiece_processor_test.cc b/src/sentencepiece_processor_test.cc index f07f07619..194290d83 100644 --- a/src/sentencepiece_processor_test.cc +++ b/src/sentencepiece_processor_test.cc @@ -77,7 +77,7 @@ class MockModel : public ModelInterface { int PieceToId(absl::string_view piece) const override { return 0; } - const std::string& IdToPiece(int id) const override { return kEmptyString; } + absl::string_view IdToPiece(int id) const override { return ""; } float GetScore(int id) const override { return 0.0; } @@ -559,7 +559,7 @@ TEST(SentencepieceProcessorTest, DecodeTest) { return port::FindWithDefault(kMap, piece, 0); } - const std::string& IdToPiece(int id) const override { + absl::string_view IdToPiece(int id) const override { static std::vector kMap = { "", "", "", WS "ABC", WS "DE", "F", "G" WS "H"}; return kMap[id]; @@ -726,7 +726,7 @@ TEST(SentencepieceProcessorTest, DummyPrefixDecodeTest) { return port::FindWithDefault(kMap, piece, 0); } - const std::string& IdToPiece(int id) const override { + absl::string_view IdToPiece(int id) const override { static std::vector kMap = { "", "", "", WS "ABC", WS "DE", "F", "G" WS "H", WS}; return kMap[id]; @@ -811,7 +811,7 @@ TEST(SentencepieceProcessorTest, ByteFallbackDecodeTest) { return port::FindWithDefault(kMap, std::string(piece), 0); } - const std::string& IdToPiece(int id) const override { + absl::string_view IdToPiece(int id) const override { static std::vector kMap = []() -> std::vector { std::vector m = {"", "", "", "A", "B", "C"}; for (int i = 0; i < 256; ++i) { diff --git a/src/sentencepiece_trainer.cc b/src/sentencepiece_trainer.cc index 989801763..77af458d2 100644 --- a/src/sentencepiece_trainer.cc +++ b/src/sentencepiece_trainer.cc @@ -119,8 +119,10 @@ absl::Status SentencePieceTrainer::Train( NormalizerSpec SentencePieceTrainer::GetNormalizerSpec(absl::string_view name) { NormalizerSpec spec; spec.set_name(name.data(), name.size()); + std::string precompiled; CHECK_OK(normalizer::Builder::GetPrecompiledCharsMap( - spec.name(), spec.mutable_precompiled_charsmap())); + spec.name(), &precompiled)); + spec.set_precompiled_charsmap(precompiled); return spec; } @@ -292,17 +294,21 @@ absl::Status SentencePieceTrainer::PopulateNormalizerSpec( normalizer::Builder::CharsMap chars_map; RETURN_IF_ERROR(normalizer::Builder::LoadCharsMap( normalizer_spec->normalization_rule_tsv(), &chars_map)); + std::string precompiled; RETURN_IF_ERROR(normalizer::Builder::CompileCharsMap( - chars_map, normalizer_spec->mutable_precompiled_charsmap())); + chars_map, &precompiled)); + normalizer_spec->set_precompiled_charsmap(precompiled); normalizer_spec->set_name("user_defined"); } else if (!is_denormalizer) { if (normalizer_spec->name().empty()) { normalizer_spec->set_name(kDefaultNormalizerName); } if (normalizer_spec->precompiled_charsmap().empty()) { + std::string precompiled; RETURN_IF_ERROR(normalizer::Builder::GetPrecompiledCharsMap( normalizer_spec->name(), - normalizer_spec->mutable_precompiled_charsmap())); + &precompiled)); + normalizer_spec->set_precompiled_charsmap(precompiled); } } @@ -474,37 +480,4 @@ std::string SentencePieceNormalizer::serialized_model_proto() const { std::string SentencePieceNormalizer::serialized_normalizer_spec() const { return normalizer_spec_ ? normalizer_spec_->SerializeAsString() : ""; } - -void ConvertToUnicodeAlignment(absl::string_view orig, absl::string_view norm, - std::vector* norm_to_orig) { - auto utf8_to_unicode_offsets = [](absl::string_view str) { - std::vector utf8_to_unicode(str.size() + 1, 0); - size_t prev = 0; - int ulen = 0; - while (!str.empty()) { - const size_t mblen = - std::min(str.size(), static_cast(std::max( - 1, string_util::OneCharLen(str.data())))); - for (size_t i = prev; i < prev + mblen; ++i) { - utf8_to_unicode[i] = ulen; - } - ++ulen; - prev += mblen; - str.remove_prefix(mblen); - } - utf8_to_unicode[prev] = ulen; - return utf8_to_unicode; - }; - - const auto orig_offsets = utf8_to_unicode_offsets(orig); - const auto norm_offsets = utf8_to_unicode_offsets(norm); - if (orig_offsets.empty() || norm_offsets.empty()) return; - - std::vector result(norm_offsets.back() + 1, 0); - for (size_t i = 0; i < norm_to_orig->size(); ++i) { - result[norm_offsets[i]] = orig_offsets[(*norm_to_orig)[i]]; - } - *norm_to_orig = std::move(result); -} - } // namespace sentencepiece diff --git a/src/trainer_interface.cc b/src/trainer_interface.cc index cf89c0ac7..1bdabf23d 100644 --- a/src/trainer_interface.cc +++ b/src/trainer_interface.cc @@ -744,8 +744,8 @@ absl::Status TrainerInterface::Save() const { if (output_model_proto_ != nullptr) { RETURN_IF_ERROR(Serialize(output_model_proto_)); } else { - RETURN_IF_ERROR(SaveModel(trainer_spec_.model_prefix() + ".model")); - RETURN_IF_ERROR(SaveVocab(trainer_spec_.model_prefix() + ".vocab")); + RETURN_IF_ERROR(SaveModel(absl::StrCat(trainer_spec_.model_prefix(), ".model"))); + RETURN_IF_ERROR(SaveVocab(absl::StrCat(trainer_spec_.model_prefix(), ".vocab"))); } return absl::OkStatus(); } diff --git a/src/util.cc b/src/util.cc index bd5ea1a16..dd7ca34c5 100644 --- a/src/util.cc +++ b/src/util.cc @@ -405,4 +405,37 @@ double LogSum(const std::vector& xs) { return sum; } } // namespace log_domain + +void ConvertToUnicodeAlignment(absl::string_view orig, absl::string_view norm, + std::vector* norm_to_orig) { + auto utf8_to_unicode_offsets = [](absl::string_view str) { + std::vector utf8_to_unicode(str.size() + 1, 0); + size_t prev = 0; + int ulen = 0; + while (!str.empty()) { + const size_t mblen = + std::min(str.size(), static_cast(std::max( + 1, string_util::OneCharLen(str.data())))); + for (size_t i = prev; i < prev + mblen; ++i) { + utf8_to_unicode[i] = ulen; + } + ++ulen; + prev += mblen; + str.remove_prefix(mblen); + } + utf8_to_unicode[prev] = ulen; + return utf8_to_unicode; + }; + + const auto orig_offsets = utf8_to_unicode_offsets(orig); + const auto norm_offsets = utf8_to_unicode_offsets(norm); + if (orig_offsets.empty() || norm_offsets.empty()) return; + + std::vector result(norm_offsets.back() + 1, 0); + for (size_t i = 0; i < norm_to_orig->size(); ++i) { + result[norm_offsets[i]] = orig_offsets[(*norm_to_orig)[i]]; + } + *norm_to_orig = std::move(result); +} + } // namespace sentencepiece diff --git a/src/util.h b/src/util.h index 07d9f37e3..512645b00 100644 --- a/src/util.h +++ b/src/util.h @@ -113,6 +113,11 @@ inline size_t OneCharLen(const char* src) { return "\1\1\1\1\1\1\1\1\1\1\1\1\2\2\3\4"[(*src & 0xFF) >> 4]; } +inline size_t OneCharLen(absl::string_view src) { + if (src.empty()) return 0; + return OneCharLen(src.data()); +} + // Return (x & 0xC0) == 0x80; // Since trail bytes are always in [0x80, 0xBF], we can optimize: inline bool IsTrailByte(char x) { return static_cast(x) < -0x40; } @@ -308,5 +313,9 @@ namespace log_domain { double LogSum(const std::vector& xs); } // namespace log_domain + +void ConvertToUnicodeAlignment(absl::string_view orig, absl::string_view norm, + std::vector* norm_to_orig); + } // namespace sentencepiece #endif // UTIL_H_ diff --git a/third_party/protobuf-lite/LICENSE b/third_party/protobuf-lite/LICENSE deleted file mode 100644 index 19b305b00..000000000 --- a/third_party/protobuf-lite/LICENSE +++ /dev/null @@ -1,32 +0,0 @@ -Copyright 2008 Google Inc. 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 Inc. 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 THE COPYRIGHT -OWNER OR CONTRIBUTORS 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. - -Code generated by the Protocol Buffer compiler is owned by the owner -of the input file used when generating it. This code is not -standalone and requires a support library to be linked with it. This -support library is itself covered by the above license. diff --git a/third_party/protobuf-lite/arena.cc b/third_party/protobuf-lite/arena.cc deleted file mode 100644 index 414e0234f..000000000 --- a/third_party/protobuf-lite/arena.cc +++ /dev/null @@ -1,449 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include -#include - -#include - -#ifdef ADDRESS_SANITIZER -#include -#endif // ADDRESS_SANITIZER - -#include - -static const size_t kMinCleanupListElements = 8; -static const size_t kMaxCleanupListElements = 64; // 1kB on 64-bit. - -namespace google { -namespace protobuf { - -PROTOBUF_EXPORT /*static*/ void* (*const ArenaOptions::kDefaultBlockAlloc)( - size_t) = &::operator new; - -namespace internal { - - -ArenaImpl::CacheAlignedLifecycleIdGenerator ArenaImpl::lifecycle_id_generator_; -#if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) -ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { - static internal::ThreadLocalStorage* thread_cache_ = - new internal::ThreadLocalStorage(); - return *thread_cache_->Get(); -} -#elif defined(PROTOBUF_USE_DLLS) -ArenaImpl::ThreadCache& ArenaImpl::thread_cache() { - static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_ = { - 0, static_cast(-1), nullptr}; - return thread_cache_; -} -#else -PROTOBUF_THREAD_LOCAL ArenaImpl::ThreadCache ArenaImpl::thread_cache_ = { - 0, static_cast(-1), nullptr}; -#endif - -void ArenaFree(void* object, size_t size) { -#if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) - ::operator delete(object, size); -#else - (void)size; - ::operator delete(object); -#endif -} - -ArenaImpl::ArenaImpl(const ArenaOptions& options) { - ArenaMetricsCollector* collector = nullptr; - bool record_allocs = false; - if (options.make_metrics_collector != nullptr) { - collector = (*options.make_metrics_collector)(); - record_allocs = (collector && collector->RecordAllocs()); - } - - // Get memory where we can store non-default options if needed. - // Use supplied initial_block if it is large enough. - size_t min_block_size = kOptionsSize + kBlockHeaderSize + kSerialArenaSize; - char* mem = options.initial_block; - size_t mem_size = options.initial_block_size; - GOOGLE_DCHECK_EQ(reinterpret_cast(mem) & 7, 0); - if (mem == nullptr || mem_size < min_block_size) { - // Supplied initial block is not big enough. - mem_size = std::max(min_block_size, options.start_block_size); - mem = reinterpret_cast((*options.block_alloc)(mem_size)); - } - - // Create the special block. - const bool special = true; - const bool user_owned = (mem == options.initial_block); - auto block = - new (mem) SerialArena::Block(mem_size, nullptr, special, user_owned); - - // Options occupy the beginning of the initial block. - options_ = new (block->Pointer(block->pos())) Options; -#ifdef ADDRESS_SANITIZER - ASAN_UNPOISON_MEMORY_REGION(options_, kOptionsSize); -#endif // ADDRESS_SANITIZER - options_->start_block_size = options.start_block_size; - options_->max_block_size = options.max_block_size; - options_->block_alloc = options.block_alloc; - options_->block_dealloc = options.block_dealloc; - options_->metrics_collector = collector; - block->set_pos(block->pos() + kOptionsSize); - - Init(record_allocs); - SetInitialBlock(block); -} - -void ArenaImpl::Init(bool record_allocs) { - ThreadCache& tc = thread_cache(); - auto id = tc.next_lifecycle_id; - constexpr uint64 kInc = ThreadCache::kPerThreadIds * 2; - if (PROTOBUF_PREDICT_FALSE((id & (kInc - 1)) == 0)) { - if (sizeof(lifecycle_id_generator_.id) == 4) { - // 2^32 is dangerous low to guarantee uniqueness. If we start dolling out - // unique id's in ranges of kInc it's unacceptably low. In this case - // we increment by 1. The additional range of kPerThreadIds that are used - // per thread effectively pushes the overflow time from weeks to years - // of continuous running. - id = lifecycle_id_generator_.id.fetch_add(1, std::memory_order_relaxed) * - kInc; - } else { - id = - lifecycle_id_generator_.id.fetch_add(kInc, std::memory_order_relaxed); - } - } - tc.next_lifecycle_id = id + 2; - // We store "record_allocs" in the low bit of lifecycle_id_. - lifecycle_id_ = id | (record_allocs ? 1 : 0); - hint_.store(nullptr, std::memory_order_relaxed); - threads_.store(nullptr, std::memory_order_relaxed); - space_allocated_.store(0, std::memory_order_relaxed); -} - -void ArenaImpl::SetInitialBlock(SerialArena::Block* block) { - // Calling thread owns the first block. This allows the single-threaded case - // to allocate on the first block without having to perform atomic operations. - SerialArena* serial = SerialArena::New(block, &thread_cache(), this); - serial->set_next(NULL); - threads_.store(serial, std::memory_order_relaxed); - space_allocated_.store(block->size(), std::memory_order_relaxed); - CacheSerialArena(serial); -} - -ArenaImpl::~ArenaImpl() { - // Have to do this in a first pass, because some of the destructors might - // refer to memory in other blocks. - CleanupList(); - - ArenaMetricsCollector* collector = nullptr; - auto deallocator = &ArenaFree; - if (options_) { - collector = options_->metrics_collector; - deallocator = options_->block_dealloc; - } - - PerBlock([deallocator](SerialArena::Block* b) { -#ifdef ADDRESS_SANITIZER - // This memory was provided by the underlying allocator as unpoisoned, so - // return it in an unpoisoned state. - ASAN_UNPOISON_MEMORY_REGION(b->Pointer(0), b->size()); -#endif // ADDRESS_SANITIZER - if (!b->user_owned()) { - (*deallocator)(b, b->size()); - } - }); - - if (collector) { - collector->OnDestroy(SpaceAllocated()); - } -} - -uint64 ArenaImpl::Reset() { - if (options_ && options_->metrics_collector) { - options_->metrics_collector->OnReset(SpaceAllocated()); - } - - // Have to do this in a first pass, because some of the destructors might - // refer to memory in other blocks. - CleanupList(); - - // Discard all blocks except the special block (if present). - uint64 space_allocated = 0; - SerialArena::Block* special_block = nullptr; - auto deallocator = (options_ ? options_->block_dealloc : &ArenaFree); - PerBlock( - [&space_allocated, &special_block, deallocator](SerialArena::Block* b) { - space_allocated += b->size(); -#ifdef ADDRESS_SANITIZER - // This memory was provided by the underlying allocator as unpoisoned, - // so return it in an unpoisoned state. - ASAN_UNPOISON_MEMORY_REGION(b->Pointer(0), b->size()); -#endif // ADDRESS_SANITIZER - if (!b->special()) { - (*deallocator)(b, b->size()); - } else { - // Prepare special block for reuse. - // Note: if options_ is present, it occupies the beginning of the - // block and therefore pos is advanced past it. - GOOGLE_DCHECK(special_block == nullptr); - special_block = b; - } - }); - - Init(record_allocs()); - if (special_block != nullptr) { - // next() should still be nullptr since we are using a stack discipline, but - // clear it anyway to reduce fragility. - GOOGLE_DCHECK_EQ(special_block->next(), nullptr); - special_block->clear_next(); - special_block->set_pos(kBlockHeaderSize + (options_ ? kOptionsSize : 0)); - SetInitialBlock(special_block); - } - return space_allocated; -} - -std::pair ArenaImpl::NewBuffer(size_t last_size, - size_t min_bytes) { - size_t size; - if (last_size != -1) { - // Double the current block size, up to a limit. - auto max_size = options_ ? options_->max_block_size : kDefaultMaxBlockSize; - size = std::min(2 * last_size, max_size); - } else { - size = options_ ? options_->start_block_size : kDefaultStartBlockSize; - } - // Verify that min_bytes + kBlockHeaderSize won't overflow. - GOOGLE_CHECK_LE(min_bytes, std::numeric_limits::max() - kBlockHeaderSize); - size = std::max(size, kBlockHeaderSize + min_bytes); - - void* mem = options_ ? (*options_->block_alloc)(size) : ::operator new(size); - space_allocated_.fetch_add(size, std::memory_order_relaxed); - return {mem, size}; -} - -SerialArena::Block* SerialArena::NewBlock(SerialArena::Block* last_block, - size_t min_bytes, ArenaImpl* arena) { - void* mem; - size_t size; - std::tie(mem, size) = - arena->NewBuffer(last_block ? last_block->size() : -1, min_bytes); - Block* b = new (mem) Block(size, last_block, false, false); - return b; -} - -PROTOBUF_NOINLINE -void SerialArena::AddCleanupFallback(void* elem, void (*cleanup)(void*)) { - size_t size = cleanup_ ? cleanup_->size * 2 : kMinCleanupListElements; - size = std::min(size, kMaxCleanupListElements); - size_t bytes = internal::AlignUpTo8(CleanupChunk::SizeOf(size)); - CleanupChunk* list = reinterpret_cast(AllocateAligned(bytes)); - list->next = cleanup_; - list->size = size; - - cleanup_ = list; - cleanup_ptr_ = &list->nodes[0]; - cleanup_limit_ = &list->nodes[size]; - - AddCleanup(elem, cleanup); -} - -void* ArenaImpl::AllocateAlignedAndAddCleanup(size_t n, - void (*cleanup)(void*)) { - SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { - return arena->AllocateAlignedAndAddCleanup(n, cleanup); - } else { - return AllocateAlignedAndAddCleanupFallback(n, cleanup); - } -} - -void ArenaImpl::AddCleanup(void* elem, void (*cleanup)(void*)) { - SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { - arena->AddCleanup(elem, cleanup); - } else { - return AddCleanupFallback(elem, cleanup); - } -} - -PROTOBUF_NOINLINE -void* ArenaImpl::AllocateAlignedFallback(size_t n) { - return GetSerialArenaFallback(&thread_cache())->AllocateAligned(n); -} - -PROTOBUF_NOINLINE -void* ArenaImpl::AllocateAlignedAndAddCleanupFallback(size_t n, - void (*cleanup)(void*)) { - return GetSerialArenaFallback( - &thread_cache())->AllocateAlignedAndAddCleanup(n, cleanup); -} - -PROTOBUF_NOINLINE -void ArenaImpl::AddCleanupFallback(void* elem, void (*cleanup)(void*)) { - GetSerialArenaFallback(&thread_cache())->AddCleanup(elem, cleanup); -} - -PROTOBUF_NOINLINE -void* SerialArena::AllocateAlignedFallback(size_t n) { - // Sync back to current's pos. - head_->set_pos(head_->size() - (limit_ - ptr_)); - - head_ = NewBlock(head_, n, arena_); - ptr_ = head_->Pointer(head_->pos()); - limit_ = head_->Pointer(head_->size()); - -#ifdef ADDRESS_SANITIZER - ASAN_POISON_MEMORY_REGION(ptr_, limit_ - ptr_); -#endif // ADDRESS_SANITIZER - - return AllocateAligned(n); -} - -uint64 ArenaImpl::SpaceAllocated() const { - return space_allocated_.load(std::memory_order_relaxed); -} - -uint64 ArenaImpl::SpaceUsed() const { - SerialArena* serial = threads_.load(std::memory_order_acquire); - uint64 space_used = 0; - for (; serial; serial = serial->next()) { - space_used += serial->SpaceUsed(); - } - // Remove the overhead of Options structure, if any. - if (options_) { - space_used -= kOptionsSize; - } - return space_used; -} - -uint64 SerialArena::SpaceUsed() const { - // Get current block's size from ptr_ (since we can't trust head_->pos(). - uint64 space_used = ptr_ - head_->Pointer(kBlockHeaderSize); - // Get subsequent block size from b->pos(). - for (Block* b = head_->next(); b; b = b->next()) { - space_used += (b->pos() - kBlockHeaderSize); - } - // Remove the overhead of the SerialArena itself. - space_used -= ArenaImpl::kSerialArenaSize; - return space_used; -} - -void ArenaImpl::CleanupList() { - // By omitting an Acquire barrier we ensure that any user code that doesn't - // properly synchronize Reset() or the destructor will throw a TSAN warning. - SerialArena* serial = threads_.load(std::memory_order_relaxed); - - for (; serial; serial = serial->next()) { - serial->CleanupList(); - } -} - -void SerialArena::CleanupList() { - if (cleanup_ != NULL) { - CleanupListFallback(); - } -} - -void SerialArena::CleanupListFallback() { - // The first chunk might be only partially full, so calculate its size - // from cleanup_ptr_. Subsequent chunks are always full, so use list->size. - size_t n = cleanup_ptr_ - &cleanup_->nodes[0]; - CleanupChunk* list = cleanup_; - while (true) { - CleanupNode* node = &list->nodes[0]; - // Cleanup newest elements first (allocated last). - for (size_t i = n; i > 0; i--) { - node[i - 1].cleanup(node[i - 1].elem); - } - list = list->next; - if (list == nullptr) { - break; - } - // All but the first chunk are always full. - n = list->size; - } -} - -SerialArena* SerialArena::New(Block* b, void* owner, ArenaImpl* arena) { - auto pos = b->pos(); - GOOGLE_DCHECK_LE(pos + ArenaImpl::kSerialArenaSize, b->size()); - SerialArena* serial = reinterpret_cast(b->Pointer(pos)); - b->set_pos(pos + ArenaImpl::kSerialArenaSize); - serial->arena_ = arena; - serial->owner_ = owner; - serial->head_ = b; - serial->ptr_ = b->Pointer(b->pos()); - serial->limit_ = b->Pointer(b->size()); - serial->cleanup_ = NULL; - serial->cleanup_ptr_ = NULL; - serial->cleanup_limit_ = NULL; - return serial; -} - -PROTOBUF_NOINLINE -SerialArena* ArenaImpl::GetSerialArenaFallback(void* me) { - // Look for this SerialArena in our linked list. - SerialArena* serial = threads_.load(std::memory_order_acquire); - for (; serial; serial = serial->next()) { - if (serial->owner() == me) { - break; - } - } - - if (!serial) { - // This thread doesn't have any SerialArena, which also means it doesn't - // have any blocks yet. So we'll allocate its first block now. - SerialArena::Block* b = SerialArena::NewBlock(NULL, kSerialArenaSize, this); - serial = SerialArena::New(b, me, this); - - SerialArena* head = threads_.load(std::memory_order_relaxed); - do { - serial->set_next(head); - } while (!threads_.compare_exchange_weak( - head, serial, std::memory_order_release, std::memory_order_relaxed)); - } - - CacheSerialArena(serial); - return serial; -} - -ArenaMetricsCollector::~ArenaMetricsCollector() {} - -} // namespace internal - -PROTOBUF_FUNC_ALIGN(32) -void* Arena::AllocateAlignedNoHook(size_t n) { - return impl_.AllocateAligned(n); -} - -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/arenastring.cc b/third_party/protobuf-lite/arenastring.cc deleted file mode 100644 index b5f48c53a..000000000 --- a/third_party/protobuf-lite/arenastring.cc +++ /dev/null @@ -1,254 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include -#include -#include -#include -#include -#include -#include - -// clang-format off -#include -// clang-format on - -namespace google { -namespace protobuf { -namespace internal { - -const std::string& LazyString::Init() const { - static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED}; - mu.Lock(); - const std::string* res = inited_.load(std::memory_order_acquire); - if (res == nullptr) { - auto init_value = init_value_; - res = ::new (static_cast(string_buf_)) - std::string(init_value.ptr, init_value.size); - inited_.store(res, std::memory_order_release); - } - mu.Unlock(); - return *res; -} - - -void ArenaStringPtr::Set(const std::string* default_value, - ConstStringParam value, ::google::protobuf::Arena* arena) { - if (IsDefault(default_value)) { - tagged_ptr_.Set(Arena::Create(arena, value)); - } else { - UnsafeMutablePointer()->assign(value.data(), value.length()); - } -} - -void ArenaStringPtr::Set(const std::string* default_value, std::string&& value, - ::google::protobuf::Arena* arena) { - if (IsDefault(default_value)) { - if (arena == nullptr) { - tagged_ptr_.Set(new std::string(std::move(value))); - } else { - tagged_ptr_.Set(Arena::Create(arena, std::move(value))); - } - } else if (IsDonatedString()) { - std::string* current = tagged_ptr_.Get(); - auto* s = new (current) std::string(std::move(value)); - arena->OwnDestructor(s); - tagged_ptr_.Set(s); - } else /* !IsDonatedString() */ { - *UnsafeMutablePointer() = std::move(value); - } -} - -void ArenaStringPtr::Set(EmptyDefault, ConstStringParam value, - ::google::protobuf::Arena* arena) { - Set(&GetEmptyStringAlreadyInited(), value, arena); -} - -void ArenaStringPtr::Set(EmptyDefault, std::string&& value, - ::google::protobuf::Arena* arena) { - Set(&GetEmptyStringAlreadyInited(), std::move(value), arena); -} - -void ArenaStringPtr::Set(NonEmptyDefault, ConstStringParam value, - ::google::protobuf::Arena* arena) { - Set(nullptr, value, arena); -} - -void ArenaStringPtr::Set(NonEmptyDefault, std::string&& value, - ::google::protobuf::Arena* arena) { - Set(nullptr, std::move(value), arena); -} - -std::string* ArenaStringPtr::Mutable(EmptyDefault, ::google::protobuf::Arena* arena) { - if (!IsDonatedString() && !IsDefault(&GetEmptyStringAlreadyInited())) { - return UnsafeMutablePointer(); - } else { - return MutableSlow(arena); - } -} - -std::string* ArenaStringPtr::Mutable(const LazyString& default_value, - ::google::protobuf::Arena* arena) { - if (!IsDonatedString() && !IsDefault(nullptr)) { - return UnsafeMutablePointer(); - } else { - return MutableSlow(arena, default_value); - } -} - -std::string* ArenaStringPtr::MutableNoCopy(const std::string* default_value, - ::google::protobuf::Arena* arena) { - if (!IsDonatedString() && !IsDefault(default_value)) { - return UnsafeMutablePointer(); - } else { - GOOGLE_DCHECK(IsDefault(default_value)); - // Allocate empty. The contents are not relevant. - std::string* new_string = Arena::Create(arena); - tagged_ptr_.Set(new_string); - return new_string; - } -} - -template -std::string* ArenaStringPtr::MutableSlow(::google::protobuf::Arena* arena, - const Lazy&... lazy_default) { - const std::string* const default_value = - sizeof...(Lazy) == 0 ? &GetEmptyStringAlreadyInited() : nullptr; - GOOGLE_DCHECK(IsDefault(default_value)); - std::string* new_string = - Arena::Create(arena, lazy_default.get()...); - tagged_ptr_.Set(new_string); - return new_string; -} - -std::string* ArenaStringPtr::Release(const std::string* default_value, - ::google::protobuf::Arena* arena) { - if (IsDefault(default_value)) { - return nullptr; - } else { - return ReleaseNonDefault(default_value, arena); - } -} - -std::string* ArenaStringPtr::ReleaseNonDefault(const std::string* default_value, - ::google::protobuf::Arena* arena) { - GOOGLE_DCHECK(!IsDefault(default_value)); - - if (!IsDonatedString()) { - std::string* released; - if (arena != nullptr) { - released = new std::string; - released->swap(*UnsafeMutablePointer()); - } else { - released = UnsafeMutablePointer(); - } - tagged_ptr_.Set(const_cast(default_value)); - return released; - } else /* IsDonatedString() */ { - GOOGLE_DCHECK(arena != nullptr); - std::string* released = new std::string(Get()); - tagged_ptr_.Set(const_cast(default_value)); - return released; - } -} - -void ArenaStringPtr::SetAllocated(const std::string* default_value, - std::string* value, ::google::protobuf::Arena* arena) { - // Release what we have first. - if (arena == nullptr && !IsDefault(default_value)) { - delete UnsafeMutablePointer(); - } - if (value == nullptr) { - tagged_ptr_.Set(const_cast(default_value)); - } else { -#ifdef NDEBUG - tagged_ptr_.Set(value); - if (arena != nullptr) { - arena->Own(value); - } -#else - // On debug builds, copy the string so the address differs. delete will - // fail if value was a stack-allocated temporary/etc., which would have - // failed when arena ran its cleanup list. - std::string* new_value = Arena::Create(arena, *value); - delete value; - tagged_ptr_.Set(new_value); -#endif - } -} - -void ArenaStringPtr::Destroy(const std::string* default_value, - ::google::protobuf::Arena* arena) { - if (arena == nullptr) { - GOOGLE_DCHECK(!IsDonatedString()); - if (!IsDefault(default_value)) { - delete UnsafeMutablePointer(); - } - } -} - -void ArenaStringPtr::Destroy(EmptyDefault, ::google::protobuf::Arena* arena) { - Destroy(&GetEmptyStringAlreadyInited(), arena); -} - -void ArenaStringPtr::Destroy(NonEmptyDefault, ::google::protobuf::Arena* arena) { - Destroy(nullptr, arena); -} - -void ArenaStringPtr::ClearToEmpty() { - if (IsDefault(&GetEmptyStringAlreadyInited())) { - // Already set to default -- do nothing. - } else { - // Unconditionally mask away the tag. - // - // UpdateDonatedString uses assign when capacity is larger than the new - // value, which is trivially true in the donated string case. - // const_cast(PtrValue())->clear(); - tagged_ptr_.Get()->clear(); - } -} - -void ArenaStringPtr::ClearToDefault(const LazyString& default_value, - ::google::protobuf::Arena* arena) { - (void)arena; - if (IsDefault(nullptr)) { - // Already set to default -- do nothing. - } else if (!IsDonatedString()) { - UnsafeMutablePointer()->assign(default_value.get()); - } -} - - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/bytestream.cc b/third_party/protobuf-lite/bytestream.cc deleted file mode 100644 index a0f298edb..000000000 --- a/third_party/protobuf-lite/bytestream.cc +++ /dev/null @@ -1,198 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace strings { - -void ByteSource::CopyTo(ByteSink* sink, size_t n) { - while (n > 0) { - StringPiece fragment = Peek(); - if (fragment.empty()) { - GOOGLE_LOG(DFATAL) << "ByteSource::CopyTo() overran input."; - break; - } - std::size_t fragment_size = std::min(n, fragment.size()); - sink->Append(fragment.data(), fragment_size); - Skip(fragment_size); - n -= fragment_size; - } -} - -void ByteSink::Flush() {} - -void UncheckedArrayByteSink::Append(const char* data, size_t n) { - if (data != dest_) { - // Catch cases where the pointer returned by GetAppendBuffer() was modified. - GOOGLE_DCHECK(!(dest_ <= data && data < (dest_ + n))) - << "Append() data[] overlaps with dest_[]"; - memcpy(dest_, data, n); - } - dest_ += n; -} - -CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, size_t capacity) - : outbuf_(outbuf), capacity_(capacity), size_(0), overflowed_(false) { -} - -void CheckedArrayByteSink::Append(const char* bytes, size_t n) { - size_t available = capacity_ - size_; - if (n > available) { - n = available; - overflowed_ = true; - } - if (n > 0 && bytes != (outbuf_ + size_)) { - // Catch cases where the pointer returned by GetAppendBuffer() was modified. - GOOGLE_DCHECK(!(outbuf_ <= bytes && bytes < (outbuf_ + capacity_))) - << "Append() bytes[] overlaps with outbuf_[]"; - memcpy(outbuf_ + size_, bytes, n); - } - size_ += n; -} - -GrowingArrayByteSink::GrowingArrayByteSink(size_t estimated_size) - : capacity_(estimated_size), - buf_(new char[estimated_size]), - size_(0) { -} - -GrowingArrayByteSink::~GrowingArrayByteSink() { - delete[] buf_; // Just in case the user didn't call GetBuffer. -} - -void GrowingArrayByteSink::Append(const char* bytes, size_t n) { - size_t available = capacity_ - size_; - if (bytes != (buf_ + size_)) { - // Catch cases where the pointer returned by GetAppendBuffer() was modified. - // We need to test for this before calling Expand() which may reallocate. - GOOGLE_DCHECK(!(buf_ <= bytes && bytes < (buf_ + capacity_))) - << "Append() bytes[] overlaps with buf_[]"; - } - if (n > available) { - Expand(n - available); - } - if (n > 0 && bytes != (buf_ + size_)) { - memcpy(buf_ + size_, bytes, n); - } - size_ += n; -} - -char* GrowingArrayByteSink::GetBuffer(size_t* nbytes) { - ShrinkToFit(); - char* b = buf_; - *nbytes = size_; - buf_ = nullptr; - size_ = capacity_ = 0; - return b; -} - -void GrowingArrayByteSink::Expand(size_t amount) { // Expand by at least 50%. - size_t new_capacity = std::max(capacity_ + amount, (3 * capacity_) / 2); - char* bigger = new char[new_capacity]; - memcpy(bigger, buf_, size_); - delete[] buf_; - buf_ = bigger; - capacity_ = new_capacity; -} - -void GrowingArrayByteSink::ShrinkToFit() { - // Shrink only if the buffer is large and size_ is less than 3/4 - // of capacity_. - if (capacity_ > 256 && size_ < (3 * capacity_) / 4) { - char* just_enough = new char[size_]; - memcpy(just_enough, buf_, size_); - delete[] buf_; - buf_ = just_enough; - capacity_ = size_; - } -} - -void StringByteSink::Append(const char* data, size_t n) { - dest_->append(data, n); -} - -size_t ArrayByteSource::Available() const { - return input_.size(); -} - -StringPiece ArrayByteSource::Peek() { - return input_; -} - -void ArrayByteSource::Skip(size_t n) { - GOOGLE_DCHECK_LE(n, input_.size()); - input_.remove_prefix(n); -} - -LimitByteSource::LimitByteSource(ByteSource *source, size_t limit) - : source_(source), - limit_(limit) { -} - -size_t LimitByteSource::Available() const { - size_t available = source_->Available(); - if (available > limit_) { - available = limit_; - } - - return available; -} - -StringPiece LimitByteSource::Peek() { - StringPiece piece(source_->Peek()); - if (piece.size() > limit_) { - piece.set(piece.data(), limit_); - } - - return piece; -} - -void LimitByteSource::Skip(size_t n) { - GOOGLE_DCHECK_LE(n, limit_); - source_->Skip(n); - limit_ -= n; -} - -void LimitByteSource::CopyTo(ByteSink *sink, size_t n) { - GOOGLE_DCHECK_LE(n, limit_); - source_->CopyTo(sink, n); - limit_ -= n; -} - -} // namespace strings -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/coded_stream.cc b/third_party/protobuf-lite/coded_stream.cc deleted file mode 100644 index 59d86f983..000000000 --- a/third_party/protobuf-lite/coded_stream.cc +++ /dev/null @@ -1,956 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This implementation is heavily optimized to make reads and writes -// of small values (especially varints) as fast as possible. In -// particular, we optimize for the common case that a read or a write -// will not cross the end of the buffer, since we can avoid a lot -// of branching in this case. - -#include - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - - -#include - -namespace google { -namespace protobuf { -namespace io { - -namespace { - -static const int kMaxVarintBytes = 10; -static const int kMaxVarint32Bytes = 5; - - -inline bool NextNonEmpty(ZeroCopyInputStream* input, const void** data, - int* size) { - bool success; - do { - success = input->Next(data, size); - } while (success && *size == 0); - return success; -} - -} // namespace - -// CodedInputStream ================================================== - -CodedInputStream::~CodedInputStream() { - if (input_ != NULL) { - BackUpInputToCurrentPosition(); - } -} - -// Static. -int CodedInputStream::default_recursion_limit_ = 100; - - -void CodedInputStream::BackUpInputToCurrentPosition() { - int backup_bytes = BufferSize() + buffer_size_after_limit_ + overflow_bytes_; - if (backup_bytes > 0) { - input_->BackUp(backup_bytes); - - // total_bytes_read_ doesn't include overflow_bytes_. - total_bytes_read_ -= BufferSize() + buffer_size_after_limit_; - buffer_end_ = buffer_; - buffer_size_after_limit_ = 0; - overflow_bytes_ = 0; - } -} - -inline void CodedInputStream::RecomputeBufferLimits() { - buffer_end_ += buffer_size_after_limit_; - int closest_limit = std::min(current_limit_, total_bytes_limit_); - if (closest_limit < total_bytes_read_) { - // The limit position is in the current buffer. We must adjust - // the buffer size accordingly. - buffer_size_after_limit_ = total_bytes_read_ - closest_limit; - buffer_end_ -= buffer_size_after_limit_; - } else { - buffer_size_after_limit_ = 0; - } -} - -CodedInputStream::Limit CodedInputStream::PushLimit(int byte_limit) { - // Current position relative to the beginning of the stream. - int current_position = CurrentPosition(); - - Limit old_limit = current_limit_; - - // security: byte_limit is possibly evil, so check for negative values - // and overflow. Also check that the new requested limit is before the - // previous limit; otherwise we continue to enforce the previous limit. - if (PROTOBUF_PREDICT_TRUE(byte_limit >= 0 && - byte_limit <= INT_MAX - current_position && - byte_limit < current_limit_ - current_position)) { - current_limit_ = current_position + byte_limit; - RecomputeBufferLimits(); - } - - return old_limit; -} - -void CodedInputStream::PopLimit(Limit limit) { - // The limit passed in is actually the *old* limit, which we returned from - // PushLimit(). - current_limit_ = limit; - RecomputeBufferLimits(); - - // We may no longer be at a legitimate message end. ReadTag() needs to be - // called again to find out. - legitimate_message_end_ = false; -} - -std::pair -CodedInputStream::IncrementRecursionDepthAndPushLimit(int byte_limit) { - return std::make_pair(PushLimit(byte_limit), --recursion_budget_); -} - -CodedInputStream::Limit CodedInputStream::ReadLengthAndPushLimit() { - uint32 length; - return PushLimit(ReadVarint32(&length) ? length : 0); -} - -bool CodedInputStream::DecrementRecursionDepthAndPopLimit(Limit limit) { - bool result = ConsumedEntireMessage(); - PopLimit(limit); - GOOGLE_DCHECK_LT(recursion_budget_, recursion_limit_); - ++recursion_budget_; - return result; -} - -bool CodedInputStream::CheckEntireMessageConsumedAndPopLimit(Limit limit) { - bool result = ConsumedEntireMessage(); - PopLimit(limit); - return result; -} - -int CodedInputStream::BytesUntilLimit() const { - if (current_limit_ == INT_MAX) return -1; - int current_position = CurrentPosition(); - - return current_limit_ - current_position; -} - -void CodedInputStream::SetTotalBytesLimit(int total_bytes_limit) { - // Make sure the limit isn't already past, since this could confuse other - // code. - int current_position = CurrentPosition(); - total_bytes_limit_ = std::max(current_position, total_bytes_limit); - RecomputeBufferLimits(); -} - -int CodedInputStream::BytesUntilTotalBytesLimit() const { - if (total_bytes_limit_ == INT_MAX) return -1; - return total_bytes_limit_ - CurrentPosition(); -} - -void CodedInputStream::PrintTotalBytesLimitError() { - GOOGLE_LOG(ERROR) - << "A protocol message was rejected because it was too " - "big (more than " - << total_bytes_limit_ - << " bytes). To increase the limit (or to disable these " - "warnings), see CodedInputStream::SetTotalBytesLimit() " - "in third_party/protobuf/src/google/protobuf/io/coded_stream.h."; -} - -bool CodedInputStream::SkipFallback(int count, int original_buffer_size) { - if (buffer_size_after_limit_ > 0) { - // We hit a limit inside this buffer. Advance to the limit and fail. - Advance(original_buffer_size); - return false; - } - - count -= original_buffer_size; - buffer_ = NULL; - buffer_end_ = buffer_; - - // Make sure this skip doesn't try to skip past the current limit. - int closest_limit = std::min(current_limit_, total_bytes_limit_); - int bytes_until_limit = closest_limit - total_bytes_read_; - if (bytes_until_limit < count) { - // We hit the limit. Skip up to it then fail. - if (bytes_until_limit > 0) { - total_bytes_read_ = closest_limit; - input_->Skip(bytes_until_limit); - } - return false; - } - - if (!input_->Skip(count)) { - total_bytes_read_ = input_->ByteCount(); - return false; - } - total_bytes_read_ += count; - return true; -} - -bool CodedInputStream::GetDirectBufferPointer(const void** data, int* size) { - if (BufferSize() == 0 && !Refresh()) return false; - - *data = buffer_; - *size = BufferSize(); - return true; -} - -bool CodedInputStream::ReadRaw(void* buffer, int size) { - int current_buffer_size; - while ((current_buffer_size = BufferSize()) < size) { - // Reading past end of buffer. Copy what we have, then refresh. - memcpy(buffer, buffer_, current_buffer_size); - buffer = reinterpret_cast(buffer) + current_buffer_size; - size -= current_buffer_size; - Advance(current_buffer_size); - if (!Refresh()) return false; - } - - memcpy(buffer, buffer_, size); - Advance(size); - - return true; -} - -bool CodedInputStream::ReadString(std::string* buffer, int size) { - if (size < 0) return false; // security: size is often user-supplied - - if (BufferSize() >= size) { - STLStringResizeUninitialized(buffer, size); - std::pair z = as_string_data(buffer); - if (z.second) { - // Oddly enough, memcpy() requires its first two args to be non-NULL even - // if we copy 0 bytes. So, we have ensured that z.first is non-NULL here. - GOOGLE_DCHECK(z.first != NULL); - memcpy(z.first, buffer_, size); - Advance(size); - } - return true; - } - - return ReadStringFallback(buffer, size); -} - -bool CodedInputStream::ReadStringFallback(std::string* buffer, int size) { - if (!buffer->empty()) { - buffer->clear(); - } - - int closest_limit = std::min(current_limit_, total_bytes_limit_); - if (closest_limit != INT_MAX) { - int bytes_to_limit = closest_limit - CurrentPosition(); - if (bytes_to_limit > 0 && size > 0 && size <= bytes_to_limit) { - buffer->reserve(size); - } - } - - int current_buffer_size; - while ((current_buffer_size = BufferSize()) < size) { - // Some STL implementations "helpfully" crash on buffer->append(NULL, 0). - if (current_buffer_size != 0) { - // Note: string1.append(string2) is O(string2.size()) (as opposed to - // O(string1.size() + string2.size()), which would be bad). - buffer->append(reinterpret_cast(buffer_), - current_buffer_size); - } - size -= current_buffer_size; - Advance(current_buffer_size); - if (!Refresh()) return false; - } - - buffer->append(reinterpret_cast(buffer_), size); - Advance(size); - - return true; -} - - -bool CodedInputStream::ReadLittleEndian32Fallback(uint32* value) { - uint8 bytes[sizeof(*value)]; - - const uint8* ptr; - if (BufferSize() >= sizeof(*value)) { - // Fast path: Enough bytes in the buffer to read directly. - ptr = buffer_; - Advance(sizeof(*value)); - } else { - // Slow path: Had to read past the end of the buffer. - if (!ReadRaw(bytes, sizeof(*value))) return false; - ptr = bytes; - } - ReadLittleEndian32FromArray(ptr, value); - return true; -} - -bool CodedInputStream::ReadLittleEndian64Fallback(uint64* value) { - uint8 bytes[sizeof(*value)]; - - const uint8* ptr; - if (BufferSize() >= sizeof(*value)) { - // Fast path: Enough bytes in the buffer to read directly. - ptr = buffer_; - Advance(sizeof(*value)); - } else { - // Slow path: Had to read past the end of the buffer. - if (!ReadRaw(bytes, sizeof(*value))) return false; - ptr = bytes; - } - ReadLittleEndian64FromArray(ptr, value); - return true; -} - -namespace { - -// Decodes varint64 with known size, N, and returns next pointer. Knowing N at -// compile time, compiler can generate optimal code. For example, instead of -// subtracting 0x80 at each iteration, it subtracts properly shifted mask once. -template -const uint8* DecodeVarint64KnownSize(const uint8* buffer, uint64* value) { - GOOGLE_DCHECK_GT(N, 0); - uint64 result = static_cast(buffer[N - 1]) << (7 * (N - 1)); - for (int i = 0, offset = 0; i < N - 1; i++, offset += 7) { - result += static_cast(buffer[i] - 0x80) << offset; - } - *value = result; - return buffer + N; -} - -// Read a varint from the given buffer, write it to *value, and return a pair. -// The first part of the pair is true iff the read was successful. The second -// part is buffer + (number of bytes read). This function is always inlined, -// so returning a pair is costless. -PROTOBUF_ALWAYS_INLINE -::std::pair ReadVarint32FromArray(uint32 first_byte, - const uint8* buffer, - uint32* value); -inline ::std::pair ReadVarint32FromArray( - uint32 first_byte, const uint8* buffer, uint32* value) { - // Fast path: We have enough bytes left in the buffer to guarantee that - // this read won't cross the end, so we can skip the checks. - GOOGLE_DCHECK_EQ(*buffer, first_byte); - GOOGLE_DCHECK_EQ(first_byte & 0x80, 0x80) << first_byte; - const uint8* ptr = buffer; - uint32 b; - uint32 result = first_byte - 0x80; - ++ptr; // We just processed the first byte. Move on to the second. - b = *(ptr++); - result += b << 7; - if (!(b & 0x80)) goto done; - result -= 0x80 << 7; - b = *(ptr++); - result += b << 14; - if (!(b & 0x80)) goto done; - result -= 0x80 << 14; - b = *(ptr++); - result += b << 21; - if (!(b & 0x80)) goto done; - result -= 0x80 << 21; - b = *(ptr++); - result += b << 28; - if (!(b & 0x80)) goto done; - // "result -= 0x80 << 28" is irrevelant. - - // If the input is larger than 32 bits, we still need to read it all - // and discard the high-order bits. - for (int i = 0; i < kMaxVarintBytes - kMaxVarint32Bytes; i++) { - b = *(ptr++); - if (!(b & 0x80)) goto done; - } - - // We have overrun the maximum size of a varint (10 bytes). Assume - // the data is corrupt. - return std::make_pair(false, ptr); - -done: - *value = result; - return std::make_pair(true, ptr); -} - -PROTOBUF_ALWAYS_INLINE::std::pair ReadVarint64FromArray( - const uint8* buffer, uint64* value); -inline ::std::pair ReadVarint64FromArray( - const uint8* buffer, uint64* value) { - // Assumes varint64 is at least 2 bytes. - GOOGLE_DCHECK_GE(buffer[0], 128); - - const uint8* next; - if (buffer[1] < 128) { - next = DecodeVarint64KnownSize<2>(buffer, value); - } else if (buffer[2] < 128) { - next = DecodeVarint64KnownSize<3>(buffer, value); - } else if (buffer[3] < 128) { - next = DecodeVarint64KnownSize<4>(buffer, value); - } else if (buffer[4] < 128) { - next = DecodeVarint64KnownSize<5>(buffer, value); - } else if (buffer[5] < 128) { - next = DecodeVarint64KnownSize<6>(buffer, value); - } else if (buffer[6] < 128) { - next = DecodeVarint64KnownSize<7>(buffer, value); - } else if (buffer[7] < 128) { - next = DecodeVarint64KnownSize<8>(buffer, value); - } else if (buffer[8] < 128) { - next = DecodeVarint64KnownSize<9>(buffer, value); - } else if (buffer[9] < 128) { - next = DecodeVarint64KnownSize<10>(buffer, value); - } else { - // We have overrun the maximum size of a varint (10 bytes). Assume - // the data is corrupt. - return std::make_pair(false, buffer + 11); - } - - return std::make_pair(true, next); -} - -} // namespace - -bool CodedInputStream::ReadVarint32Slow(uint32* value) { - // Directly invoke ReadVarint64Fallback, since we already tried to optimize - // for one-byte varints. - std::pair p = ReadVarint64Fallback(); - *value = static_cast(p.first); - return p.second; -} - -int64 CodedInputStream::ReadVarint32Fallback(uint32 first_byte_or_zero) { - if (BufferSize() >= kMaxVarintBytes || - // Optimization: We're also safe if the buffer is non-empty and it ends - // with a byte that would terminate a varint. - (buffer_end_ > buffer_ && !(buffer_end_[-1] & 0x80))) { - GOOGLE_DCHECK_NE(first_byte_or_zero, 0) - << "Caller should provide us with *buffer_ when buffer is non-empty"; - uint32 temp; - ::std::pair p = - ReadVarint32FromArray(first_byte_or_zero, buffer_, &temp); - if (!p.first) return -1; - buffer_ = p.second; - return temp; - } else { - // Really slow case: we will incur the cost of an extra function call here, - // but moving this out of line reduces the size of this function, which - // improves the common case. In micro benchmarks, this is worth about 10-15% - uint32 temp; - return ReadVarint32Slow(&temp) ? static_cast(temp) : -1; - } -} - -int CodedInputStream::ReadVarintSizeAsIntSlow() { - // Directly invoke ReadVarint64Fallback, since we already tried to optimize - // for one-byte varints. - std::pair p = ReadVarint64Fallback(); - if (!p.second || p.first > static_cast(INT_MAX)) return -1; - return p.first; -} - -int CodedInputStream::ReadVarintSizeAsIntFallback() { - if (BufferSize() >= kMaxVarintBytes || - // Optimization: We're also safe if the buffer is non-empty and it ends - // with a byte that would terminate a varint. - (buffer_end_ > buffer_ && !(buffer_end_[-1] & 0x80))) { - uint64 temp; - ::std::pair p = ReadVarint64FromArray(buffer_, &temp); - if (!p.first || temp > static_cast(INT_MAX)) return -1; - buffer_ = p.second; - return temp; - } else { - // Really slow case: we will incur the cost of an extra function call here, - // but moving this out of line reduces the size of this function, which - // improves the common case. In micro benchmarks, this is worth about 10-15% - return ReadVarintSizeAsIntSlow(); - } -} - -uint32 CodedInputStream::ReadTagSlow() { - if (buffer_ == buffer_end_) { - // Call refresh. - if (!Refresh()) { - // Refresh failed. Make sure that it failed due to EOF, not because - // we hit total_bytes_limit_, which, unlike normal limits, is not a - // valid place to end a message. - int current_position = total_bytes_read_ - buffer_size_after_limit_; - if (current_position >= total_bytes_limit_) { - // Hit total_bytes_limit_. But if we also hit the normal limit, - // we're still OK. - legitimate_message_end_ = current_limit_ == total_bytes_limit_; - } else { - legitimate_message_end_ = true; - } - return 0; - } - } - - // For the slow path, just do a 64-bit read. Try to optimize for one-byte tags - // again, since we have now refreshed the buffer. - uint64 result = 0; - if (!ReadVarint64(&result)) return 0; - return static_cast(result); -} - -uint32 CodedInputStream::ReadTagFallback(uint32 first_byte_or_zero) { - const int buf_size = BufferSize(); - if (buf_size >= kMaxVarintBytes || - // Optimization: We're also safe if the buffer is non-empty and it ends - // with a byte that would terminate a varint. - (buf_size > 0 && !(buffer_end_[-1] & 0x80))) { - GOOGLE_DCHECK_EQ(first_byte_or_zero, buffer_[0]); - if (first_byte_or_zero == 0) { - ++buffer_; - return 0; - } - uint32 tag; - ::std::pair p = - ReadVarint32FromArray(first_byte_or_zero, buffer_, &tag); - if (!p.first) { - return 0; - } - buffer_ = p.second; - return tag; - } else { - // We are commonly at a limit when attempting to read tags. Try to quickly - // detect this case without making another function call. - if ((buf_size == 0) && - ((buffer_size_after_limit_ > 0) || - (total_bytes_read_ == current_limit_)) && - // Make sure that the limit we hit is not total_bytes_limit_, since - // in that case we still need to call Refresh() so that it prints an - // error. - total_bytes_read_ - buffer_size_after_limit_ < total_bytes_limit_) { - // We hit a byte limit. - legitimate_message_end_ = true; - return 0; - } - return ReadTagSlow(); - } -} - -bool CodedInputStream::ReadVarint64Slow(uint64* value) { - // Slow path: This read might cross the end of the buffer, so we - // need to check and refresh the buffer if and when it does. - - uint64 result = 0; - int count = 0; - uint32 b; - - do { - if (count == kMaxVarintBytes) { - *value = 0; - return false; - } - while (buffer_ == buffer_end_) { - if (!Refresh()) { - *value = 0; - return false; - } - } - b = *buffer_; - result |= static_cast(b & 0x7F) << (7 * count); - Advance(1); - ++count; - } while (b & 0x80); - - *value = result; - return true; -} - -std::pair CodedInputStream::ReadVarint64Fallback() { - if (BufferSize() >= kMaxVarintBytes || - // Optimization: We're also safe if the buffer is non-empty and it ends - // with a byte that would terminate a varint. - (buffer_end_ > buffer_ && !(buffer_end_[-1] & 0x80))) { - uint64 temp; - ::std::pair p = ReadVarint64FromArray(buffer_, &temp); - if (!p.first) { - return std::make_pair(0, false); - } - buffer_ = p.second; - return std::make_pair(temp, true); - } else { - uint64 temp; - bool success = ReadVarint64Slow(&temp); - return std::make_pair(temp, success); - } -} - -bool CodedInputStream::Refresh() { - GOOGLE_DCHECK_EQ(0, BufferSize()); - - if (buffer_size_after_limit_ > 0 || overflow_bytes_ > 0 || - total_bytes_read_ == current_limit_) { - // We've hit a limit. Stop. - int current_position = total_bytes_read_ - buffer_size_after_limit_; - - if (current_position >= total_bytes_limit_ && - total_bytes_limit_ != current_limit_) { - // Hit total_bytes_limit_. - PrintTotalBytesLimitError(); - } - - return false; - } - - const void* void_buffer; - int buffer_size; - if (NextNonEmpty(input_, &void_buffer, &buffer_size)) { - buffer_ = reinterpret_cast(void_buffer); - buffer_end_ = buffer_ + buffer_size; - GOOGLE_CHECK_GE(buffer_size, 0); - - if (total_bytes_read_ <= INT_MAX - buffer_size) { - total_bytes_read_ += buffer_size; - } else { - // Overflow. Reset buffer_end_ to not include the bytes beyond INT_MAX. - // We can't get that far anyway, because total_bytes_limit_ is guaranteed - // to be less than it. We need to keep track of the number of bytes - // we discarded, though, so that we can call input_->BackUp() to back - // up over them on destruction. - - // The following line is equivalent to: - // overflow_bytes_ = total_bytes_read_ + buffer_size - INT_MAX; - // except that it avoids overflows. Signed integer overflow has - // undefined results according to the C standard. - overflow_bytes_ = total_bytes_read_ - (INT_MAX - buffer_size); - buffer_end_ -= overflow_bytes_; - total_bytes_read_ = INT_MAX; - } - - RecomputeBufferLimits(); - return true; - } else { - buffer_ = NULL; - buffer_end_ = NULL; - return false; - } -} - -// CodedOutputStream ================================================= - -void EpsCopyOutputStream::EnableAliasing(bool enabled) { - aliasing_enabled_ = enabled && stream_->AllowsAliasing(); -} - -int64 EpsCopyOutputStream::ByteCount(uint8* ptr) const { - // Calculate the current offset relative to the end of the stream buffer. - int delta = (end_ - ptr) + (buffer_end_ ? 0 : kSlopBytes); - return stream_->ByteCount() - delta; -} - -// Flushes what's written out to the underlying ZeroCopyOutputStream buffers. -// Returns the size remaining in the buffer and sets buffer_end_ to the start -// of the remaining buffer, ie. [buffer_end_, buffer_end_ + return value) -int EpsCopyOutputStream::Flush(uint8* ptr) { - while (buffer_end_ && ptr > end_) { - int overrun = ptr - end_; - GOOGLE_DCHECK(!had_error_); - GOOGLE_DCHECK(overrun <= kSlopBytes); // NOLINT - ptr = Next() + overrun; - if (had_error_) return 0; - } - int s; - if (buffer_end_) { - std::memcpy(buffer_end_, buffer_, ptr - buffer_); - buffer_end_ += ptr - buffer_; - s = end_ - ptr; - } else { - // The stream is writing directly in the ZeroCopyOutputStream buffer. - s = end_ + kSlopBytes - ptr; - buffer_end_ = ptr; - } - GOOGLE_DCHECK(s >= 0); // NOLINT - return s; -} - -uint8* EpsCopyOutputStream::Trim(uint8* ptr) { - if (had_error_) return ptr; - int s = Flush(ptr); - if (s) stream_->BackUp(s); - // Reset to initial state (expecting new buffer) - buffer_end_ = end_ = buffer_; - return buffer_; -} - - -uint8* EpsCopyOutputStream::FlushAndResetBuffer(uint8* ptr) { - if (had_error_) return buffer_; - int s = Flush(ptr); - if (had_error_) return buffer_; - return SetInitialBuffer(buffer_end_, s); -} - -bool EpsCopyOutputStream::Skip(int count, uint8** pp) { - if (count < 0) return false; - if (had_error_) { - *pp = buffer_; - return false; - } - int size = Flush(*pp); - if (had_error_) { - *pp = buffer_; - return false; - } - void* data = buffer_end_; - while (count > size) { - count -= size; - if (!stream_->Next(&data, &size)) { - *pp = Error(); - return false; - } - } - *pp = SetInitialBuffer(static_cast(data) + count, size - count); - return true; -} - -bool EpsCopyOutputStream::GetDirectBufferPointer(void** data, int* size, - uint8** pp) { - if (had_error_) { - *pp = buffer_; - return false; - } - *size = Flush(*pp); - if (had_error_) { - *pp = buffer_; - return false; - } - *data = buffer_end_; - while (*size == 0) { - if (!stream_->Next(data, size)) { - *pp = Error(); - return false; - } - } - *pp = SetInitialBuffer(*data, *size); - return true; -} - -uint8* EpsCopyOutputStream::GetDirectBufferForNBytesAndAdvance(int size, - uint8** pp) { - if (had_error_) { - *pp = buffer_; - return nullptr; - } - int s = Flush(*pp); - if (had_error_) { - *pp = buffer_; - return nullptr; - } - if (s >= size) { - auto res = buffer_end_; - *pp = SetInitialBuffer(buffer_end_ + size, s - size); - return res; - } else { - *pp = SetInitialBuffer(buffer_end_, s); - return nullptr; - } -} - -uint8* EpsCopyOutputStream::Next() { - GOOGLE_DCHECK(!had_error_); // NOLINT - if (PROTOBUF_PREDICT_FALSE(stream_ == nullptr)) return Error(); - if (buffer_end_) { - // We're in the patch buffer and need to fill up the previous buffer. - std::memcpy(buffer_end_, buffer_, end_ - buffer_); - uint8* ptr; - int size; - do { - void* data; - if (PROTOBUF_PREDICT_FALSE(!stream_->Next(&data, &size))) { - // Stream has an error, we use the patch buffer to continue to be - // able to write. - return Error(); - } - ptr = static_cast(data); - } while (size == 0); - if (PROTOBUF_PREDICT_TRUE(size > kSlopBytes)) { - std::memcpy(ptr, end_, kSlopBytes); - end_ = ptr + size - kSlopBytes; - buffer_end_ = nullptr; - return ptr; - } else { - GOOGLE_DCHECK(size > 0); // NOLINT - // Buffer to small - std::memmove(buffer_, end_, kSlopBytes); - buffer_end_ = ptr; - end_ = buffer_ + size; - return buffer_; - } - } else { - std::memcpy(buffer_, end_, kSlopBytes); - buffer_end_ = end_; - end_ = buffer_ + kSlopBytes; - return buffer_; - } -} - -uint8* EpsCopyOutputStream::EnsureSpaceFallback(uint8* ptr) { - do { - if (PROTOBUF_PREDICT_FALSE(had_error_)) return buffer_; - int overrun = ptr - end_; - GOOGLE_DCHECK(overrun >= 0); // NOLINT - GOOGLE_DCHECK(overrun <= kSlopBytes); // NOLINT - ptr = Next() + overrun; - } while (ptr >= end_); - GOOGLE_DCHECK(ptr < end_); // NOLINT - return ptr; -} - -uint8* EpsCopyOutputStream::WriteRawFallback(const void* data, int size, - uint8* ptr) { - int s = GetSize(ptr); - while (s < size) { - std::memcpy(ptr, data, s); - size -= s; - data = static_cast(data) + s; - ptr = EnsureSpaceFallback(ptr + s); - s = GetSize(ptr); - } - std::memcpy(ptr, data, size); - return ptr + size; -} - -uint8* EpsCopyOutputStream::WriteAliasedRaw(const void* data, int size, - uint8* ptr) { - if (size < GetSize(ptr) - ) { - return WriteRaw(data, size, ptr); - } else { - ptr = Trim(ptr); - if (stream_->WriteAliasedRaw(data, size)) return ptr; - return Error(); - } -} - -#ifndef PROTOBUF_LITTLE_ENDIAN -uint8* EpsCopyOutputStream::WriteRawLittleEndian32(const void* data, int size, - uint8* ptr) { - auto p = static_cast(data); - auto end = p + size; - while (end - p >= kSlopBytes) { - ptr = EnsureSpace(ptr); - uint32 buffer[4]; - static_assert(sizeof(buffer) == kSlopBytes, "Buffer must be kSlopBytes"); - std::memcpy(buffer, p, kSlopBytes); - p += kSlopBytes; - for (auto x : buffer) - ptr = CodedOutputStream::WriteLittleEndian32ToArray(x, ptr); - } - while (p < end) { - ptr = EnsureSpace(ptr); - uint32 buffer; - std::memcpy(&buffer, p, 4); - p += 4; - ptr = CodedOutputStream::WriteLittleEndian32ToArray(buffer, ptr); - } - return ptr; -} - -uint8* EpsCopyOutputStream::WriteRawLittleEndian64(const void* data, int size, - uint8* ptr) { - auto p = static_cast(data); - auto end = p + size; - while (end - p >= kSlopBytes) { - ptr = EnsureSpace(ptr); - uint64 buffer[2]; - static_assert(sizeof(buffer) == kSlopBytes, "Buffer must be kSlopBytes"); - std::memcpy(buffer, p, kSlopBytes); - p += kSlopBytes; - for (auto x : buffer) - ptr = CodedOutputStream::WriteLittleEndian64ToArray(x, ptr); - } - while (p < end) { - ptr = EnsureSpace(ptr); - uint64 buffer; - std::memcpy(&buffer, p, 8); - p += 8; - ptr = CodedOutputStream::WriteLittleEndian64ToArray(buffer, ptr); - } - return ptr; -} -#endif - - -uint8* EpsCopyOutputStream::WriteStringMaybeAliasedOutline(uint32 num, - const std::string& s, - uint8* ptr) { - ptr = EnsureSpace(ptr); - uint32 size = s.size(); - ptr = WriteLengthDelim(num, size, ptr); - return WriteRawMaybeAliased(s.data(), size, ptr); -} - -uint8* EpsCopyOutputStream::WriteStringOutline(uint32 num, const std::string& s, - uint8* ptr) { - ptr = EnsureSpace(ptr); - uint32 size = s.size(); - ptr = WriteLengthDelim(num, size, ptr); - return WriteRaw(s.data(), size, ptr); -} - -std::atomic CodedOutputStream::default_serialization_deterministic_{ - false}; - -CodedOutputStream::CodedOutputStream(ZeroCopyOutputStream* stream, - bool do_eager_refresh) - : impl_(stream, IsDefaultSerializationDeterministic(), &cur_), - start_count_(stream->ByteCount()) { - if (do_eager_refresh) { - void* data; - int size; - if (!stream->Next(&data, &size) || size == 0) return; - cur_ = impl_.SetInitialBuffer(data, size); - } -} - -CodedOutputStream::~CodedOutputStream() { Trim(); } - - -uint8* CodedOutputStream::WriteStringWithSizeToArray(const std::string& str, - uint8* target) { - GOOGLE_DCHECK_LE(str.size(), kuint32max); - target = WriteVarint32ToArray(str.size(), target); - return WriteStringToArray(str, target); -} - -} // namespace io -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/common.cc b/third_party/protobuf-lite/common.cc deleted file mode 100644 index bc150f56a..000000000 --- a/third_party/protobuf-lite/common.cc +++ /dev/null @@ -1,329 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) - -#include - -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN // We only need minimal includes -#endif -#include -#define snprintf _snprintf // see comment in strutil.cc -#elif defined(HAVE_PTHREAD) -#include -#else -#error "No suitable threading library available." -#endif -#if defined(__ANDROID__) -#include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { - -namespace internal { - -void VerifyVersion(int headerVersion, - int minLibraryVersion, - const char* filename) { - if (GOOGLE_PROTOBUF_VERSION < minLibraryVersion) { - // Library is too old for headers. - GOOGLE_LOG(FATAL) - << "This program requires version " << VersionString(minLibraryVersion) - << " of the Protocol Buffer runtime library, but the installed version " - "is " << VersionString(GOOGLE_PROTOBUF_VERSION) << ". Please update " - "your library. If you compiled the program yourself, make sure that " - "your headers are from the same version of Protocol Buffers as your " - "link-time library. (Version verification failed in \"" - << filename << "\".)"; - } - if (headerVersion < kMinHeaderVersionForLibrary) { - // Headers are too old for library. - GOOGLE_LOG(FATAL) - << "This program was compiled against version " - << VersionString(headerVersion) << " of the Protocol Buffer runtime " - "library, which is not compatible with the installed version (" - << VersionString(GOOGLE_PROTOBUF_VERSION) << "). Contact the program " - "author for an update. If you compiled the program yourself, make " - "sure that your headers are from the same version of Protocol Buffers " - "as your link-time library. (Version verification failed in \"" - << filename << "\".)"; - } -} - -std::string VersionString(int version) { - int major = version / 1000000; - int minor = (version / 1000) % 1000; - int micro = version % 1000; - - // 128 bytes should always be enough, but we use snprintf() anyway to be - // safe. - char buffer[128]; - snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro); - - // Guard against broken MSVC snprintf(). - buffer[sizeof(buffer)-1] = '\0'; - - return buffer; -} - -} // namespace internal - -// =================================================================== -// emulates google3/base/logging.cc - -// If the minimum logging level is not set, we default to logging messages for -// all levels. -#ifndef GOOGLE_PROTOBUF_MIN_LOG_LEVEL -#define GOOGLE_PROTOBUF_MIN_LOG_LEVEL LOGLEVEL_INFO -#endif - -namespace internal { - -#if defined(__ANDROID__) -inline void DefaultLogHandler(LogLevel level, const char* filename, int line, - const std::string& message) { - if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) { - return; - } - static const char* level_names[] = {"INFO", "WARNING", "ERROR", "FATAL"}; - - static const int android_log_levels[] = { - ANDROID_LOG_INFO, // LOG(INFO), - ANDROID_LOG_WARN, // LOG(WARNING) - ANDROID_LOG_ERROR, // LOG(ERROR) - ANDROID_LOG_FATAL, // LOG(FATAL) - }; - - // Bound the logging level. - const int android_log_level = android_log_levels[level]; - ::std::ostringstream ostr; - ostr << "[libprotobuf " << level_names[level] << " " << filename << ":" - << line << "] " << message.c_str(); - - // Output the log string the Android log at the appropriate level. - __android_log_write(android_log_level, "libprotobuf-native", - ostr.str().c_str()); - // Also output to std::cerr. - fprintf(stderr, "%s", ostr.str().c_str()); - fflush(stderr); - - // Indicate termination if needed. - if (android_log_level == ANDROID_LOG_FATAL) { - __android_log_write(ANDROID_LOG_FATAL, "libprotobuf-native", - "terminating.\n"); - } -} - -#else -void DefaultLogHandler(LogLevel level, const char* filename, int line, - const std::string& message) { - if (level < GOOGLE_PROTOBUF_MIN_LOG_LEVEL) { - return; - } - static const char* level_names[] = { "INFO", "WARNING", "ERROR", "FATAL" }; - - // We use fprintf() instead of cerr because we want this to work at static - // initialization time. - fprintf(stderr, "[libprotobuf %s %s:%d] %s\n", - level_names[level], filename, line, message.c_str()); - fflush(stderr); // Needed on MSVC. -} -#endif - -void NullLogHandler(LogLevel /* level */, const char* /* filename */, - int /* line */, const std::string& /* message */) { - // Nothing. -} - -static LogHandler* log_handler_ = &DefaultLogHandler; -static std::atomic log_silencer_count_ = ATOMIC_VAR_INIT(0); - -LogMessage& LogMessage::operator<<(const std::string& value) { - message_ += value; - return *this; -} - -LogMessage& LogMessage::operator<<(const char* value) { - message_ += value; - return *this; -} - -LogMessage& LogMessage::operator<<(const StringPiece& value) { - message_ += value.ToString(); - return *this; -} - -LogMessage& LogMessage::operator<<(const util::Status& status) { - message_ += status.ToString(); - return *this; -} - -LogMessage& LogMessage::operator<<(const uint128& value) { - std::ostringstream str; - str << value; - message_ += str.str(); - return *this; -} - -// Since this is just for logging, we don't care if the current locale changes -// the results -- in fact, we probably prefer that. So we use snprintf() -// instead of Simple*toa(). -#undef DECLARE_STREAM_OPERATOR -#define DECLARE_STREAM_OPERATOR(TYPE, FORMAT) \ - LogMessage& LogMessage::operator<<(TYPE value) { \ - /* 128 bytes should be big enough for any of the primitive */ \ - /* values which we print with this, but well use snprintf() */ \ - /* anyway to be extra safe. */ \ - char buffer[128]; \ - snprintf(buffer, sizeof(buffer), FORMAT, value); \ - /* Guard against broken MSVC snprintf(). */ \ - buffer[sizeof(buffer)-1] = '\0'; \ - message_ += buffer; \ - return *this; \ - } - -DECLARE_STREAM_OPERATOR(char , "%c" ) -DECLARE_STREAM_OPERATOR(int , "%d" ) -DECLARE_STREAM_OPERATOR(unsigned int , "%u" ) -DECLARE_STREAM_OPERATOR(long , "%ld") -DECLARE_STREAM_OPERATOR(unsigned long, "%lu") -DECLARE_STREAM_OPERATOR(double , "%g" ) -DECLARE_STREAM_OPERATOR(void* , "%p" ) -DECLARE_STREAM_OPERATOR(long long , "%" PROTOBUF_LL_FORMAT "d") -DECLARE_STREAM_OPERATOR(unsigned long long, "%" PROTOBUF_LL_FORMAT "u") -#undef DECLARE_STREAM_OPERATOR - -LogMessage::LogMessage(LogLevel level, const char* filename, int line) - : level_(level), filename_(filename), line_(line) {} -LogMessage::~LogMessage() {} - -void LogMessage::Finish() { - bool suppress = false; - - if (level_ != LOGLEVEL_FATAL) { - suppress = log_silencer_count_ > 0; - } - - if (!suppress) { - log_handler_(level_, filename_, line_, message_); - } - - if (level_ == LOGLEVEL_FATAL) { -#if PROTOBUF_USE_EXCEPTIONS - throw FatalException(filename_, line_, message_); -#else - abort(); -#endif - } -} - -void LogFinisher::operator=(LogMessage& other) { - other.Finish(); -} - -} // namespace internal - -LogHandler* SetLogHandler(LogHandler* new_func) { - LogHandler* old = internal::log_handler_; - if (old == &internal::NullLogHandler) { - old = nullptr; - } - if (new_func == nullptr) { - internal::log_handler_ = &internal::NullLogHandler; - } else { - internal::log_handler_ = new_func; - } - return old; -} - -LogSilencer::LogSilencer() { - ++internal::log_silencer_count_; -}; - -LogSilencer::~LogSilencer() { - --internal::log_silencer_count_; -}; - -// =================================================================== -// emulates google3/base/callback.cc - -Closure::~Closure() {} - -namespace internal { FunctionClosure0::~FunctionClosure0() {} } - -void DoNothing() {} - -// =================================================================== -// emulates google3/util/endian/endian.h -// -// TODO(xiaofeng): PROTOBUF_LITTLE_ENDIAN is unfortunately defined in -// google/protobuf/io/coded_stream.h and therefore can not be used here. -// Maybe move that macro definition here in the future. -uint32 ghtonl(uint32 x) { - union { - uint32 result; - uint8 result_array[4]; - }; - result_array[0] = static_cast(x >> 24); - result_array[1] = static_cast((x >> 16) & 0xFF); - result_array[2] = static_cast((x >> 8) & 0xFF); - result_array[3] = static_cast(x & 0xFF); - return result; -} - -#if PROTOBUF_USE_EXCEPTIONS -FatalException::~FatalException() throw() {} - -const char* FatalException::what() const throw() { - return message_.c_str(); -} -#endif - -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/extension_set.cc b/third_party/protobuf-lite/extension_set.cc deleted file mode 100644 index bfa1c42a8..000000000 --- a/third_party/protobuf-lite/extension_set.cc +++ /dev/null @@ -1,2143 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { - -namespace { - -inline WireFormatLite::FieldType real_type(FieldType type) { - GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE); - return static_cast(type); -} - -inline WireFormatLite::CppType cpp_type(FieldType type) { - return WireFormatLite::FieldTypeToCppType(real_type(type)); -} - -inline bool is_packable(WireFormatLite::WireType type) { - switch (type) { - case WireFormatLite::WIRETYPE_VARINT: - case WireFormatLite::WIRETYPE_FIXED64: - case WireFormatLite::WIRETYPE_FIXED32: - return true; - case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: - case WireFormatLite::WIRETYPE_START_GROUP: - case WireFormatLite::WIRETYPE_END_GROUP: - return false; - - // Do not add a default statement. Let the compiler complain when someone - // adds a new wire type. - } - GOOGLE_LOG(FATAL) << "can't reach here."; - return false; -} - -// Registry stuff. -struct ExtensionHasher { - std::size_t operator()(const std::pair& p) const { - return std::hash{}(p.first) ^ - std::hash{}(p.second); - } -}; - -typedef std::unordered_map, ExtensionInfo, - ExtensionHasher> - ExtensionRegistry; - -static const ExtensionRegistry* global_registry = nullptr; - -// This function is only called at startup, so there is no need for thread- -// safety. -void Register(const MessageLite* containing_type, int number, - ExtensionInfo info) { - static auto local_static_registry = OnShutdownDelete(new ExtensionRegistry); - global_registry = local_static_registry; - if (!InsertIfNotPresent(local_static_registry, - std::make_pair(containing_type, number), info)) { - GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \"" - << containing_type->GetTypeName() << "\", field number " - << number << "."; - } -} - -const ExtensionInfo* FindRegisteredExtension(const MessageLite* containing_type, - int number) { - return global_registry == nullptr - ? nullptr - : FindOrNull(*global_registry, - std::make_pair(containing_type, number)); -} - -} // namespace - -ExtensionFinder::~ExtensionFinder() {} - -bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) { - const ExtensionInfo* extension = - FindRegisteredExtension(containing_type_, number); - if (extension == NULL) { - return false; - } else { - *output = *extension; - return true; - } -} - -void ExtensionSet::RegisterExtension(const MessageLite* containing_type, - int number, FieldType type, - bool is_repeated, bool is_packed) { - GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_ENUM); - GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_MESSAGE); - GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_GROUP); - ExtensionInfo info(type, is_repeated, is_packed); - Register(containing_type, number, info); -} - -static bool CallNoArgValidityFunc(const void* arg, int number) { - // Note: Must use C-style cast here rather than reinterpret_cast because - // the C++ standard at one point did not allow casts between function and - // data pointers and some compilers enforce this for C++-style casts. No - // compiler enforces it for C-style casts since lots of C-style code has - // relied on these kinds of casts for a long time, despite being - // technically undefined. See: - // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195 - // Also note: Some compilers do not allow function pointers to be "const". - // Which makes sense, I suppose, because it's meaningless. - return ((EnumValidityFunc*)arg)(number); -} - -void ExtensionSet::RegisterEnumExtension(const MessageLite* containing_type, - int number, FieldType type, - bool is_repeated, bool is_packed, - EnumValidityFunc* is_valid) { - GOOGLE_CHECK_EQ(type, WireFormatLite::TYPE_ENUM); - ExtensionInfo info(type, is_repeated, is_packed); - info.enum_validity_check.func = CallNoArgValidityFunc; - // See comment in CallNoArgValidityFunc() about why we use a c-style cast. - info.enum_validity_check.arg = (void*)is_valid; - Register(containing_type, number, info); -} - -void ExtensionSet::RegisterMessageExtension(const MessageLite* containing_type, - int number, FieldType type, - bool is_repeated, bool is_packed, - const MessageLite* prototype) { - GOOGLE_CHECK(type == WireFormatLite::TYPE_MESSAGE || - type == WireFormatLite::TYPE_GROUP); - ExtensionInfo info(type, is_repeated, is_packed); - info.message_info = {prototype}; - Register(containing_type, number, info); -} - - -// =================================================================== -// Constructors and basic methods. - -ExtensionSet::ExtensionSet(Arena* arena) - : arena_(arena), - flat_capacity_(0), - flat_size_(0), - map_{flat_capacity_ == 0 - ? NULL - : Arena::CreateArray(arena_, flat_capacity_)} {} - -ExtensionSet::~ExtensionSet() { - // Deletes all allocated extensions. - if (arena_ == NULL) { - ForEach([](int /* number */, Extension& ext) { ext.Free(); }); - if (PROTOBUF_PREDICT_FALSE(is_large())) { - delete map_.large; - } else { - DeleteFlatMap(map_.flat, flat_capacity_); - } - } -} - -void ExtensionSet::DeleteFlatMap(const ExtensionSet::KeyValue* flat, - uint16 flat_capacity) { -#ifdef __cpp_sized_deallocation - // Arena::CreateArray already requires a trivially destructible type, but - // ensure this constraint is not violated in the future. - static_assert(std::is_trivially_destructible::value, - "CreateArray requires a trivially destructible type"); - // A const-cast is needed, but this is safe as we are about to deallocate the - // array. - ::operator delete[](const_cast(flat), - sizeof(*flat) * flat_capacity); -#else // !__cpp_sized_deallocation - delete[] flat; -#endif // !__cpp_sized_deallocation -} - -// Defined in extension_set_heavy.cc. -// void ExtensionSet::AppendToList(const Descriptor* containing_type, -// const DescriptorPool* pool, -// vector* output) const - -bool ExtensionSet::Has(int number) const { - const Extension* ext = FindOrNull(number); - if (ext == NULL) return false; - GOOGLE_DCHECK(!ext->is_repeated); - return !ext->is_cleared; -} - -int ExtensionSet::NumExtensions() const { - int result = 0; - ForEach([&result](int /* number */, const Extension& ext) { - if (!ext.is_cleared) { - ++result; - } - }); - return result; -} - -int ExtensionSet::ExtensionSize(int number) const { - const Extension* ext = FindOrNull(number); - return ext == NULL ? 0 : ext->GetSize(); -} - -FieldType ExtensionSet::ExtensionType(int number) const { - const Extension* ext = FindOrNull(number); - if (ext == NULL) { - GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (1). "; - return 0; - } - if (ext->is_cleared) { - GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (2). "; - } - return ext->type; -} - -void ExtensionSet::ClearExtension(int number) { - Extension* ext = FindOrNull(number); - if (ext == NULL) return; - ext->Clear(); -} - -// =================================================================== -// Field accessors - -namespace { - -enum { REPEATED_FIELD, OPTIONAL_FIELD }; - -} // namespace - -#define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \ - GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? REPEATED_FIELD : OPTIONAL_FIELD, LABEL); \ - GOOGLE_DCHECK_EQ(cpp_type((EXTENSION).type), WireFormatLite::CPPTYPE_##CPPTYPE) - -// ------------------------------------------------------------------- -// Primitives - -#define PRIMITIVE_ACCESSORS(UPPERCASE, LOWERCASE, CAMELCASE) \ - \ - LOWERCASE ExtensionSet::Get##CAMELCASE(int number, LOWERCASE default_value) \ - const { \ - const Extension* extension = FindOrNull(number); \ - if (extension == NULL || extension->is_cleared) { \ - return default_value; \ - } else { \ - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \ - return extension->LOWERCASE##_value; \ - } \ - } \ - \ - void ExtensionSet::Set##CAMELCASE(int number, FieldType type, \ - LOWERCASE value, \ - const FieldDescriptor* descriptor) { \ - Extension* extension; \ - if (MaybeNewExtension(number, descriptor, &extension)) { \ - extension->type = type; \ - GOOGLE_DCHECK_EQ(cpp_type(extension->type), \ - WireFormatLite::CPPTYPE_##UPPERCASE); \ - extension->is_repeated = false; \ - } else { \ - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \ - } \ - extension->is_cleared = false; \ - extension->LOWERCASE##_value = value; \ - } \ - \ - LOWERCASE ExtensionSet::GetRepeated##CAMELCASE(int number, int index) \ - const { \ - const Extension* extension = FindOrNull(number); \ - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \ - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ - return extension->repeated_##LOWERCASE##_value->Get(index); \ - } \ - \ - void ExtensionSet::SetRepeated##CAMELCASE(int number, int index, \ - LOWERCASE value) { \ - Extension* extension = FindOrNull(number); \ - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \ - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ - extension->repeated_##LOWERCASE##_value->Set(index, value); \ - } \ - \ - void ExtensionSet::Add##CAMELCASE(int number, FieldType type, bool packed, \ - LOWERCASE value, \ - const FieldDescriptor* descriptor) { \ - Extension* extension; \ - if (MaybeNewExtension(number, descriptor, &extension)) { \ - extension->type = type; \ - GOOGLE_DCHECK_EQ(cpp_type(extension->type), \ - WireFormatLite::CPPTYPE_##UPPERCASE); \ - extension->is_repeated = true; \ - extension->is_packed = packed; \ - extension->repeated_##LOWERCASE##_value = \ - Arena::CreateMessage>(arena_); \ - } else { \ - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \ - GOOGLE_DCHECK_EQ(extension->is_packed, packed); \ - } \ - extension->repeated_##LOWERCASE##_value->Add(value); \ - } - -PRIMITIVE_ACCESSORS(INT32, int32, Int32) -PRIMITIVE_ACCESSORS(INT64, int64, Int64) -PRIMITIVE_ACCESSORS(UINT32, uint32, UInt32) -PRIMITIVE_ACCESSORS(UINT64, uint64, UInt64) -PRIMITIVE_ACCESSORS(FLOAT, float, Float) -PRIMITIVE_ACCESSORS(DOUBLE, double, Double) -PRIMITIVE_ACCESSORS(BOOL, bool, Bool) - -#undef PRIMITIVE_ACCESSORS - -const void* ExtensionSet::GetRawRepeatedField(int number, - const void* default_value) const { - const Extension* extension = FindOrNull(number); - if (extension == NULL) { - return default_value; - } - // We assume that all the RepeatedField<>* pointers have the same - // size and alignment within the anonymous union in Extension. - return extension->repeated_int32_value; -} - -void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type, - bool packed, - const FieldDescriptor* desc) { - Extension* extension; - - // We instantiate an empty Repeated{,Ptr}Field if one doesn't exist for this - // extension. - if (MaybeNewExtension(number, desc, &extension)) { - extension->is_repeated = true; - extension->type = field_type; - extension->is_packed = packed; - - switch (WireFormatLite::FieldTypeToCppType( - static_cast(field_type))) { - case WireFormatLite::CPPTYPE_INT32: - extension->repeated_int32_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_INT64: - extension->repeated_int64_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_UINT32: - extension->repeated_uint32_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_UINT64: - extension->repeated_uint64_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_DOUBLE: - extension->repeated_double_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_FLOAT: - extension->repeated_float_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_BOOL: - extension->repeated_bool_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_ENUM: - extension->repeated_enum_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_STRING: - extension->repeated_string_value = - Arena::CreateMessage>(arena_); - break; - case WireFormatLite::CPPTYPE_MESSAGE: - extension->repeated_message_value = - Arena::CreateMessage>(arena_); - break; - } - } - - // We assume that all the RepeatedField<>* pointers have the same - // size and alignment within the anonymous union in Extension. - return extension->repeated_int32_value; -} - -// Compatible version using old call signature. Does not create extensions when -// the don't already exist; instead, just GOOGLE_CHECK-fails. -void* ExtensionSet::MutableRawRepeatedField(int number) { - Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Extension not found."; - // We assume that all the RepeatedField<>* pointers have the same - // size and alignment within the anonymous union in Extension. - return extension->repeated_int32_value; -} - -// ------------------------------------------------------------------- -// Enums - -int ExtensionSet::GetEnum(int number, int default_value) const { - const Extension* extension = FindOrNull(number); - if (extension == NULL || extension->is_cleared) { - // Not present. Return the default value. - return default_value; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, ENUM); - return extension->enum_value; - } -} - -void ExtensionSet::SetEnum(int number, FieldType type, int value, - const FieldDescriptor* descriptor) { - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM); - extension->is_repeated = false; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, ENUM); - } - extension->is_cleared = false; - extension->enum_value = value; -} - -int ExtensionSet::GetRepeatedEnum(int number, int index) const { - const Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM); - return extension->repeated_enum_value->Get(index); -} - -void ExtensionSet::SetRepeatedEnum(int number, int index, int value) { - Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM); - extension->repeated_enum_value->Set(index, value); -} - -void ExtensionSet::AddEnum(int number, FieldType type, bool packed, int value, - const FieldDescriptor* descriptor) { - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM); - extension->is_repeated = true; - extension->is_packed = packed; - extension->repeated_enum_value = - Arena::CreateMessage>(arena_); - } else { - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM); - GOOGLE_DCHECK_EQ(extension->is_packed, packed); - } - extension->repeated_enum_value->Add(value); -} - -// ------------------------------------------------------------------- -// Strings - -const std::string& ExtensionSet::GetString( - int number, const std::string& default_value) const { - const Extension* extension = FindOrNull(number); - if (extension == NULL || extension->is_cleared) { - // Not present. Return the default value. - return default_value; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, STRING); - return *extension->string_value; - } -} - -std::string* ExtensionSet::MutableString(int number, FieldType type, - const FieldDescriptor* descriptor) { - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING); - extension->is_repeated = false; - extension->string_value = Arena::Create(arena_); - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, STRING); - } - extension->is_cleared = false; - return extension->string_value; -} - -const std::string& ExtensionSet::GetRepeatedString(int number, - int index) const { - const Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING); - return extension->repeated_string_value->Get(index); -} - -std::string* ExtensionSet::MutableRepeatedString(int number, int index) { - Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING); - return extension->repeated_string_value->Mutable(index); -} - -std::string* ExtensionSet::AddString(int number, FieldType type, - const FieldDescriptor* descriptor) { - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING); - extension->is_repeated = true; - extension->is_packed = false; - extension->repeated_string_value = - Arena::CreateMessage>(arena_); - } else { - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING); - } - return extension->repeated_string_value->Add(); -} - -// ------------------------------------------------------------------- -// Messages - -const MessageLite& ExtensionSet::GetMessage( - int number, const MessageLite& default_value) const { - const Extension* extension = FindOrNull(number); - if (extension == NULL) { - // Not present. Return the default value. - return default_value; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); - if (extension->is_lazy) { - return extension->lazymessage_value->GetMessage(default_value); - } else { - return *extension->message_value; - } - } -} - -// Defined in extension_set_heavy.cc. -// const MessageLite& ExtensionSet::GetMessage(int number, -// const Descriptor* message_type, -// MessageFactory* factory) const - -MessageLite* ExtensionSet::MutableMessage(int number, FieldType type, - const MessageLite& prototype, - const FieldDescriptor* descriptor) { - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE); - extension->is_repeated = false; - extension->is_lazy = false; - extension->message_value = prototype.New(arena_); - extension->is_cleared = false; - return extension->message_value; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); - extension->is_cleared = false; - if (extension->is_lazy) { - return extension->lazymessage_value->MutableMessage(prototype); - } else { - return extension->message_value; - } - } -} - -// Defined in extension_set_heavy.cc. -// MessageLite* ExtensionSet::MutableMessage(int number, FieldType type, -// const Descriptor* message_type, -// MessageFactory* factory) - -void ExtensionSet::SetAllocatedMessage(int number, FieldType type, - const FieldDescriptor* descriptor, - MessageLite* message) { - if (message == NULL) { - ClearExtension(number); - return; - } - Arena* message_arena = message->GetArena(); - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE); - extension->is_repeated = false; - extension->is_lazy = false; - if (message_arena == arena_) { - extension->message_value = message; - } else if (message_arena == NULL) { - extension->message_value = message; - arena_->Own(message); // not NULL because not equal to message_arena - } else { - extension->message_value = message->New(arena_); - extension->message_value->CheckTypeAndMergeFrom(*message); - } - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); - if (extension->is_lazy) { - extension->lazymessage_value->SetAllocatedMessage(message); - } else { - if (arena_ == NULL) { - delete extension->message_value; - } - if (message_arena == arena_) { - extension->message_value = message; - } else if (message_arena == NULL) { - extension->message_value = message; - arena_->Own(message); // not NULL because not equal to message_arena - } else { - extension->message_value = message->New(arena_); - extension->message_value->CheckTypeAndMergeFrom(*message); - } - } - } - extension->is_cleared = false; -} - -void ExtensionSet::UnsafeArenaSetAllocatedMessage( - int number, FieldType type, const FieldDescriptor* descriptor, - MessageLite* message) { - if (message == NULL) { - ClearExtension(number); - return; - } - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE); - extension->is_repeated = false; - extension->is_lazy = false; - extension->message_value = message; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); - if (extension->is_lazy) { - extension->lazymessage_value->UnsafeArenaSetAllocatedMessage(message); - } else { - if (arena_ == NULL) { - delete extension->message_value; - } - extension->message_value = message; - } - } - extension->is_cleared = false; -} - -MessageLite* ExtensionSet::ReleaseMessage(int number, - const MessageLite& prototype) { - Extension* extension = FindOrNull(number); - if (extension == NULL) { - // Not present. Return NULL. - return NULL; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); - MessageLite* ret = NULL; - if (extension->is_lazy) { - ret = extension->lazymessage_value->ReleaseMessage(prototype); - if (arena_ == NULL) { - delete extension->lazymessage_value; - } - } else { - if (arena_ == NULL) { - ret = extension->message_value; - } else { - // ReleaseMessage() always returns a heap-allocated message, and we are - // on an arena, so we need to make a copy of this message to return. - ret = extension->message_value->New(); - ret->CheckTypeAndMergeFrom(*extension->message_value); - } - } - Erase(number); - return ret; - } -} - -MessageLite* ExtensionSet::UnsafeArenaReleaseMessage( - int number, const MessageLite& prototype) { - Extension* extension = FindOrNull(number); - if (extension == NULL) { - // Not present. Return NULL. - return NULL; - } else { - GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE); - MessageLite* ret = NULL; - if (extension->is_lazy) { - ret = extension->lazymessage_value->UnsafeArenaReleaseMessage(prototype); - if (arena_ == NULL) { - delete extension->lazymessage_value; - } - } else { - ret = extension->message_value; - } - Erase(number); - return ret; - } -} - -// Defined in extension_set_heavy.cc. -// MessageLite* ExtensionSet::ReleaseMessage(const FieldDescriptor* descriptor, -// MessageFactory* factory); - -const MessageLite& ExtensionSet::GetRepeatedMessage(int number, - int index) const { - const Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE); - return extension->repeated_message_value->Get(index); -} - -MessageLite* ExtensionSet::MutableRepeatedMessage(int number, int index) { - Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE); - return extension->repeated_message_value->Mutable(index); -} - -MessageLite* ExtensionSet::AddMessage(int number, FieldType type, - const MessageLite& prototype, - const FieldDescriptor* descriptor) { - Extension* extension; - if (MaybeNewExtension(number, descriptor, &extension)) { - extension->type = type; - GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE); - extension->is_repeated = true; - extension->repeated_message_value = - Arena::CreateMessage>(arena_); - } else { - GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE); - } - - // RepeatedPtrField does not know how to Add() since it cannot - // allocate an abstract object, so we have to be tricky. - MessageLite* result = reinterpret_cast( - extension->repeated_message_value) - ->AddFromCleared>(); - if (result == NULL) { - result = prototype.New(arena_); - extension->repeated_message_value->AddAllocated(result); - } - return result; -} - -// Defined in extension_set_heavy.cc. -// MessageLite* ExtensionSet::AddMessage(int number, FieldType type, -// const Descriptor* message_type, -// MessageFactory* factory) - -#undef GOOGLE_DCHECK_TYPE - -void ExtensionSet::RemoveLast(int number) { - Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK(extension->is_repeated); - - switch (cpp_type(extension->type)) { - case WireFormatLite::CPPTYPE_INT32: - extension->repeated_int32_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_INT64: - extension->repeated_int64_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_UINT32: - extension->repeated_uint32_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_UINT64: - extension->repeated_uint64_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_FLOAT: - extension->repeated_float_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_DOUBLE: - extension->repeated_double_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_BOOL: - extension->repeated_bool_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_ENUM: - extension->repeated_enum_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_STRING: - extension->repeated_string_value->RemoveLast(); - break; - case WireFormatLite::CPPTYPE_MESSAGE: - extension->repeated_message_value->RemoveLast(); - break; - } -} - -MessageLite* ExtensionSet::ReleaseLast(int number) { - Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK(extension->is_repeated); - GOOGLE_DCHECK(cpp_type(extension->type) == WireFormatLite::CPPTYPE_MESSAGE); - return extension->repeated_message_value->ReleaseLast(); -} - -void ExtensionSet::SwapElements(int number, int index1, int index2) { - Extension* extension = FindOrNull(number); - GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; - GOOGLE_DCHECK(extension->is_repeated); - - switch (cpp_type(extension->type)) { - case WireFormatLite::CPPTYPE_INT32: - extension->repeated_int32_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_INT64: - extension->repeated_int64_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_UINT32: - extension->repeated_uint32_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_UINT64: - extension->repeated_uint64_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_FLOAT: - extension->repeated_float_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_DOUBLE: - extension->repeated_double_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_BOOL: - extension->repeated_bool_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_ENUM: - extension->repeated_enum_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_STRING: - extension->repeated_string_value->SwapElements(index1, index2); - break; - case WireFormatLite::CPPTYPE_MESSAGE: - extension->repeated_message_value->SwapElements(index1, index2); - break; - } -} - -// =================================================================== - -void ExtensionSet::Clear() { - ForEach([](int /* number */, Extension& ext) { ext.Clear(); }); -} - -namespace { -// Computes the size of a std::set_union without constructing the union. -template -size_t SizeOfUnion(ItX it_xs, ItX end_xs, ItY it_ys, ItY end_ys) { - size_t result = 0; - while (it_xs != end_xs && it_ys != end_ys) { - ++result; - if (it_xs->first < it_ys->first) { - ++it_xs; - } else if (it_xs->first == it_ys->first) { - ++it_xs; - ++it_ys; - } else { - ++it_ys; - } - } - result += std::distance(it_xs, end_xs); - result += std::distance(it_ys, end_ys); - return result; -} -} // namespace - -void ExtensionSet::MergeFrom(const ExtensionSet& other) { - if (PROTOBUF_PREDICT_TRUE(!is_large())) { - if (PROTOBUF_PREDICT_TRUE(!other.is_large())) { - GrowCapacity(SizeOfUnion(flat_begin(), flat_end(), other.flat_begin(), - other.flat_end())); - } else { - GrowCapacity(SizeOfUnion(flat_begin(), flat_end(), - other.map_.large->begin(), - other.map_.large->end())); - } - } - other.ForEach([this](int number, const Extension& ext) { - this->InternalExtensionMergeFrom(number, ext); - }); -} - -void ExtensionSet::InternalExtensionMergeFrom( - int number, const Extension& other_extension) { - if (other_extension.is_repeated) { - Extension* extension; - bool is_new = - MaybeNewExtension(number, other_extension.descriptor, &extension); - if (is_new) { - // Extension did not already exist in set. - extension->type = other_extension.type; - extension->is_packed = other_extension.is_packed; - extension->is_repeated = true; - } else { - GOOGLE_DCHECK_EQ(extension->type, other_extension.type); - GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed); - GOOGLE_DCHECK(extension->is_repeated); - } - - switch (cpp_type(other_extension.type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - if (is_new) { \ - extension->repeated_##LOWERCASE##_value = \ - Arena::CreateMessage(arena_); \ - } \ - extension->repeated_##LOWERCASE##_value->MergeFrom( \ - *other_extension.repeated_##LOWERCASE##_value); \ - break; - - HANDLE_TYPE(INT32, int32, RepeatedField); - HANDLE_TYPE(INT64, int64, RepeatedField); - HANDLE_TYPE(UINT32, uint32, RepeatedField); - HANDLE_TYPE(UINT64, uint64, RepeatedField); - HANDLE_TYPE(FLOAT, float, RepeatedField); - HANDLE_TYPE(DOUBLE, double, RepeatedField); - HANDLE_TYPE(BOOL, bool, RepeatedField); - HANDLE_TYPE(ENUM, enum, RepeatedField); - HANDLE_TYPE(STRING, string, RepeatedPtrField); -#undef HANDLE_TYPE - - case WireFormatLite::CPPTYPE_MESSAGE: - if (is_new) { - extension->repeated_message_value = - Arena::CreateMessage>(arena_); - } - // We can't call RepeatedPtrField::MergeFrom() because - // it would attempt to allocate new objects. - RepeatedPtrField* other_repeated_message = - other_extension.repeated_message_value; - for (int i = 0; i < other_repeated_message->size(); i++) { - const MessageLite& other_message = other_repeated_message->Get(i); - MessageLite* target = - reinterpret_cast( - extension->repeated_message_value) - ->AddFromCleared>(); - if (target == NULL) { - target = other_message.New(arena_); - extension->repeated_message_value->AddAllocated(target); - } - target->CheckTypeAndMergeFrom(other_message); - } - break; - } - } else { - if (!other_extension.is_cleared) { - switch (cpp_type(other_extension.type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - Set##CAMELCASE(number, other_extension.type, \ - other_extension.LOWERCASE##_value, \ - other_extension.descriptor); \ - break; - - HANDLE_TYPE(INT32, int32, Int32); - HANDLE_TYPE(INT64, int64, Int64); - HANDLE_TYPE(UINT32, uint32, UInt32); - HANDLE_TYPE(UINT64, uint64, UInt64); - HANDLE_TYPE(FLOAT, float, Float); - HANDLE_TYPE(DOUBLE, double, Double); - HANDLE_TYPE(BOOL, bool, Bool); - HANDLE_TYPE(ENUM, enum, Enum); -#undef HANDLE_TYPE - case WireFormatLite::CPPTYPE_STRING: - SetString(number, other_extension.type, *other_extension.string_value, - other_extension.descriptor); - break; - case WireFormatLite::CPPTYPE_MESSAGE: { - Extension* extension; - bool is_new = - MaybeNewExtension(number, other_extension.descriptor, &extension); - if (is_new) { - extension->type = other_extension.type; - extension->is_packed = other_extension.is_packed; - extension->is_repeated = false; - if (other_extension.is_lazy) { - extension->is_lazy = true; - extension->lazymessage_value = - other_extension.lazymessage_value->New(arena_); - extension->lazymessage_value->MergeFrom( - *other_extension.lazymessage_value); - } else { - extension->is_lazy = false; - extension->message_value = - other_extension.message_value->New(arena_); - extension->message_value->CheckTypeAndMergeFrom( - *other_extension.message_value); - } - } else { - GOOGLE_DCHECK_EQ(extension->type, other_extension.type); - GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed); - GOOGLE_DCHECK(!extension->is_repeated); - if (other_extension.is_lazy) { - if (extension->is_lazy) { - extension->lazymessage_value->MergeFrom( - *other_extension.lazymessage_value); - } else { - extension->message_value->CheckTypeAndMergeFrom( - other_extension.lazymessage_value->GetMessage( - *extension->message_value)); - } - } else { - if (extension->is_lazy) { - extension->lazymessage_value - ->MutableMessage(*other_extension.message_value) - ->CheckTypeAndMergeFrom(*other_extension.message_value); - } else { - extension->message_value->CheckTypeAndMergeFrom( - *other_extension.message_value); - } - } - } - extension->is_cleared = false; - break; - } - } - } - } -} - -void ExtensionSet::Swap(ExtensionSet* x) { - if (GetArena() == x->GetArena()) { - using std::swap; - swap(flat_capacity_, x->flat_capacity_); - swap(flat_size_, x->flat_size_); - swap(map_, x->map_); - } else { - // TODO(cfallin, rohananil): We maybe able to optimize a case where we are - // swapping from heap to arena-allocated extension set, by just Own()'ing - // the extensions. - ExtensionSet extension_set; - extension_set.MergeFrom(*x); - x->Clear(); - x->MergeFrom(*this); - Clear(); - MergeFrom(extension_set); - } -} - -void ExtensionSet::SwapExtension(ExtensionSet* other, int number) { - if (this == other) return; - Extension* this_ext = FindOrNull(number); - Extension* other_ext = other->FindOrNull(number); - - if (this_ext == NULL && other_ext == NULL) { - return; - } - - if (this_ext != NULL && other_ext != NULL) { - if (GetArena() == other->GetArena()) { - using std::swap; - swap(*this_ext, *other_ext); - } else { - // TODO(cfallin, rohananil): We could further optimize these cases, - // especially avoid creation of ExtensionSet, and move MergeFrom logic - // into Extensions itself (which takes arena as an argument). - // We do it this way to reuse the copy-across-arenas logic already - // implemented in ExtensionSet's MergeFrom. - ExtensionSet temp; - temp.InternalExtensionMergeFrom(number, *other_ext); - Extension* temp_ext = temp.FindOrNull(number); - other_ext->Clear(); - other->InternalExtensionMergeFrom(number, *this_ext); - this_ext->Clear(); - InternalExtensionMergeFrom(number, *temp_ext); - } - return; - } - - if (this_ext == NULL) { - if (GetArena() == other->GetArena()) { - *Insert(number).first = *other_ext; - } else { - InternalExtensionMergeFrom(number, *other_ext); - } - other->Erase(number); - return; - } - - if (other_ext == NULL) { - if (GetArena() == other->GetArena()) { - *other->Insert(number).first = *this_ext; - } else { - other->InternalExtensionMergeFrom(number, *this_ext); - } - Erase(number); - return; - } -} - -bool ExtensionSet::IsInitialized() const { - // Extensions are never required. However, we need to check that all - // embedded messages are initialized. - if (PROTOBUF_PREDICT_FALSE(is_large())) { - for (const auto& kv : *map_.large) { - if (!kv.second.IsInitialized()) return false; - } - return true; - } - for (const KeyValue* it = flat_begin(); it != flat_end(); ++it) { - if (!it->second.IsInitialized()) return false; - } - return true; -} - -bool ExtensionSet::FindExtensionInfoFromTag(uint32 tag, - ExtensionFinder* extension_finder, - int* field_number, - ExtensionInfo* extension, - bool* was_packed_on_wire) { - *field_number = WireFormatLite::GetTagFieldNumber(tag); - WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag); - return FindExtensionInfoFromFieldNumber(wire_type, *field_number, - extension_finder, extension, - was_packed_on_wire); -} - -bool ExtensionSet::FindExtensionInfoFromFieldNumber( - int wire_type, int field_number, ExtensionFinder* extension_finder, - ExtensionInfo* extension, bool* was_packed_on_wire) { - if (!extension_finder->Find(field_number, extension)) { - return false; - } - - WireFormatLite::WireType expected_wire_type = - WireFormatLite::WireTypeForFieldType(real_type(extension->type)); - - // Check if this is a packed field. - *was_packed_on_wire = false; - if (extension->is_repeated && - wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED && - is_packable(expected_wire_type)) { - *was_packed_on_wire = true; - return true; - } - // Otherwise the wire type must match. - return expected_wire_type == wire_type; -} - -bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input, - ExtensionFinder* extension_finder, - FieldSkipper* field_skipper) { - int number; - bool was_packed_on_wire; - ExtensionInfo extension; - if (!FindExtensionInfoFromTag(tag, extension_finder, &number, &extension, - &was_packed_on_wire)) { - return field_skipper->SkipField(input, tag); - } else { - return ParseFieldWithExtensionInfo(number, was_packed_on_wire, extension, - input, field_skipper); - } -} - -const char* ExtensionSet::ParseField(uint64 tag, const char* ptr, - const MessageLite* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx) { - GeneratedExtensionFinder finder(containing_type); - int number = tag >> 3; - bool was_packed_on_wire; - ExtensionInfo extension; - if (!FindExtensionInfoFromFieldNumber(tag & 7, number, &finder, &extension, - &was_packed_on_wire)) { - return UnknownFieldParse( - tag, metadata->mutable_unknown_fields(), ptr, ctx); - } - return ParseFieldWithExtensionInfo( - number, was_packed_on_wire, extension, metadata, ptr, ctx); -} - -const char* ExtensionSet::ParseMessageSetItem( - const char* ptr, const MessageLite* containing_type, - internal::InternalMetadata* metadata, internal::ParseContext* ctx) { - return ParseMessageSetItemTmpl(ptr, containing_type, - metadata, ctx); -} - -bool ExtensionSet::ParseFieldWithExtensionInfo(int number, - bool was_packed_on_wire, - const ExtensionInfo& extension, - io::CodedInputStream* input, - FieldSkipper* field_skipper) { - // Explicitly not read extension.is_packed, instead check whether the field - // was encoded in packed form on the wire. - if (was_packed_on_wire) { - uint32 size; - if (!input->ReadVarint32(&size)) return false; - io::CodedInputStream::Limit limit = input->PushLimit(size); - - switch (extension.type) { -#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - while (input->BytesUntilLimit() > 0) { \ - CPP_LOWERCASE value; \ - if (!WireFormatLite::ReadPrimitive( \ - input, &value)) \ - return false; \ - Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ - extension.is_packed, value, extension.descriptor); \ - } \ - break - - HANDLE_TYPE(INT32, Int32, int32); - HANDLE_TYPE(INT64, Int64, int64); - HANDLE_TYPE(UINT32, UInt32, uint32); - HANDLE_TYPE(UINT64, UInt64, uint64); - HANDLE_TYPE(SINT32, Int32, int32); - HANDLE_TYPE(SINT64, Int64, int64); - HANDLE_TYPE(FIXED32, UInt32, uint32); - HANDLE_TYPE(FIXED64, UInt64, uint64); - HANDLE_TYPE(SFIXED32, Int32, int32); - HANDLE_TYPE(SFIXED64, Int64, int64); - HANDLE_TYPE(FLOAT, Float, float); - HANDLE_TYPE(DOUBLE, Double, double); - HANDLE_TYPE(BOOL, Bool, bool); -#undef HANDLE_TYPE - - case WireFormatLite::TYPE_ENUM: - while (input->BytesUntilLimit() > 0) { - int value; - if (!WireFormatLite::ReadPrimitive( - input, &value)) - return false; - if (extension.enum_validity_check.func( - extension.enum_validity_check.arg, value)) { - AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, - value, extension.descriptor); - } else { - // Invalid value. Treat as unknown. - field_skipper->SkipUnknownEnum(number, value); - } - } - break; - - case WireFormatLite::TYPE_STRING: - case WireFormatLite::TYPE_BYTES: - case WireFormatLite::TYPE_GROUP: - case WireFormatLite::TYPE_MESSAGE: - GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed."; - break; - } - - input->PopLimit(limit); - } else { - switch (extension.type) { -#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: { \ - CPP_LOWERCASE value; \ - if (!WireFormatLite::ReadPrimitive( \ - input, &value)) \ - return false; \ - if (extension.is_repeated) { \ - Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ - extension.is_packed, value, extension.descriptor); \ - } else { \ - Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \ - extension.descriptor); \ - } \ - } break - - HANDLE_TYPE(INT32, Int32, int32); - HANDLE_TYPE(INT64, Int64, int64); - HANDLE_TYPE(UINT32, UInt32, uint32); - HANDLE_TYPE(UINT64, UInt64, uint64); - HANDLE_TYPE(SINT32, Int32, int32); - HANDLE_TYPE(SINT64, Int64, int64); - HANDLE_TYPE(FIXED32, UInt32, uint32); - HANDLE_TYPE(FIXED64, UInt64, uint64); - HANDLE_TYPE(SFIXED32, Int32, int32); - HANDLE_TYPE(SFIXED64, Int64, int64); - HANDLE_TYPE(FLOAT, Float, float); - HANDLE_TYPE(DOUBLE, Double, double); - HANDLE_TYPE(BOOL, Bool, bool); -#undef HANDLE_TYPE - - case WireFormatLite::TYPE_ENUM: { - int value; - if (!WireFormatLite::ReadPrimitive( - input, &value)) - return false; - - if (!extension.enum_validity_check.func( - extension.enum_validity_check.arg, value)) { - // Invalid value. Treat as unknown. - field_skipper->SkipUnknownEnum(number, value); - } else if (extension.is_repeated) { - AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, value, - extension.descriptor); - } else { - SetEnum(number, WireFormatLite::TYPE_ENUM, value, - extension.descriptor); - } - break; - } - - case WireFormatLite::TYPE_STRING: { - std::string* value = - extension.is_repeated - ? AddString(number, WireFormatLite::TYPE_STRING, - extension.descriptor) - : MutableString(number, WireFormatLite::TYPE_STRING, - extension.descriptor); - if (!WireFormatLite::ReadString(input, value)) return false; - break; - } - - case WireFormatLite::TYPE_BYTES: { - std::string* value = - extension.is_repeated - ? AddString(number, WireFormatLite::TYPE_BYTES, - extension.descriptor) - : MutableString(number, WireFormatLite::TYPE_BYTES, - extension.descriptor); - if (!WireFormatLite::ReadBytes(input, value)) return false; - break; - } - - case WireFormatLite::TYPE_GROUP: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_info.prototype, - extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_info.prototype, - extension.descriptor); - if (!WireFormatLite::ReadGroup(number, input, value)) return false; - break; - } - - case WireFormatLite::TYPE_MESSAGE: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_info.prototype, - extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_info.prototype, - extension.descriptor); - if (!WireFormatLite::ReadMessage(input, value)) return false; - break; - } - } - } - - return true; -} - -bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input, - const MessageLite* containing_type) { - FieldSkipper skipper; - GeneratedExtensionFinder finder(containing_type); - return ParseField(tag, input, &finder, &skipper); -} - -bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input, - const MessageLite* containing_type, - io::CodedOutputStream* unknown_fields) { - CodedOutputStreamFieldSkipper skipper(unknown_fields); - GeneratedExtensionFinder finder(containing_type); - return ParseField(tag, input, &finder, &skipper); -} - -bool ExtensionSet::ParseMessageSetLite(io::CodedInputStream* input, - ExtensionFinder* extension_finder, - FieldSkipper* field_skipper) { - while (true) { - const uint32 tag = input->ReadTag(); - switch (tag) { - case 0: - return true; - case WireFormatLite::kMessageSetItemStartTag: - if (!ParseMessageSetItemLite(input, extension_finder, field_skipper)) { - return false; - } - break; - default: - if (!ParseField(tag, input, extension_finder, field_skipper)) { - return false; - } - break; - } - } -} - -bool ExtensionSet::ParseMessageSetItemLite(io::CodedInputStream* input, - ExtensionFinder* extension_finder, - FieldSkipper* field_skipper) { - struct MSLite { - bool ParseField(int type_id, io::CodedInputStream* input) { - return me->ParseField( - WireFormatLite::WIRETYPE_LENGTH_DELIMITED + 8 * type_id, input, - extension_finder, field_skipper); - } - - bool SkipField(uint32 tag, io::CodedInputStream* input) { - return field_skipper->SkipField(input, tag); - } - - ExtensionSet* me; - ExtensionFinder* extension_finder; - FieldSkipper* field_skipper; - }; - - return ParseMessageSetItemImpl(input, - MSLite{this, extension_finder, field_skipper}); -} - -bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input, - const MessageLite* containing_type, - std::string* unknown_fields) { - io::StringOutputStream zcis(unknown_fields); - io::CodedOutputStream output(&zcis); - CodedOutputStreamFieldSkipper skipper(&output); - GeneratedExtensionFinder finder(containing_type); - return ParseMessageSetLite(input, &finder, &skipper); -} - -uint8* ExtensionSet::_InternalSerialize(int start_field_number, - int end_field_number, uint8* target, - io::EpsCopyOutputStream* stream) const { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - const auto& end = map_.large->end(); - for (auto it = map_.large->lower_bound(start_field_number); - it != end && it->first < end_field_number; ++it) { - target = it->second.InternalSerializeFieldWithCachedSizesToArray( - it->first, target, stream); - } - return target; - } - const KeyValue* end = flat_end(); - for (const KeyValue* it = std::lower_bound( - flat_begin(), end, start_field_number, KeyValue::FirstComparator()); - it != end && it->first < end_field_number; ++it) { - target = it->second.InternalSerializeFieldWithCachedSizesToArray( - it->first, target, stream); - } - return target; -} - -uint8* ExtensionSet::InternalSerializeMessageSetWithCachedSizesToArray( - uint8* target, io::EpsCopyOutputStream* stream) const { - ForEach([&target, stream](int number, const Extension& ext) { - target = ext.InternalSerializeMessageSetItemWithCachedSizesToArray( - number, target, stream); - }); - return target; -} - -size_t ExtensionSet::ByteSize() const { - size_t total_size = 0; - ForEach([&total_size](int number, const Extension& ext) { - total_size += ext.ByteSize(number); - }); - return total_size; -} - -// Defined in extension_set_heavy.cc. -// int ExtensionSet::SpaceUsedExcludingSelf() const - -bool ExtensionSet::MaybeNewExtension(int number, - const FieldDescriptor* descriptor, - Extension** result) { - bool extension_is_new = false; - std::tie(*result, extension_is_new) = Insert(number); - (*result)->descriptor = descriptor; - return extension_is_new; -} - -// =================================================================== -// Methods of ExtensionSet::Extension - -void ExtensionSet::Extension::Clear() { - if (is_repeated) { - switch (cpp_type(type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - repeated_##LOWERCASE##_value->Clear(); \ - break - - HANDLE_TYPE(INT32, int32); - HANDLE_TYPE(INT64, int64); - HANDLE_TYPE(UINT32, uint32); - HANDLE_TYPE(UINT64, uint64); - HANDLE_TYPE(FLOAT, float); - HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(BOOL, bool); - HANDLE_TYPE(ENUM, enum); - HANDLE_TYPE(STRING, string); - HANDLE_TYPE(MESSAGE, message); -#undef HANDLE_TYPE - } - } else { - if (!is_cleared) { - switch (cpp_type(type)) { - case WireFormatLite::CPPTYPE_STRING: - string_value->clear(); - break; - case WireFormatLite::CPPTYPE_MESSAGE: - if (is_lazy) { - lazymessage_value->Clear(); - } else { - message_value->Clear(); - } - break; - default: - // No need to do anything. Get*() will return the default value - // as long as is_cleared is true and Set*() will overwrite the - // previous value. - break; - } - - is_cleared = true; - } - } -} - -size_t ExtensionSet::Extension::ByteSize(int number) const { - size_t result = 0; - - if (is_repeated) { - if (is_packed) { - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - result += WireFormatLite::CAMELCASE##Size( \ - repeated_##LOWERCASE##_value->Get(i)); \ - } \ - break - - HANDLE_TYPE(INT32, Int32, int32); - HANDLE_TYPE(INT64, Int64, int64); - HANDLE_TYPE(UINT32, UInt32, uint32); - HANDLE_TYPE(UINT64, UInt64, uint64); - HANDLE_TYPE(SINT32, SInt32, int32); - HANDLE_TYPE(SINT64, SInt64, int64); - HANDLE_TYPE(ENUM, Enum, enum); -#undef HANDLE_TYPE - - // Stuff with fixed size. -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += WireFormatLite::k##CAMELCASE##Size * \ - FromIntSize(repeated_##LOWERCASE##_value->size()); \ - break - HANDLE_TYPE(FIXED32, Fixed32, uint32); - HANDLE_TYPE(FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE(FLOAT, Float, float); - HANDLE_TYPE(DOUBLE, Double, double); - HANDLE_TYPE(BOOL, Bool, bool); -#undef HANDLE_TYPE - - case WireFormatLite::TYPE_STRING: - case WireFormatLite::TYPE_BYTES: - case WireFormatLite::TYPE_GROUP: - case WireFormatLite::TYPE_MESSAGE: - GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed."; - break; - } - - cached_size = ToCachedSize(result); - if (result > 0) { - result += io::CodedOutputStream::VarintSize32(result); - result += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag( - number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); - } - } else { - size_t tag_size = WireFormatLite::TagSize(number, real_type(type)); - - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += tag_size * FromIntSize(repeated_##LOWERCASE##_value->size()); \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - result += WireFormatLite::CAMELCASE##Size( \ - repeated_##LOWERCASE##_value->Get(i)); \ - } \ - break - - HANDLE_TYPE(INT32, Int32, int32); - HANDLE_TYPE(INT64, Int64, int64); - HANDLE_TYPE(UINT32, UInt32, uint32); - HANDLE_TYPE(UINT64, UInt64, uint64); - HANDLE_TYPE(SINT32, SInt32, int32); - HANDLE_TYPE(SINT64, SInt64, int64); - HANDLE_TYPE(STRING, String, string); - HANDLE_TYPE(BYTES, Bytes, string); - HANDLE_TYPE(ENUM, Enum, enum); - HANDLE_TYPE(GROUP, Group, message); - HANDLE_TYPE(MESSAGE, Message, message); -#undef HANDLE_TYPE - - // Stuff with fixed size. -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += (tag_size + WireFormatLite::k##CAMELCASE##Size) * \ - FromIntSize(repeated_##LOWERCASE##_value->size()); \ - break - HANDLE_TYPE(FIXED32, Fixed32, uint32); - HANDLE_TYPE(FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE(FLOAT, Float, float); - HANDLE_TYPE(DOUBLE, Double, double); - HANDLE_TYPE(BOOL, Bool, bool); -#undef HANDLE_TYPE - } - } - } else if (!is_cleared) { - result += WireFormatLite::TagSize(number, real_type(type)); - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += WireFormatLite::CAMELCASE##Size(LOWERCASE); \ - break - - HANDLE_TYPE(INT32, Int32, int32_value); - HANDLE_TYPE(INT64, Int64, int64_value); - HANDLE_TYPE(UINT32, UInt32, uint32_value); - HANDLE_TYPE(UINT64, UInt64, uint64_value); - HANDLE_TYPE(SINT32, SInt32, int32_value); - HANDLE_TYPE(SINT64, SInt64, int64_value); - HANDLE_TYPE(STRING, String, *string_value); - HANDLE_TYPE(BYTES, Bytes, *string_value); - HANDLE_TYPE(ENUM, Enum, enum_value); - HANDLE_TYPE(GROUP, Group, *message_value); -#undef HANDLE_TYPE - case WireFormatLite::TYPE_MESSAGE: { - if (is_lazy) { - size_t size = lazymessage_value->ByteSizeLong(); - result += io::CodedOutputStream::VarintSize32(size) + size; - } else { - result += WireFormatLite::MessageSize(*message_value); - } - break; - } - - // Stuff with fixed size. -#define HANDLE_TYPE(UPPERCASE, CAMELCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - result += WireFormatLite::k##CAMELCASE##Size; \ - break - HANDLE_TYPE(FIXED32, Fixed32); - HANDLE_TYPE(FIXED64, Fixed64); - HANDLE_TYPE(SFIXED32, SFixed32); - HANDLE_TYPE(SFIXED64, SFixed64); - HANDLE_TYPE(FLOAT, Float); - HANDLE_TYPE(DOUBLE, Double); - HANDLE_TYPE(BOOL, Bool); -#undef HANDLE_TYPE - } - } - - return result; -} - -int ExtensionSet::Extension::GetSize() const { - GOOGLE_DCHECK(is_repeated); - switch (cpp_type(type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - return repeated_##LOWERCASE##_value->size() - - HANDLE_TYPE(INT32, int32); - HANDLE_TYPE(INT64, int64); - HANDLE_TYPE(UINT32, uint32); - HANDLE_TYPE(UINT64, uint64); - HANDLE_TYPE(FLOAT, float); - HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(BOOL, bool); - HANDLE_TYPE(ENUM, enum); - HANDLE_TYPE(STRING, string); - HANDLE_TYPE(MESSAGE, message); -#undef HANDLE_TYPE - } - - GOOGLE_LOG(FATAL) << "Can't get here."; - return 0; -} - -// This function deletes all allocated objects. This function should be only -// called if the Extension was created with an arena. -void ExtensionSet::Extension::Free() { - if (is_repeated) { - switch (cpp_type(type)) { -#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ - case WireFormatLite::CPPTYPE_##UPPERCASE: \ - delete repeated_##LOWERCASE##_value; \ - break - - HANDLE_TYPE(INT32, int32); - HANDLE_TYPE(INT64, int64); - HANDLE_TYPE(UINT32, uint32); - HANDLE_TYPE(UINT64, uint64); - HANDLE_TYPE(FLOAT, float); - HANDLE_TYPE(DOUBLE, double); - HANDLE_TYPE(BOOL, bool); - HANDLE_TYPE(ENUM, enum); - HANDLE_TYPE(STRING, string); - HANDLE_TYPE(MESSAGE, message); -#undef HANDLE_TYPE - } - } else { - switch (cpp_type(type)) { - case WireFormatLite::CPPTYPE_STRING: - delete string_value; - break; - case WireFormatLite::CPPTYPE_MESSAGE: - if (is_lazy) { - delete lazymessage_value; - } else { - delete message_value; - } - break; - default: - break; - } - } -} - -// Defined in extension_set_heavy.cc. -// int ExtensionSet::Extension::SpaceUsedExcludingSelf() const - -bool ExtensionSet::Extension::IsInitialized() const { - if (cpp_type(type) == WireFormatLite::CPPTYPE_MESSAGE) { - if (is_repeated) { - for (int i = 0; i < repeated_message_value->size(); i++) { - if (!repeated_message_value->Get(i).IsInitialized()) { - return false; - } - } - } else { - if (!is_cleared) { - if (is_lazy) { - if (!lazymessage_value->IsInitialized()) return false; - } else { - if (!message_value->IsInitialized()) return false; - } - } - } - } - return true; -} - -// Dummy key method to avoid weak vtable. -void ExtensionSet::LazyMessageExtension::UnusedKeyMethod() {} - -const ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) const { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - return FindOrNullInLargeMap(key); - } - const KeyValue* end = flat_end(); - const KeyValue* it = - std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator()); - if (it != end && it->first == key) { - return &it->second; - } - return NULL; -} - -const ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap( - int key) const { - assert(is_large()); - LargeMap::const_iterator it = map_.large->find(key); - if (it != map_.large->end()) { - return &it->second; - } - return NULL; -} - -ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - return FindOrNullInLargeMap(key); - } - KeyValue* end = flat_end(); - KeyValue* it = - std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator()); - if (it != end && it->first == key) { - return &it->second; - } - return NULL; -} - -ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(int key) { - assert(is_large()); - LargeMap::iterator it = map_.large->find(key); - if (it != map_.large->end()) { - return &it->second; - } - return NULL; -} - -std::pair ExtensionSet::Insert(int key) { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - auto maybe = map_.large->insert({key, Extension()}); - return {&maybe.first->second, maybe.second}; - } - KeyValue* end = flat_end(); - KeyValue* it = - std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator()); - if (it != end && it->first == key) { - return {&it->second, false}; - } - if (flat_size_ < flat_capacity_) { - std::copy_backward(it, end, end + 1); - ++flat_size_; - it->first = key; - it->second = Extension(); - return {&it->second, true}; - } - GrowCapacity(flat_size_ + 1); - return Insert(key); -} - -void ExtensionSet::GrowCapacity(size_t minimum_new_capacity) { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - return; // LargeMap does not have a "reserve" method. - } - if (flat_capacity_ >= minimum_new_capacity) { - return; - } - - auto new_flat_capacity = flat_capacity_; - do { - new_flat_capacity = new_flat_capacity == 0 ? 1 : new_flat_capacity * 4; - } while (new_flat_capacity < minimum_new_capacity); - - const KeyValue* begin = flat_begin(); - const KeyValue* end = flat_end(); - AllocatedData new_map; - if (new_flat_capacity > kMaximumFlatCapacity) { - new_map.large = Arena::Create(arena_); - LargeMap::iterator hint = new_map.large->begin(); - for (const KeyValue* it = begin; it != end; ++it) { - hint = new_map.large->insert(hint, {it->first, it->second}); - } - } else { - new_map.flat = Arena::CreateArray(arena_, new_flat_capacity); - std::copy(begin, end, new_map.flat); - } - - if (arena_ == nullptr) { - DeleteFlatMap(begin, flat_capacity_); - } - flat_capacity_ = new_flat_capacity; - map_ = new_map; - if (is_large()) { - flat_size_ = 0; - } -} - -// static -constexpr uint16 ExtensionSet::kMaximumFlatCapacity; - -void ExtensionSet::Erase(int key) { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - map_.large->erase(key); - return; - } - KeyValue* end = flat_end(); - KeyValue* it = - std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator()); - if (it != end && it->first == key) { - std::copy(it + 1, end, it); - --flat_size_; - } -} - -// ================================================================== -// Default repeated field instances for iterator-compatible accessors - -const RepeatedPrimitiveDefaults* RepeatedPrimitiveDefaults::default_instance() { - static auto instance = OnShutdownDelete(new RepeatedPrimitiveDefaults); - return instance; -} - -const RepeatedStringTypeTraits::RepeatedFieldType* -RepeatedStringTypeTraits::GetDefaultRepeatedField() { - static auto instance = OnShutdownDelete(new RepeatedFieldType); - return instance; -} - -uint8* ExtensionSet::Extension::InternalSerializeFieldWithCachedSizesToArray( - int number, uint8* target, io::EpsCopyOutputStream* stream) const { - if (is_repeated) { - if (is_packed) { - if (cached_size == 0) return target; - - target = stream->EnsureSpace(target); - target = WireFormatLite::WriteTagToArray( - number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, target); - target = WireFormatLite::WriteInt32NoTagToArray(cached_size, target); - - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - target = stream->EnsureSpace(target); \ - target = WireFormatLite::Write##CAMELCASE##NoTagToArray( \ - repeated_##LOWERCASE##_value->Get(i), target); \ - } \ - break - - HANDLE_TYPE(INT32, Int32, int32); - HANDLE_TYPE(INT64, Int64, int64); - HANDLE_TYPE(UINT32, UInt32, uint32); - HANDLE_TYPE(UINT64, UInt64, uint64); - HANDLE_TYPE(SINT32, SInt32, int32); - HANDLE_TYPE(SINT64, SInt64, int64); - HANDLE_TYPE(FIXED32, Fixed32, uint32); - HANDLE_TYPE(FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE(FLOAT, Float, float); - HANDLE_TYPE(DOUBLE, Double, double); - HANDLE_TYPE(BOOL, Bool, bool); - HANDLE_TYPE(ENUM, Enum, enum); -#undef HANDLE_TYPE - - case WireFormatLite::TYPE_STRING: - case WireFormatLite::TYPE_BYTES: - case WireFormatLite::TYPE_GROUP: - case WireFormatLite::TYPE_MESSAGE: - GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed."; - break; - } - } else { - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - target = stream->EnsureSpace(target); \ - target = WireFormatLite::Write##CAMELCASE##ToArray( \ - number, repeated_##LOWERCASE##_value->Get(i), target); \ - } \ - break - - HANDLE_TYPE(INT32, Int32, int32); - HANDLE_TYPE(INT64, Int64, int64); - HANDLE_TYPE(UINT32, UInt32, uint32); - HANDLE_TYPE(UINT64, UInt64, uint64); - HANDLE_TYPE(SINT32, SInt32, int32); - HANDLE_TYPE(SINT64, SInt64, int64); - HANDLE_TYPE(FIXED32, Fixed32, uint32); - HANDLE_TYPE(FIXED64, Fixed64, uint64); - HANDLE_TYPE(SFIXED32, SFixed32, int32); - HANDLE_TYPE(SFIXED64, SFixed64, int64); - HANDLE_TYPE(FLOAT, Float, float); - HANDLE_TYPE(DOUBLE, Double, double); - HANDLE_TYPE(BOOL, Bool, bool); - HANDLE_TYPE(ENUM, Enum, enum); -#undef HANDLE_TYPE -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - target = stream->EnsureSpace(target); \ - target = stream->WriteString( \ - number, repeated_##LOWERCASE##_value->Get(i), target); \ - } \ - break - HANDLE_TYPE(STRING, String, string); - HANDLE_TYPE(BYTES, Bytes, string); -#undef HANDLE_TYPE -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \ - target = stream->EnsureSpace(target); \ - target = WireFormatLite::InternalWrite##CAMELCASE( \ - number, repeated_##LOWERCASE##_value->Get(i), target, stream); \ - } \ - break - - HANDLE_TYPE(GROUP, Group, message); - HANDLE_TYPE(MESSAGE, Message, message); -#undef HANDLE_TYPE - } - } - } else if (!is_cleared) { - switch (real_type(type)) { -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - target = stream->EnsureSpace(target); \ - target = WireFormatLite::Write##CAMELCASE##ToArray(number, VALUE, target); \ - break - - HANDLE_TYPE(INT32, Int32, int32_value); - HANDLE_TYPE(INT64, Int64, int64_value); - HANDLE_TYPE(UINT32, UInt32, uint32_value); - HANDLE_TYPE(UINT64, UInt64, uint64_value); - HANDLE_TYPE(SINT32, SInt32, int32_value); - HANDLE_TYPE(SINT64, SInt64, int64_value); - HANDLE_TYPE(FIXED32, Fixed32, uint32_value); - HANDLE_TYPE(FIXED64, Fixed64, uint64_value); - HANDLE_TYPE(SFIXED32, SFixed32, int32_value); - HANDLE_TYPE(SFIXED64, SFixed64, int64_value); - HANDLE_TYPE(FLOAT, Float, float_value); - HANDLE_TYPE(DOUBLE, Double, double_value); - HANDLE_TYPE(BOOL, Bool, bool_value); - HANDLE_TYPE(ENUM, Enum, enum_value); -#undef HANDLE_TYPE -#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - target = stream->EnsureSpace(target); \ - target = stream->WriteString(number, VALUE, target); \ - break - HANDLE_TYPE(STRING, String, *string_value); - HANDLE_TYPE(BYTES, Bytes, *string_value); -#undef HANDLE_TYPE - case WireFormatLite::TYPE_GROUP: - target = stream->EnsureSpace(target); - target = WireFormatLite::InternalWriteGroup(number, *message_value, - target, stream); - break; - case WireFormatLite::TYPE_MESSAGE: - if (is_lazy) { - target = - lazymessage_value->WriteMessageToArray(number, target, stream); - } else { - target = stream->EnsureSpace(target); - target = WireFormatLite::InternalWriteMessage(number, *message_value, - target, stream); - } - break; - } - } - return target; -} - -uint8* -ExtensionSet::Extension::InternalSerializeMessageSetItemWithCachedSizesToArray( - int number, uint8* target, io::EpsCopyOutputStream* stream) const { - if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) { - // Not a valid MessageSet extension, but serialize it the normal way. - GOOGLE_LOG(WARNING) << "Invalid message set extension."; - return InternalSerializeFieldWithCachedSizesToArray(number, target, stream); - } - - if (is_cleared) return target; - - target = stream->EnsureSpace(target); - // Start group. - target = io::CodedOutputStream::WriteTagToArray( - WireFormatLite::kMessageSetItemStartTag, target); - // Write type ID. - target = WireFormatLite::WriteUInt32ToArray( - WireFormatLite::kMessageSetTypeIdNumber, number, target); - // Write message. - if (is_lazy) { - target = lazymessage_value->WriteMessageToArray( - WireFormatLite::kMessageSetMessageNumber, target, stream); - } else { - target = WireFormatLite::InternalWriteMessage( - WireFormatLite::kMessageSetMessageNumber, *message_value, target, - stream); - } - // End group. - target = stream->EnsureSpace(target); - target = io::CodedOutputStream::WriteTagToArray( - WireFormatLite::kMessageSetItemEndTag, target); - return target; -} - -size_t ExtensionSet::Extension::MessageSetItemByteSize(int number) const { - if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) { - // Not a valid MessageSet extension, but compute the byte size for it the - // normal way. - return ByteSize(number); - } - - if (is_cleared) return 0; - - size_t our_size = WireFormatLite::kMessageSetItemTagsSize; - - // type_id - our_size += io::CodedOutputStream::VarintSize32(number); - - // message - size_t message_size = 0; - if (is_lazy) { - message_size = lazymessage_value->ByteSizeLong(); - } else { - message_size = message_value->ByteSizeLong(); - } - - our_size += io::CodedOutputStream::VarintSize32(message_size); - our_size += message_size; - - return our_size; -} - -size_t ExtensionSet::MessageSetByteSize() const { - size_t total_size = 0; - ForEach([&total_size](int number, const Extension& ext) { - total_size += ext.MessageSetItemByteSize(number); - }); - return total_size; -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/generated_enum_util.cc b/third_party/protobuf-lite/generated_enum_util.cc deleted file mode 100644 index d0c25a96d..000000000 --- a/third_party/protobuf-lite/generated_enum_util.cc +++ /dev/null @@ -1,95 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { -namespace { - -bool EnumCompareByName(const EnumEntry& a, const EnumEntry& b) { - return StringPiece(a.name) < StringPiece(b.name); -} - -// Gets the numeric value of the EnumEntry at the given index, but returns a -// special value for the index -1. This gives a way to use std::lower_bound on a -// sorted array of indices while searching for value that we associate with -1. -int GetValue(const EnumEntry* enums, int i, int target) { - if (i == -1) { - return target; - } else { - return enums[i].value; - } -} - -} // namespace - -bool LookUpEnumValue(const EnumEntry* enums, size_t size, - StringPiece name, int* value) { - EnumEntry target{name, 0}; - auto it = std::lower_bound(enums, enums + size, target, EnumCompareByName); - if (it != enums + size && it->name == name) { - *value = it->value; - return true; - } - return false; -} - -int LookUpEnumName(const EnumEntry* enums, const int* sorted_indices, - size_t size, int value) { - auto comparator = [enums, value](int a, int b) { - return GetValue(enums, a, value) < GetValue(enums, b, value); - }; - auto it = - std::lower_bound(sorted_indices, sorted_indices + size, -1, comparator); - if (it != sorted_indices + size && enums[*it].value == value) { - return it - sorted_indices; - } - return -1; -} - -bool InitializeEnumStrings( - const EnumEntry* enums, const int* sorted_indices, size_t size, - internal::ExplicitlyConstructed* enum_strings) { - for (int i = 0; i < size; ++i) { - enum_strings[i].Construct(enums[sorted_indices[i]].name); - internal::OnShutdownDestroyString(enum_strings[i].get_mutable()); - } - return true; -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/generated_message_table_driven_lite.cc b/third_party/protobuf-lite/generated_message_table_driven_lite.cc deleted file mode 100644 index 02e6dacee..000000000 --- a/third_party/protobuf-lite/generated_message_table_driven_lite.cc +++ /dev/null @@ -1,106 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include - -#include -#include -#include -#include - -namespace google { -namespace protobuf { -namespace internal { - -namespace { - -std::string* MutableUnknownFields(MessageLite* msg, int64 arena_offset) { - return Raw(msg, arena_offset) - ->mutable_unknown_fields(); -} - -struct UnknownFieldHandlerLite { - // TODO(mvels): consider renaming UnknownFieldHandler to (TableDrivenTraits?), - // and conflating InternalMetaData into it, simplifying the template. - static constexpr bool IsLite() { return true; } - - static bool Skip(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, int tag) { - GOOGLE_DCHECK(!table.unknown_field_set); - io::StringOutputStream unknown_fields_string( - MutableUnknownFields(msg, table.arena_offset)); - io::CodedOutputStream unknown_fields_stream(&unknown_fields_string, false); - - return internal::WireFormatLite::SkipField(input, tag, - &unknown_fields_stream); - } - - static void Varint(MessageLite* msg, const ParseTable& table, int tag, - int value) { - GOOGLE_DCHECK(!table.unknown_field_set); - - io::StringOutputStream unknown_fields_string( - MutableUnknownFields(msg, table.arena_offset)); - io::CodedOutputStream unknown_fields_stream(&unknown_fields_string, false); - unknown_fields_stream.WriteVarint32(tag); - unknown_fields_stream.WriteVarint32(value); - } - - static bool ParseExtension(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input, int tag) { - ExtensionSet* extensions = GetExtensionSet(msg, table.extension_offset); - if (extensions == NULL) { - return false; - } - - const MessageLite* prototype = table.default_instance(); - - GOOGLE_DCHECK(!table.unknown_field_set); - io::StringOutputStream unknown_fields_string( - MutableUnknownFields(msg, table.arena_offset)); - io::CodedOutputStream unknown_fields_stream(&unknown_fields_string, false); - return extensions->ParseField(tag, input, prototype, - &unknown_fields_stream); - } -}; - -} // namespace - -bool MergePartialFromCodedStreamLite(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input) { - return MergePartialFromCodedStreamImpl(msg, table, - input); -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/generated_message_util.cc b/third_party/protobuf-lite/generated_message_util.cc deleted file mode 100644 index f1f6f8833..000000000 --- a/third_party/protobuf-lite/generated_message_util.cc +++ /dev/null @@ -1,804 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include - -#include - -#ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP -// We're only using this as a standard way for getting the thread id. -// We're not using any thread functionality. -#include // NOLINT -#endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -namespace google { -namespace protobuf { -namespace internal { - -void DestroyMessage(const void* message) { - static_cast(message)->~MessageLite(); -} -void DestroyString(const void* s) { - static_cast(s)->~basic_string(); -} - -PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT EmptyString - fixed_address_empty_string; // NOLINT - - -PROTOBUF_CONSTINIT std::atomic init_protobuf_defaults_state{false}; -static bool InitProtobufDefaultsImpl() { - ::new (static_cast(&fixed_address_empty_string.value)) std::string(); - OnShutdownDestroyString(&fixed_address_empty_string.value); - - // Verify that we can indeed get the address during constant evaluation. - PROTOBUF_CONSTINIT static const std::string& fixed_address_empty_string_test = - GetEmptyStringAlreadyInited(); - (void)fixed_address_empty_string_test; - - init_protobuf_defaults_state.store(true, std::memory_order_release); - return true; -} - -void InitProtobufDefaultsSlow() { - static bool is_inited = InitProtobufDefaultsImpl(); - (void)is_inited; -} - -size_t StringSpaceUsedExcludingSelfLong(const std::string& str) { - const void* start = &str; - const void* end = &str + 1; - if (start <= str.data() && str.data() < end) { - // The string's data is stored inside the string object itself. - return 0; - } else { - return str.capacity(); - } -} - -template -const T& Get(const void* ptr) { - return *static_cast(ptr); -} - -// PrimitiveTypeHelper is a wrapper around the interface of WireFormatLite. -// WireFormatLite has a very inconvenient interface with respect to template -// meta-programming. This class wraps the different named functions into -// a single Serialize / SerializeToArray interface. -template -struct PrimitiveTypeHelper; - -template <> -struct PrimitiveTypeHelper { - typedef bool Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteBoolNoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteBoolNoTagToArray(Get(ptr), buffer); - } -}; - -template <> -struct PrimitiveTypeHelper { - typedef int32 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteInt32NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteInt32NoTagToArray(Get(ptr), buffer); - } -}; - -template <> -struct PrimitiveTypeHelper { - typedef int32 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteSInt32NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteSInt32NoTagToArray(Get(ptr), buffer); - } -}; - -template <> -struct PrimitiveTypeHelper { - typedef uint32 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteUInt32NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteUInt32NoTagToArray(Get(ptr), buffer); - } -}; -template <> -struct PrimitiveTypeHelper { - typedef int64 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteInt64NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteInt64NoTagToArray(Get(ptr), buffer); - } -}; - -template <> -struct PrimitiveTypeHelper { - typedef int64 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteSInt64NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteSInt64NoTagToArray(Get(ptr), buffer); - } -}; -template <> -struct PrimitiveTypeHelper { - typedef uint64 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteUInt64NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteUInt64NoTagToArray(Get(ptr), buffer); - } -}; - -template <> -struct PrimitiveTypeHelper { - typedef uint32 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteFixed32NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteFixed32NoTagToArray(Get(ptr), buffer); - } -}; - -template <> -struct PrimitiveTypeHelper { - typedef uint64 Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - WireFormatLite::WriteFixed64NoTag(Get(ptr), output); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - return WireFormatLite::WriteFixed64NoTagToArray(Get(ptr), buffer); - } -}; - -template <> -struct PrimitiveTypeHelper - : PrimitiveTypeHelper {}; - -template <> -struct PrimitiveTypeHelper - : PrimitiveTypeHelper { - typedef int32 Type; -}; -template <> -struct PrimitiveTypeHelper - : PrimitiveTypeHelper { - typedef int64 Type; -}; -template <> -struct PrimitiveTypeHelper - : PrimitiveTypeHelper { - typedef float Type; -}; -template <> -struct PrimitiveTypeHelper - : PrimitiveTypeHelper { - typedef double Type; -}; - -template <> -struct PrimitiveTypeHelper { - typedef std::string Type; - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - const Type& value = *static_cast(ptr); - output->WriteVarint32(value.size()); - output->WriteRawMaybeAliased(value.data(), value.size()); - } - static uint8* SerializeToArray(const void* ptr, uint8* buffer) { - const Type& value = *static_cast(ptr); - return io::CodedOutputStream::WriteStringWithSizeToArray(value, buffer); - } -}; - -template <> -struct PrimitiveTypeHelper - : PrimitiveTypeHelper {}; - - -// We want to serialize to both CodedOutputStream and directly into byte arrays -// without duplicating the code. In fact we might want extra output channels in -// the future. -template -struct OutputHelper; - -template -void SerializeTo(const void* ptr, O* output) { - OutputHelper::Serialize(ptr, output); -} - -template -void WriteTagTo(uint32 tag, O* output) { - SerializeTo(&tag, output); -} - -template -void WriteLengthTo(uint32 length, O* output) { - SerializeTo(&length, output); -} - -// Specialization for coded output stream -template -struct OutputHelper { - static void Serialize(const void* ptr, io::CodedOutputStream* output) { - PrimitiveTypeHelper::Serialize(ptr, output); - } -}; - -// Specialization for writing into a plain array -struct ArrayOutput { - uint8* ptr; - bool is_deterministic; -}; - -template -struct OutputHelper { - static void Serialize(const void* ptr, ArrayOutput* output) { - output->ptr = PrimitiveTypeHelper::SerializeToArray(ptr, output->ptr); - } -}; - -void SerializeMessageNoTable(const MessageLite* msg, - io::CodedOutputStream* output) { - msg->SerializeWithCachedSizes(output); -} - -void SerializeMessageNoTable(const MessageLite* msg, ArrayOutput* output) { - io::ArrayOutputStream array_stream(output->ptr, INT_MAX); - io::CodedOutputStream o(&array_stream); - o.SetSerializationDeterministic(output->is_deterministic); - msg->SerializeWithCachedSizes(&o); - output->ptr += o.ByteCount(); -} - -// Helper to branch to fast path if possible -void SerializeMessageDispatch(const MessageLite& msg, - const FieldMetadata* field_table, int num_fields, - int32 cached_size, - io::CodedOutputStream* output) { - const uint8* base = reinterpret_cast(&msg); - SerializeInternal(base, field_table, num_fields, output); -} - -// Helper to branch to fast path if possible -void SerializeMessageDispatch(const MessageLite& msg, - const FieldMetadata* field_table, int num_fields, - int32 cached_size, ArrayOutput* output) { - const uint8* base = reinterpret_cast(&msg); - output->ptr = SerializeInternalToArray(base, field_table, num_fields, - output->is_deterministic, output->ptr); -} - -// Serializing messages is special as it's not a primitive type and needs an -// explicit overload for each output type. -template -void SerializeMessageTo(const MessageLite* msg, const void* table_ptr, - O* output) { - const SerializationTable* table = - static_cast(table_ptr); - if (!table) { - // Proto1 - WriteLengthTo(msg->GetCachedSize(), output); - SerializeMessageNoTable(msg, output); - return; - } - const FieldMetadata* field_table = table->field_table; - const uint8* base = reinterpret_cast(msg); - int cached_size = *reinterpret_cast(base + field_table->offset); - WriteLengthTo(cached_size, output); - int num_fields = table->num_fields - 1; - SerializeMessageDispatch(*msg, field_table + 1, num_fields, cached_size, - output); -} - -// Almost the same as above only it doesn't output the length field. -template -void SerializeGroupTo(const MessageLite* msg, const void* table_ptr, - O* output) { - const SerializationTable* table = - static_cast(table_ptr); - if (!table) { - // Proto1 - SerializeMessageNoTable(msg, output); - return; - } - const FieldMetadata* field_table = table->field_table; - const uint8* base = reinterpret_cast(msg); - int cached_size = *reinterpret_cast(base + field_table->offset); - int num_fields = table->num_fields - 1; - SerializeMessageDispatch(*msg, field_table + 1, num_fields, cached_size, - output); -} - -template -struct SingularFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - WriteTagTo(md.tag, output); - SerializeTo(field, output); - } -}; - -template <> -struct SingularFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - WriteTagTo(md.tag, output); - SerializeTo(&Get(field).Get(), - output); - } -}; - -template <> -struct SingularFieldHelper - : SingularFieldHelper {}; - -template <> -struct SingularFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - WriteTagTo(md.tag, output); - SerializeGroupTo(Get(field), - static_cast(md.ptr), output); - WriteTagTo(md.tag + 1, output); - } -}; - -template <> -struct SingularFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - WriteTagTo(md.tag, output); - SerializeMessageTo(Get(field), - static_cast(md.ptr), output); - } -}; - -template -struct RepeatedFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - typedef typename PrimitiveTypeHelper::Type T; - const RepeatedField& array = Get >(field); - for (int i = 0; i < array.size(); i++) { - WriteTagTo(md.tag, output); - SerializeTo(&array[i], output); - } - } -}; - -// We need to use a helper class to get access to the private members -class AccessorHelper { - public: - static int Size(const RepeatedPtrFieldBase& x) { return x.size(); } - static void const* Get(const RepeatedPtrFieldBase& x, int idx) { - return x.raw_data()[idx]; - } -}; - -template <> -struct RepeatedFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - const internal::RepeatedPtrFieldBase& array = - Get(field); - for (int i = 0; i < AccessorHelper::Size(array); i++) { - WriteTagTo(md.tag, output); - SerializeTo(AccessorHelper::Get(array, i), - output); - } - } -}; - -template <> -struct RepeatedFieldHelper - : RepeatedFieldHelper {}; - -template <> -struct RepeatedFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - const internal::RepeatedPtrFieldBase& array = - Get(field); - for (int i = 0; i < AccessorHelper::Size(array); i++) { - WriteTagTo(md.tag, output); - SerializeGroupTo( - static_cast(AccessorHelper::Get(array, i)), - static_cast(md.ptr), output); - WriteTagTo(md.tag + 1, output); - } - } -}; - -template <> -struct RepeatedFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - const internal::RepeatedPtrFieldBase& array = - Get(field); - for (int i = 0; i < AccessorHelper::Size(array); i++) { - WriteTagTo(md.tag, output); - SerializeMessageTo( - static_cast(AccessorHelper::Get(array, i)), - md.ptr, output); - } - } -}; - - -template -struct PackedFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - typedef typename PrimitiveTypeHelper::Type T; - const RepeatedField& array = Get >(field); - if (array.empty()) return; - WriteTagTo(md.tag, output); - int cached_size = - Get(static_cast(field) + sizeof(RepeatedField)); - WriteLengthTo(cached_size, output); - for (int i = 0; i < array.size(); i++) { - SerializeTo(&array[i], output); - } - } -}; - -template <> -struct PackedFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - GOOGLE_LOG(FATAL) << "Not implemented field number " << md.tag << " with type " - << md.type; - } -}; - -template <> -struct PackedFieldHelper - : PackedFieldHelper {}; -template <> -struct PackedFieldHelper - : PackedFieldHelper {}; -template <> -struct PackedFieldHelper - : PackedFieldHelper {}; - -template -struct OneOfFieldHelper { - template - static void Serialize(const void* field, const FieldMetadata& md, O* output) { - SingularFieldHelper::Serialize(field, md, output); - } -}; - - -void SerializeNotImplemented(int field) { - GOOGLE_LOG(FATAL) << "Not implemented field number " << field; -} - -// When switching to c++11 we should make these constexpr functions -#define SERIALIZE_TABLE_OP(type, type_class) \ - ((type - 1) + static_cast(type_class) * FieldMetadata::kNumTypes) - -int FieldMetadata::CalculateType(int type, - FieldMetadata::FieldTypeClass type_class) { - return SERIALIZE_TABLE_OP(type, type_class); -} - -template -bool IsNull(const void* ptr) { - return *static_cast::Type*>(ptr) == - 0; -} - -template <> -bool IsNull(const void* ptr) { - return static_cast(ptr)->Get().size() == 0; -} - -template <> -bool IsNull(const void* ptr) { - return static_cast(ptr)->Get().size() == 0; -} - -template <> -bool IsNull(const void* ptr) { - return Get(ptr) == NULL; -} - -template <> -bool IsNull(const void* ptr) { - return Get(ptr) == NULL; -} - - -#define SERIALIZERS_FOR_TYPE(type) \ - case SERIALIZE_TABLE_OP(type, FieldMetadata::kPresence): \ - if (!IsPresent(base, field_metadata.has_offset)) continue; \ - SingularFieldHelper::Serialize(ptr, field_metadata, output); \ - break; \ - case SERIALIZE_TABLE_OP(type, FieldMetadata::kNoPresence): \ - if (IsNull(ptr)) continue; \ - SingularFieldHelper::Serialize(ptr, field_metadata, output); \ - break; \ - case SERIALIZE_TABLE_OP(type, FieldMetadata::kRepeated): \ - RepeatedFieldHelper::Serialize(ptr, field_metadata, output); \ - break; \ - case SERIALIZE_TABLE_OP(type, FieldMetadata::kPacked): \ - PackedFieldHelper::Serialize(ptr, field_metadata, output); \ - break; \ - case SERIALIZE_TABLE_OP(type, FieldMetadata::kOneOf): \ - if (!IsOneofPresent(base, field_metadata.has_offset, field_metadata.tag)) \ - continue; \ - OneOfFieldHelper::Serialize(ptr, field_metadata, output); \ - break - -void SerializeInternal(const uint8* base, - const FieldMetadata* field_metadata_table, - int32 num_fields, io::CodedOutputStream* output) { - SpecialSerializer func = nullptr; - for (int i = 0; i < num_fields; i++) { - const FieldMetadata& field_metadata = field_metadata_table[i]; - const uint8* ptr = base + field_metadata.offset; - switch (field_metadata.type) { - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_DOUBLE); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FLOAT); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BOOL); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_STRING); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_GROUP); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_MESSAGE); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BYTES); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_ENUM); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT64); - - // Special cases - case FieldMetadata::kSpecial: - func = reinterpret_cast( - const_cast(field_metadata.ptr)); - func(base, field_metadata.offset, field_metadata.tag, - field_metadata.has_offset, output); - break; - default: - // __builtin_unreachable() - SerializeNotImplemented(field_metadata.type); - } - } -} - -uint8* SerializeInternalToArray(const uint8* base, - const FieldMetadata* field_metadata_table, - int32 num_fields, bool is_deterministic, - uint8* buffer) { - ArrayOutput array_output = {buffer, is_deterministic}; - ArrayOutput* output = &array_output; - SpecialSerializer func = nullptr; - for (int i = 0; i < num_fields; i++) { - const FieldMetadata& field_metadata = field_metadata_table[i]; - const uint8* ptr = base + field_metadata.offset; - switch (field_metadata.type) { - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_DOUBLE); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FLOAT); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_INT32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_FIXED32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BOOL); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_STRING); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_GROUP); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_MESSAGE); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_BYTES); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_UINT32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_ENUM); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SFIXED64); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT32); - SERIALIZERS_FOR_TYPE(WireFormatLite::TYPE_SINT64); - // Special cases - case FieldMetadata::kSpecial: { - io::ArrayOutputStream array_stream(array_output.ptr, INT_MAX); - io::CodedOutputStream output(&array_stream); - output.SetSerializationDeterministic(is_deterministic); - func = reinterpret_cast( - const_cast(field_metadata.ptr)); - func(base, field_metadata.offset, field_metadata.tag, - field_metadata.has_offset, &output); - array_output.ptr += output.ByteCount(); - } break; - default: - // __builtin_unreachable() - SerializeNotImplemented(field_metadata.type); - } - } - return array_output.ptr; -} -#undef SERIALIZERS_FOR_TYPE - -void ExtensionSerializer(const uint8* ptr, uint32 offset, uint32 tag, - uint32 has_offset, io::CodedOutputStream* output) { - reinterpret_cast(ptr + offset) - ->SerializeWithCachedSizes(tag, has_offset, output); -} - -void UnknownFieldSerializerLite(const uint8* ptr, uint32 offset, uint32 tag, - uint32 has_offset, - io::CodedOutputStream* output) { - output->WriteString( - reinterpret_cast(ptr + offset) - ->unknown_fields(&internal::GetEmptyString)); -} - -MessageLite* DuplicateIfNonNullInternal(MessageLite* message) { - if (message) { - MessageLite* ret = message->New(); - ret->CheckTypeAndMergeFrom(*message); - return ret; - } else { - return NULL; - } -} - -void GenericSwap(MessageLite* m1, MessageLite* m2) { - std::unique_ptr tmp(m1->New()); - tmp->CheckTypeAndMergeFrom(*m1); - m1->Clear(); - m1->CheckTypeAndMergeFrom(*m2); - m2->Clear(); - m2->CheckTypeAndMergeFrom(*tmp); -} - -// Returns a message owned by this Arena. This may require Own()ing or -// duplicating the message. -MessageLite* GetOwnedMessageInternal(Arena* message_arena, - MessageLite* submessage, - Arena* submessage_arena) { - GOOGLE_DCHECK(submessage->GetArena() == submessage_arena); - GOOGLE_DCHECK(message_arena != submessage_arena); - if (message_arena != NULL && submessage_arena == NULL) { - message_arena->Own(submessage); - return submessage; - } else { - MessageLite* ret = submessage->New(message_arena); - ret->CheckTypeAndMergeFrom(*submessage); - return ret; - } -} - -namespace { - -void InitSCC_DFS(SCCInfoBase* scc) { - if (scc->visit_status.load(std::memory_order_relaxed) != - SCCInfoBase::kUninitialized) - return; - scc->visit_status.store(SCCInfoBase::kRunning, std::memory_order_relaxed); - // Each base is followed by an array of void*, containing first pointers to - // SCCInfoBase and then pointers-to-pointers to SCCInfoBase. - auto deps = reinterpret_cast(scc + 1); - auto strong_deps = reinterpret_cast(deps); - for (int i = 0; i < scc->num_deps; ++i) { - if (strong_deps[i]) InitSCC_DFS(strong_deps[i]); - } - auto implicit_weak_deps = - reinterpret_cast(deps + scc->num_deps); - for (int i = 0; i < scc->num_implicit_weak_deps; ++i) { - if (*implicit_weak_deps[i]) { - InitSCC_DFS(*implicit_weak_deps[i]); - } - } - scc->init_func(); - // Mark done (note we use memory order release here), other threads could - // now see this as initialized and thus the initialization must have happened - // before. - scc->visit_status.store(SCCInfoBase::kInitialized, std::memory_order_release); -} - -} // namespace - -void InitSCCImpl(SCCInfoBase* scc) { - static WrappedMutex mu{GOOGLE_PROTOBUF_LINKER_INITIALIZED}; - // Either the default in case no initialization is running or the id of the - // thread that is currently initializing. -#ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - static std::atomic runner; - auto me = std::this_thread::get_id(); -#else - // This is a lightweight replacement for std::thread::id. std::thread does not - // work on Windows XP SP2 with the latest VC++ libraries, because it utilizes - // the Concurrency Runtime that is only supported on Windows XP SP3 and above. - static std::atomic_llong runner(-1); - auto me = ::GetCurrentThreadId(); -#endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - - // This will only happen because the constructor will call InitSCC while - // constructing the default instance. - if (runner.load(std::memory_order_relaxed) == me) { - // Because we're in the process of constructing the default instance. - // We can be assured that we're already exploring this SCC. - GOOGLE_CHECK_EQ(scc->visit_status.load(std::memory_order_relaxed), - SCCInfoBase::kRunning); - return; - } - InitProtobufDefaults(); - mu.Lock(); - runner.store(me, std::memory_order_relaxed); - InitSCC_DFS(scc); - -#ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - runner.store(std::thread::id{}, std::memory_order_relaxed); -#else - runner.store(-1, std::memory_order_relaxed); -#endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - - mu.Unlock(); -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/google/protobuf/any.h b/third_party/protobuf-lite/google/protobuf/any.h deleted file mode 100644 index e8f2cacf1..000000000 --- a/third_party/protobuf-lite/google/protobuf/any.h +++ /dev/null @@ -1,150 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_ANY_H__ -#define GOOGLE_PROTOBUF_ANY_H__ - -#include - -#include -#include -#include - -#include - -namespace google { -namespace protobuf { - -class FieldDescriptor; -class Message; - -namespace internal { - -extern const char kAnyFullTypeName[]; // "google.protobuf.Any". -extern const char kTypeGoogleApisComPrefix[]; // "type.googleapis.com/". -extern const char kTypeGoogleProdComPrefix[]; // "type.googleprod.com/". - -std::string GetTypeUrl(StringPiece message_name, - StringPiece type_url_prefix); - -// Helper class used to implement google::protobuf::Any. -class PROTOBUF_EXPORT AnyMetadata { - typedef ArenaStringPtr UrlType; - typedef ArenaStringPtr ValueType; - public: - // AnyMetadata does not take ownership of "type_url" and "value". - constexpr AnyMetadata(UrlType* type_url, ValueType* value) - : type_url_(type_url), value_(value) {} - - // Packs a message using the default type URL prefix: "type.googleapis.com". - // The resulted type URL will be "type.googleapis.com/". - template - void PackFrom(const T& message) { - InternalPackFrom(message, kTypeGoogleApisComPrefix, T::FullMessageName()); - } - - void PackFrom(const Message& message); - - // Packs a message using the given type URL prefix. The type URL will be - // constructed by concatenating the message type's full name to the prefix - // with an optional "/" separator if the prefix doesn't already end with "/". - // For example, both PackFrom(message, "type.googleapis.com") and - // PackFrom(message, "type.googleapis.com/") yield the same result type - // URL: "type.googleapis.com/". - template - void PackFrom(const T& message, StringPiece type_url_prefix) { - InternalPackFrom(message, type_url_prefix, T::FullMessageName()); - } - - void PackFrom(const Message& message, StringPiece type_url_prefix); - - // Unpacks the payload into the given message. Returns false if the message's - // type doesn't match the type specified in the type URL (i.e., the full - // name after the last "/" of the type URL doesn't match the message's actual - // full name) or parsing the payload has failed. - template - bool UnpackTo(T* message) const { - return InternalUnpackTo(T::FullMessageName(), message); - } - - bool UnpackTo(Message* message) const; - - // Checks whether the type specified in the type URL matches the given type. - // A type is considered matching if its full name matches the full name after - // the last "/" in the type URL. - template - bool Is() const { - return InternalIs(T::FullMessageName()); - } - - private: - void InternalPackFrom(const MessageLite& message, - StringPiece type_url_prefix, - StringPiece type_name); - bool InternalUnpackTo(StringPiece type_name, - MessageLite* message) const; - bool InternalIs(StringPiece type_name) const; - - UrlType* type_url_; - ValueType* value_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AnyMetadata); -}; - -// Get the proto type name from Any::type_url value. For example, passing -// "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in -// *full_type_name. Returns false if the type_url does not have a "/" -// in the type url separating the full type name. -// -// NOTE: this function is available publicly as: -// google::protobuf::Any() // static method on the generated message type. -bool ParseAnyTypeUrl(StringPiece type_url, std::string* full_type_name); - -// Get the proto type name and prefix from Any::type_url value. For example, -// passing "type.googleapis.com/rpc.QueryOrigin" will return -// "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in -// *full_type_name. Returns false if the type_url does not have a "/" in the -// type url separating the full type name. -bool ParseAnyTypeUrl(StringPiece type_url, std::string* url_prefix, - std::string* full_type_name); - -// See if message is of type google.protobuf.Any, if so, return the descriptors -// for "type_url" and "value" fields. -bool GetAnyFieldDescriptors(const Message& message, - const FieldDescriptor** type_url_field, - const FieldDescriptor** value_field); - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_ANY_H__ diff --git a/third_party/protobuf-lite/google/protobuf/arena.h b/third_party/protobuf-lite/google/protobuf/arena.h deleted file mode 100644 index f28bebfd3..000000000 --- a/third_party/protobuf-lite/google/protobuf/arena.h +++ /dev/null @@ -1,694 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// This file defines an Arena allocator for better allocation performance. - -#ifndef GOOGLE_PROTOBUF_ARENA_H__ -#define GOOGLE_PROTOBUF_ARENA_H__ - - -#include -#include -#include -#ifdef max -#undef max // Visual Studio defines this macro -#endif -#if defined(_MSC_VER) && !defined(_LIBCPP_STD_VER) && !_HAS_EXCEPTIONS -// Work around bugs in MSVC header when _HAS_EXCEPTIONS=0. -#include -#include -namespace std { -using type_info = ::type_info; -} -#else -#include -#endif - -#include -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { - -struct ArenaOptions; // defined below - -} // namespace protobuf -} // namespace google - -namespace google { -namespace protobuf { - -class Arena; // defined below -class Message; // defined in message.h -class MessageLite; -template -class Map; - -namespace arena_metrics { - -void EnableArenaMetrics(ArenaOptions* options); - -} // namespace arena_metrics - -namespace internal { - -struct ArenaStringPtr; // defined in arenastring.h -class LazyField; // defined in lazy_field.h -class EpsCopyInputStream; // defined in parse_context.h - -template -class GenericTypeHandler; // defined in repeated_field.h - -// Templated cleanup methods. -template -void arena_destruct_object(void* object) { - reinterpret_cast(object)->~T(); -} -template -void arena_delete_object(void* object) { - delete reinterpret_cast(object); -} -} // namespace internal - -// ArenaOptions provides optional additional parameters to arena construction -// that control its block-allocation behavior. -struct ArenaOptions { - // This defines the size of the first block requested from the system malloc. - // Subsequent block sizes will increase in a geometric series up to a maximum. - size_t start_block_size; - - // This defines the maximum block size requested from system malloc (unless an - // individual arena allocation request occurs with a size larger than this - // maximum). Requested block sizes increase up to this value, then remain - // here. - size_t max_block_size; - - // An initial block of memory for the arena to use, or NULL for none. If - // provided, the block must live at least as long as the arena itself. The - // creator of the Arena retains ownership of the block after the Arena is - // destroyed. - char* initial_block; - - // The size of the initial block, if provided. - size_t initial_block_size; - - // A function pointer to an alloc method that returns memory blocks of size - // requested. By default, it contains a ptr to the malloc function. - // - // NOTE: block_alloc and dealloc functions are expected to behave like - // malloc and free, including Asan poisoning. - void* (*block_alloc)(size_t); - // A function pointer to a dealloc method that takes ownership of the blocks - // from the arena. By default, it contains a ptr to a wrapper function that - // calls free. - void (*block_dealloc)(void*, size_t); - - ArenaOptions() - : start_block_size(kDefaultStartBlockSize), - max_block_size(kDefaultMaxBlockSize), - initial_block(NULL), - initial_block_size(0), - block_alloc(kDefaultBlockAlloc), - block_dealloc(&internal::ArenaFree), - make_metrics_collector(nullptr) {} - - PROTOBUF_EXPORT static void* (*const kDefaultBlockAlloc)(size_t); - - private: - // If make_metrics_collector is not nullptr, it will be called at Arena init - // time. It may return a pointer to a collector instance that will be notified - // of interesting events related to the arena. - internal::ArenaMetricsCollector* (*make_metrics_collector)(); - - // Constants define default starting block size and max block size for - // arena allocator behavior -- see descriptions above. - static const size_t kDefaultStartBlockSize = - internal::ArenaImpl::kDefaultStartBlockSize; - static const size_t kDefaultMaxBlockSize = - internal::ArenaImpl::kDefaultMaxBlockSize; - - friend void arena_metrics::EnableArenaMetrics(ArenaOptions*); - - friend class Arena; - friend class ArenaOptionsTestFriend; - friend class internal::ArenaImpl; -}; - -// Support for non-RTTI environments. (The metrics hooks API uses type -// information.) -#if PROTOBUF_RTTI -#define RTTI_TYPE_ID(type) (&typeid(type)) -#else -#define RTTI_TYPE_ID(type) (NULL) -#endif - -// Arena allocator. Arena allocation replaces ordinary (heap-based) allocation -// with new/delete, and improves performance by aggregating allocations into -// larger blocks and freeing allocations all at once. Protocol messages are -// allocated on an arena by using Arena::CreateMessage(Arena*), below, and -// are automatically freed when the arena is destroyed. -// -// This is a thread-safe implementation: multiple threads may allocate from the -// arena concurrently. Destruction is not thread-safe and the destructing -// thread must synchronize with users of the arena first. -// -// An arena provides two allocation interfaces: CreateMessage, which works -// for arena-enabled proto2 message types as well as other types that satisfy -// the appropriate protocol (described below), and Create, which works for -// any arbitrary type T. CreateMessage is better when the type T supports it, -// because this interface (i) passes the arena pointer to the created object so -// that its sub-objects and internal allocations can use the arena too, and (ii) -// elides the object's destructor call when possible. Create does not place -// any special requirements on the type T, and will invoke the object's -// destructor when the arena is destroyed. -// -// The arena message allocation protocol, required by -// CreateMessage(Arena* arena, Args&&... args), is as follows: -// -// - The type T must have (at least) two constructors: a constructor callable -// with `args` (without `arena`), called when a T is allocated on the heap; -// and a constructor callable with `Arena* arena, Args&&... args`, called when -// a T is allocated on an arena. If the second constructor is called with a -// NULL arena pointer, it must be equivalent to invoking the first -// (`args`-only) constructor. -// -// - The type T must have a particular type trait: a nested type -// |InternalArenaConstructable_|. This is usually a typedef to |void|. If no -// such type trait exists, then the instantiation CreateMessage will fail -// to compile. -// -// - The type T *may* have the type trait |DestructorSkippable_|. If this type -// trait is present in the type, then its destructor will not be called if and -// only if it was passed a non-NULL arena pointer. If this type trait is not -// present on the type, then its destructor is always called when the -// containing arena is destroyed. -// -// This protocol is implemented by all arena-enabled proto2 message classes as -// well as protobuf container types like RepeatedPtrField and Map. The protocol -// is internal to protobuf and is not guaranteed to be stable. Non-proto types -// should not rely on this protocol. -class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final { - public: - // Default constructor with sensible default options, tuned for average - // use-cases. - inline Arena() : impl_() {} - - // Construct an arena with default options, except for the supplied - // initial block. It is more efficient to use this constructor - // instead of passing ArenaOptions if the only configuration needed - // by the caller is supplying an initial block. - inline Arena(char* initial_block, size_t initial_block_size) - : impl_(initial_block, initial_block_size) {} - - // Arena constructor taking custom options. See ArenaOptions above for - // descriptions of the options available. - explicit Arena(const ArenaOptions& options) : impl_(options) {} - - // Block overhead. Use this as a guide for how much to over-allocate the - // initial block if you want an allocation of size N to fit inside it. - // - // WARNING: if you allocate multiple objects, it is difficult to guarantee - // that a series of allocations will fit in the initial block, especially if - // Arena changes its alignment guarantees in the future! - static const size_t kBlockOverhead = internal::ArenaImpl::kBlockHeaderSize + - internal::ArenaImpl::kSerialArenaSize; - - inline ~Arena() {} - - // TODO(protobuf-team): Fix callers to use constructor and delete this method. - void Init(const ArenaOptions&) {} - - // API to create proto2 message objects on the arena. If the arena passed in - // is NULL, then a heap allocated object is returned. Type T must be a message - // defined in a .proto file with cc_enable_arenas set to true, otherwise a - // compilation error will occur. - // - // RepeatedField and RepeatedPtrField may also be instantiated directly on an - // arena with this method. - // - // This function also accepts any type T that satisfies the arena message - // allocation protocol, documented above. - template - PROTOBUF_ALWAYS_INLINE static T* CreateMessage(Arena* arena, Args&&... args) { - static_assert( - InternalHelper::is_arena_constructable::value, - "CreateMessage can only construct types that are ArenaConstructable"); - // We must delegate to CreateMaybeMessage() and NOT CreateMessageInternal() - // because protobuf generated classes specialize CreateMaybeMessage() and we - // need to use that specialization for code size reasons. - return Arena::CreateMaybeMessage(arena, std::forward(args)...); - } - - // API to create any objects on the arena. Note that only the object will - // be created on the arena; the underlying ptrs (in case of a proto2 message) - // will be still heap allocated. Proto messages should usually be allocated - // with CreateMessage() instead. - // - // Note that even if T satisfies the arena message construction protocol - // (InternalArenaConstructable_ trait and optional DestructorSkippable_ - // trait), as described above, this function does not follow the protocol; - // instead, it treats T as a black-box type, just as if it did not have these - // traits. Specifically, T's constructor arguments will always be only those - // passed to Create() -- no additional arena pointer is implicitly added. - // Furthermore, the destructor will always be called at arena destruction time - // (unless the destructor is trivial). Hence, from T's point of view, it is as - // if the object were allocated on the heap (except that the underlying memory - // is obtained from the arena). - template - PROTOBUF_ALWAYS_INLINE static T* Create(Arena* arena, Args&&... args) { - return CreateNoMessage(arena, is_arena_constructable(), - std::forward(args)...); - } - - // Create an array of object type T on the arena *without* invoking the - // constructor of T. If `arena` is null, then the return value should be freed - // with `delete[] x;` (or `::operator delete[](x);`). - // To ensure safe uses, this function checks at compile time - // (when compiled as C++11) that T is trivially default-constructible and - // trivially destructible. - template - PROTOBUF_ALWAYS_INLINE static T* CreateArray(Arena* arena, - size_t num_elements) { - static_assert(std::is_pod::value, - "CreateArray requires a trivially constructible type"); - static_assert(std::is_trivially_destructible::value, - "CreateArray requires a trivially destructible type"); - GOOGLE_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) - << "Requested size is too large to fit into size_t."; - if (arena == NULL) { - return static_cast(::operator new[](num_elements * sizeof(T))); - } else { - return arena->CreateInternalRawArray(num_elements); - } - } - - // Returns the total space allocated by the arena, which is the sum of the - // sizes of the underlying blocks. This method is relatively fast; a counter - // is kept as blocks are allocated. - uint64 SpaceAllocated() const { return impl_.SpaceAllocated(); } - // Returns the total space used by the arena. Similar to SpaceAllocated but - // does not include free space and block overhead. The total space returned - // may not include space used by other threads executing concurrently with - // the call to this method. - uint64 SpaceUsed() const { return impl_.SpaceUsed(); } - - // Frees all storage allocated by this arena after calling destructors - // registered with OwnDestructor() and freeing objects registered with Own(). - // Any objects allocated on this arena are unusable after this call. It also - // returns the total space used by the arena which is the sums of the sizes - // of the allocated blocks. This method is not thread-safe. - uint64 Reset() { return impl_.Reset(); } - - // Adds |object| to a list of heap-allocated objects to be freed with |delete| - // when the arena is destroyed or reset. - template - PROTOBUF_NOINLINE void Own(T* object) { - OwnInternal(object, std::is_convertible()); - } - - // Adds |object| to a list of objects whose destructors will be manually - // called when the arena is destroyed or reset. This differs from Own() in - // that it does not free the underlying memory with |delete|; hence, it is - // normally only used for objects that are placement-newed into - // arena-allocated memory. - template - PROTOBUF_NOINLINE void OwnDestructor(T* object) { - if (object != NULL) { - impl_.AddCleanup(object, &internal::arena_destruct_object); - } - } - - // Adds a custom member function on an object to the list of destructors that - // will be manually called when the arena is destroyed or reset. This differs - // from OwnDestructor() in that any member function may be specified, not only - // the class destructor. - PROTOBUF_NOINLINE void OwnCustomDestructor(void* object, - void (*destruct)(void*)) { - impl_.AddCleanup(object, destruct); - } - - // Retrieves the arena associated with |value| if |value| is an arena-capable - // message, or NULL otherwise. If possible, the call resolves at compile time. - // Note that we can often devirtualize calls to `value->GetArena()` so usually - // calling this method is unnecessary. - template - PROTOBUF_ALWAYS_INLINE static Arena* GetArena(const T* value) { - return GetArenaInternal(value); - } - - template - class InternalHelper { - template - static char DestructorSkippable(const typename U::DestructorSkippable_*); - template - static double DestructorSkippable(...); - - typedef std::integral_constant< - bool, sizeof(DestructorSkippable(static_cast(0))) == - sizeof(char) || - std::is_trivially_destructible::value> - is_destructor_skippable; - - template - static char ArenaConstructable( - const typename U::InternalArenaConstructable_*); - template - static double ArenaConstructable(...); - - typedef std::integral_constant( - static_cast(0))) == - sizeof(char)> - is_arena_constructable; - - template () - .GetArena())>::value, - int>::type = 0> - static char HasGetArena(decltype(&U::GetArena)); - template - static double HasGetArena(...); - - typedef std::integral_constant(nullptr)) == - sizeof(char)> - has_get_arena; - - template - static T* Construct(void* ptr, Args&&... args) { - return new (ptr) T(std::forward(args)...); - } - - static Arena* GetArena(const T* p) { return p->GetArena(); } - - friend class Arena; - }; - - // Helper typetraits that indicates support for arenas in a type T at compile - // time. This is public only to allow construction of higher-level templated - // utilities. - // - // is_arena_constructable::value is true if the message type T has arena - // support enabled, and false otherwise. - // - // is_destructor_skippable::value is true if the message type T has told - // the arena that it is safe to skip the destructor, and false otherwise. - // - // This is inside Arena because only Arena has the friend relationships - // necessary to see the underlying generated code traits. - template - struct is_arena_constructable : InternalHelper::is_arena_constructable {}; - template - struct is_destructor_skippable : InternalHelper::is_destructor_skippable { - }; - - private: - template - struct has_get_arena : InternalHelper::has_get_arena {}; - - template - PROTOBUF_ALWAYS_INLINE static T* CreateMessageInternal(Arena* arena, - Args&&... args) { - static_assert( - InternalHelper::is_arena_constructable::value, - "CreateMessage can only construct types that are ArenaConstructable"); - if (arena == NULL) { - return new T(nullptr, std::forward(args)...); - } else { - return arena->DoCreateMessage(std::forward(args)...); - } - } - - // This specialization for no arguments is necessary, because its behavior is - // slightly different. When the arena pointer is nullptr, it calls T() - // instead of T(nullptr). - template - PROTOBUF_ALWAYS_INLINE static T* CreateMessageInternal(Arena* arena) { - static_assert( - InternalHelper::is_arena_constructable::value, - "CreateMessage can only construct types that are ArenaConstructable"); - if (arena == NULL) { - return new T(); - } else { - return arena->DoCreateMessage(); - } - } - - template - PROTOBUF_ALWAYS_INLINE static T* CreateInternal(Arena* arena, - Args&&... args) { - if (arena == NULL) { - return new T(std::forward(args)...); - } else { - return arena->DoCreate(std::is_trivially_destructible::value, - std::forward(args)...); - } - } - - inline void AllocHook(const std::type_info* allocated_type, size_t n) const { - impl_.RecordAlloc(allocated_type, n); - } - - // Allocate and also optionally call collector with the allocated type info - // when allocation recording is enabled. - template - PROTOBUF_ALWAYS_INLINE void* AllocateInternal(bool skip_explicit_ownership) { - const size_t n = internal::AlignUpTo8(sizeof(T)); - // Monitor allocation if needed. - impl_.RecordAlloc(RTTI_TYPE_ID(T), n); - if (skip_explicit_ownership) { - return AllocateAlignedTo(sizeof(T)); - } else { - if (alignof(T) <= 8) { - return impl_.AllocateAlignedAndAddCleanup( - n, &internal::arena_destruct_object); - } else { - auto ptr = - reinterpret_cast(impl_.AllocateAlignedAndAddCleanup( - sizeof(T) + alignof(T) - 8, - &internal::arena_destruct_object)); - return reinterpret_cast((ptr + alignof(T) - 8) & - (~alignof(T) + 1)); - } - } - } - - // CreateMessage requires that T supports arenas, but this private method - // works whether or not T supports arenas. These are not exposed to user code - // as it can cause confusing API usages, and end up having double free in - // user code. These are used only internally from LazyField and Repeated - // fields, since they are designed to work in all mode combinations. - template - PROTOBUF_ALWAYS_INLINE static Msg* DoCreateMaybeMessage(Arena* arena, - std::true_type, - Args&&... args) { - return CreateMessageInternal(arena, std::forward(args)...); - } - - template - PROTOBUF_ALWAYS_INLINE static T* DoCreateMaybeMessage(Arena* arena, - std::false_type, - Args&&... args) { - return CreateInternal(arena, std::forward(args)...); - } - - template - PROTOBUF_ALWAYS_INLINE static T* CreateMaybeMessage(Arena* arena, - Args&&... args) { - return DoCreateMaybeMessage(arena, is_arena_constructable(), - std::forward(args)...); - } - - template - PROTOBUF_ALWAYS_INLINE static T* CreateNoMessage(Arena* arena, std::true_type, - Args&&... args) { - // User is constructing with Create() despite the fact that T supports arena - // construction. In this case we have to delegate to CreateInternal(), and - // we can't use any CreateMaybeMessage() specialization that may be defined. - return CreateInternal(arena, std::forward(args)...); - } - - template - PROTOBUF_ALWAYS_INLINE static T* CreateNoMessage(Arena* arena, - std::false_type, - Args&&... args) { - // User is constructing with Create() and the type does not support arena - // construction. In this case we can delegate to CreateMaybeMessage() and - // use any specialization that may be available for that. - return CreateMaybeMessage(arena, std::forward(args)...); - } - - // Just allocate the required size for the given type assuming the - // type has a trivial constructor. - template - PROTOBUF_ALWAYS_INLINE T* CreateInternalRawArray(size_t num_elements) { - GOOGLE_CHECK_LE(num_elements, std::numeric_limits::max() / sizeof(T)) - << "Requested size is too large to fit into size_t."; - // We count on compiler to realize that if sizeof(T) is a multiple of - // 8 AlignUpTo can be elided. - const size_t n = internal::AlignUpTo8(sizeof(T) * num_elements); - // Monitor allocation if needed. - impl_.RecordAlloc(RTTI_TYPE_ID(T), n); - return static_cast(AllocateAlignedTo(n)); - } - - template - PROTOBUF_ALWAYS_INLINE T* DoCreate(bool skip_explicit_ownership, - Args&&... args) { - return new (AllocateInternal(skip_explicit_ownership)) - T(std::forward(args)...); - } - template - PROTOBUF_ALWAYS_INLINE T* DoCreateMessage(Args&&... args) { - return InternalHelper::Construct( - AllocateInternal(InternalHelper::is_destructor_skippable::value), - this, std::forward(args)...); - } - - // CreateInArenaStorage is used to implement map field. Without it, - // Map need to call generated message's protected arena constructor, - // which needs to declare Map as friend of generated message. - template - static void CreateInArenaStorage(T* ptr, Arena* arena, Args&&... args) { - CreateInArenaStorageInternal(ptr, arena, - typename is_arena_constructable::type(), - std::forward(args)...); - RegisterDestructorInternal( - ptr, arena, - typename InternalHelper::is_destructor_skippable::type()); - } - - template - static void CreateInArenaStorageInternal(T* ptr, Arena* arena, - std::true_type, Args&&... args) { - InternalHelper::Construct(ptr, arena, std::forward(args)...); - } - template - static void CreateInArenaStorageInternal(T* ptr, Arena* /* arena */, - std::false_type, Args&&... args) { - new (ptr) T(std::forward(args)...); - } - - template - static void RegisterDestructorInternal(T* /* ptr */, Arena* /* arena */, - std::true_type) {} - template - static void RegisterDestructorInternal(T* ptr, Arena* arena, - std::false_type) { - arena->OwnDestructor(ptr); - } - - // These implement Own(), which registers an object for deletion (destructor - // call and operator delete()). The second parameter has type 'true_type' if T - // is a subtype of Message and 'false_type' otherwise. Collapsing - // all template instantiations to one for generic Message reduces code size, - // using the virtual destructor instead. - template - PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::true_type) { - if (object != NULL) { - impl_.AddCleanup(object, &internal::arena_delete_object); - } - } - template - PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::false_type) { - if (object != NULL) { - impl_.AddCleanup(object, &internal::arena_delete_object); - } - } - - // Implementation for GetArena(). Only message objects with - // InternalArenaConstructable_ tags can be associated with an arena, and such - // objects must implement a GetArena() method. - template ::value, int>::type = 0> - PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) { - return InternalHelper::GetArena(value); - } - template ::value && - has_get_arena::value, - int>::type = 0> - PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) { - return value->GetArena(); - } - template ::value && - !has_get_arena::value, - int>::type = 0> - PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) { - (void)value; - return nullptr; - } - - // For friends of arena. - void* AllocateAligned(size_t n) { - return AllocateAlignedNoHook(internal::AlignUpTo8(n)); - } - template - void* AllocateAlignedTo(size_t n) { - static_assert(Align > 0, "Alignment must be greater than 0"); - static_assert((Align & (Align - 1)) == 0, "Alignment must be power of two"); - if (Align <= 8) return AllocateAligned(n); - // TODO(b/151247138): if the pointer would have been aligned already, - // this is wasting space. We should pass the alignment down. - uintptr_t ptr = reinterpret_cast(AllocateAligned(n + Align - 8)); - ptr = (ptr + Align - 1) & (~Align + 1); - return reinterpret_cast(ptr); - } - - void* AllocateAlignedNoHook(size_t n); - - internal::ArenaImpl impl_; - - template - friend class internal::GenericTypeHandler; - friend struct internal::ArenaStringPtr; // For AllocateAligned. - friend class internal::LazyField; // For CreateMaybeMessage. - friend class internal::EpsCopyInputStream; // For parser performance - friend class MessageLite; - template - friend class Map; -}; - -// Defined above for supporting environments without RTTI. -#undef RTTI_TYPE_ID - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_ARENA_H__ diff --git a/third_party/protobuf-lite/google/protobuf/arena_impl.h b/third_party/protobuf-lite/google/protobuf/arena_impl.h deleted file mode 100644 index 137726862..000000000 --- a/third_party/protobuf-lite/google/protobuf/arena_impl.h +++ /dev/null @@ -1,491 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// This file defines an Arena allocator for better allocation performance. - -#ifndef GOOGLE_PROTOBUF_ARENA_IMPL_H__ -#define GOOGLE_PROTOBUF_ARENA_IMPL_H__ - -#include -#include - -#include -#include - -#ifdef ADDRESS_SANITIZER -#include -#endif // ADDRESS_SANITIZER - -#include - - -namespace google { -namespace protobuf { - -struct ArenaOptions; - -namespace internal { - -inline size_t AlignUpTo8(size_t n) { - // Align n to next multiple of 8 (from Hacker's Delight, Chapter 3.) - return (n + 7) & static_cast(-8); -} - -using LifecycleIdAtomic = uint64_t; - -void PROTOBUF_EXPORT ArenaFree(void* object, size_t size); - -// MetricsCollector collects stats for a particular arena. -class PROTOBUF_EXPORT ArenaMetricsCollector { - public: - virtual ~ArenaMetricsCollector(); - - // Invoked when the arena is about to be destroyed. This method will - // typically finalize any metric collection and delete the collector. - // space_allocated is the space used by the arena. - virtual void OnDestroy(uint64 space_allocated) = 0; - - // OnReset() is called when the associated arena is reset. - // space_allocated is the space used by the arena just before the reset. - virtual void OnReset(uint64 space_allocated) = 0; - - // Does OnAlloc() need to be called? If false, metric collection overhead - // will be reduced since we will not do extra work per allocation. - virtual bool RecordAllocs() = 0; - - // OnAlloc is called when an allocation happens. - // type_info is promised to be static - its lifetime extends to - // match program's lifetime (It is given by typeid operator). - // Note: typeid(void) will be passed as allocated_type every time we - // intentionally want to avoid monitoring an allocation. (i.e. internal - // allocations for managing the arena) - virtual void OnAlloc(const std::type_info* allocated_type, - uint64 alloc_size) = 0; -}; - -class ArenaImpl; - -// A thread-unsafe Arena that can only be used within its owning thread. -class PROTOBUF_EXPORT SerialArena { - public: - // Blocks are variable length malloc-ed objects. The following structure - // describes the common header for all blocks. - class PROTOBUF_EXPORT Block { - public: - Block(size_t size, Block* next, bool special, bool user_owned) - : next_and_bits_(reinterpret_cast(next) | (special ? 1 : 0) | - (user_owned ? 2 : 0)), - pos_(kBlockHeaderSize), - size_(size) { - GOOGLE_DCHECK_EQ(reinterpret_cast(next) & 3, 0u); - } - - char* Pointer(size_t n) { - GOOGLE_DCHECK(n <= size_); - return reinterpret_cast(this) + n; - } - - // One of the blocks may be special. This is either a user-supplied - // initial block, or a block we created at startup to hold Options info. - // A special block is not deleted by Reset. - bool special() const { return (next_and_bits_ & 1) != 0; } - - // Whether or not this current block is owned by the user. - // Only special blocks can be user_owned. - bool user_owned() const { return (next_and_bits_ & 2) != 0; } - - Block* next() const { - const uintptr_t bottom_bits = 3; - return reinterpret_cast(next_and_bits_ & ~bottom_bits); - } - - void clear_next() { - next_and_bits_ &= 3; // Set next to nullptr, preserve bottom bits. - } - - size_t pos() const { return pos_; } - size_t size() const { return size_; } - void set_pos(size_t pos) { pos_ = pos; } - - private: - // Holds pointer to next block for this thread + special/user_owned bits. - uintptr_t next_and_bits_; - - size_t pos_; - size_t size_; - // data follows - }; - - // The allocate/free methods here are a little strange, since SerialArena is - // allocated inside a Block which it also manages. This is to avoid doing - // an extra allocation for the SerialArena itself. - - // Creates a new SerialArena inside Block* and returns it. - static SerialArena* New(Block* b, void* owner, ArenaImpl* arena); - - void CleanupList(); - uint64 SpaceUsed() const; - - bool HasSpace(size_t n) { return n <= static_cast(limit_ - ptr_); } - - void* AllocateAligned(size_t n) { - GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. - GOOGLE_DCHECK_GE(limit_, ptr_); - if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) { - return AllocateAlignedFallback(n); - } - void* ret = ptr_; - ptr_ += n; -#ifdef ADDRESS_SANITIZER - ASAN_UNPOISON_MEMORY_REGION(ret, n); -#endif // ADDRESS_SANITIZER - return ret; - } - - // Allocate space if the current region provides enough space. - bool MaybeAllocateAligned(size_t n, void** out) { - GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned. - GOOGLE_DCHECK_GE(limit_, ptr_); - if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) return false; - void* ret = ptr_; - ptr_ += n; -#ifdef ADDRESS_SANITIZER - ASAN_UNPOISON_MEMORY_REGION(ret, n); -#endif // ADDRESS_SANITIZER - *out = ret; - return true; - } - - void AddCleanup(void* elem, void (*cleanup)(void*)) { - if (PROTOBUF_PREDICT_FALSE(cleanup_ptr_ == cleanup_limit_)) { - AddCleanupFallback(elem, cleanup); - return; - } - cleanup_ptr_->elem = elem; - cleanup_ptr_->cleanup = cleanup; - cleanup_ptr_++; - } - - void* AllocateAlignedAndAddCleanup(size_t n, void (*cleanup)(void*)) { - void* ret = AllocateAligned(n); - AddCleanup(ret, cleanup); - return ret; - } - - Block* head() const { return head_; } - void* owner() const { return owner_; } - SerialArena* next() const { return next_; } - void set_next(SerialArena* next) { next_ = next; } - static Block* NewBlock(Block* last_block, size_t min_bytes, ArenaImpl* arena); - - private: - // Node contains the ptr of the object to be cleaned up and the associated - // cleanup function ptr. - struct CleanupNode { - void* elem; // Pointer to the object to be cleaned up. - void (*cleanup)(void*); // Function pointer to the destructor or deleter. - }; - - // Cleanup uses a chunked linked list, to reduce pointer chasing. - struct CleanupChunk { - static size_t SizeOf(size_t i) { - return sizeof(CleanupChunk) + (sizeof(CleanupNode) * (i - 1)); - } - size_t size; // Total elements in the list. - CleanupChunk* next; // Next node in the list. - CleanupNode nodes[1]; // True length is |size|. - }; - - ArenaImpl* arena_; // Containing arena. - void* owner_; // &ThreadCache of this thread; - Block* head_; // Head of linked list of blocks. - CleanupChunk* cleanup_; // Head of cleanup list. - SerialArena* next_; // Next SerialArena in this linked list. - - // Next pointer to allocate from. Always 8-byte aligned. Points inside - // head_ (and head_->pos will always be non-canonical). We keep these - // here to reduce indirection. - char* ptr_; - char* limit_; - - // Next CleanupList members to append to. These point inside cleanup_. - CleanupNode* cleanup_ptr_; - CleanupNode* cleanup_limit_; - - void* AllocateAlignedFallback(size_t n); - void AddCleanupFallback(void* elem, void (*cleanup)(void*)); - void CleanupListFallback(); - - public: - static constexpr size_t kBlockHeaderSize = - (sizeof(Block) + 7) & static_cast(-8); -}; - -// This class provides the core Arena memory allocation library. Different -// implementations only need to implement the public interface below. -// Arena is not a template type as that would only be useful if all protos -// in turn would be templates, which will/cannot happen. However separating -// the memory allocation part from the cruft of the API users expect we can -// use #ifdef the select the best implementation based on hardware / OS. -class PROTOBUF_EXPORT ArenaImpl { - public: - static const size_t kDefaultStartBlockSize = 256; - static const size_t kDefaultMaxBlockSize = 8192; - - ArenaImpl() { Init(false); } - - ArenaImpl(char* mem, size_t size) { - GOOGLE_DCHECK_EQ(reinterpret_cast(mem) & 7, 0u); - Init(false); - - // Ignore initial block if it is too small. - if (mem != nullptr && size >= kBlockHeaderSize + kSerialArenaSize) { - SetInitialBlock(new (mem) SerialArena::Block(size, nullptr, true, true)); - } - } - - explicit ArenaImpl(const ArenaOptions& options); - - // Destructor deletes all owned heap allocated objects, and destructs objects - // that have non-trivial destructors, except for proto2 message objects whose - // destructors can be skipped. Also, frees all blocks except the initial block - // if it was passed in. - ~ArenaImpl(); - - uint64 Reset(); - - uint64 SpaceAllocated() const; - uint64 SpaceUsed() const; - - void* AllocateAligned(size_t n) { - SerialArena* arena; - if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) { - return arena->AllocateAligned(n); - } else { - return AllocateAlignedFallback(n); - } - } - - // This function allocates n bytes if the common happy case is true and - // returns true. Otherwise does nothing and returns false. This strange - // semantics is necessary to allow callers to program functions that only - // have fallback function calls in tail position. This substantially improves - // code for the happy path. - PROTOBUF_ALWAYS_INLINE bool MaybeAllocateAligned(size_t n, void** out) { - SerialArena* a; - if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFromThreadCache(&a))) { - return a->MaybeAllocateAligned(n, out); - } - return false; - } - - void* AllocateAlignedAndAddCleanup(size_t n, void (*cleanup)(void*)); - - // Add object pointer and cleanup function pointer to the list. - void AddCleanup(void* elem, void (*cleanup)(void*)); - - inline void RecordAlloc(const std::type_info* allocated_type, - size_t n) const { - if (PROTOBUF_PREDICT_FALSE(record_allocs())) { - options_->metrics_collector->OnAlloc(allocated_type, n); - } - } - - std::pair NewBuffer(size_t last_size, size_t min_bytes); - - private: - // Pointer to a linked list of SerialArena. - std::atomic threads_; - std::atomic hint_; // Fast thread-local block access - std::atomic space_allocated_; // Total size of all allocated blocks. - - // Unique for each arena. Changes on Reset(). - // Least-significant-bit is 1 iff allocations should be recorded. - uint64 lifecycle_id_; - - struct Options { - size_t start_block_size; - size_t max_block_size; - void* (*block_alloc)(size_t); - void (*block_dealloc)(void*, size_t); - ArenaMetricsCollector* metrics_collector; - }; - - Options* options_ = nullptr; - - void* AllocateAlignedFallback(size_t n); - void* AllocateAlignedAndAddCleanupFallback(size_t n, void (*cleanup)(void*)); - void AddCleanupFallback(void* elem, void (*cleanup)(void*)); - - void Init(bool record_allocs); - void SetInitialBlock( - SerialArena::Block* block); // Can be called right after Init() - - // Return true iff allocations should be recorded in a metrics collector. - inline bool record_allocs() const { return lifecycle_id_ & 1; } - - // Invoke fn(b) for every Block* b. - template - void PerBlock(Functor fn) { - // By omitting an Acquire barrier we ensure that any user code that doesn't - // properly synchronize Reset() or the destructor will throw a TSAN warning. - SerialArena* serial = threads_.load(std::memory_order_relaxed); - while (serial) { - // fn() may delete blocks and arenas, so fetch next pointers before fn(); - SerialArena* cur = serial; - serial = serial->next(); - for (auto* block = cur->head(); block != nullptr;) { - auto* b = block; - block = b->next(); - fn(b); - } - } - } - - // Delete or Destruct all objects owned by the arena. - void CleanupList(); - - inline void CacheSerialArena(SerialArena* serial) { - thread_cache().last_serial_arena = serial; - thread_cache().last_lifecycle_id_seen = lifecycle_id_; - // TODO(haberman): evaluate whether we would gain efficiency by getting rid - // of hint_. It's the only write we do to ArenaImpl in the allocation path, - // which will dirty the cache line. - - hint_.store(serial, std::memory_order_release); - } - - PROTOBUF_ALWAYS_INLINE bool GetSerialArenaFast(SerialArena** arena) { - if (GetSerialArenaFromThreadCache(arena)) return true; - - // Check whether we own the last accessed SerialArena on this arena. This - // fast path optimizes the case where a single thread uses multiple arenas. - ThreadCache* tc = &thread_cache(); - SerialArena* serial = hint_.load(std::memory_order_acquire); - if (PROTOBUF_PREDICT_TRUE(serial != NULL && serial->owner() == tc)) { - *arena = serial; - return true; - } - return false; - } - - PROTOBUF_ALWAYS_INLINE bool GetSerialArenaFromThreadCache( - SerialArena** arena) { - // If this thread already owns a block in this arena then try to use that. - // This fast path optimizes the case where multiple threads allocate from - // the same arena. - ThreadCache* tc = &thread_cache(); - if (PROTOBUF_PREDICT_TRUE(tc->last_lifecycle_id_seen == lifecycle_id_)) { - *arena = tc->last_serial_arena; - return true; - } - return false; - } - SerialArena* GetSerialArenaFallback(void* me); - -#ifdef _MSC_VER -#pragma warning(disable : 4324) -#endif - struct alignas(64) ThreadCache { -#if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) - // If we are using the ThreadLocalStorage class to store the ThreadCache, - // then the ThreadCache's default constructor has to be responsible for - // initializing it. - ThreadCache() - : next_lifecycle_id(0), - last_lifecycle_id_seen(-1), - last_serial_arena(NULL) {} -#endif - - // Number of per-thread lifecycle IDs to reserve. Must be power of two. - // To reduce contention on a global atomic, each thread reserves a batch of - // IDs. The following number is calculated based on a stress test with - // ~6500 threads all frequently allocating a new arena. - static constexpr size_t kPerThreadIds = 256; - // Next lifecycle ID available to this thread. We need to reserve a new - // batch, if `next_lifecycle_id & (kPerThreadIds - 1) == 0`. - uint64 next_lifecycle_id; - // The ThreadCache is considered valid as long as this matches the - // lifecycle_id of the arena being used. - uint64 last_lifecycle_id_seen; - SerialArena* last_serial_arena; - }; - - // Lifecycle_id can be highly contended variable in a situation of lots of - // arena creation. Make sure that other global variables are not sharing the - // cacheline. -#ifdef _MSC_VER -#pragma warning(disable : 4324) -#endif - struct alignas(64) CacheAlignedLifecycleIdGenerator { - std::atomic id; - }; - static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_; -#if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) - // Android ndk does not support __thread keyword so we use a custom thread - // local storage class we implemented. - // iOS also does not support the __thread keyword. - static ThreadCache& thread_cache(); -#elif defined(PROTOBUF_USE_DLLS) - // Thread local variables cannot be exposed through DLL interface but we can - // wrap them in static functions. - static ThreadCache& thread_cache(); -#else - static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_; - static ThreadCache& thread_cache() { return thread_cache_; } -#endif - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArenaImpl); - // All protos have pointers back to the arena hence Arena must have - // pointer stability. - ArenaImpl(ArenaImpl&&) = delete; - ArenaImpl& operator=(ArenaImpl&&) = delete; - - public: - // kBlockHeaderSize is sizeof(Block), aligned up to the nearest multiple of 8 - // to protect the invariant that pos is always at a multiple of 8. - static constexpr size_t kBlockHeaderSize = SerialArena::kBlockHeaderSize; - static constexpr size_t kSerialArenaSize = - (sizeof(SerialArena) + 7) & static_cast(-8); - static constexpr size_t kOptionsSize = - (sizeof(Options) + 7) & static_cast(-8); - static_assert(kBlockHeaderSize % 8 == 0, - "kBlockHeaderSize must be a multiple of 8."); - static_assert(kSerialArenaSize % 8 == 0, - "kSerialArenaSize must be a multiple of 8."); -}; - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_ARENA_IMPL_H__ diff --git a/third_party/protobuf-lite/google/protobuf/arenastring.h b/third_party/protobuf-lite/google/protobuf/arenastring.h deleted file mode 100644 index 60307b276..000000000 --- a/third_party/protobuf-lite/google/protobuf/arenastring.h +++ /dev/null @@ -1,375 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_ARENASTRING_H__ -#define GOOGLE_PROTOBUF_ARENASTRING_H__ - -#include -#include -#include - -#include -#include -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - - -namespace google { -namespace protobuf { -namespace internal { - -// Lazy string instance to support string fields with non-empty default. -// These are initialized on the first call to .get(). -class PROTOBUF_EXPORT LazyString { - public: - // We explicitly make LazyString an aggregate so that MSVC can do constant - // initialization on it without marking it `constexpr`. - // We do not want to use `constexpr` because it makes it harder to have extern - // storage for it and causes library bloat. - struct InitValue { - const char* ptr; - size_t size; - }; - // We keep a union of the initialization value and the std::string to save on - // space. We don't need the string array after Init() is done. - union { - mutable InitValue init_value_; - alignas(std::string) mutable char string_buf_[sizeof(std::string)]; - }; - mutable std::atomic inited_; - - const std::string& get() const { - // This check generates less code than a call-once invocation. - auto* res = inited_.load(std::memory_order_acquire); - if (PROTOBUF_PREDICT_FALSE(res == nullptr)) return Init(); - return *res; - } - - private: - // Initialize the string in `string_buf_`, update `inited_` and return it. - // We return it here to avoid having to read it again in the inlined code. - const std::string& Init() const; -}; - -template -class TaggedPtr { - public: - TaggedPtr() = default; - explicit constexpr TaggedPtr(const std::string* ptr) - : ptr_(const_cast(ptr)) {} - - void SetTagged(T* p) { - Set(p); - ptr_ = reinterpret_cast(as_int() | 1); - } - void Set(T* p) { ptr_ = p; } - T* Get() const { return reinterpret_cast(as_int() & -2); } - bool IsTagged() const { return as_int() & 1; } - - // Returned value is only safe to dereference if IsTagged() == false. - // It is safe to compare. - T* UnsafeGet() const { return static_cast(ptr_); } - - bool IsNull() { return ptr_ == nullptr; } - - private: - uintptr_t as_int() const { return reinterpret_cast(ptr_); } - void* ptr_; -}; - -static_assert(std::is_trivial>::value, - "TaggedPtr must be trivial"); - -// This class encapsulates a pointer to a std::string with or without a donated -// buffer, tagged by bottom bit. It is a high-level wrapper that almost directly -// corresponds to the interface required by string fields in generated -// code. It replaces the old std::string* pointer in such cases. -// -// The object has different but similar code paths for when the default value is -// the empty string and when it is a non-empty string. -// The empty string is handled different throughout the library and there is a -// single global instance of it we can share. -// -// For fields with an empty string default value, there are three distinct -// states: -// -// - Pointer set to 'String' tag (LSB is 0), equal to -// &GetEmptyStringAlreadyInited(): field is set to its default value. Points -// to a true std::string*, but we do not own that std::string* (it's a -// globally shared instance). -// -// - Pointer set to 'String' tag (LSB is 0), but not equal to the global empty -// string: field points to a true std::string* instance that we own. This -// instance is either on the heap or on the arena (i.e. registered on -// free()/destructor-call list) as appropriate. -// -// - Pointer set to 'DonatedString' tag (LSB is 1): points to a std::string -// instance with a buffer on the arena (arena != NULL, always, in this case). -// -// For fields with a non-empty string default value, there are three distinct -// states: -// -// - Pointer set to 'String' tag (LSB is 0), equal to `nullptr`: -// Field is in "default" mode and does not point to any actual instance. -// Methods that might need to create an instance of the object will pass a -// `const LazyString&` for it. -// -// - Pointer set to 'String' tag (LSB is 0), but not equal to `nullptr`: -// field points to a true std::string* instance that we own. This instance is -// either on the heap or on the arena (i.e. registered on -// free()/destructor-call list) as appropriate. -// -// - Pointer set to 'DonatedString' tag (LSB is 1): points to a std::string -// instance with a buffer on the arena (arena != NULL, always, in this case). -// -// Generated code and reflection code both ensure that ptr_ is never null for -// fields with an empty default. -// Because ArenaStringPtr is used in oneof unions, its constructor is a NOP and -// so the field is always manually initialized via method calls. -// -// Side-note: why pass information about the default on every API call? Because -// we don't want to hold it in a member variable, or else this would go into -// every proto message instance. This would be a huge waste of space, since the -// default instance pointer is typically a global (static class field). We want -// the generated code to be as efficient as possible, and if we take -// the default value information as a parameter that's in practice taken from a -// static class field, and compare ptr_ to the default value, we end up with a -// single "cmp %reg, GLOBAL" in the resulting machine code. (Note that this also -// requires the String tag to be 0 so we can avoid the mask before comparing.) -struct PROTOBUF_EXPORT ArenaStringPtr { - ArenaStringPtr() = default; - explicit constexpr ArenaStringPtr(const std::string* default_value) - : tagged_ptr_(default_value) {} - - // Some methods below are overloaded on a `default_value` and on tags. - // The tagged overloads help reduce code size in the callers in generated - // code, while the `default_value` overloads are useful from reflection. - // By-value empty struct arguments are elided in the ABI. - struct EmptyDefault {}; - struct NonEmptyDefault {}; - - void Set(const std::string* default_value, ConstStringParam value, - ::google::protobuf::Arena* arena); - void Set(const std::string* default_value, std::string&& value, - ::google::protobuf::Arena* arena); - void Set(EmptyDefault, ConstStringParam value, ::google::protobuf::Arena* arena); - void Set(EmptyDefault, std::string&& value, ::google::protobuf::Arena* arena); - void Set(NonEmptyDefault, ConstStringParam value, ::google::protobuf::Arena* arena); - void Set(NonEmptyDefault, std::string&& value, ::google::protobuf::Arena* arena); - - // Basic accessors. - const std::string& Get() const PROTOBUF_ALWAYS_INLINE { - // Unconditionally mask away the tag. - return *tagged_ptr_.Get(); - } - const std::string* GetPointer() const PROTOBUF_ALWAYS_INLINE { - // Unconditionally mask away the tag. - return tagged_ptr_.Get(); - } - - // For fields with an empty default value. - std::string* Mutable(EmptyDefault, ::google::protobuf::Arena* arena); - // For fields with a non-empty default value. - std::string* Mutable(const LazyString& default_value, ::google::protobuf::Arena* arena); - - // Release returns a std::string* instance that is heap-allocated and is not - // Own()'d by any arena. If the field is not set, this returns NULL. The - // caller retains ownership. Clears this field back to NULL state. Used to - // implement release_() methods on generated classes. - std::string* Release(const std::string* default_value, - ::google::protobuf::Arena* arena); - std::string* ReleaseNonDefault(const std::string* default_value, - ::google::protobuf::Arena* arena); - - // Takes a std::string that is heap-allocated, and takes ownership. The - // std::string's destructor is registered with the arena. Used to implement - // set_allocated_ in generated classes. - void SetAllocated(const std::string* default_value, std::string* value, - ::google::protobuf::Arena* arena); - - // Swaps internal pointers. Arena-safety semantics: this is guarded by the - // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is - // 'unsafe' if called directly. - inline void Swap(ArenaStringPtr* other, const std::string* default_value, - Arena* arena) PROTOBUF_ALWAYS_INLINE; - - // Frees storage (if not on an arena). - void Destroy(const std::string* default_value, ::google::protobuf::Arena* arena); - void Destroy(EmptyDefault, ::google::protobuf::Arena* arena); - void Destroy(NonEmptyDefault, ::google::protobuf::Arena* arena); - - // Clears content, but keeps allocated std::string, to avoid the overhead of - // heap operations. After this returns, the content (as seen by the user) will - // always be the empty std::string. Assumes that |default_value| is an empty - // std::string. - void ClearToEmpty(); - - // Clears content, assuming that the current value is not the empty - // string default. - void ClearNonDefaultToEmpty(); - - // Clears content, but keeps allocated std::string if arena != NULL, to avoid - // the overhead of heap operations. After this returns, the content (as seen - // by the user) will always be equal to |default_value|. - void ClearToDefault(const LazyString& default_value, ::google::protobuf::Arena* arena); - - // Called from generated code / reflection runtime only. Resets value to point - // to a default string pointer, with the semantics that this - // ArenaStringPtr does not own the pointed-to memory. Disregards initial value - // of ptr_ (so this is the *ONLY* safe method to call after construction or - // when reinitializing after becoming the active field in a oneof union). - inline void UnsafeSetDefault(const std::string* default_value); - - // Returns a mutable pointer, but doesn't initialize the string to the - // default value. - std::string* MutableNoArenaNoDefault(const std::string* default_value); - - // Get a mutable pointer with unspecified contents. - // Similar to `MutableNoArenaNoDefault`, but also handles the arena case. - // If the value was donated, the contents are discarded. - std::string* MutableNoCopy(const std::string* default_value, - ::google::protobuf::Arena* arena); - - // Destroy the string. Assumes `arena == nullptr`. - void DestroyNoArena(const std::string* default_value); - - // Internal setter used only at parse time to directly set a donated string - // value. - void UnsafeSetTaggedPointer(TaggedPtr value) { - tagged_ptr_ = value; - } - // Generated code only! An optimization, in certain cases the generated - // code is certain we can obtain a std::string with no default checks and - // tag tests. - std::string* UnsafeMutablePointer() PROTOBUF_RETURNS_NONNULL; - - inline bool IsDefault(const std::string* default_value) const { - // Relies on the fact that kPtrTagString == 0, so if IsString(), ptr_ is the - // actual std::string pointer (and if !IsString(), ptr_ will never be equal - // to any aligned |default_value| pointer). The key is that we want to avoid - // masking in the fastpath const-pointer Get() case for non-arena code. - return tagged_ptr_.UnsafeGet() == default_value; - } - - private: - TaggedPtr tagged_ptr_; - - bool IsDonatedString() const { return false; } - - // Slow paths. - - // MutableSlow requires that !IsString() || IsDefault - // Variadic to support 0 args for EmptyDefault and 1 arg for LazyString. - template - std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default); - -}; - -inline void ArenaStringPtr::UnsafeSetDefault(const std::string* value) { - tagged_ptr_.Set(const_cast(value)); -} - -inline void ArenaStringPtr::Swap(ArenaStringPtr* other, - const std::string* default_value, - Arena* arena) { -#ifndef NDEBUG - // For debug builds, we swap the contents of the string, rather than the - // std::string instances themselves. This invalidates previously taken const - // references that are (per our documentation) invalidated by calling Swap() - // on the message. - // - // If both strings are the default_value, swapping is uninteresting. - // Otherwise, we use ArenaStringPtr::Mutable() to access the std::string, to - // ensure that we do not try to mutate default_value itself. - if (IsDefault(default_value) && other->IsDefault(default_value)) { - return; - } - - if (default_value == nullptr) { - // If we have non-empty default, then `default_value` is null and we can't - // call Mutable the same way. Just do the regular swap. - std::swap(tagged_ptr_, other->tagged_ptr_); - } else { - std::string* this_ptr = Mutable(EmptyDefault{}, arena); - std::string* other_ptr = other->Mutable(EmptyDefault{}, arena); - - this_ptr->swap(*other_ptr); - } -#else - std::swap(tagged_ptr_, other->tagged_ptr_); -#endif -} - -inline void ArenaStringPtr::ClearNonDefaultToEmpty() { - // Unconditionally mask away the tag. - tagged_ptr_.Get()->clear(); -} - -inline std::string* ArenaStringPtr::MutableNoArenaNoDefault( - const std::string* default_value) { - // VERY IMPORTANT for performance and code size: this will reduce to a member - // variable load, a pointer check (against |default_value|, in practice a - // static global) and a branch to the slowpath (which calls operator new and - // the ctor). DO NOT add any tagged-pointer operations here. - if (IsDefault(default_value)) { - std::string* new_string = new std::string(); - tagged_ptr_.Set(new_string); - return new_string; - } else { - return UnsafeMutablePointer(); - } -} - -inline void ArenaStringPtr::DestroyNoArena(const std::string* default_value) { - if (!IsDefault(default_value)) { - delete UnsafeMutablePointer(); - } -} - -inline std::string* ArenaStringPtr::UnsafeMutablePointer() { - GOOGLE_DCHECK(!tagged_ptr_.IsTagged()); - GOOGLE_DCHECK(tagged_ptr_.UnsafeGet() != nullptr); - return tagged_ptr_.UnsafeGet(); -} - - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_ARENASTRING_H__ diff --git a/third_party/protobuf-lite/google/protobuf/descriptor.h b/third_party/protobuf-lite/google/protobuf/descriptor.h deleted file mode 100644 index 5bfecf508..000000000 --- a/third_party/protobuf-lite/google/protobuf/descriptor.h +++ /dev/null @@ -1,2324 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This file contains classes which describe a type of protocol message. -// You can use a message's descriptor to learn at runtime what fields -// it contains and what the types of those fields are. The Message -// interface also allows you to dynamically access and modify individual -// fields by passing the FieldDescriptor of the field you are interested -// in. -// -// Most users will not care about descriptors, because they will write -// code specific to certain protocol types and will simply use the classes -// generated by the protocol compiler directly. Advanced users who want -// to operate on arbitrary types (not known at compile time) may want to -// read descriptors in order to learn about the contents of a message. -// A very small number of users will want to construct their own -// Descriptors, either because they are implementing Message manually or -// because they are writing something like the protocol compiler. -// -// For an example of how you might use descriptors, see the code example -// at the top of message.h. - -#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__ -#define GOOGLE_PROTOBUF_DESCRIPTOR_H__ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -// TYPE_BOOL is defined in the MacOS's ConditionalMacros.h. -#ifdef TYPE_BOOL -#undef TYPE_BOOL -#endif // TYPE_BOOL - -#ifdef SWIG -#define PROTOBUF_EXPORT -#endif - - -namespace google { -namespace protobuf { - -// Defined in this file. -class Descriptor; -class FieldDescriptor; -class OneofDescriptor; -class EnumDescriptor; -class EnumValueDescriptor; -class ServiceDescriptor; -class MethodDescriptor; -class FileDescriptor; -class DescriptorDatabase; -class DescriptorPool; - -// Defined in descriptor.proto -class DescriptorProto; -class DescriptorProto_ExtensionRange; -class FieldDescriptorProto; -class OneofDescriptorProto; -class EnumDescriptorProto; -class EnumValueDescriptorProto; -class ServiceDescriptorProto; -class MethodDescriptorProto; -class FileDescriptorProto; -class MessageOptions; -class FieldOptions; -class OneofOptions; -class EnumOptions; -class EnumValueOptions; -class ExtensionRangeOptions; -class ServiceOptions; -class MethodOptions; -class FileOptions; -class UninterpretedOption; -class SourceCodeInfo; - -// Defined in message.h -class Message; -class Reflection; - -// Defined in descriptor.cc -class DescriptorBuilder; -class FileDescriptorTables; -struct Symbol; - -// Defined in unknown_field_set.h. -class UnknownField; - -// Defined in command_line_interface.cc -namespace compiler { -class CommandLineInterface; -namespace cpp { -// Defined in helpers.h -class Formatter; -} // namespace cpp -} // namespace compiler - -namespace descriptor_unittest { -class DescriptorTest; -} // namespace descriptor_unittest - -// Defined in printer.h -namespace io { -class Printer; -} // namespace io - -// NB, all indices are zero-based. -struct SourceLocation { - int start_line; - int end_line; - int start_column; - int end_column; - - // Doc comments found at the source location. - // See the comments in SourceCodeInfo.Location (descriptor.proto) for details. - std::string leading_comments; - std::string trailing_comments; - std::vector leading_detached_comments; -}; - -// Options when generating machine-parsable output from a descriptor with -// DebugString(). -struct DebugStringOptions { - // include original user comments as recorded in SourceLocation entries. N.B. - // that this must be |false| by default: several other pieces of code (for - // example, the C++ code generation for fields in the proto compiler) rely on - // DebugString() output being unobstructed by user comments. - bool include_comments; - // If true, elide the braced body in the debug string. - bool elide_group_body; - bool elide_oneof_body; - - DebugStringOptions() - : include_comments(false), - elide_group_body(false), - elide_oneof_body(false) { - } -}; - -// A class to handle the simplest cases of a lazily linked descriptor -// for a message type that isn't built at the time of cross linking, -// which is needed when a pool has lazily_build_dependencies_ set. -// Must be instantiated as mutable in a descriptor. -namespace internal { -class PROTOBUF_EXPORT LazyDescriptor { - public: - // Init function to be called at init time of a descriptor containing - // a LazyDescriptor. - void Init() { - descriptor_ = nullptr; - name_ = nullptr; - once_ = nullptr; - file_ = nullptr; - } - - // Sets the value of the descriptor if it is known during the descriptor - // building process. Not thread safe, should only be called during the - // descriptor build process. Should not be called after SetLazy has been - // called. - void Set(const Descriptor* descriptor); - - // Sets the information needed to lazily cross link the descriptor at a later - // time, SetLazy is not thread safe, should be called only once at descriptor - // build time if the symbol wasn't found and building of the file containing - // that type is delayed because lazily_build_dependencies_ is set on the pool. - // Should not be called after Set() has been called. - void SetLazy(StringPiece name, const FileDescriptor* file); - - // Returns the current value of the descriptor, thread-safe. If SetLazy(...) - // has been called, will do a one-time cross link of the type specified, - // building the descriptor file that contains the type if necessary. - inline const Descriptor* Get() { - Once(); - return descriptor_; - } - - private: - static void OnceStatic(LazyDescriptor* lazy); - void OnceInternal(); - void Once(); - - const Descriptor* descriptor_; - const std::string* name_; - internal::once_flag* once_; - const FileDescriptor* file_; -}; -} // namespace internal - -// Describes a type of protocol message, or a particular group within a -// message. To obtain the Descriptor for a given message object, call -// Message::GetDescriptor(). Generated message classes also have a -// static method called descriptor() which returns the type's descriptor. -// Use DescriptorPool to construct your own descriptors. -class PROTOBUF_EXPORT Descriptor { - public: - typedef DescriptorProto Proto; - - // The name of the message type, not including its scope. - const std::string& name() const; - - // The fully-qualified name of the message type, scope delimited by - // periods. For example, message type "Foo" which is declared in package - // "bar" has full name "bar.Foo". If a type "Baz" is nested within - // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that - // comes after the last '.', use name(). - const std::string& full_name() const; - - // Index of this descriptor within the file or containing type's message - // type array. - int index() const; - - // The .proto file in which this message type was defined. Never nullptr. - const FileDescriptor* file() const; - - // If this Descriptor describes a nested type, this returns the type - // in which it is nested. Otherwise, returns nullptr. - const Descriptor* containing_type() const; - - // Get options for this message type. These are specified in the .proto file - // by placing lines like "option foo = 1234;" in the message definition. - // Allowed options are defined by MessageOptions in descriptor.proto, and any - // available extensions of that message. - const MessageOptions& options() const; - - // Write the contents of this Descriptor into the given DescriptorProto. - // The target DescriptorProto must be clear before calling this; if it - // isn't, the result may be garbage. - void CopyTo(DescriptorProto* proto) const; - - // Write the contents of this descriptor in a human-readable form. Output - // will be suitable for re-parsing. - std::string DebugString() const; - - // Similar to DebugString(), but additionally takes options (e.g., - // include original user comments in output). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Returns true if this is a placeholder for an unknown type. This will - // only be the case if this descriptor comes from a DescriptorPool - // with AllowUnknownDependencies() set. - bool is_placeholder() const; - - enum WellKnownType { - WELLKNOWNTYPE_UNSPECIFIED, // Not a well-known type. - - // Wrapper types. - WELLKNOWNTYPE_DOUBLEVALUE, // google.protobuf.DoubleValue - WELLKNOWNTYPE_FLOATVALUE, // google.protobuf.FloatValue - WELLKNOWNTYPE_INT64VALUE, // google.protobuf.Int64Value - WELLKNOWNTYPE_UINT64VALUE, // google.protobuf.UInt64Value - WELLKNOWNTYPE_INT32VALUE, // google.protobuf.Int32Value - WELLKNOWNTYPE_UINT32VALUE, // google.protobuf.UInt32Value - WELLKNOWNTYPE_STRINGVALUE, // google.protobuf.StringValue - WELLKNOWNTYPE_BYTESVALUE, // google.protobuf.BytesValue - WELLKNOWNTYPE_BOOLVALUE, // google.protobuf.BoolValue - - // Other well known types. - WELLKNOWNTYPE_ANY, // google.protobuf.Any - WELLKNOWNTYPE_FIELDMASK, // google.protobuf.FieldMask - WELLKNOWNTYPE_DURATION, // google.protobuf.Duration - WELLKNOWNTYPE_TIMESTAMP, // google.protobuf.Timestamp - WELLKNOWNTYPE_VALUE, // google.protobuf.Value - WELLKNOWNTYPE_LISTVALUE, // google.protobuf.ListValue - WELLKNOWNTYPE_STRUCT, // google.protobuf.Struct - - // New well-known types may be added in the future. - // Please make sure any switch() statements have a 'default' case. - __WELLKNOWNTYPE__DO_NOT_USE__ADD_DEFAULT_INSTEAD__, - }; - - WellKnownType well_known_type() const; - - // Field stuff ----------------------------------------------------- - - // The number of fields in this message type. - int field_count() const; - // Gets a field by index, where 0 <= index < field_count(). - // These are returned in the order they were defined in the .proto file. - const FieldDescriptor* field(int index) const; - - // Looks up a field by declared tag number. Returns nullptr if no such field - // exists. - const FieldDescriptor* FindFieldByNumber(int number) const; - // Looks up a field by name. Returns nullptr if no such field exists. - const FieldDescriptor* FindFieldByName(ConstStringParam name) const; - - // Looks up a field by lowercased name (as returned by lowercase_name()). - // This lookup may be ambiguous if multiple field names differ only by case, - // in which case the field returned is chosen arbitrarily from the matches. - const FieldDescriptor* FindFieldByLowercaseName( - ConstStringParam lowercase_name) const; - - // Looks up a field by camel-case name (as returned by camelcase_name()). - // This lookup may be ambiguous if multiple field names differ in a way that - // leads them to have identical camel-case names, in which case the field - // returned is chosen arbitrarily from the matches. - const FieldDescriptor* FindFieldByCamelcaseName( - ConstStringParam camelcase_name) const; - - // The number of oneofs in this message type. - int oneof_decl_count() const; - // The number of oneofs in this message type, excluding synthetic oneofs. - // Real oneofs always come first, so iterating up to real_oneof_decl_cout() - // will yield all real oneofs. - int real_oneof_decl_count() const; - // Get a oneof by index, where 0 <= index < oneof_decl_count(). - // These are returned in the order they were defined in the .proto file. - const OneofDescriptor* oneof_decl(int index) const; - - // Looks up a oneof by name. Returns nullptr if no such oneof exists. - const OneofDescriptor* FindOneofByName(ConstStringParam name) const; - - // Nested type stuff ----------------------------------------------- - - // The number of nested types in this message type. - int nested_type_count() const; - // Gets a nested type by index, where 0 <= index < nested_type_count(). - // These are returned in the order they were defined in the .proto file. - const Descriptor* nested_type(int index) const; - - // Looks up a nested type by name. Returns nullptr if no such nested type - // exists. - const Descriptor* FindNestedTypeByName(ConstStringParam name) const; - - // Enum stuff ------------------------------------------------------ - - // The number of enum types in this message type. - int enum_type_count() const; - // Gets an enum type by index, where 0 <= index < enum_type_count(). - // These are returned in the order they were defined in the .proto file. - const EnumDescriptor* enum_type(int index) const; - - // Looks up an enum type by name. Returns nullptr if no such enum type - // exists. - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; - - // Looks up an enum value by name, among all enum types in this message. - // Returns nullptr if no such value exists. - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; - - // Extensions ------------------------------------------------------ - - // A range of field numbers which are designated for third-party - // extensions. - struct ExtensionRange { - typedef DescriptorProto_ExtensionRange Proto; - - typedef ExtensionRangeOptions OptionsType; - - // See Descriptor::CopyTo(). - void CopyTo(DescriptorProto_ExtensionRange* proto) const; - - int start; // inclusive - int end; // exclusive - - const ExtensionRangeOptions* options_; - }; - - // The number of extension ranges in this message type. - int extension_range_count() const; - // Gets an extension range by index, where 0 <= index < - // extension_range_count(). These are returned in the order they were defined - // in the .proto file. - const ExtensionRange* extension_range(int index) const; - - // Returns true if the number is in one of the extension ranges. - bool IsExtensionNumber(int number) const; - - // Returns nullptr if no extension range contains the given number. - const ExtensionRange* FindExtensionRangeContainingNumber(int number) const; - - // The number of extensions defined nested within this message type's scope. - // See doc: - // https://developers.google.com/protocol-buffers/docs/proto#nested-extensions - // - // Note that the extensions may be extending *other* messages. - // - // For example: - // message M1 { - // extensions 1 to max; - // } - // - // message M2 { - // extend M1 { - // optional int32 foo = 1; - // } - // } - // - // In this case, - // DescriptorPool::generated_pool() - // ->FindMessageTypeByName("M2") - // ->extension(0) - // will return "foo", even though "foo" is an extension of M1. - // To find all known extensions of a given message, instead use - // DescriptorPool::FindAllExtensions. - int extension_count() const; - // Get an extension by index, where 0 <= index < extension_count(). - // These are returned in the order they were defined in the .proto file. - const FieldDescriptor* extension(int index) const; - - // Looks up a named extension (which extends some *other* message type) - // defined within this message type's scope. - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; - - // Similar to FindFieldByLowercaseName(), but finds extensions defined within - // this message type's scope. - const FieldDescriptor* FindExtensionByLowercaseName( - ConstStringParam name) const; - - // Similar to FindFieldByCamelcaseName(), but finds extensions defined within - // this message type's scope. - const FieldDescriptor* FindExtensionByCamelcaseName( - ConstStringParam name) const; - - // Reserved fields ------------------------------------------------- - - // A range of reserved field numbers. - struct ReservedRange { - int start; // inclusive - int end; // exclusive - }; - - // The number of reserved ranges in this message type. - int reserved_range_count() const; - // Gets an reserved range by index, where 0 <= index < - // reserved_range_count(). These are returned in the order they were defined - // in the .proto file. - const ReservedRange* reserved_range(int index) const; - - // Returns true if the number is in one of the reserved ranges. - bool IsReservedNumber(int number) const; - - // Returns nullptr if no reserved range contains the given number. - const ReservedRange* FindReservedRangeContainingNumber(int number) const; - - // The number of reserved field names in this message type. - int reserved_name_count() const; - - // Gets a reserved name by index, where 0 <= index < reserved_name_count(). - const std::string& reserved_name(int index) const; - - // Returns true if the field name is reserved. - bool IsReservedName(ConstStringParam name) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this message declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - // Maps -------------------------------------------------------------- - - // Returns the FieldDescriptor for the "key" field. If this isn't a map entry - // field, returns nullptr. - const FieldDescriptor* map_key() const; - - // Returns the FieldDescriptor for the "value" field. If this isn't a map - // entry field, returns nullptr. - const FieldDescriptor* map_value() const; - - private: - typedef MessageOptions OptionsType; - - // Allows tests to test CopyTo(proto, true). - friend class descriptor_unittest::DescriptorTest; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // Fill the json_name field of FieldDescriptorProto. - void CopyJsonNameTo(DescriptorProto* proto) const; - - // Internal version of DebugString; controls the level of indenting for - // correct depth. Takes |options| to control debug-string options, and - // |include_opening_clause| to indicate whether the "message ... " part of the - // clause has already been generated (this varies depending on context). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options, - bool include_opening_clause) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - const std::string* name_; - const std::string* full_name_; - const FileDescriptor* file_; - const Descriptor* containing_type_; - const MessageOptions* options_; - - // These arrays are separated from their sizes to minimize padding on 64-bit. - FieldDescriptor* fields_; - OneofDescriptor* oneof_decls_; - Descriptor* nested_types_; - EnumDescriptor* enum_types_; - ExtensionRange* extension_ranges_; - FieldDescriptor* extensions_; - ReservedRange* reserved_ranges_; - const std::string** reserved_names_; - - int field_count_; - int oneof_decl_count_; - int real_oneof_decl_count_; - int nested_type_count_; - int enum_type_count_; - int extension_range_count_; - int extension_count_; - int reserved_range_count_; - int reserved_name_count_; - - // True if this is a placeholder for an unknown type. - bool is_placeholder_; - // True if this is a placeholder and the type name wasn't fully-qualified. - bool is_unqualified_placeholder_; - // Well known type. Stored as char to conserve space. - char well_known_type_; - - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in descriptor.cc - // and update them to initialize the field. - - // Must be constructed using DescriptorPool. - Descriptor() {} - friend class DescriptorBuilder; - friend class DescriptorPool; - friend class EnumDescriptor; - friend class FieldDescriptor; - friend class OneofDescriptor; - friend class MethodDescriptor; - friend class FileDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor); -}; - - -// Describes a single field of a message. To get the descriptor for a given -// field, first get the Descriptor for the message in which it is defined, -// then call Descriptor::FindFieldByName(). To get a FieldDescriptor for -// an extension, do one of the following: -// - Get the Descriptor or FileDescriptor for its containing scope, then -// call Descriptor::FindExtensionByName() or -// FileDescriptor::FindExtensionByName(). -// - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber() or -// DescriptorPool::FindExtensionByPrintableName(). -// Use DescriptorPool to construct your own descriptors. -class PROTOBUF_EXPORT FieldDescriptor { - public: - typedef FieldDescriptorProto Proto; - - // Identifies a field type. 0 is reserved for errors. The order is weird - // for historical reasons. Types 12 and up are new in proto2. - enum Type { - TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire. - TYPE_FLOAT = 2, // float, exactly four bytes on the wire. - TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers - // take 10 bytes. Use TYPE_SINT64 if negative - // values are likely. - TYPE_UINT64 = 4, // uint64, varint on the wire. - TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers - // take 10 bytes. Use TYPE_SINT32 if negative - // values are likely. - TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire. - TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire. - TYPE_BOOL = 8, // bool, varint on the wire. - TYPE_STRING = 9, // UTF-8 text. - TYPE_GROUP = 10, // Tag-delimited message. Deprecated. - TYPE_MESSAGE = 11, // Length-delimited message. - - TYPE_BYTES = 12, // Arbitrary byte array. - TYPE_UINT32 = 13, // uint32, varint on the wire - TYPE_ENUM = 14, // Enum, varint on the wire - TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire - TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire - TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire - TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire - - MAX_TYPE = 18, // Constant useful for defining lookup tables - // indexed by Type. - }; - - // Specifies the C++ data type used to represent the field. There is a - // fixed mapping from Type to CppType where each Type maps to exactly one - // CppType. 0 is reserved for errors. - enum CppType { - CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32 - CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 - CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32 - CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 - CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE - CPPTYPE_FLOAT = 6, // TYPE_FLOAT - CPPTYPE_BOOL = 7, // TYPE_BOOL - CPPTYPE_ENUM = 8, // TYPE_ENUM - CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES - CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP - - MAX_CPPTYPE = 10, // Constant useful for defining lookup tables - // indexed by CppType. - }; - - // Identifies whether the field is optional, required, or repeated. 0 is - // reserved for errors. - enum Label { - LABEL_OPTIONAL = 1, // optional - LABEL_REQUIRED = 2, // required - LABEL_REPEATED = 3, // repeated - - MAX_LABEL = 3, // Constant useful for defining lookup tables - // indexed by Label. - }; - - // Valid field numbers are positive integers up to kMaxNumber. - static const int kMaxNumber = (1 << 29) - 1; - - // First field number reserved for the protocol buffer library implementation. - // Users may not declare fields that use reserved numbers. - static const int kFirstReservedNumber = 19000; - // Last field number reserved for the protocol buffer library implementation. - // Users may not declare fields that use reserved numbers. - static const int kLastReservedNumber = 19999; - - const std::string& name() const; // Name of this field within the message. - const std::string& full_name() const; // Fully-qualified name of the field. - const std::string& json_name() const; // JSON name of this field. - const FileDescriptor* file() const; // File in which this field was defined. - bool is_extension() const; // Is this an extension field? - int number() const; // Declared tag number. - - // Same as name() except converted to lower-case. This (and especially the - // FindFieldByLowercaseName() method) can be useful when parsing formats - // which prefer to use lowercase naming style. (Although, technically - // field names should be lowercased anyway according to the protobuf style - // guide, so this only makes a difference when dealing with old .proto files - // which do not follow the guide.) - const std::string& lowercase_name() const; - - // Same as name() except converted to camel-case. In this conversion, any - // time an underscore appears in the name, it is removed and the next - // letter is capitalized. Furthermore, the first letter of the name is - // lower-cased. Examples: - // FooBar -> fooBar - // foo_bar -> fooBar - // fooBar -> fooBar - // This (and especially the FindFieldByCamelcaseName() method) can be useful - // when parsing formats which prefer to use camel-case naming style. - const std::string& camelcase_name() const; - - Type type() const; // Declared type of this field. - const char* type_name() const; // Name of the declared type. - CppType cpp_type() const; // C++ type of this field. - const char* cpp_type_name() const; // Name of the C++ type. - Label label() const; // optional/required/repeated - - bool is_required() const; // shorthand for label() == LABEL_REQUIRED - bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL - bool is_repeated() const; // shorthand for label() == LABEL_REPEATED - bool is_packable() const; // shorthand for is_repeated() && - // IsTypePackable(type()) - bool is_packed() const; // shorthand for is_packable() && - // options().packed() - bool is_map() const; // shorthand for type() == TYPE_MESSAGE && - // message_type()->options().map_entry() - - // Returns true if this field was syntactically written with "optional" in the - // .proto file. Excludes singular proto3 fields that do not have a label. - bool has_optional_keyword() const; - - // Returns true if this field tracks presence, ie. does the field - // distinguish between "unset" and "present with default value." - // This includes required, optional, and oneof fields. It excludes maps, - // repeated fields, and singular proto3 fields without "optional". - // - // For fields where has_presence() == true, the return value of - // Reflection::HasField() is semantically meaningful. - bool has_presence() const; - - // Index of this field within the message's field array, or the file or - // extension scope's extensions array. - int index() const; - - // Does this field have an explicitly-declared default value? - bool has_default_value() const; - - // Whether the user has specified the json_name field option in the .proto - // file. - bool has_json_name() const; - - // Get the field default value if cpp_type() == CPPTYPE_INT32. If no - // explicit default was defined, the default is 0. - int32 default_value_int32() const; - // Get the field default value if cpp_type() == CPPTYPE_INT64. If no - // explicit default was defined, the default is 0. - int64 default_value_int64() const; - // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no - // explicit default was defined, the default is 0. - uint32 default_value_uint32() const; - // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no - // explicit default was defined, the default is 0. - uint64 default_value_uint64() const; - // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no - // explicit default was defined, the default is 0.0. - float default_value_float() const; - // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no - // explicit default was defined, the default is 0.0. - double default_value_double() const; - // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no - // explicit default was defined, the default is false. - bool default_value_bool() const; - // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no - // explicit default was defined, the default is the first value defined - // in the enum type (all enum types are required to have at least one value). - // This never returns nullptr. - const EnumValueDescriptor* default_value_enum() const; - // Get the field default value if cpp_type() == CPPTYPE_STRING. If no - // explicit default was defined, the default is the empty string. - const std::string& default_value_string() const; - - // The Descriptor for the message of which this is a field. For extensions, - // this is the extended type. Never nullptr. - const Descriptor* containing_type() const; - - // If the field is a member of a oneof, this is the one, otherwise this is - // nullptr. - const OneofDescriptor* containing_oneof() const; - - // If the field is a member of a non-synthetic oneof, returns the descriptor - // for the oneof, otherwise returns nullptr. - const OneofDescriptor* real_containing_oneof() const; - - // If the field is a member of a oneof, returns the index in that oneof. - int index_in_oneof() const; - - // An extension may be declared within the scope of another message. If this - // field is an extension (is_extension() is true), then extension_scope() - // returns that message, or nullptr if the extension was declared at global - // scope. If this is not an extension, extension_scope() is undefined (may - // assert-fail). - const Descriptor* extension_scope() const; - - // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the - // message or the group type. Otherwise, returns null. - const Descriptor* message_type() const; - // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise, - // returns null. - const EnumDescriptor* enum_type() const; - - // Get the FieldOptions for this field. This includes things listed in - // square brackets after the field definition. E.g., the field: - // optional string text = 1 [ctype=CORD]; - // has the "ctype" option set. Allowed options are defined by FieldOptions in - // descriptor.proto, and any available extensions of that message. - const FieldOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(FieldDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Helper method to get the CppType for a particular Type. - static CppType TypeToCppType(Type type); - - // Helper method to get the name of a Type. - static const char* TypeName(Type type); - - // Helper method to get the name of a CppType. - static const char* CppTypeName(CppType cpp_type); - - // Return true iff [packed = true] is valid for fields of this type. - static inline bool IsTypePackable(Type field_type); - - // Returns full_name() except if the field is a MessageSet extension, - // in which case it returns the full_name() of the containing message type - // for backwards compatibility with proto1. - // - // A MessageSet extension is defined as an optional message extension - // whose containing type has the message_set_wire_format option set. - // This should be true of extensions of google.protobuf.bridge.MessageSet; - // by convention, such extensions are named "message_set_extension". - // - // The opposite operation (looking up an extension's FieldDescriptor given - // its printable name) can be accomplished with - // message->file()->pool()->FindExtensionByPrintableName(message, name) - // where the extension extends "message". - const std::string& PrintableNameForExtension() const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this field declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - typedef FieldOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - friend class Reflection; - - // Fill the json_name field of FieldDescriptorProto. - void CopyJsonNameTo(FieldDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // formats the default value appropriately and returns it as a string. - // Must have a default value to call this. If quote_string_type is true, then - // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped. - std::string DefaultValueAsString(bool quote_string_type) const; - - // Helper function that returns the field type name for DebugString. - std::string FieldTypeNameDebugString() const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - // Returns true if this is a map message type. - bool is_map_message_type() const; - - const std::string* name_; - const std::string* full_name_; - const std::string* lowercase_name_; - const std::string* camelcase_name_; - // If has_json_name_ is true, it's the value specified by the user. - // Otherwise, it has the same value as camelcase_name_. - const std::string* json_name_; - const FileDescriptor* file_; - internal::once_flag* type_once_; - static void TypeOnceInit(const FieldDescriptor* to_init); - void InternalTypeOnceInit() const; - mutable Type type_; - Label label_; - bool has_default_value_; - bool proto3_optional_; - // Whether the user has specified the json_name field option in the .proto - // file. - bool has_json_name_; - bool is_extension_; - int number_; - int index_in_oneof_; - const Descriptor* containing_type_; - const OneofDescriptor* containing_oneof_; - const Descriptor* extension_scope_; - mutable const Descriptor* message_type_; - mutable const EnumDescriptor* enum_type_; - const FieldOptions* options_; - const std::string* type_name_; - const std::string* default_value_enum_name_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - union { - int32 default_value_int32_; - int64 default_value_int64_; - uint32 default_value_uint32_; - uint64 default_value_uint64_; - float default_value_float_; - double default_value_double_; - bool default_value_bool_; - - mutable const EnumValueDescriptor* default_value_enum_; - const std::string* default_value_string_; - mutable std::atomic default_generated_instance_; - }; - - static const CppType kTypeToCppTypeMap[MAX_TYPE + 1]; - - static const char* const kTypeToName[MAX_TYPE + 1]; - - static const char* const kCppTypeToName[MAX_CPPTYPE + 1]; - - static const char* const kLabelToName[MAX_LABEL + 1]; - - // Must be constructed using DescriptorPool. - FieldDescriptor() {} - friend class DescriptorBuilder; - friend class FileDescriptor; - friend class Descriptor; - friend class OneofDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor); -}; - - -// Describes a oneof defined in a message type. -class PROTOBUF_EXPORT OneofDescriptor { - public: - typedef OneofDescriptorProto Proto; - - const std::string& name() const; // Name of this oneof. - const std::string& full_name() const; // Fully-qualified name of the oneof. - - // Index of this oneof within the message's oneof array. - int index() const; - - // Returns whether this oneof was inserted by the compiler to wrap a proto3 - // optional field. If this returns true, code generators should *not* emit it. - bool is_synthetic() const; - - // The .proto file in which this oneof was defined. Never nullptr. - const FileDescriptor* file() const; - // The Descriptor for the message containing this oneof. - const Descriptor* containing_type() const; - - // The number of (non-extension) fields which are members of this oneof. - int field_count() const; - // Get a member of this oneof, in the order in which they were declared in the - // .proto file. Does not include extensions. - const FieldDescriptor* field(int index) const; - - const OneofOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(OneofDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this oneof declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - typedef OneofOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - const std::string* name_; - const std::string* full_name_; - const Descriptor* containing_type_; - int field_count_; - const FieldDescriptor** fields_; - const OneofOptions* options_; - - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() - // in descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - OneofDescriptor() {} - friend class DescriptorBuilder; - friend class Descriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofDescriptor); -}; - -// Describes an enum type defined in a .proto file. To get the EnumDescriptor -// for a generated enum type, call TypeName_descriptor(). Use DescriptorPool -// to construct your own descriptors. -class PROTOBUF_EXPORT EnumDescriptor { - public: - typedef EnumDescriptorProto Proto; - - // The name of this enum type in the containing scope. - const std::string& name() const; - - // The fully-qualified name of the enum type, scope delimited by periods. - const std::string& full_name() const; - - // Index of this enum within the file or containing message's enum array. - int index() const; - - // The .proto file in which this enum type was defined. Never nullptr. - const FileDescriptor* file() const; - - // The number of values for this EnumDescriptor. Guaranteed to be greater - // than zero. - int value_count() const; - // Gets a value by index, where 0 <= index < value_count(). - // These are returned in the order they were defined in the .proto file. - const EnumValueDescriptor* value(int index) const; - - // Looks up a value by name. Returns nullptr if no such value exists. - const EnumValueDescriptor* FindValueByName(ConstStringParam name) const; - // Looks up a value by number. Returns nullptr if no such value exists. If - // multiple values have this number, the first one defined is returned. - const EnumValueDescriptor* FindValueByNumber(int number) const; - - // If this enum type is nested in a message type, this is that message type. - // Otherwise, nullptr. - const Descriptor* containing_type() const; - - // Get options for this enum type. These are specified in the .proto file by - // placing lines like "option foo = 1234;" in the enum definition. Allowed - // options are defined by EnumOptions in descriptor.proto, and any available - // extensions of that message. - const EnumOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(EnumDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Returns true if this is a placeholder for an unknown enum. This will - // only be the case if this descriptor comes from a DescriptorPool - // with AllowUnknownDependencies() set. - bool is_placeholder() const; - - // Reserved fields ------------------------------------------------- - - // A range of reserved field numbers. - struct ReservedRange { - int start; // inclusive - int end; // inclusive - }; - - // The number of reserved ranges in this message type. - int reserved_range_count() const; - // Gets an reserved range by index, where 0 <= index < - // reserved_range_count(). These are returned in the order they were defined - // in the .proto file. - const EnumDescriptor::ReservedRange* reserved_range(int index) const; - - // Returns true if the number is in one of the reserved ranges. - bool IsReservedNumber(int number) const; - - // Returns nullptr if no reserved range contains the given number. - const EnumDescriptor::ReservedRange* FindReservedRangeContainingNumber( - int number) const; - - // The number of reserved field names in this message type. - int reserved_name_count() const; - - // Gets a reserved name by index, where 0 <= index < reserved_name_count(). - const std::string& reserved_name(int index) const; - - // Returns true if the field name is reserved. - bool IsReservedName(ConstStringParam name) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this enum declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - typedef EnumOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // Looks up a value by number. If the value does not exist, dynamically - // creates a new EnumValueDescriptor for that value, assuming that it was - // unknown. If a new descriptor is created, this is done in a thread-safe way, - // and future calls will return the same value descriptor pointer. - // - // This is private but is used by Reflection (which is friended below) to - // return a valid EnumValueDescriptor from GetEnum() when this feature is - // enabled. - const EnumValueDescriptor* FindValueByNumberCreatingIfUnknown( - int number) const; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - const std::string* name_; - const std::string* full_name_; - const FileDescriptor* file_; - const Descriptor* containing_type_; - const EnumOptions* options_; - - // True if this is a placeholder for an unknown type. - bool is_placeholder_; - // True if this is a placeholder and the type name wasn't fully-qualified. - bool is_unqualified_placeholder_; - - int value_count_; - EnumValueDescriptor* values_; - - int reserved_range_count_; - int reserved_name_count_; - EnumDescriptor::ReservedRange* reserved_ranges_; - const std::string** reserved_names_; - - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - EnumDescriptor() {} - friend class DescriptorBuilder; - friend class Descriptor; - friend class FieldDescriptor; - friend class EnumValueDescriptor; - friend class FileDescriptor; - friend class DescriptorPool; - friend class Reflection; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor); -}; - -// Describes an individual enum constant of a particular type. To get the -// EnumValueDescriptor for a given enum value, first get the EnumDescriptor -// for its type, then use EnumDescriptor::FindValueByName() or -// EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct -// your own descriptors. -class PROTOBUF_EXPORT EnumValueDescriptor { - public: - typedef EnumValueDescriptorProto Proto; - - const std::string& name() const; // Name of this enum constant. - int index() const; // Index within the enums's Descriptor. - int number() const; // Numeric value of this enum constant. - - // The full_name of an enum value is a sibling symbol of the enum type. - // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually - // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT - // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform - // with C++ scoping rules for enums. - const std::string& full_name() const; - - // The .proto file in which this value was defined. Never nullptr. - const FileDescriptor* file() const; - // The type of this value. Never nullptr. - const EnumDescriptor* type() const; - - // Get options for this enum value. These are specified in the .proto file by - // adding text like "[foo = 1234]" after an enum value definition. Allowed - // options are defined by EnumValueOptions in descriptor.proto, and any - // available extensions of that message. - const EnumValueOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(EnumValueDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this enum value declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - typedef EnumValueOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - const std::string* name_; - const std::string* full_name_; - int number_; - const EnumDescriptor* type_; - const EnumValueOptions* options_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() - // in descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - EnumValueDescriptor() {} - friend class DescriptorBuilder; - friend class EnumDescriptor; - friend class DescriptorPool; - friend class FileDescriptorTables; - friend class Reflection; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor); -}; - -// Describes an RPC service. Use DescriptorPool to construct your own -// descriptors. -class PROTOBUF_EXPORT ServiceDescriptor { - public: - typedef ServiceDescriptorProto Proto; - - // The name of the service, not including its containing scope. - const std::string& name() const; - // The fully-qualified name of the service, scope delimited by periods. - const std::string& full_name() const; - // Index of this service within the file's services array. - int index() const; - - // The .proto file in which this service was defined. Never nullptr. - const FileDescriptor* file() const; - - // Get options for this service type. These are specified in the .proto file - // by placing lines like "option foo = 1234;" in the service definition. - // Allowed options are defined by ServiceOptions in descriptor.proto, and any - // available extensions of that message. - const ServiceOptions& options() const; - - // The number of methods this service defines. - int method_count() const; - // Gets a MethodDescriptor by index, where 0 <= index < method_count(). - // These are returned in the order they were defined in the .proto file. - const MethodDescriptor* method(int index) const; - - // Look up a MethodDescriptor by name. - const MethodDescriptor* FindMethodByName(ConstStringParam name) const; - // See Descriptor::CopyTo(). - void CopyTo(ServiceDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this service declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - typedef ServiceOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // See Descriptor::DebugString(). - void DebugString(std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - const std::string* name_; - const std::string* full_name_; - const FileDescriptor* file_; - const ServiceOptions* options_; - MethodDescriptor* methods_; - int method_count_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - ServiceDescriptor() {} - friend class DescriptorBuilder; - friend class FileDescriptor; - friend class MethodDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor); -}; - - -// Describes an individual service method. To obtain a MethodDescriptor given -// a service, first get its ServiceDescriptor, then call -// ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your -// own descriptors. -class PROTOBUF_EXPORT MethodDescriptor { - public: - typedef MethodDescriptorProto Proto; - - // Name of this method, not including containing scope. - const std::string& name() const; - // The fully-qualified name of the method, scope delimited by periods. - const std::string& full_name() const; - // Index within the service's Descriptor. - int index() const; - - // The .proto file in which this method was defined. Never nullptr. - const FileDescriptor* file() const; - // Gets the service to which this method belongs. Never nullptr. - const ServiceDescriptor* service() const; - - // Gets the type of protocol message which this method accepts as input. - const Descriptor* input_type() const; - // Gets the type of protocol message which this message produces as output. - const Descriptor* output_type() const; - - // Gets whether the client streams multiple requests. - bool client_streaming() const; - // Gets whether the server streams multiple responses. - bool server_streaming() const; - - // Get options for this method. These are specified in the .proto file by - // placing lines like "option foo = 1234;" in curly-braces after a method - // declaration. Allowed options are defined by MethodOptions in - // descriptor.proto, and any available extensions of that message. - const MethodOptions& options() const; - - // See Descriptor::CopyTo(). - void CopyTo(MethodDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Source Location --------------------------------------------------- - - // Updates |*out_location| to the source location of the complete - // extent of this method declaration. Returns false and leaves - // |*out_location| unchanged iff location information was not available. - bool GetSourceLocation(SourceLocation* out_location) const; - - private: - typedef MethodOptions OptionsType; - - // Allows access to GetLocationPath for annotations. - friend class io::Printer; - friend class compiler::cpp::Formatter; - - // See Descriptor::DebugString(). - void DebugString(int depth, std::string* contents, - const DebugStringOptions& options) const; - - // Walks up the descriptor tree to generate the source location path - // to this descriptor from the file root. - void GetLocationPath(std::vector* output) const; - - const std::string* name_; - const std::string* full_name_; - const ServiceDescriptor* service_; - mutable internal::LazyDescriptor input_type_; - mutable internal::LazyDescriptor output_type_; - const MethodOptions* options_; - bool client_streaming_; - bool server_streaming_; - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - // Must be constructed using DescriptorPool. - MethodDescriptor() {} - friend class DescriptorBuilder; - friend class ServiceDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor); -}; - - -// Describes a whole .proto file. To get the FileDescriptor for a compiled-in -// file, get the descriptor for something defined in that file and call -// descriptor->file(). Use DescriptorPool to construct your own descriptors. -class PROTOBUF_EXPORT FileDescriptor { - public: - typedef FileDescriptorProto Proto; - - // The filename, relative to the source tree. - // e.g. "foo/bar/baz.proto" - const std::string& name() const; - - // The package, e.g. "google.protobuf.compiler". - const std::string& package() const; - - // The DescriptorPool in which this FileDescriptor and all its contents were - // allocated. Never nullptr. - const DescriptorPool* pool() const; - - // The number of files imported by this one. - int dependency_count() const; - // Gets an imported file by index, where 0 <= index < dependency_count(). - // These are returned in the order they were defined in the .proto file. - const FileDescriptor* dependency(int index) const; - - // The number of files public imported by this one. - // The public dependency list is a subset of the dependency list. - int public_dependency_count() const; - // Gets a public imported file by index, where 0 <= index < - // public_dependency_count(). - // These are returned in the order they were defined in the .proto file. - const FileDescriptor* public_dependency(int index) const; - - // The number of files that are imported for weak fields. - // The weak dependency list is a subset of the dependency list. - int weak_dependency_count() const; - // Gets a weak imported file by index, where 0 <= index < - // weak_dependency_count(). - // These are returned in the order they were defined in the .proto file. - const FileDescriptor* weak_dependency(int index) const; - - // Number of top-level message types defined in this file. (This does not - // include nested types.) - int message_type_count() const; - // Gets a top-level message type, where 0 <= index < message_type_count(). - // These are returned in the order they were defined in the .proto file. - const Descriptor* message_type(int index) const; - - // Number of top-level enum types defined in this file. (This does not - // include nested types.) - int enum_type_count() const; - // Gets a top-level enum type, where 0 <= index < enum_type_count(). - // These are returned in the order they were defined in the .proto file. - const EnumDescriptor* enum_type(int index) const; - - // Number of services defined in this file. - int service_count() const; - // Gets a service, where 0 <= index < service_count(). - // These are returned in the order they were defined in the .proto file. - const ServiceDescriptor* service(int index) const; - - // Number of extensions defined at file scope. (This does not include - // extensions nested within message types.) - int extension_count() const; - // Gets an extension's descriptor, where 0 <= index < extension_count(). - // These are returned in the order they were defined in the .proto file. - const FieldDescriptor* extension(int index) const; - - // Get options for this file. These are specified in the .proto file by - // placing lines like "option foo = 1234;" at the top level, outside of any - // other definitions. Allowed options are defined by FileOptions in - // descriptor.proto, and any available extensions of that message. - const FileOptions& options() const; - - // Syntax of this file. - enum Syntax { - SYNTAX_UNKNOWN = 0, - SYNTAX_PROTO2 = 2, - SYNTAX_PROTO3 = 3, - }; - Syntax syntax() const; - static const char* SyntaxName(Syntax syntax); - - // Find a top-level message type by name. Returns nullptr if not found. - const Descriptor* FindMessageTypeByName(ConstStringParam name) const; - // Find a top-level enum type by name. Returns nullptr if not found. - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; - // Find an enum value defined in any top-level enum by name. Returns nullptr - // if not found. - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; - // Find a service definition by name. Returns nullptr if not found. - const ServiceDescriptor* FindServiceByName(ConstStringParam name) const; - // Find a top-level extension definition by name. Returns nullptr if not - // found. - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; - // Similar to FindExtensionByName(), but searches by lowercased-name. See - // Descriptor::FindFieldByLowercaseName(). - const FieldDescriptor* FindExtensionByLowercaseName( - ConstStringParam name) const; - // Similar to FindExtensionByName(), but searches by camelcased-name. See - // Descriptor::FindFieldByCamelcaseName(). - const FieldDescriptor* FindExtensionByCamelcaseName( - ConstStringParam name) const; - - // See Descriptor::CopyTo(). - // Notes: - // - This method does NOT copy source code information since it is relatively - // large and rarely needed. See CopySourceCodeInfoTo() below. - void CopyTo(FileDescriptorProto* proto) const; - // Write the source code information of this FileDescriptor into the given - // FileDescriptorProto. See CopyTo() above. - void CopySourceCodeInfoTo(FileDescriptorProto* proto) const; - // Fill the json_name field of FieldDescriptorProto for all fields. Can only - // be called after CopyTo(). - void CopyJsonNameTo(FileDescriptorProto* proto) const; - - // See Descriptor::DebugString(). - std::string DebugString() const; - - // See Descriptor::DebugStringWithOptions(). - std::string DebugStringWithOptions(const DebugStringOptions& options) const; - - // Returns true if this is a placeholder for an unknown file. This will - // only be the case if this descriptor comes from a DescriptorPool - // with AllowUnknownDependencies() set. - bool is_placeholder() const; - - // Updates |*out_location| to the source location of the complete extent of - // this file declaration (namely, the empty path). - bool GetSourceLocation(SourceLocation* out_location) const; - - // Updates |*out_location| to the source location of the complete - // extent of the declaration or declaration-part denoted by |path|. - // Returns false and leaves |*out_location| unchanged iff location - // information was not available. (See SourceCodeInfo for - // description of path encoding.) - bool GetSourceLocation(const std::vector& path, - SourceLocation* out_location) const; - - private: - typedef FileOptions OptionsType; - - const std::string* name_; - const std::string* package_; - const DescriptorPool* pool_; - internal::once_flag* dependencies_once_; - static void DependenciesOnceInit(const FileDescriptor* to_init); - void InternalDependenciesOnceInit() const; - - // These are arranged to minimize padding on 64-bit. - int dependency_count_; - int public_dependency_count_; - int weak_dependency_count_; - int message_type_count_; - int enum_type_count_; - int service_count_; - int extension_count_; - Syntax syntax_; - bool is_placeholder_; - - // Indicates the FileDescriptor is completed building. Used to verify - // that type accessor functions that can possibly build a dependent file - // aren't called during the process of building the file. - bool finished_building_; - - mutable const FileDescriptor** dependencies_; - const std::string** dependencies_names_; - int* public_dependencies_; - int* weak_dependencies_; - Descriptor* message_types_; - EnumDescriptor* enum_types_; - ServiceDescriptor* services_; - FieldDescriptor* extensions_; - const FileOptions* options_; - - const FileDescriptorTables* tables_; - const SourceCodeInfo* source_code_info_; - - // IMPORTANT: If you add a new field, make sure to search for all instances - // of Allocate() and AllocateArray() in - // descriptor.cc and update them to initialize the field. - - FileDescriptor() {} - friend class DescriptorBuilder; - friend class DescriptorPool; - friend class Descriptor; - friend class FieldDescriptor; - friend class internal::LazyDescriptor; - friend class OneofDescriptor; - friend class EnumDescriptor; - friend class EnumValueDescriptor; - friend class MethodDescriptor; - friend class ServiceDescriptor; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor); -}; - - -// =================================================================== - -// Used to construct descriptors. -// -// Normally you won't want to build your own descriptors. Message classes -// constructed by the protocol compiler will provide them for you. However, -// if you are implementing Message on your own, or if you are writing a -// program which can operate on totally arbitrary types and needs to load -// them from some sort of database, you might need to. -// -// Since Descriptors are composed of a whole lot of cross-linked bits of -// data that would be a pain to put together manually, the -// DescriptorPool class is provided to make the process easier. It can -// take a FileDescriptorProto (defined in descriptor.proto), validate it, -// and convert it to a set of nicely cross-linked Descriptors. -// -// DescriptorPool also helps with memory management. Descriptors are -// composed of many objects containing static data and pointers to each -// other. In all likelihood, when it comes time to delete this data, -// you'll want to delete it all at once. In fact, it is not uncommon to -// have a whole pool of descriptors all cross-linked with each other which -// you wish to delete all at once. This class represents such a pool, and -// handles the memory management for you. -// -// You can also search for descriptors within a DescriptorPool by name, and -// extensions by number. -class PROTOBUF_EXPORT DescriptorPool { - public: - // Create a normal, empty DescriptorPool. - DescriptorPool(); - - // Constructs a DescriptorPool that, when it can't find something among the - // descriptors already in the pool, looks for it in the given - // DescriptorDatabase. - // Notes: - // - If a DescriptorPool is constructed this way, its BuildFile*() methods - // must not be called (they will assert-fail). The only way to populate - // the pool with descriptors is to call the Find*By*() methods. - // - The Find*By*() methods may block the calling thread if the - // DescriptorDatabase blocks. This in turn means that parsing messages - // may block if they need to look up extensions. - // - The Find*By*() methods will use mutexes for thread-safety, thus making - // them slower even when they don't have to fall back to the database. - // In fact, even the Find*By*() methods of descriptor objects owned by - // this pool will be slower, since they will have to obtain locks too. - // - An ErrorCollector may optionally be given to collect validation errors - // in files loaded from the database. If not given, errors will be printed - // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this - // ErrorCollector may be called from any thread that calls one of the - // Find*By*() methods. - // - The DescriptorDatabase must not be mutated during the lifetime of - // the DescriptorPool. Even if the client takes care to avoid data races, - // changes to the content of the DescriptorDatabase may not be reflected - // in subsequent lookups in the DescriptorPool. - class ErrorCollector; - explicit DescriptorPool(DescriptorDatabase* fallback_database, - ErrorCollector* error_collector = nullptr); - - ~DescriptorPool(); - - // Get a pointer to the generated pool. Generated protocol message classes - // which are compiled into the binary will allocate their descriptors in - // this pool. Do not add your own descriptors to this pool. - static const DescriptorPool* generated_pool(); - - - // Find a FileDescriptor in the pool by file name. Returns nullptr if not - // found. - const FileDescriptor* FindFileByName(ConstStringParam name) const; - - // Find the FileDescriptor in the pool which defines the given symbol. - // If any of the Find*ByName() methods below would succeed, then this is - // equivalent to calling that method and calling the result's file() method. - // Otherwise this returns nullptr. - const FileDescriptor* FindFileContainingSymbol( - ConstStringParam symbol_name) const; - - // Looking up descriptors ------------------------------------------ - // These find descriptors by fully-qualified name. These will find both - // top-level descriptors and nested descriptors. They return nullptr if not - // found. - - const Descriptor* FindMessageTypeByName(ConstStringParam name) const; - const FieldDescriptor* FindFieldByName(ConstStringParam name) const; - const FieldDescriptor* FindExtensionByName(ConstStringParam name) const; - const OneofDescriptor* FindOneofByName(ConstStringParam name) const; - const EnumDescriptor* FindEnumTypeByName(ConstStringParam name) const; - const EnumValueDescriptor* FindEnumValueByName(ConstStringParam name) const; - const ServiceDescriptor* FindServiceByName(ConstStringParam name) const; - const MethodDescriptor* FindMethodByName(ConstStringParam name) const; - - // Finds an extension of the given type by number. The extendee must be - // a member of this DescriptorPool or one of its underlays. - const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee, - int number) const; - - // Finds an extension of the given type by its printable name. - // See comments above PrintableNameForExtension() for the definition of - // "printable name". The extendee must be a member of this DescriptorPool - // or one of its underlays. Returns nullptr if there is no known message - // extension with the given printable name. - const FieldDescriptor* FindExtensionByPrintableName( - const Descriptor* extendee, ConstStringParam printable_name) const; - - // Finds extensions of extendee. The extensions will be appended to - // out in an undefined order. Only extensions defined directly in - // this DescriptorPool or one of its underlays are guaranteed to be - // found: extensions defined in the fallback database might not be found - // depending on the database implementation. - void FindAllExtensions(const Descriptor* extendee, - std::vector* out) const; - - // Building descriptors -------------------------------------------- - - // When converting a FileDescriptorProto to a FileDescriptor, various - // errors might be detected in the input. The caller may handle these - // programmatically by implementing an ErrorCollector. - class PROTOBUF_EXPORT ErrorCollector { - public: - inline ErrorCollector() {} - virtual ~ErrorCollector(); - - // These constants specify what exact part of the construct is broken. - // This is useful e.g. for mapping the error back to an exact location - // in a .proto file. - enum ErrorLocation { - NAME, // the symbol name, or the package name for files - NUMBER, // field or extension range number - TYPE, // field type - EXTENDEE, // field extendee - DEFAULT_VALUE, // field default value - INPUT_TYPE, // method input type - OUTPUT_TYPE, // method output type - OPTION_NAME, // name in assignment - OPTION_VALUE, // value in option assignment - IMPORT, // import error - OTHER // some other problem - }; - - // Reports an error in the FileDescriptorProto. Use this function if the - // problem occurred should interrupt building the FileDescriptorProto. - virtual void AddError( - const std::string& filename, // File name in which the error occurred. - const std::string& element_name, // Full name of the erroneous element. - const Message* descriptor, // Descriptor of the erroneous element. - ErrorLocation location, // One of the location constants, above. - const std::string& message // Human-readable error message. - ) = 0; - - // Reports a warning in the FileDescriptorProto. Use this function if the - // problem occurred should NOT interrupt building the FileDescriptorProto. - virtual void AddWarning( - const std::string& /*filename*/, // File name in which the error - // occurred. - const std::string& /*element_name*/, // Full name of the erroneous - // element. - const Message* /*descriptor*/, // Descriptor of the erroneous element. - ErrorLocation /*location*/, // One of the location constants, above. - const std::string& /*message*/ // Human-readable error message. - ) {} - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector); - }; - - // Convert the FileDescriptorProto to real descriptors and place them in - // this DescriptorPool. All dependencies of the file must already be in - // the pool. Returns the resulting FileDescriptor, or nullptr if there were - // problems with the input (e.g. the message was invalid, or dependencies - // were missing). Details about the errors are written to GOOGLE_LOG(ERROR). - const FileDescriptor* BuildFile(const FileDescriptorProto& proto); - - // Same as BuildFile() except errors are sent to the given ErrorCollector. - const FileDescriptor* BuildFileCollectingErrors( - const FileDescriptorProto& proto, ErrorCollector* error_collector); - - // By default, it is an error if a FileDescriptorProto contains references - // to types or other files that are not found in the DescriptorPool (or its - // backing DescriptorDatabase, if any). If you call - // AllowUnknownDependencies(), however, then unknown types and files - // will be replaced by placeholder descriptors (which can be identified by - // the is_placeholder() method). This can allow you to - // perform some useful operations with a .proto file even if you do not - // have access to other .proto files on which it depends. However, some - // heuristics must be used to fill in the gaps in information, and these - // can lead to descriptors which are inaccurate. For example, the - // DescriptorPool may be forced to guess whether an unknown type is a message - // or an enum, as well as what package it resides in. Furthermore, - // placeholder types will not be discoverable via FindMessageTypeByName() - // and similar methods, which could confuse some descriptor-based algorithms. - // Generally, the results of this option should be handled with extreme care. - void AllowUnknownDependencies() { allow_unknown_ = true; } - - // By default, weak imports are allowed to be missing, in which case we will - // use a placeholder for the dependency and convert the field to be an Empty - // message field. If you call EnforceWeakDependencies(true), however, the - // DescriptorPool will report a import not found error. - void EnforceWeakDependencies(bool enforce) { enforce_weak_ = enforce; } - - // Internal stuff -------------------------------------------------- - // These methods MUST NOT be called from outside the proto2 library. - // These methods may contain hidden pitfalls and may be removed in a - // future library version. - - // Create a DescriptorPool which is overlaid on top of some other pool. - // If you search for a descriptor in the overlay and it is not found, the - // underlay will be searched as a backup. If the underlay has its own - // underlay, that will be searched next, and so on. This also means that - // files built in the overlay will be cross-linked with the underlay's - // descriptors if necessary. The underlay remains property of the caller; - // it must remain valid for the lifetime of the newly-constructed pool. - // - // Example: Say you want to parse a .proto file at runtime in order to use - // its type with a DynamicMessage. Say this .proto file has dependencies, - // but you know that all the dependencies will be things that are already - // compiled into the binary. For ease of use, you'd like to load the types - // right out of generated_pool() rather than have to parse redundant copies - // of all these .protos and runtime. But, you don't want to add the parsed - // types directly into generated_pool(): this is not allowed, and would be - // bad design anyway. So, instead, you could use generated_pool() as an - // underlay for a new DescriptorPool in which you add only the new file. - // - // WARNING: Use of underlays can lead to many subtle gotchas. Instead, - // try to formulate what you want to do in terms of DescriptorDatabases. - explicit DescriptorPool(const DescriptorPool* underlay); - - // Called by generated classes at init time to add their descriptors to - // generated_pool. Do NOT call this in your own code! filename must be a - // permanent string (e.g. a string literal). - static void InternalAddGeneratedFile(const void* encoded_file_descriptor, - int size); - - // Disallow [enforce_utf8 = false] in .proto files. - void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; } - - - // For internal use only: Gets a non-const pointer to the generated pool. - // This is called at static-initialization time only, so thread-safety is - // not a concern. If both an underlay and a fallback database are present, - // the underlay takes precedence. - static DescriptorPool* internal_generated_pool(); - - // For internal use only: Gets a non-const pointer to the generated - // descriptor database. - // Only used for testing. - static DescriptorDatabase* internal_generated_database(); - - // For internal use only: Changes the behavior of BuildFile() such that it - // allows the file to make reference to message types declared in other files - // which it did not officially declare as dependencies. - void InternalDontEnforceDependencies(); - - // For internal use only: Enables lazy building of dependencies of a file. - // Delay the building of dependencies of a file descriptor until absolutely - // necessary, like when message_type() is called on a field that is defined - // in that dependency's file. This will cause functional issues if a proto - // or one of its dependencies has errors. Should only be enabled for the - // generated_pool_ (because no descriptor build errors are guaranteed by - // the compilation generation process), testing, or if a lack of descriptor - // build errors can be guaranteed for a pool. - void InternalSetLazilyBuildDependencies() { - lazily_build_dependencies_ = true; - // This needs to be set when lazily building dependencies, as it breaks - // dependency checking. - InternalDontEnforceDependencies(); - } - - // For internal use only. - void internal_set_underlay(const DescriptorPool* underlay) { - underlay_ = underlay; - } - - // For internal (unit test) use only: Returns true if a FileDescriptor has - // been constructed for the given file, false otherwise. Useful for testing - // lazy descriptor initialization behavior. - bool InternalIsFileLoaded(ConstStringParam filename) const; - - // Add a file to unused_import_track_files_. DescriptorBuilder will log - // warnings or errors for those files if there is any unused import. - void AddUnusedImportTrackFile(ConstStringParam file_name, - bool is_error = false); - void ClearUnusedImportTrackFiles(); - - private: - friend class Descriptor; - friend class internal::LazyDescriptor; - friend class FieldDescriptor; - friend class EnumDescriptor; - friend class ServiceDescriptor; - friend class MethodDescriptor; - friend class FileDescriptor; - friend class StreamDescriptor; - friend class DescriptorBuilder; - friend class FileDescriptorTables; - - // Return true if the given name is a sub-symbol of any non-package - // descriptor that already exists in the descriptor pool. (The full - // definition of such types is already known.) - bool IsSubSymbolOfBuiltType(StringPiece name) const; - - // Tries to find something in the fallback database and link in the - // corresponding proto file. Returns true if successful, in which case - // the caller should search for the thing again. These are declared - // const because they are called by (semantically) const methods. - bool TryFindFileInFallbackDatabase(StringPiece name) const; - bool TryFindSymbolInFallbackDatabase(StringPiece name) const; - bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type, - int field_number) const; - - // This internal find extension method only check with its table and underlay - // descriptor_pool's table. It does not check with fallback DB and no - // additional proto file will be build in this method. - const FieldDescriptor* InternalFindExtensionByNumberNoLock( - const Descriptor* extendee, int number) const; - - // Like BuildFile() but called internally when the file has been loaded from - // fallback_database_. Declared const because it is called by (semantically) - // const methods. - const FileDescriptor* BuildFileFromDatabase( - const FileDescriptorProto& proto) const; - - // Helper for when lazily_build_dependencies_ is set, can look up a symbol - // after the file's descriptor is built, and can build the file where that - // symbol is defined if necessary. Will create a placeholder if the type - // doesn't exist in the fallback database, or the file doesn't build - // successfully. - Symbol CrossLinkOnDemandHelper(StringPiece name, - bool expecting_enum) const; - - // Create a placeholder FileDescriptor of the specified name - FileDescriptor* NewPlaceholderFile(StringPiece name) const; - FileDescriptor* NewPlaceholderFileWithMutexHeld(StringPiece name) const; - - enum PlaceholderType { - PLACEHOLDER_MESSAGE, - PLACEHOLDER_ENUM, - PLACEHOLDER_EXTENDABLE_MESSAGE - }; - // Create a placeholder Descriptor of the specified name - Symbol NewPlaceholder(StringPiece name, - PlaceholderType placeholder_type) const; - Symbol NewPlaceholderWithMutexHeld(StringPiece name, - PlaceholderType placeholder_type) const; - - // If fallback_database_ is nullptr, this is nullptr. Otherwise, this is a - // mutex which must be locked while accessing tables_. - internal::WrappedMutex* mutex_; - - // See constructor. - DescriptorDatabase* fallback_database_; - ErrorCollector* default_error_collector_; - const DescriptorPool* underlay_; - - // This class contains a lot of hash maps with complicated types that - // we'd like to keep out of the header. - class Tables; - std::unique_ptr tables_; - - bool enforce_dependencies_; - bool lazily_build_dependencies_; - bool allow_unknown_; - bool enforce_weak_; - bool disallow_enforce_utf8_; - - // Set of files to track for unused imports. The bool value when true means - // unused imports are treated as errors (and as warnings when false). - std::map unused_import_track_files_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool); -}; - - -// inline methods ==================================================== - -// These macros makes this repetitive code more readable. -#define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \ - inline TYPE CLASS::FIELD() const { return FIELD##_; } - -// Strings fields are stored as pointers but returned as const references. -#define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \ - inline const std::string& CLASS::FIELD() const { return *FIELD##_; } - -// Arrays take an index parameter, obviously. -#define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \ - inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; } - -#define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \ - inline const TYPE& CLASS::options() const { return *options_; } - -PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*) - -PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, oneof_decl_count, int) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, real_oneof_decl_count, int) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int) - -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*) - -PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range, - const Descriptor::ExtensionRange*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor*) - -PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range, - const Descriptor::ReservedRange*) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_name_count, int) - -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions) -PROTOBUF_DEFINE_ACCESSOR(Descriptor, is_placeholder, bool) - -PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name) -PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, json_name) -PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name) -PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_oneof, - const OneofDescriptor*) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, index_in_oneof, int) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*) -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_json_name, bool) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32, int32) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64, int64) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float, float) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double) -PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool, bool) -PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string) - -PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, full_name) -PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor*) -PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, field_count, int) -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(OneofDescriptor, OneofOptions) - -PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value, - const EnumValueDescriptor*) -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, is_placeholder, bool) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_range_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, reserved_range, - const EnumDescriptor::ReservedRange*) -PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_name_count, int) - -PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name) -PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int) -PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*) -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions) - -PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name) -PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*) -PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method, - const MethodDescriptor*) -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions) - -PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name) -PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*) -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions) -PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, client_streaming, bool) -PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, server_streaming, bool) - -PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name) -PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int) -PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions) -PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, is_placeholder, bool) - -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service, - const ServiceDescriptor*) -PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension, - const FieldDescriptor*) - -#undef PROTOBUF_DEFINE_ACCESSOR -#undef PROTOBUF_DEFINE_STRING_ACCESSOR -#undef PROTOBUF_DEFINE_ARRAY_ACCESSOR - -// A few accessors differ from the macros... - -inline Descriptor::WellKnownType Descriptor::well_known_type() const { - return static_cast(well_known_type_); -} - -inline bool Descriptor::IsExtensionNumber(int number) const { - return FindExtensionRangeContainingNumber(number) != nullptr; -} - -inline bool Descriptor::IsReservedNumber(int number) const { - return FindReservedRangeContainingNumber(number) != nullptr; -} - -inline bool Descriptor::IsReservedName(ConstStringParam name) const { - for (int i = 0; i < reserved_name_count(); i++) { - if (name == static_cast(reserved_name(i))) { - return true; - } - } - return false; -} - -// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually -// an array of pointers rather than the usual array of objects. -inline const std::string& Descriptor::reserved_name(int index) const { - return *reserved_names_[index]; -} - -inline bool EnumDescriptor::IsReservedNumber(int number) const { - return FindReservedRangeContainingNumber(number) != nullptr; -} - -inline bool EnumDescriptor::IsReservedName(ConstStringParam name) const { - for (int i = 0; i < reserved_name_count(); i++) { - if (name == static_cast(reserved_name(i))) { - return true; - } - } - return false; -} - -// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually -// an array of pointers rather than the usual array of objects. -inline const std::string& EnumDescriptor::reserved_name(int index) const { - return *reserved_names_[index]; -} - -inline FieldDescriptor::Type FieldDescriptor::type() const { - if (type_once_) { - internal::call_once(*type_once_, &FieldDescriptor::TypeOnceInit, this); - } - return type_; -} - -inline bool FieldDescriptor::is_required() const { - return label() == LABEL_REQUIRED; -} - -inline bool FieldDescriptor::is_optional() const { - return label() == LABEL_OPTIONAL; -} - -inline bool FieldDescriptor::is_repeated() const { - return label() == LABEL_REPEATED; -} - -inline bool FieldDescriptor::is_packable() const { - return is_repeated() && IsTypePackable(type()); -} - -inline bool FieldDescriptor::is_map() const { - return type() == TYPE_MESSAGE && is_map_message_type(); -} - -inline bool FieldDescriptor::has_optional_keyword() const { - return proto3_optional_ || - (file()->syntax() == FileDescriptor::SYNTAX_PROTO2 && is_optional() && - !containing_oneof()); -} - -inline const OneofDescriptor* FieldDescriptor::real_containing_oneof() const { - return containing_oneof_ && !containing_oneof_->is_synthetic() - ? containing_oneof_ - : nullptr; -} - -inline bool FieldDescriptor::has_presence() const { - if (is_repeated()) return false; - return cpp_type() == CPPTYPE_MESSAGE || containing_oneof() || - file()->syntax() == FileDescriptor::SYNTAX_PROTO2; -} - -// To save space, index() is computed by looking at the descriptor's position -// in the parent's array of children. -inline int FieldDescriptor::index() const { - if (!is_extension_) { - return static_cast(this - containing_type()->fields_); - } else if (extension_scope_ != nullptr) { - return static_cast(this - extension_scope_->extensions_); - } else { - return static_cast(this - file_->extensions_); - } -} - -inline int Descriptor::index() const { - if (containing_type_ == nullptr) { - return static_cast(this - file_->message_types_); - } else { - return static_cast(this - containing_type_->nested_types_); - } -} - -inline const FileDescriptor* OneofDescriptor::file() const { - return containing_type()->file(); -} - -inline int OneofDescriptor::index() const { - return static_cast(this - containing_type_->oneof_decls_); -} - -inline bool OneofDescriptor::is_synthetic() const { - return field_count() == 1 && field(0)->proto3_optional_; -} - -inline int EnumDescriptor::index() const { - if (containing_type_ == nullptr) { - return static_cast(this - file_->enum_types_); - } else { - return static_cast(this - containing_type_->enum_types_); - } -} - -inline const FileDescriptor* EnumValueDescriptor::file() const { - return type()->file(); -} - -inline int EnumValueDescriptor::index() const { - return static_cast(this - type_->values_); -} - -inline int ServiceDescriptor::index() const { - return static_cast(this - file_->services_); -} - -inline const FileDescriptor* MethodDescriptor::file() const { - return service()->file(); -} - -inline int MethodDescriptor::index() const { - return static_cast(this - service_->methods_); -} - -inline const char* FieldDescriptor::type_name() const { - return kTypeToName[type()]; -} - -inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const { - return kTypeToCppTypeMap[type()]; -} - -inline const char* FieldDescriptor::cpp_type_name() const { - return kCppTypeToName[kTypeToCppTypeMap[type()]]; -} - -inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) { - return kTypeToCppTypeMap[type]; -} - -inline const char* FieldDescriptor::TypeName(Type type) { - return kTypeToName[type]; -} - -inline const char* FieldDescriptor::CppTypeName(CppType cpp_type) { - return kCppTypeToName[cpp_type]; -} - -inline bool FieldDescriptor::IsTypePackable(Type field_type) { - return (field_type != FieldDescriptor::TYPE_STRING && - field_type != FieldDescriptor::TYPE_GROUP && - field_type != FieldDescriptor::TYPE_MESSAGE && - field_type != FieldDescriptor::TYPE_BYTES); -} - -inline const FileDescriptor* FileDescriptor::public_dependency( - int index) const { - return dependency(public_dependencies_[index]); -} - -inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const { - return dependency(weak_dependencies_[index]); -} - -inline FileDescriptor::Syntax FileDescriptor::syntax() const { return syntax_; } - -// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array -// of pointers rather than the usual array of objects. -inline const FieldDescriptor* OneofDescriptor::field(int index) const { - return fields_[index]; -} - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__ diff --git a/third_party/protobuf-lite/google/protobuf/extension_set.h b/third_party/protobuf-lite/google/protobuf/extension_set.h deleted file mode 100644 index b3a6e3a07..000000000 --- a/third_party/protobuf-lite/google/protobuf/extension_set.h +++ /dev/null @@ -1,1591 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This header is logically internal, but is made public because it is used -// from protocol-compiler-generated code, which may reside in other components. - -#ifndef GOOGLE_PROTOBUF_EXTENSION_SET_H__ -#define GOOGLE_PROTOBUF_EXTENSION_SET_H__ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { -class Arena; -class Descriptor; // descriptor.h -class FieldDescriptor; // descriptor.h -class DescriptorPool; // descriptor.h -class MessageLite; // message_lite.h -class Message; // message.h -class MessageFactory; // message.h -class UnknownFieldSet; // unknown_field_set.h -namespace internal { -class FieldSkipper; // wire_format_lite.h -} // namespace internal -} // namespace protobuf -} // namespace google - -namespace google { -namespace protobuf { -namespace internal { - -class InternalMetadata; - -// Used to store values of type WireFormatLite::FieldType without having to -// #include wire_format_lite.h. Also, ensures that we use only one byte to -// store these values, which is important to keep the layout of -// ExtensionSet::Extension small. -typedef uint8 FieldType; - -// A function which, given an integer value, returns true if the number -// matches one of the defined values for the corresponding enum type. This -// is used with RegisterEnumExtension, below. -typedef bool EnumValidityFunc(int number); - -// Version of the above which takes an argument. This is needed to deal with -// extensions that are not compiled in. -typedef bool EnumValidityFuncWithArg(const void* arg, int number); - -// Information about a registered extension. -struct ExtensionInfo { - inline ExtensionInfo() {} - inline ExtensionInfo(FieldType type_param, bool isrepeated, bool ispacked) - : type(type_param), - is_repeated(isrepeated), - is_packed(ispacked), - descriptor(NULL) {} - - FieldType type; - bool is_repeated; - bool is_packed; - - struct EnumValidityCheck { - EnumValidityFuncWithArg* func; - const void* arg; - }; - - struct MessageInfo { - const MessageLite* prototype; - }; - - union { - EnumValidityCheck enum_validity_check; - MessageInfo message_info; - }; - - // The descriptor for this extension, if one exists and is known. May be - // NULL. Must not be NULL if the descriptor for the extension does not - // live in the same pool as the descriptor for the containing type. - const FieldDescriptor* descriptor; -}; - -// Abstract interface for an object which looks up extension definitions. Used -// when parsing. -class PROTOBUF_EXPORT ExtensionFinder { - public: - virtual ~ExtensionFinder(); - - // Find the extension with the given containing type and number. - virtual bool Find(int number, ExtensionInfo* output) = 0; -}; - -// Implementation of ExtensionFinder which finds extensions defined in .proto -// files which have been compiled into the binary. -class PROTOBUF_EXPORT GeneratedExtensionFinder : public ExtensionFinder { - public: - GeneratedExtensionFinder(const MessageLite* containing_type) - : containing_type_(containing_type) {} - ~GeneratedExtensionFinder() override {} - - // Returns true and fills in *output if found, otherwise returns false. - bool Find(int number, ExtensionInfo* output) override; - - private: - const MessageLite* containing_type_; -}; - -// A FieldSkipper used for parsing MessageSet. -class MessageSetFieldSkipper; - -// Note: extension_set_heavy.cc defines DescriptorPoolExtensionFinder for -// finding extensions from a DescriptorPool. - -// This is an internal helper class intended for use within the protocol buffer -// library and generated classes. Clients should not use it directly. Instead, -// use the generated accessors such as GetExtension() of the class being -// extended. -// -// This class manages extensions for a protocol message object. The -// message's HasExtension(), GetExtension(), MutableExtension(), and -// ClearExtension() methods are just thin wrappers around the embedded -// ExtensionSet. When parsing, if a tag number is encountered which is -// inside one of the message type's extension ranges, the tag is passed -// off to the ExtensionSet for parsing. Etc. -class PROTOBUF_EXPORT ExtensionSet { - public: - constexpr ExtensionSet(); - explicit ExtensionSet(Arena* arena); - ~ExtensionSet(); - - // These are called at startup by protocol-compiler-generated code to - // register known extensions. The registrations are used by ParseField() - // to look up extensions for parsed field numbers. Note that dynamic parsing - // does not use ParseField(); only protocol-compiler-generated parsing - // methods do. - static void RegisterExtension(const MessageLite* containing_type, int number, - FieldType type, bool is_repeated, - bool is_packed); - static void RegisterEnumExtension(const MessageLite* containing_type, - int number, FieldType type, - bool is_repeated, bool is_packed, - EnumValidityFunc* is_valid); - static void RegisterMessageExtension(const MessageLite* containing_type, - int number, FieldType type, - bool is_repeated, bool is_packed, - const MessageLite* prototype); - - // ================================================================= - - // Add all fields which are currently present to the given vector. This - // is useful to implement Reflection::ListFields(). - void AppendToList(const Descriptor* containing_type, - const DescriptorPool* pool, - std::vector* output) const; - - // ================================================================= - // Accessors - // - // Generated message classes include type-safe templated wrappers around - // these methods. Generally you should use those rather than call these - // directly, unless you are doing low-level memory management. - // - // When calling any of these accessors, the extension number requested - // MUST exist in the DescriptorPool provided to the constructor. Otherwise, - // the method will fail an assert. Normally, though, you would not call - // these directly; you would either call the generated accessors of your - // message class (e.g. GetExtension()) or you would call the accessors - // of the reflection interface. In both cases, it is impossible to - // trigger this assert failure: the generated accessors only accept - // linked-in extension types as parameters, while the Reflection interface - // requires you to provide the FieldDescriptor describing the extension. - // - // When calling any of these accessors, a protocol-compiler-generated - // implementation of the extension corresponding to the number MUST - // be linked in, and the FieldDescriptor used to refer to it MUST be - // the one generated by that linked-in code. Otherwise, the method will - // die on an assert failure. The message objects returned by the message - // accessors are guaranteed to be of the correct linked-in type. - // - // These methods pretty much match Reflection except that: - // - They're not virtual. - // - They identify fields by number rather than FieldDescriptors. - // - They identify enum values using integers rather than descriptors. - // - Strings provide Mutable() in addition to Set() accessors. - - bool Has(int number) const; - int ExtensionSize(int number) const; // Size of a repeated extension. - int NumExtensions() const; // The number of extensions - FieldType ExtensionType(int number) const; - void ClearExtension(int number); - - // singular fields ------------------------------------------------- - - int32 GetInt32(int number, int32 default_value) const; - int64 GetInt64(int number, int64 default_value) const; - uint32 GetUInt32(int number, uint32 default_value) const; - uint64 GetUInt64(int number, uint64 default_value) const; - float GetFloat(int number, float default_value) const; - double GetDouble(int number, double default_value) const; - bool GetBool(int number, bool default_value) const; - int GetEnum(int number, int default_value) const; - const std::string& GetString(int number, - const std::string& default_value) const; - const MessageLite& GetMessage(int number, - const MessageLite& default_value) const; - const MessageLite& GetMessage(int number, const Descriptor* message_type, - MessageFactory* factory) const; - - // |descriptor| may be NULL so long as it is known that the descriptor for - // the extension lives in the same pool as the descriptor for the containing - // type. -#define desc const FieldDescriptor* descriptor // avoid line wrapping - void SetInt32(int number, FieldType type, int32 value, desc); - void SetInt64(int number, FieldType type, int64 value, desc); - void SetUInt32(int number, FieldType type, uint32 value, desc); - void SetUInt64(int number, FieldType type, uint64 value, desc); - void SetFloat(int number, FieldType type, float value, desc); - void SetDouble(int number, FieldType type, double value, desc); - void SetBool(int number, FieldType type, bool value, desc); - void SetEnum(int number, FieldType type, int value, desc); - void SetString(int number, FieldType type, std::string value, desc); - std::string* MutableString(int number, FieldType type, desc); - MessageLite* MutableMessage(int number, FieldType type, - const MessageLite& prototype, desc); - MessageLite* MutableMessage(const FieldDescriptor* descriptor, - MessageFactory* factory); - // Adds the given message to the ExtensionSet, taking ownership of the - // message object. Existing message with the same number will be deleted. - // If "message" is NULL, this is equivalent to "ClearExtension(number)". - void SetAllocatedMessage(int number, FieldType type, - const FieldDescriptor* descriptor, - MessageLite* message); - void UnsafeArenaSetAllocatedMessage(int number, FieldType type, - const FieldDescriptor* descriptor, - MessageLite* message); - MessageLite* ReleaseMessage(int number, const MessageLite& prototype); - MessageLite* UnsafeArenaReleaseMessage(int number, - const MessageLite& prototype); - - MessageLite* ReleaseMessage(const FieldDescriptor* descriptor, - MessageFactory* factory); - MessageLite* UnsafeArenaReleaseMessage(const FieldDescriptor* descriptor, - MessageFactory* factory); -#undef desc - Arena* GetArena() const { return arena_; } - - // repeated fields ------------------------------------------------- - - // Fetches a RepeatedField extension by number; returns |default_value| - // if no such extension exists. User should not touch this directly; it is - // used by the GetRepeatedExtension() method. - const void* GetRawRepeatedField(int number, const void* default_value) const; - // Fetches a mutable version of a RepeatedField extension by number, - // instantiating one if none exists. Similar to above, user should not use - // this directly; it underlies MutableRepeatedExtension(). - void* MutableRawRepeatedField(int number, FieldType field_type, bool packed, - const FieldDescriptor* desc); - - // This is an overload of MutableRawRepeatedField to maintain compatibility - // with old code using a previous API. This version of - // MutableRawRepeatedField() will GOOGLE_CHECK-fail on a missing extension. - // (E.g.: borg/clients/internal/proto1/proto2_reflection.cc.) - void* MutableRawRepeatedField(int number); - - int32 GetRepeatedInt32(int number, int index) const; - int64 GetRepeatedInt64(int number, int index) const; - uint32 GetRepeatedUInt32(int number, int index) const; - uint64 GetRepeatedUInt64(int number, int index) const; - float GetRepeatedFloat(int number, int index) const; - double GetRepeatedDouble(int number, int index) const; - bool GetRepeatedBool(int number, int index) const; - int GetRepeatedEnum(int number, int index) const; - const std::string& GetRepeatedString(int number, int index) const; - const MessageLite& GetRepeatedMessage(int number, int index) const; - - void SetRepeatedInt32(int number, int index, int32 value); - void SetRepeatedInt64(int number, int index, int64 value); - void SetRepeatedUInt32(int number, int index, uint32 value); - void SetRepeatedUInt64(int number, int index, uint64 value); - void SetRepeatedFloat(int number, int index, float value); - void SetRepeatedDouble(int number, int index, double value); - void SetRepeatedBool(int number, int index, bool value); - void SetRepeatedEnum(int number, int index, int value); - void SetRepeatedString(int number, int index, std::string value); - std::string* MutableRepeatedString(int number, int index); - MessageLite* MutableRepeatedMessage(int number, int index); - -#define desc const FieldDescriptor* descriptor // avoid line wrapping - void AddInt32(int number, FieldType type, bool packed, int32 value, desc); - void AddInt64(int number, FieldType type, bool packed, int64 value, desc); - void AddUInt32(int number, FieldType type, bool packed, uint32 value, desc); - void AddUInt64(int number, FieldType type, bool packed, uint64 value, desc); - void AddFloat(int number, FieldType type, bool packed, float value, desc); - void AddDouble(int number, FieldType type, bool packed, double value, desc); - void AddBool(int number, FieldType type, bool packed, bool value, desc); - void AddEnum(int number, FieldType type, bool packed, int value, desc); - void AddString(int number, FieldType type, std::string value, desc); - std::string* AddString(int number, FieldType type, desc); - MessageLite* AddMessage(int number, FieldType type, - const MessageLite& prototype, desc); - MessageLite* AddMessage(const FieldDescriptor* descriptor, - MessageFactory* factory); - void AddAllocatedMessage(const FieldDescriptor* descriptor, - MessageLite* new_entry); -#undef desc - - void RemoveLast(int number); - MessageLite* ReleaseLast(int number); - void SwapElements(int number, int index1, int index2); - - // ----------------------------------------------------------------- - // TODO(kenton): Hardcore memory management accessors - - // ================================================================= - // convenience methods for implementing methods of Message - // - // These could all be implemented in terms of the other methods of this - // class, but providing them here helps keep the generated code size down. - - void Clear(); - void MergeFrom(const ExtensionSet& other); - void Swap(ExtensionSet* other); - void SwapExtension(ExtensionSet* other, int number); - bool IsInitialized() const; - - // Parses a single extension from the input. The input should start out - // positioned immediately after the tag. - bool ParseField(uint32 tag, io::CodedInputStream* input, - ExtensionFinder* extension_finder, - FieldSkipper* field_skipper); - - // Specific versions for lite or full messages (constructs the appropriate - // FieldSkipper automatically). |containing_type| is the default - // instance for the containing message; it is used only to look up the - // extension by number. See RegisterExtension(), above. Unlike the other - // methods of ExtensionSet, this only works for generated message types -- - // it looks up extensions registered using RegisterExtension(). - bool ParseField(uint32 tag, io::CodedInputStream* input, - const MessageLite* containing_type); - bool ParseField(uint32 tag, io::CodedInputStream* input, - const Message* containing_type, - UnknownFieldSet* unknown_fields); - bool ParseField(uint32 tag, io::CodedInputStream* input, - const MessageLite* containing_type, - io::CodedOutputStream* unknown_fields); - - // Lite parser - const char* ParseField(uint64 tag, const char* ptr, - const MessageLite* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx); - // Full parser - const char* ParseField(uint64 tag, const char* ptr, - const Message* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx); - template - const char* ParseMessageSet(const char* ptr, const Msg* containing_type, - InternalMetadata* metadata, - internal::ParseContext* ctx) { - struct MessageSetItem { - const char* _InternalParse(const char* ptr, ParseContext* ctx) { - return me->ParseMessageSetItem(ptr, containing_type, metadata, ctx); - } - ExtensionSet* me; - const Msg* containing_type; - InternalMetadata* metadata; - } item{this, containing_type, metadata}; - while (!ctx->Done(&ptr)) { - uint32 tag; - ptr = ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (tag == WireFormatLite::kMessageSetItemStartTag) { - ptr = ctx->ParseGroup(&item, ptr, tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - } else { - if (tag == 0 || (tag & 7) == 4) { - ctx->SetLastTag(tag); - return ptr; - } - ptr = ParseField(tag, ptr, containing_type, metadata, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - } - } - return ptr; - } - - // Parse an entire message in MessageSet format. Such messages have no - // fields, only extensions. - bool ParseMessageSetLite(io::CodedInputStream* input, - ExtensionFinder* extension_finder, - FieldSkipper* field_skipper); - bool ParseMessageSet(io::CodedInputStream* input, - ExtensionFinder* extension_finder, - MessageSetFieldSkipper* field_skipper); - - // Specific versions for lite or full messages (constructs the appropriate - // FieldSkipper automatically). - bool ParseMessageSet(io::CodedInputStream* input, - const MessageLite* containing_type, - std::string* unknown_fields); - bool ParseMessageSet(io::CodedInputStream* input, - const Message* containing_type, - UnknownFieldSet* unknown_fields); - - // Write all extension fields with field numbers in the range - // [start_field_number, end_field_number) - // to the output stream, using the cached sizes computed when ByteSize() was - // last called. Note that the range bounds are inclusive-exclusive. - void SerializeWithCachedSizes(int start_field_number, int end_field_number, - io::CodedOutputStream* output) const { - output->SetCur(_InternalSerialize(start_field_number, end_field_number, - output->Cur(), output->EpsCopy())); - } - - // Same as SerializeWithCachedSizes, but without any bounds checking. - // The caller must ensure that target has sufficient capacity for the - // serialized extensions. - // - // Returns a pointer past the last written byte. - uint8* _InternalSerialize(int start_field_number, int end_field_number, - uint8* target, - io::EpsCopyOutputStream* stream) const; - - // Like above but serializes in MessageSet format. - void SerializeMessageSetWithCachedSizes(io::CodedOutputStream* output) const { - output->SetCur(InternalSerializeMessageSetWithCachedSizesToArray( - output->Cur(), output->EpsCopy())); - } - uint8* InternalSerializeMessageSetWithCachedSizesToArray( - uint8* target, io::EpsCopyOutputStream* stream) const; - - // For backward-compatibility, versions of two of the above methods that - // serialize deterministically iff SetDefaultSerializationDeterministic() - // has been called. - uint8* SerializeWithCachedSizesToArray(int start_field_number, - int end_field_number, - uint8* target) const; - uint8* SerializeMessageSetWithCachedSizesToArray(uint8* target) const; - - // Returns the total serialized size of all the extensions. - size_t ByteSize() const; - - // Like ByteSize() but uses MessageSet format. - size_t MessageSetByteSize() const; - - // Returns (an estimate of) the total number of bytes used for storing the - // extensions in memory, excluding sizeof(*this). If the ExtensionSet is - // for a lite message (and thus possibly contains lite messages), the results - // are undefined (might work, might crash, might corrupt data, might not even - // be linked in). It's up to the protocol compiler to avoid calling this on - // such ExtensionSets (easy enough since lite messages don't implement - // SpaceUsed()). - size_t SpaceUsedExcludingSelfLong() const; - - // This method just calls SpaceUsedExcludingSelfLong() but it can not be - // inlined because the definition of SpaceUsedExcludingSelfLong() is not - // included in lite runtime and when an inline method refers to it MSVC - // will complain about unresolved symbols when building the lite runtime - // as .dll. - int SpaceUsedExcludingSelf() const; - - private: - // Interface of a lazily parsed singular message extension. - class PROTOBUF_EXPORT LazyMessageExtension { - public: - LazyMessageExtension() {} - virtual ~LazyMessageExtension() {} - - virtual LazyMessageExtension* New(Arena* arena) const = 0; - virtual const MessageLite& GetMessage( - const MessageLite& prototype) const = 0; - virtual MessageLite* MutableMessage(const MessageLite& prototype) = 0; - virtual void SetAllocatedMessage(MessageLite* message) = 0; - virtual void UnsafeArenaSetAllocatedMessage(MessageLite* message) = 0; - virtual MessageLite* ReleaseMessage(const MessageLite& prototype) = 0; - virtual MessageLite* UnsafeArenaReleaseMessage( - const MessageLite& prototype) = 0; - - virtual bool IsInitialized() const = 0; - - PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead") - virtual int ByteSize() const { return internal::ToIntSize(ByteSizeLong()); } - virtual size_t ByteSizeLong() const = 0; - virtual size_t SpaceUsedLong() const = 0; - - virtual void MergeFrom(const LazyMessageExtension& other) = 0; - virtual void Clear() = 0; - - virtual bool ReadMessage(const MessageLite& prototype, - io::CodedInputStream* input) = 0; - virtual const char* _InternalParse(const char* ptr, ParseContext* ctx) = 0; - virtual uint8* WriteMessageToArray( - int number, uint8* target, io::EpsCopyOutputStream* stream) const = 0; - - private: - virtual void UnusedKeyMethod(); // Dummy key method to avoid weak vtable. - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LazyMessageExtension); - }; - struct Extension { - // The order of these fields packs Extension into 24 bytes when using 8 - // byte alignment. Consider this when adding or removing fields here. - union { - int32 int32_value; - int64 int64_value; - uint32 uint32_value; - uint64 uint64_value; - float float_value; - double double_value; - bool bool_value; - int enum_value; - std::string* string_value; - MessageLite* message_value; - LazyMessageExtension* lazymessage_value; - - RepeatedField* repeated_int32_value; - RepeatedField* repeated_int64_value; - RepeatedField* repeated_uint32_value; - RepeatedField* repeated_uint64_value; - RepeatedField* repeated_float_value; - RepeatedField* repeated_double_value; - RepeatedField* repeated_bool_value; - RepeatedField* repeated_enum_value; - RepeatedPtrField* repeated_string_value; - RepeatedPtrField* repeated_message_value; - }; - - FieldType type; - bool is_repeated; - - // For singular types, indicates if the extension is "cleared". This - // happens when an extension is set and then later cleared by the caller. - // We want to keep the Extension object around for reuse, so instead of - // removing it from the map, we just set is_cleared = true. This has no - // meaning for repeated types; for those, the size of the RepeatedField - // simply becomes zero when cleared. - bool is_cleared : 4; - - // For singular message types, indicates whether lazy parsing is enabled - // for this extension. This field is only valid when type == TYPE_MESSAGE - // and !is_repeated because we only support lazy parsing for singular - // message types currently. If is_lazy = true, the extension is stored in - // lazymessage_value. Otherwise, the extension will be message_value. - bool is_lazy : 4; - - // For repeated types, this indicates if the [packed=true] option is set. - bool is_packed; - - // For packed fields, the size of the packed data is recorded here when - // ByteSize() is called then used during serialization. - // TODO(kenton): Use atomic when C++ supports it. - mutable int cached_size; - - // The descriptor for this extension, if one exists and is known. May be - // NULL. Must not be NULL if the descriptor for the extension does not - // live in the same pool as the descriptor for the containing type. - const FieldDescriptor* descriptor; - - // Some helper methods for operations on a single Extension. - uint8* InternalSerializeFieldWithCachedSizesToArray( - int number, uint8* target, io::EpsCopyOutputStream* stream) const; - uint8* InternalSerializeMessageSetItemWithCachedSizesToArray( - int number, uint8* target, io::EpsCopyOutputStream* stream) const; - size_t ByteSize(int number) const; - size_t MessageSetItemByteSize(int number) const; - void Clear(); - int GetSize() const; - void Free(); - size_t SpaceUsedExcludingSelfLong() const; - bool IsInitialized() const; - }; - - // The Extension struct is small enough to be passed by value, so we use it - // directly as the value type in mappings rather than use pointers. We use - // sorted maps rather than hash-maps because we expect most ExtensionSets will - // only contain a small number of extension. Also, we want AppendToList and - // deterministic serialization to order fields by field number. - - struct KeyValue { - int first; - Extension second; - - struct FirstComparator { - bool operator()(const KeyValue& lhs, const KeyValue& rhs) const { - return lhs.first < rhs.first; - } - bool operator()(const KeyValue& lhs, int key) const { - return lhs.first < key; - } - bool operator()(int key, const KeyValue& rhs) const { - return key < rhs.first; - } - }; - }; - - typedef std::map LargeMap; - - // Wrapper API that switches between flat-map and LargeMap. - - // Finds a key (if present) in the ExtensionSet. - const Extension* FindOrNull(int key) const; - Extension* FindOrNull(int key); - - // Helper-functions that only inspect the LargeMap. - const Extension* FindOrNullInLargeMap(int key) const; - Extension* FindOrNullInLargeMap(int key); - - // Inserts a new (key, Extension) into the ExtensionSet (and returns true), or - // finds the already-existing Extension for that key (returns false). - // The Extension* will point to the new-or-found Extension. - std::pair Insert(int key); - - // Grows the flat_capacity_. - // If flat_capacity_ > kMaximumFlatCapacity, converts to LargeMap. - void GrowCapacity(size_t minimum_new_capacity); - static constexpr uint16 kMaximumFlatCapacity = 256; - bool is_large() const { return flat_capacity_ > kMaximumFlatCapacity; } - - // Removes a key from the ExtensionSet. - void Erase(int key); - - size_t Size() const { - return PROTOBUF_PREDICT_FALSE(is_large()) ? map_.large->size() : flat_size_; - } - - // Similar to std::for_each. - // Each Iterator is decomposed into ->first and ->second fields, so - // that the KeyValueFunctor can be agnostic vis-a-vis KeyValue-vs-std::pair. - template - static KeyValueFunctor ForEach(Iterator begin, Iterator end, - KeyValueFunctor func) { - for (Iterator it = begin; it != end; ++it) func(it->first, it->second); - return std::move(func); - } - - // Applies a functor to the pairs in sorted order. - template - KeyValueFunctor ForEach(KeyValueFunctor func) { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - return ForEach(map_.large->begin(), map_.large->end(), std::move(func)); - } - return ForEach(flat_begin(), flat_end(), std::move(func)); - } - - // Applies a functor to the pairs in sorted order. - template - KeyValueFunctor ForEach(KeyValueFunctor func) const { - if (PROTOBUF_PREDICT_FALSE(is_large())) { - return ForEach(map_.large->begin(), map_.large->end(), std::move(func)); - } - return ForEach(flat_begin(), flat_end(), std::move(func)); - } - - // Merges existing Extension from other_extension - void InternalExtensionMergeFrom(int number, const Extension& other_extension); - - // Returns true and fills field_number and extension if extension is found. - // Note to support packed repeated field compatibility, it also fills whether - // the tag on wire is packed, which can be different from - // extension->is_packed (whether packed=true is specified). - bool FindExtensionInfoFromTag(uint32 tag, ExtensionFinder* extension_finder, - int* field_number, ExtensionInfo* extension, - bool* was_packed_on_wire); - - // Returns true and fills extension if extension is found. - // Note to support packed repeated field compatibility, it also fills whether - // the tag on wire is packed, which can be different from - // extension->is_packed (whether packed=true is specified). - bool FindExtensionInfoFromFieldNumber(int wire_type, int field_number, - ExtensionFinder* extension_finder, - ExtensionInfo* extension, - bool* was_packed_on_wire); - - // Parses a single extension from the input. The input should start out - // positioned immediately after the wire tag. This method is called in - // ParseField() after field number and was_packed_on_wire is extracted from - // the wire tag and ExtensionInfo is found by the field number. - bool ParseFieldWithExtensionInfo(int field_number, bool was_packed_on_wire, - const ExtensionInfo& extension, - io::CodedInputStream* input, - FieldSkipper* field_skipper); - - // Like ParseField(), but this method may parse singular message extensions - // lazily depending on the value of FLAGS_eagerly_parse_message_sets. - bool ParseFieldMaybeLazily(int wire_type, int field_number, - io::CodedInputStream* input, - ExtensionFinder* extension_finder, - MessageSetFieldSkipper* field_skipper); - - // Gets the extension with the given number, creating it if it does not - // already exist. Returns true if the extension did not already exist. - bool MaybeNewExtension(int number, const FieldDescriptor* descriptor, - Extension** result); - - // Gets the repeated extension for the given descriptor, creating it if - // it does not exist. - Extension* MaybeNewRepeatedExtension(const FieldDescriptor* descriptor); - - // Parse a single MessageSet item -- called just after the item group start - // tag has been read. - bool ParseMessageSetItemLite(io::CodedInputStream* input, - ExtensionFinder* extension_finder, - FieldSkipper* field_skipper); - // Parse a single MessageSet item -- called just after the item group start - // tag has been read. - bool ParseMessageSetItem(io::CodedInputStream* input, - ExtensionFinder* extension_finder, - MessageSetFieldSkipper* field_skipper); - - bool FindExtension(int wire_type, uint32 field, - const MessageLite* containing_type, - const internal::ParseContext* /*ctx*/, - ExtensionInfo* extension, bool* was_packed_on_wire) { - GeneratedExtensionFinder finder(containing_type); - return FindExtensionInfoFromFieldNumber(wire_type, field, &finder, - extension, was_packed_on_wire); - } - inline bool FindExtension(int wire_type, uint32 field, - const Message* containing_type, - const internal::ParseContext* ctx, - ExtensionInfo* extension, bool* was_packed_on_wire); - // Used for MessageSet only - const char* ParseFieldMaybeLazily(uint64 tag, const char* ptr, - const MessageLite* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx) { - // Lite MessageSet doesn't implement lazy. - return ParseField(tag, ptr, containing_type, metadata, ctx); - } - const char* ParseFieldMaybeLazily(uint64 tag, const char* ptr, - const Message* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx); - const char* ParseMessageSetItem(const char* ptr, - const MessageLite* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx); - const char* ParseMessageSetItem(const char* ptr, - const Message* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx); - - // Implemented in extension_set_inl.h to keep code out of the header file. - template - const char* ParseFieldWithExtensionInfo(int number, bool was_packed_on_wire, - const ExtensionInfo& info, - internal::InternalMetadata* metadata, - const char* ptr, - internal::ParseContext* ctx); - template - const char* ParseMessageSetItemTmpl(const char* ptr, - const Msg* containing_type, - internal::InternalMetadata* metadata, - internal::ParseContext* ctx); - - // Hack: RepeatedPtrFieldBase declares ExtensionSet as a friend. This - // friendship should automatically extend to ExtensionSet::Extension, but - // unfortunately some older compilers (e.g. GCC 3.4.4) do not implement this - // correctly. So, we must provide helpers for calling methods of that - // class. - - // Defined in extension_set_heavy.cc. - static inline size_t RepeatedMessage_SpaceUsedExcludingSelfLong( - RepeatedPtrFieldBase* field); - - KeyValue* flat_begin() { - assert(!is_large()); - return map_.flat; - } - const KeyValue* flat_begin() const { - assert(!is_large()); - return map_.flat; - } - KeyValue* flat_end() { - assert(!is_large()); - return map_.flat + flat_size_; - } - const KeyValue* flat_end() const { - assert(!is_large()); - return map_.flat + flat_size_; - } - - Arena* arena_; - - // Manual memory-management: - // map_.flat is an allocated array of flat_capacity_ elements. - // [map_.flat, map_.flat + flat_size_) is the currently-in-use prefix. - uint16 flat_capacity_; - uint16 flat_size_; - union AllocatedData { - KeyValue* flat; - - // If flat_capacity_ > kMaximumFlatCapacity, switch to LargeMap, - // which guarantees O(n lg n) CPU but larger constant factors. - LargeMap* large; - } map_; - - static void DeleteFlatMap(const KeyValue* flat, uint16 flat_capacity); - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionSet); -}; - -constexpr ExtensionSet::ExtensionSet() - : arena_(nullptr), flat_capacity_(0), flat_size_(0), map_{nullptr} {} - -// These are just for convenience... -inline void ExtensionSet::SetString(int number, FieldType type, - std::string value, - const FieldDescriptor* descriptor) { - MutableString(number, type, descriptor)->assign(std::move(value)); -} -inline void ExtensionSet::SetRepeatedString(int number, int index, - std::string value) { - MutableRepeatedString(number, index)->assign(std::move(value)); -} -inline void ExtensionSet::AddString(int number, FieldType type, - std::string value, - const FieldDescriptor* descriptor) { - AddString(number, type, descriptor)->assign(std::move(value)); -} -// =================================================================== -// Glue for generated extension accessors - -// ------------------------------------------------------------------- -// Template magic - -// First we have a set of classes representing "type traits" for different -// field types. A type traits class knows how to implement basic accessors -// for extensions of a particular type given an ExtensionSet. The signature -// for a type traits class looks like this: -// -// class TypeTraits { -// public: -// typedef ? ConstType; -// typedef ? MutableType; -// // TypeTraits for singular fields and repeated fields will define the -// // symbol "Singular" or "Repeated" respectively. These two symbols will -// // be used in extension accessors to distinguish between singular -// // extensions and repeated extensions. If the TypeTraits for the passed -// // in extension doesn't have the expected symbol defined, it means the -// // user is passing a repeated extension to a singular accessor, or the -// // opposite. In that case the C++ compiler will generate an error -// // message "no matching member function" to inform the user. -// typedef ? Singular -// typedef ? Repeated -// -// static inline ConstType Get(int number, const ExtensionSet& set); -// static inline void Set(int number, ConstType value, ExtensionSet* set); -// static inline MutableType Mutable(int number, ExtensionSet* set); -// -// // Variants for repeated fields. -// static inline ConstType Get(int number, const ExtensionSet& set, -// int index); -// static inline void Set(int number, int index, -// ConstType value, ExtensionSet* set); -// static inline MutableType Mutable(int number, int index, -// ExtensionSet* set); -// static inline void Add(int number, ConstType value, ExtensionSet* set); -// static inline MutableType Add(int number, ExtensionSet* set); -// This is used by the ExtensionIdentifier constructor to register -// the extension at dynamic initialization. -// template -// static void Register(int number, FieldType type, bool is_packed); -// }; -// -// Not all of these methods make sense for all field types. For example, the -// "Mutable" methods only make sense for strings and messages, and the -// repeated methods only make sense for repeated types. So, each type -// traits class implements only the set of methods from this signature that it -// actually supports. This will cause a compiler error if the user tries to -// access an extension using a method that doesn't make sense for its type. -// For example, if "foo" is an extension of type "optional int32", then if you -// try to write code like: -// my_message.MutableExtension(foo) -// you will get a compile error because PrimitiveTypeTraits does not -// have a "Mutable()" method. - -// ------------------------------------------------------------------- -// PrimitiveTypeTraits - -// Since the ExtensionSet has different methods for each primitive type, -// we must explicitly define the methods of the type traits class for each -// known type. -template -class PrimitiveTypeTraits { - public: - typedef Type ConstType; - typedef Type MutableType; - typedef PrimitiveTypeTraits Singular; - - static inline ConstType Get(int number, const ExtensionSet& set, - ConstType default_value); - static inline void Set(int number, FieldType field_type, ConstType value, - ExtensionSet* set); - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, - type, false, is_packed); - } -}; - -template -class RepeatedPrimitiveTypeTraits { - public: - typedef Type ConstType; - typedef Type MutableType; - typedef RepeatedPrimitiveTypeTraits Repeated; - - typedef RepeatedField RepeatedFieldType; - - static inline Type Get(int number, const ExtensionSet& set, int index); - static inline void Set(int number, int index, Type value, ExtensionSet* set); - static inline void Add(int number, FieldType field_type, bool is_packed, - Type value, ExtensionSet* set); - - static inline const RepeatedField& GetRepeated( - int number, const ExtensionSet& set); - static inline RepeatedField* MutableRepeated(int number, - FieldType field_type, - bool is_packed, - ExtensionSet* set); - - static const RepeatedFieldType* GetDefaultRepeatedField(); - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, - type, true, is_packed); - } -}; - -class PROTOBUF_EXPORT RepeatedPrimitiveDefaults { - private: - template - friend class RepeatedPrimitiveTypeTraits; - static const RepeatedPrimitiveDefaults* default_instance(); - RepeatedField default_repeated_field_int32_; - RepeatedField default_repeated_field_int64_; - RepeatedField default_repeated_field_uint32_; - RepeatedField default_repeated_field_uint64_; - RepeatedField default_repeated_field_double_; - RepeatedField default_repeated_field_float_; - RepeatedField default_repeated_field_bool_; -}; - -#define PROTOBUF_DEFINE_PRIMITIVE_TYPE(TYPE, METHOD) \ - template <> \ - inline TYPE PrimitiveTypeTraits::Get( \ - int number, const ExtensionSet& set, TYPE default_value) { \ - return set.Get##METHOD(number, default_value); \ - } \ - template <> \ - inline void PrimitiveTypeTraits::Set(int number, FieldType field_type, \ - TYPE value, ExtensionSet* set) { \ - set->Set##METHOD(number, field_type, value, NULL); \ - } \ - \ - template <> \ - inline TYPE RepeatedPrimitiveTypeTraits::Get( \ - int number, const ExtensionSet& set, int index) { \ - return set.GetRepeated##METHOD(number, index); \ - } \ - template <> \ - inline void RepeatedPrimitiveTypeTraits::Set( \ - int number, int index, TYPE value, ExtensionSet* set) { \ - set->SetRepeated##METHOD(number, index, value); \ - } \ - template <> \ - inline void RepeatedPrimitiveTypeTraits::Add( \ - int number, FieldType field_type, bool is_packed, TYPE value, \ - ExtensionSet* set) { \ - set->Add##METHOD(number, field_type, is_packed, value, NULL); \ - } \ - template <> \ - inline const RepeatedField* \ - RepeatedPrimitiveTypeTraits::GetDefaultRepeatedField() { \ - return &RepeatedPrimitiveDefaults::default_instance() \ - ->default_repeated_field_##TYPE##_; \ - } \ - template <> \ - inline const RepeatedField& \ - RepeatedPrimitiveTypeTraits::GetRepeated(int number, \ - const ExtensionSet& set) { \ - return *reinterpret_cast*>( \ - set.GetRawRepeatedField(number, GetDefaultRepeatedField())); \ - } \ - template <> \ - inline RepeatedField* \ - RepeatedPrimitiveTypeTraits::MutableRepeated( \ - int number, FieldType field_type, bool is_packed, ExtensionSet* set) { \ - return reinterpret_cast*>( \ - set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); \ - } - -PROTOBUF_DEFINE_PRIMITIVE_TYPE(int32, Int32) -PROTOBUF_DEFINE_PRIMITIVE_TYPE(int64, Int64) -PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint32, UInt32) -PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint64, UInt64) -PROTOBUF_DEFINE_PRIMITIVE_TYPE(float, Float) -PROTOBUF_DEFINE_PRIMITIVE_TYPE(double, Double) -PROTOBUF_DEFINE_PRIMITIVE_TYPE(bool, Bool) - -#undef PROTOBUF_DEFINE_PRIMITIVE_TYPE - -// ------------------------------------------------------------------- -// StringTypeTraits - -// Strings support both Set() and Mutable(). -class PROTOBUF_EXPORT StringTypeTraits { - public: - typedef const std::string& ConstType; - typedef std::string* MutableType; - typedef StringTypeTraits Singular; - - static inline const std::string& Get(int number, const ExtensionSet& set, - ConstType default_value) { - return set.GetString(number, default_value); - } - static inline void Set(int number, FieldType field_type, - const std::string& value, ExtensionSet* set) { - set->SetString(number, field_type, value, NULL); - } - static inline std::string* Mutable(int number, FieldType field_type, - ExtensionSet* set) { - return set->MutableString(number, field_type, NULL); - } - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, - type, false, is_packed); - } -}; - -class PROTOBUF_EXPORT RepeatedStringTypeTraits { - public: - typedef const std::string& ConstType; - typedef std::string* MutableType; - typedef RepeatedStringTypeTraits Repeated; - - typedef RepeatedPtrField RepeatedFieldType; - - static inline const std::string& Get(int number, const ExtensionSet& set, - int index) { - return set.GetRepeatedString(number, index); - } - static inline void Set(int number, int index, const std::string& value, - ExtensionSet* set) { - set->SetRepeatedString(number, index, value); - } - static inline std::string* Mutable(int number, int index, ExtensionSet* set) { - return set->MutableRepeatedString(number, index); - } - static inline void Add(int number, FieldType field_type, bool /*is_packed*/, - const std::string& value, ExtensionSet* set) { - set->AddString(number, field_type, value, NULL); - } - static inline std::string* Add(int number, FieldType field_type, - ExtensionSet* set) { - return set->AddString(number, field_type, NULL); - } - static inline const RepeatedPtrField& GetRepeated( - int number, const ExtensionSet& set) { - return *reinterpret_cast*>( - set.GetRawRepeatedField(number, GetDefaultRepeatedField())); - } - - static inline RepeatedPtrField* MutableRepeated( - int number, FieldType field_type, bool is_packed, ExtensionSet* set) { - return reinterpret_cast*>( - set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); - } - - static const RepeatedFieldType* GetDefaultRepeatedField(); - - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, - type, true, is_packed); - } - - private: - static void InitializeDefaultRepeatedFields(); - static void DestroyDefaultRepeatedFields(); -}; - -// ------------------------------------------------------------------- -// EnumTypeTraits - -// ExtensionSet represents enums using integers internally, so we have to -// static_cast around. -template -class EnumTypeTraits { - public: - typedef Type ConstType; - typedef Type MutableType; - typedef EnumTypeTraits Singular; - - static inline ConstType Get(int number, const ExtensionSet& set, - ConstType default_value) { - return static_cast(set.GetEnum(number, default_value)); - } - static inline void Set(int number, FieldType field_type, ConstType value, - ExtensionSet* set) { - GOOGLE_DCHECK(IsValid(value)); - set->SetEnum(number, field_type, value, NULL); - } - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterEnumExtension(&ExtendeeT::default_instance(), number, - type, false, is_packed, IsValid); - } -}; - -template -class RepeatedEnumTypeTraits { - public: - typedef Type ConstType; - typedef Type MutableType; - typedef RepeatedEnumTypeTraits Repeated; - - typedef RepeatedField RepeatedFieldType; - - static inline ConstType Get(int number, const ExtensionSet& set, int index) { - return static_cast(set.GetRepeatedEnum(number, index)); - } - static inline void Set(int number, int index, ConstType value, - ExtensionSet* set) { - GOOGLE_DCHECK(IsValid(value)); - set->SetRepeatedEnum(number, index, value); - } - static inline void Add(int number, FieldType field_type, bool is_packed, - ConstType value, ExtensionSet* set) { - GOOGLE_DCHECK(IsValid(value)); - set->AddEnum(number, field_type, is_packed, value, NULL); - } - static inline const RepeatedField& GetRepeated( - int number, const ExtensionSet& set) { - // Hack: the `Extension` struct stores a RepeatedField for enums. - // RepeatedField cannot implicitly convert to RepeatedField - // so we need to do some casting magic. See message.h for similar - // contortions for non-extension fields. - return *reinterpret_cast*>( - set.GetRawRepeatedField(number, GetDefaultRepeatedField())); - } - - static inline RepeatedField* MutableRepeated(int number, - FieldType field_type, - bool is_packed, - ExtensionSet* set) { - return reinterpret_cast*>( - set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); - } - - static const RepeatedFieldType* GetDefaultRepeatedField() { - // Hack: as noted above, repeated enum fields are internally stored as a - // RepeatedField. We need to be able to instantiate global static - // objects to return as default (empty) repeated fields on non-existent - // extensions. We would not be able to know a-priori all of the enum types - // (values of |Type|) to instantiate all of these, so we just re-use int32's - // default repeated field object. - return reinterpret_cast*>( - RepeatedPrimitiveTypeTraits::GetDefaultRepeatedField()); - } - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterEnumExtension(&ExtendeeT::default_instance(), number, - type, true, is_packed, IsValid); - } -}; - -// ------------------------------------------------------------------- -// MessageTypeTraits - -// ExtensionSet guarantees that when manipulating extensions with message -// types, the implementation used will be the compiled-in class representing -// that type. So, we can static_cast down to the exact type we expect. -template -class MessageTypeTraits { - public: - typedef const Type& ConstType; - typedef Type* MutableType; - typedef MessageTypeTraits Singular; - - static inline ConstType Get(int number, const ExtensionSet& set, - ConstType default_value) { - return static_cast(set.GetMessage(number, default_value)); - } - static inline MutableType Mutable(int number, FieldType field_type, - ExtensionSet* set) { - return static_cast(set->MutableMessage( - number, field_type, Type::default_instance(), NULL)); - } - static inline void SetAllocated(int number, FieldType field_type, - MutableType message, ExtensionSet* set) { - set->SetAllocatedMessage(number, field_type, NULL, message); - } - static inline void UnsafeArenaSetAllocated(int number, FieldType field_type, - MutableType message, - ExtensionSet* set) { - set->UnsafeArenaSetAllocatedMessage(number, field_type, NULL, message); - } - static inline MutableType Release(int number, FieldType /* field_type */, - ExtensionSet* set) { - return static_cast( - set->ReleaseMessage(number, Type::default_instance())); - } - static inline MutableType UnsafeArenaRelease(int number, - FieldType /* field_type */, - ExtensionSet* set) { - return static_cast( - set->UnsafeArenaReleaseMessage(number, Type::default_instance())); - } - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterMessageExtension(&ExtendeeT::default_instance(), - number, type, false, is_packed, - &Type::default_instance()); - } -}; - -// forward declaration -class RepeatedMessageGenericTypeTraits; - -template -class RepeatedMessageTypeTraits { - public: - typedef const Type& ConstType; - typedef Type* MutableType; - typedef RepeatedMessageTypeTraits Repeated; - - typedef RepeatedPtrField RepeatedFieldType; - - static inline ConstType Get(int number, const ExtensionSet& set, int index) { - return static_cast(set.GetRepeatedMessage(number, index)); - } - static inline MutableType Mutable(int number, int index, ExtensionSet* set) { - return static_cast(set->MutableRepeatedMessage(number, index)); - } - static inline MutableType Add(int number, FieldType field_type, - ExtensionSet* set) { - return static_cast( - set->AddMessage(number, field_type, Type::default_instance(), NULL)); - } - static inline const RepeatedPtrField& GetRepeated( - int number, const ExtensionSet& set) { - // See notes above in RepeatedEnumTypeTraits::GetRepeated(): same - // casting hack applies here, because a RepeatedPtrField - // cannot naturally become a RepeatedPtrType even though Type is - // presumably a message. google::protobuf::Message goes through similar contortions - // with a reinterpret_cast<>. - return *reinterpret_cast*>( - set.GetRawRepeatedField(number, GetDefaultRepeatedField())); - } - static inline RepeatedPtrField* MutableRepeated(int number, - FieldType field_type, - bool is_packed, - ExtensionSet* set) { - return reinterpret_cast*>( - set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); - } - - static const RepeatedFieldType* GetDefaultRepeatedField(); - template - static void Register(int number, FieldType type, bool is_packed) { - ExtensionSet::RegisterMessageExtension(&ExtendeeT::default_instance(), - number, type, true, is_packed, - &Type::default_instance()); - } -}; - -template -inline const typename RepeatedMessageTypeTraits::RepeatedFieldType* -RepeatedMessageTypeTraits::GetDefaultRepeatedField() { - static auto instance = OnShutdownDelete(new RepeatedFieldType); - return instance; -} - -// ------------------------------------------------------------------- -// ExtensionIdentifier - -// This is the type of actual extension objects. E.g. if you have: -// extends Foo with optional int32 bar = 1234; -// then "bar" will be defined in C++ as: -// ExtensionIdentifier, 5, false> bar(1234); -// -// Note that we could, in theory, supply the field number as a template -// parameter, and thus make an instance of ExtensionIdentifier have no -// actual contents. However, if we did that, then using an extension -// identifier would not necessarily cause the compiler to output any sort -// of reference to any symbol defined in the extension's .pb.o file. Some -// linkers will actually drop object files that are not explicitly referenced, -// but that would be bad because it would cause this extension to not be -// registered at static initialization, and therefore using it would crash. - -template -class ExtensionIdentifier { - public: - typedef TypeTraitsType TypeTraits; - typedef ExtendeeType Extendee; - - ExtensionIdentifier(int number, typename TypeTraits::ConstType default_value) - : number_(number), default_value_(default_value) { - Register(number); - } - inline int number() const { return number_; } - typename TypeTraits::ConstType default_value() const { - return default_value_; - } - - static void Register(int number) { - TypeTraits::template Register(number, field_type, is_packed); - } - - private: - const int number_; - typename TypeTraits::ConstType default_value_; -}; - -// ------------------------------------------------------------------- -// Generated accessors - -// This macro should be expanded in the context of a generated type which -// has extensions. -// -// We use "_proto_TypeTraits" as a type name below because "TypeTraits" -// causes problems if the class has a nested message or enum type with that -// name and "_TypeTraits" is technically reserved for the C++ library since -// it starts with an underscore followed by a capital letter. -// -// For similar reason, we use "_field_type" and "_is_packed" as parameter names -// below, so that "field_type" and "is_packed" can be used as field names. -#define GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(CLASSNAME) \ - /* Has, Size, Clear */ \ - template \ - inline bool HasExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ - return _extensions_.Has(id.number()); \ - } \ - \ - template \ - inline void ClearExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ - _extensions_.ClearExtension(id.number()); \ - } \ - \ - template \ - inline int ExtensionSize( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ - return _extensions_.ExtensionSize(id.number()); \ - } \ - \ - /* Singular accessors */ \ - template \ - inline typename _proto_TypeTraits::Singular::ConstType GetExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ - return _proto_TypeTraits::Get(id.number(), _extensions_, \ - id.default_value()); \ - } \ - \ - template \ - inline typename _proto_TypeTraits::Singular::MutableType MutableExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ - return _proto_TypeTraits::Mutable(id.number(), _field_type, \ - &_extensions_); \ - } \ - \ - template \ - inline void SetExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ - typename _proto_TypeTraits::Singular::ConstType value) { \ - _proto_TypeTraits::Set(id.number(), _field_type, value, &_extensions_); \ - } \ - \ - template \ - inline void SetAllocatedExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ - typename _proto_TypeTraits::Singular::MutableType value) { \ - _proto_TypeTraits::SetAllocated(id.number(), _field_type, value, \ - &_extensions_); \ - } \ - template \ - inline void UnsafeArenaSetAllocatedExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ - typename _proto_TypeTraits::Singular::MutableType value) { \ - _proto_TypeTraits::UnsafeArenaSetAllocated(id.number(), _field_type, \ - value, &_extensions_); \ - } \ - template \ - inline typename _proto_TypeTraits::Singular::MutableType ReleaseExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ - return _proto_TypeTraits::Release(id.number(), _field_type, \ - &_extensions_); \ - } \ - template \ - inline typename _proto_TypeTraits::Singular::MutableType \ - UnsafeArenaReleaseExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ - return _proto_TypeTraits::UnsafeArenaRelease(id.number(), _field_type, \ - &_extensions_); \ - } \ - \ - /* Repeated accessors */ \ - template \ - inline typename _proto_TypeTraits::Repeated::ConstType GetExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ - int index) const { \ - return _proto_TypeTraits::Get(id.number(), _extensions_, index); \ - } \ - \ - template \ - inline typename _proto_TypeTraits::Repeated::MutableType MutableExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ - int index) { \ - return _proto_TypeTraits::Mutable(id.number(), index, &_extensions_); \ - } \ - \ - template \ - inline void SetExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ - int index, typename _proto_TypeTraits::Repeated::ConstType value) { \ - _proto_TypeTraits::Set(id.number(), index, value, &_extensions_); \ - } \ - \ - template \ - inline typename _proto_TypeTraits::Repeated::MutableType AddExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ - return _proto_TypeTraits::Add(id.number(), _field_type, &_extensions_); \ - } \ - \ - template \ - inline void AddExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ - typename _proto_TypeTraits::Repeated::ConstType value) { \ - _proto_TypeTraits::Add(id.number(), _field_type, _is_packed, value, \ - &_extensions_); \ - } \ - \ - template \ - inline const typename _proto_TypeTraits::Repeated::RepeatedFieldType& \ - GetRepeatedExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ - return _proto_TypeTraits::GetRepeated(id.number(), _extensions_); \ - } \ - \ - template \ - inline typename _proto_TypeTraits::Repeated::RepeatedFieldType* \ - MutableRepeatedExtension( \ - const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ - CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ - return _proto_TypeTraits::MutableRepeated(id.number(), _field_type, \ - _is_packed, &_extensions_); \ - } - -} // namespace internal - -// Call this function to ensure that this extensions's reflection is linked into -// the binary: -// -// google::protobuf::LinkExtensionReflection(Foo::my_extension); -// -// This will ensure that the following lookup will succeed: -// -// DescriptorPool::generated_pool()->FindExtensionByName("Foo.my_extension"); -// -// This is often relevant for parsing extensions in text mode. -// -// As a side-effect, it will also guarantee that anything else from the same -// .proto file will also be available for lookup in the generated pool. -// -// This function does not actually register the extension, so it does not need -// to be called before the lookup. However it does need to occur in a function -// that cannot be stripped from the binary (ie. it must be reachable from main). -// -// Best practice is to call this function as close as possible to where the -// reflection is actually needed. This function is very cheap to call, so you -// should not need to worry about its runtime overhead except in tight loops (on -// x86-64 it compiles into two "mov" instructions). -template -void LinkExtensionReflection( - const google::protobuf::internal::ExtensionIdentifier< - ExtendeeType, TypeTraitsType, field_type, is_packed>& extension) { - internal::StrongReference(extension); -} - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_EXTENSION_SET_H__ diff --git a/third_party/protobuf-lite/google/protobuf/extension_set_inl.h b/third_party/protobuf-lite/google/protobuf/extension_set_inl.h deleted file mode 100644 index 074784b96..000000000 --- a/third_party/protobuf-lite/google/protobuf/extension_set_inl.h +++ /dev/null @@ -1,276 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_EXTENSION_SET_INL_H__ -#define GOOGLE_PROTOBUF_EXTENSION_SET_INL_H__ - -#include -#include -#include - -namespace google { -namespace protobuf { -namespace internal { - -template -const char* ExtensionSet::ParseFieldWithExtensionInfo( - int number, bool was_packed_on_wire, const ExtensionInfo& extension, - InternalMetadata* metadata, const char* ptr, internal::ParseContext* ctx) { - if (was_packed_on_wire) { - switch (extension.type) { -#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: \ - return internal::Packed##CPP_CAMELCASE##Parser( \ - MutableRawRepeatedField(number, extension.type, extension.is_packed, \ - extension.descriptor), \ - ptr, ctx); - HANDLE_TYPE(INT32, Int32); - HANDLE_TYPE(INT64, Int64); - HANDLE_TYPE(UINT32, UInt32); - HANDLE_TYPE(UINT64, UInt64); - HANDLE_TYPE(SINT32, SInt32); - HANDLE_TYPE(SINT64, SInt64); - HANDLE_TYPE(FIXED32, Fixed32); - HANDLE_TYPE(FIXED64, Fixed64); - HANDLE_TYPE(SFIXED32, SFixed32); - HANDLE_TYPE(SFIXED64, SFixed64); - HANDLE_TYPE(FLOAT, Float); - HANDLE_TYPE(DOUBLE, Double); - HANDLE_TYPE(BOOL, Bool); -#undef HANDLE_TYPE - - case WireFormatLite::TYPE_ENUM: - return internal::PackedEnumParserArg( - MutableRawRepeatedField(number, extension.type, extension.is_packed, - extension.descriptor), - ptr, ctx, extension.enum_validity_check.func, - extension.enum_validity_check.arg, metadata, number); - case WireFormatLite::TYPE_STRING: - case WireFormatLite::TYPE_BYTES: - case WireFormatLite::TYPE_GROUP: - case WireFormatLite::TYPE_MESSAGE: - GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed."; - break; - } - } else { - switch (extension.type) { -#define HANDLE_VARINT_TYPE(UPPERCASE, CPP_CAMELCASE) \ - case WireFormatLite::TYPE_##UPPERCASE: { \ - uint64 value; \ - ptr = VarintParse(ptr, &value); \ - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); \ - if (extension.is_repeated) { \ - Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ - extension.is_packed, value, extension.descriptor); \ - } else { \ - Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \ - extension.descriptor); \ - } \ - } break - - HANDLE_VARINT_TYPE(INT32, Int32); - HANDLE_VARINT_TYPE(INT64, Int64); - HANDLE_VARINT_TYPE(UINT32, UInt32); - HANDLE_VARINT_TYPE(UINT64, UInt64); - HANDLE_VARINT_TYPE(BOOL, Bool); -#undef HANDLE_VARINT_TYPE -#define HANDLE_SVARINT_TYPE(UPPERCASE, CPP_CAMELCASE, SIZE) \ - case WireFormatLite::TYPE_##UPPERCASE: { \ - uint64 val; \ - ptr = VarintParse(ptr, &val); \ - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); \ - auto value = WireFormatLite::ZigZagDecode##SIZE(val); \ - if (extension.is_repeated) { \ - Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ - extension.is_packed, value, extension.descriptor); \ - } else { \ - Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \ - extension.descriptor); \ - } \ - } break - - HANDLE_SVARINT_TYPE(SINT32, Int32, 32); - HANDLE_SVARINT_TYPE(SINT64, Int64, 64); -#undef HANDLE_SVARINT_TYPE -#define HANDLE_FIXED_TYPE(UPPERCASE, CPP_CAMELCASE, CPPTYPE) \ - case WireFormatLite::TYPE_##UPPERCASE: { \ - auto value = UnalignedLoad(ptr); \ - ptr += sizeof(CPPTYPE); \ - if (extension.is_repeated) { \ - Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \ - extension.is_packed, value, extension.descriptor); \ - } else { \ - Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \ - extension.descriptor); \ - } \ - } break - - HANDLE_FIXED_TYPE(FIXED32, UInt32, uint32); - HANDLE_FIXED_TYPE(FIXED64, UInt64, uint64); - HANDLE_FIXED_TYPE(SFIXED32, Int32, int32); - HANDLE_FIXED_TYPE(SFIXED64, Int64, int64); - HANDLE_FIXED_TYPE(FLOAT, Float, float); - HANDLE_FIXED_TYPE(DOUBLE, Double, double); -#undef HANDLE_FIXED_TYPE - - case WireFormatLite::TYPE_ENUM: { - uint64 val; - ptr = VarintParse(ptr, &val); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - int value = val; - - if (!extension.enum_validity_check.func( - extension.enum_validity_check.arg, value)) { - WriteVarint(number, val, metadata->mutable_unknown_fields()); - } else if (extension.is_repeated) { - AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, value, - extension.descriptor); - } else { - SetEnum(number, WireFormatLite::TYPE_ENUM, value, - extension.descriptor); - } - break; - } - - case WireFormatLite::TYPE_BYTES: - case WireFormatLite::TYPE_STRING: { - std::string* value = - extension.is_repeated - ? AddString(number, WireFormatLite::TYPE_STRING, - extension.descriptor) - : MutableString(number, WireFormatLite::TYPE_STRING, - extension.descriptor); - int size = ReadSize(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - return ctx->ReadString(ptr, size, value); - } - - case WireFormatLite::TYPE_GROUP: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_info.prototype, - extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_GROUP, - *extension.message_info.prototype, - extension.descriptor); - uint32 tag = (number << 3) + WireFormatLite::WIRETYPE_START_GROUP; - return ctx->ParseGroup(value, ptr, tag); - } - - case WireFormatLite::TYPE_MESSAGE: { - MessageLite* value = - extension.is_repeated - ? AddMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_info.prototype, - extension.descriptor) - : MutableMessage(number, WireFormatLite::TYPE_MESSAGE, - *extension.message_info.prototype, - extension.descriptor); - return ctx->ParseMessage(value, ptr); - } - } - } - return ptr; -} - -template -const char* ExtensionSet::ParseMessageSetItemTmpl( - const char* ptr, const Msg* containing_type, - internal::InternalMetadata* metadata, internal::ParseContext* ctx) { - std::string payload; - uint32 type_id = 0; - bool payload_read = false; - while (!ctx->Done(&ptr)) { - uint32 tag = static_cast(*ptr++); - if (tag == WireFormatLite::kMessageSetTypeIdTag) { - uint64 tmp; - ptr = ParseBigVarint(ptr, &tmp); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - type_id = tmp; - if (payload_read) { - ExtensionInfo extension; - bool was_packed_on_wire; - if (!FindExtension(2, type_id, containing_type, ctx, &extension, - &was_packed_on_wire)) { - WriteLengthDelimited(type_id, payload, - metadata->mutable_unknown_fields()); - } else { - MessageLite* value = - extension.is_repeated - ? AddMessage(type_id, WireFormatLite::TYPE_MESSAGE, - *extension.message_info.prototype, - extension.descriptor) - : MutableMessage(type_id, WireFormatLite::TYPE_MESSAGE, - *extension.message_info.prototype, - extension.descriptor); - - const char* p; - // We can't use regular parse from string as we have to track - // proper recursion depth and descriptor pools. - ParseContext tmp_ctx(ctx->depth(), false, &p, payload); - tmp_ctx.data().pool = ctx->data().pool; - tmp_ctx.data().factory = ctx->data().factory; - GOOGLE_PROTOBUF_PARSER_ASSERT(value->_InternalParse(p, &tmp_ctx) && - tmp_ctx.EndedAtLimit()); - } - type_id = 0; - } - } else if (tag == WireFormatLite::kMessageSetMessageTag) { - if (type_id != 0) { - ptr = ParseFieldMaybeLazily(static_cast(type_id) * 8 + 2, ptr, - containing_type, metadata, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); - type_id = 0; - } else { - int32 size = ReadSize(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - ptr = ctx->ReadString(ptr, size, &payload); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - payload_read = true; - } - } else { - ptr = ReadTag(ptr - 1, &tag); - if (tag == 0 || (tag & 7) == 4) { - ctx->SetLastTag(tag); - return ptr; - } - ptr = ParseField(tag, ptr, containing_type, metadata, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - } - } - return ptr; -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_EXTENSION_SET_INL_H__ diff --git a/third_party/protobuf-lite/google/protobuf/generated_enum_reflection.h b/third_party/protobuf-lite/google/protobuf/generated_enum_reflection.h deleted file mode 100644 index 5debc0a25..000000000 --- a/third_party/protobuf-lite/google/protobuf/generated_enum_reflection.h +++ /dev/null @@ -1,98 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: jasonh@google.com (Jason Hsueh) -// -// This header is logically internal, but is made public because it is used -// from protocol-compiler-generated code, which may reside in other components. -// It provides reflection support for generated enums, and is included in -// generated .pb.h files and should have minimal dependencies. The methods are -// implemented in generated_message_reflection.cc. - -#ifndef GOOGLE_PROTOBUF_GENERATED_ENUM_REFLECTION_H__ -#define GOOGLE_PROTOBUF_GENERATED_ENUM_REFLECTION_H__ - -#include - -#include -#include -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -#include - -namespace google { -namespace protobuf { -class EnumDescriptor; -} // namespace protobuf -} // namespace google - -namespace google { -namespace protobuf { - -// Returns the EnumDescriptor for enum type E, which must be a -// proto-declared enum type. Code generated by the protocol compiler -// will include specializations of this template for each enum type declared. -template -const EnumDescriptor* GetEnumDescriptor(); - -namespace internal { - -// Helper for EnumType_Parse functions: try to parse the string 'name' as -// an enum name of the given type, returning true and filling in value on -// success, or returning false and leaving value unchanged on failure. -PROTOBUF_EXPORT bool ParseNamedEnum(const EnumDescriptor* descriptor, - ConstStringParam name, int* value); - -template -bool ParseNamedEnum(const EnumDescriptor* descriptor, ConstStringParam name, - EnumType* value) { - int tmp; - if (!ParseNamedEnum(descriptor, name, &tmp)) return false; - *value = static_cast(tmp); - return true; -} - -// Just a wrapper around printing the name of a value. The main point of this -// function is not to be inlined, so that you can do this without including -// descriptor.h. -PROTOBUF_EXPORT const std::string& NameOfEnum(const EnumDescriptor* descriptor, - int value); - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_GENERATED_ENUM_REFLECTION_H__ diff --git a/third_party/protobuf-lite/google/protobuf/generated_enum_util.h b/third_party/protobuf-lite/google/protobuf/generated_enum_util.h deleted file mode 100644 index f1002e2d1..000000000 --- a/third_party/protobuf-lite/google/protobuf/generated_enum_util.h +++ /dev/null @@ -1,83 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_GENERATED_ENUM_UTIL_H__ -#define GOOGLE_PROTOBUF_GENERATED_ENUM_UTIL_H__ - -#include - -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { - -// This type trait can be used to cause templates to only match proto2 enum -// types. -template -struct is_proto_enum : ::std::false_type {}; - -namespace internal { - -// The table entry format for storing enum name-to-value mapping used with lite -// protos. This struct and the following related functions should only be used -// by protobuf generated code. -struct EnumEntry { - StringPiece name; - int value; -}; - -// Looks up a numeric enum value given the string name. -PROTOBUF_EXPORT bool LookUpEnumValue(const EnumEntry* enums, size_t size, - StringPiece name, int* value); - -// Looks up an enum name given the numeric value. -PROTOBUF_EXPORT int LookUpEnumName(const EnumEntry* enums, - const int* sorted_indices, size_t size, - int value); - -// Initializes the list of enum names in std::string form. -PROTOBUF_EXPORT bool InitializeEnumStrings( - const EnumEntry* enums, const int* sorted_indices, size_t size, - internal::ExplicitlyConstructed* enum_strings); - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_GENERATED_ENUM_UTIL_H__ diff --git a/third_party/protobuf-lite/google/protobuf/generated_message_table_driven.h b/third_party/protobuf-lite/google/protobuf/generated_message_table_driven.h deleted file mode 100644 index 731d6c521..000000000 --- a/third_party/protobuf-lite/google/protobuf/generated_message_table_driven.h +++ /dev/null @@ -1,336 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_H__ -#define GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_H__ - -#include -#include -#include -#include -#include - -// We require C++11 and Clang to use constexpr for variables, as GCC 4.8 -// requires constexpr to be consistent between declarations of variables -// unnecessarily (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58541). -// VS 2017 Update 3 also supports this usage of constexpr. -#if defined(__clang__) || (defined(_MSC_VER) && _MSC_VER >= 1911) -#define PROTOBUF_CONSTEXPR_VAR constexpr -#else // !__clang__ -#define PROTOBUF_CONSTEXPR_VAR -#endif // !_clang - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -#include - -namespace google { -namespace protobuf { -namespace internal { - -// Processing-type masks. -static constexpr const unsigned char kOneofMask = 0x40; -static constexpr const unsigned char kRepeatedMask = 0x20; -// Mask for the raw type: either a WireFormatLite::FieldType or one of the -// ProcessingTypes below, without the oneof or repeated flag. -static constexpr const unsigned char kTypeMask = 0x1f; - -// Wire type masks. -static constexpr const unsigned char kNotPackedMask = 0x10; -static constexpr const unsigned char kInvalidMask = 0x20; - -enum ProcessingTypes { - TYPE_STRING_CORD = 19, - TYPE_STRING_STRING_PIECE = 20, - TYPE_BYTES_CORD = 21, - TYPE_BYTES_STRING_PIECE = 22, - TYPE_MAP = 23, -}; - -static_assert(TYPE_MAP < kRepeatedMask, "Invalid enum"); - -struct PROTOBUF_EXPORT FieldMetadata { - uint32 offset; // offset of this field in the struct - uint32 tag; // field * 8 + wire_type - // byte offset * 8 + bit_offset; - // if the high bit is set then this is the byte offset of the oneof_case - // for this field. - uint32 has_offset; - uint32 type; // the type of this field. - const void* ptr; // auxiliary data - - // From the serializer point of view each fundamental type can occur in - // 4 different ways. For simplicity we treat all combinations as a cartesion - // product although not all combinations are allowed. - enum FieldTypeClass { - kPresence, - kNoPresence, - kRepeated, - kPacked, - kOneOf, - kNumTypeClasses // must be last enum - }; - // C++ protobuf has 20 fundamental types, were we added Cord and StringPiece - // and also distinguish the same types if they have different wire format. - enum { - kCordType = 19, - kStringPieceType = 20, - kNumTypes = 20, - kSpecial = kNumTypes * kNumTypeClasses, - }; - - static int CalculateType(int fundamental_type, FieldTypeClass type_class); -}; - -// TODO(ckennelly): Add a static assertion to ensure that these masks do not -// conflict with wiretypes. - -// ParseTableField is kept small to help simplify instructions for computing -// offsets, as we will always need this information to parse a field. -// Additional data, needed for some types, is stored in -// AuxiliaryParseTableField. -struct ParseTableField { - uint32 offset; - // The presence_index ordinarily represents a has_bit index, but for fields - // inside a oneof it represents the index in _oneof_case_. - uint32 presence_index; - unsigned char normal_wiretype; - unsigned char packed_wiretype; - - // processing_type is given by: - // (FieldDescriptor->type() << 1) | FieldDescriptor->is_packed() - unsigned char processing_type; - - unsigned char tag_size; -}; - -struct ParseTable; - -union AuxiliaryParseTableField { - typedef bool (*EnumValidator)(int); - - // Enums - struct enum_aux { - EnumValidator validator; - }; - enum_aux enums; - // Group, messages - struct message_aux { - // ExplicitlyInitialized -> T requires a reinterpret_cast, which prevents - // the tables from being constructed as a constexpr. We use void to avoid - // the cast. - const void* default_message_void; - const MessageLite* default_message() const { - return static_cast(default_message_void); - } - }; - message_aux messages; - // Strings - struct string_aux { - const void* default_ptr; - const char* field_name; - }; - string_aux strings; - - struct map_aux { - bool (*parse_map)(io::CodedInputStream*, void*); - }; - map_aux maps; - - AuxiliaryParseTableField() = default; - constexpr AuxiliaryParseTableField(AuxiliaryParseTableField::enum_aux e) - : enums(e) {} - constexpr AuxiliaryParseTableField(AuxiliaryParseTableField::message_aux m) - : messages(m) {} - constexpr AuxiliaryParseTableField(AuxiliaryParseTableField::string_aux s) - : strings(s) {} - constexpr AuxiliaryParseTableField(AuxiliaryParseTableField::map_aux m) - : maps(m) {} -}; - -struct ParseTable { - const ParseTableField* fields; - const AuxiliaryParseTableField* aux; - int max_field_number; - // TODO(ckennelly): Do something with this padding. - - // TODO(ckennelly): Vet these for sign extension. - int64 has_bits_offset; - int64 oneof_case_offset; - int64 extension_offset; - int64 arena_offset; - - // ExplicitlyInitialized -> T requires a reinterpret_cast, which prevents - // the tables from being constructed as a constexpr. We use void to avoid - // the cast. - const void* default_instance_void; - const MessageLite* default_instance() const { - return static_cast(default_instance_void); - } - - bool unknown_field_set; -}; - -static_assert(sizeof(ParseTableField) <= 16, "ParseTableField is too large"); -// The tables must be composed of POD components to ensure link-time -// initialization. -static_assert(std::is_pod::value, ""); -static_assert(std::is_pod::value, ""); -static_assert(std::is_pod::value, ""); -static_assert(std::is_pod::value, ""); -static_assert(std::is_pod::value, ""); -static_assert(std::is_pod::value, ""); - -// TODO(ckennelly): Consolidate these implementations into a single one, using -// dynamic dispatch to the appropriate unknown field handler. -bool MergePartialFromCodedStream(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input); -bool MergePartialFromCodedStreamLite(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input); - -template -bool ParseMap(io::CodedInputStream* input, void* map_field) { - typedef typename MapEntryToMapField::MapFieldType MapFieldType; - typedef Map - MapType; - typedef typename Entry::template Parser ParserType; - - ParserType parser(static_cast(map_field)); - return WireFormatLite::ReadMessageNoVirtual(input, &parser); -} - -struct SerializationTable { - int num_fields; - const FieldMetadata* field_table; -}; - -PROTOBUF_EXPORT void SerializeInternal(const uint8* base, - const FieldMetadata* table, - int32 num_fields, - io::CodedOutputStream* output); - -inline void TableSerialize(const MessageLite& msg, - const SerializationTable* table, - io::CodedOutputStream* output) { - const FieldMetadata* field_table = table->field_table; - int num_fields = table->num_fields - 1; - const uint8* base = reinterpret_cast(&msg); - // TODO(gerbens) This skips the first test if we could use the fast - // array serialization path, we should make this - // int cached_size = - // *reinterpret_cast(base + field_table->offset); - // SerializeWithCachedSize(msg, field_table + 1, num_fields, cached_size, ...) - // But we keep conformance with the old way for now. - SerializeInternal(base, field_table + 1, num_fields, output); -} - -uint8* SerializeInternalToArray(const uint8* base, const FieldMetadata* table, - int32 num_fields, bool is_deterministic, - uint8* buffer); - -inline uint8* TableSerializeToArray(const MessageLite& msg, - const SerializationTable* table, - bool is_deterministic, uint8* buffer) { - const uint8* base = reinterpret_cast(&msg); - const FieldMetadata* field_table = table->field_table + 1; - int num_fields = table->num_fields - 1; - return SerializeInternalToArray(base, field_table, num_fields, - is_deterministic, buffer); -} - -template -struct CompareHelper { - bool operator()(const T& a, const T& b) const { return a < b; } -}; - -template <> -struct CompareHelper { - bool operator()(const ArenaStringPtr& a, const ArenaStringPtr& b) const { - return a.Get() < b.Get(); - } -}; - -struct CompareMapKey { - template - bool operator()(const MapEntryHelper& a, - const MapEntryHelper& b) const { - return Compare(a.key_, b.key_); - } - template - bool Compare(const T& a, const T& b) const { - return CompareHelper()(a, b); - } -}; - -template -void MapFieldSerializer(const uint8* base, uint32 offset, uint32 tag, - uint32 has_offset, io::CodedOutputStream* output) { - typedef MapEntryHelper Entry; - typedef typename MapFieldType::MapType::const_iterator Iter; - - const MapFieldType& map_field = - *reinterpret_cast(base + offset); - const SerializationTable* t = - table + - has_offset; // has_offset is overloaded for maps to mean table offset - if (!output->IsSerializationDeterministic()) { - for (Iter it = map_field.GetMap().begin(); it != map_field.GetMap().end(); - ++it) { - Entry map_entry(*it); - output->WriteVarint32(tag); - output->WriteVarint32(map_entry._cached_size_); - SerializeInternal(reinterpret_cast(&map_entry), - t->field_table, t->num_fields, output); - } - } else { - std::vector v; - for (Iter it = map_field.GetMap().begin(); it != map_field.GetMap().end(); - ++it) { - v.push_back(Entry(*it)); - } - std::sort(v.begin(), v.end(), CompareMapKey()); - for (int i = 0; i < v.size(); i++) { - output->WriteVarint32(tag); - output->WriteVarint32(v[i]._cached_size_); - SerializeInternal(reinterpret_cast(&v[i]), t->field_table, - t->num_fields, output); - } - } -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_H__ diff --git a/third_party/protobuf-lite/google/protobuf/generated_message_table_driven_lite.h b/third_party/protobuf-lite/google/protobuf/generated_message_table_driven_lite.h deleted file mode 100644 index 3c65acdfe..000000000 --- a/third_party/protobuf-lite/google/protobuf/generated_message_table_driven_lite.h +++ /dev/null @@ -1,802 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_LITE_H__ -#define GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_LITE_H__ - -#include -#include -#include -#include -#include -#include -#include - - -#include - -namespace google { -namespace protobuf { -namespace internal { - - -enum StringType { - StringType_STRING = 0, -}; - -// Logically a superset of StringType, consisting of all field types that -// require special initialization. -enum ProcessingType { - ProcessingType_STRING = 0, - ProcessingType_CORD = 1, - ProcessingType_STRING_PIECE = 2, - ProcessingType_MESSAGE = 3, -}; - -enum Cardinality { - Cardinality_SINGULAR = 0, - Cardinality_REPEATED = 1, - Cardinality_ONEOF = 3 -}; - -template -inline Type* Raw(MessageLite* msg, int64 offset) { - return reinterpret_cast(reinterpret_cast(msg) + offset); -} - -template -inline const Type* Raw(const MessageLite* msg, int64 offset) { - return reinterpret_cast(reinterpret_cast(msg) + - offset); -} - -inline ExtensionSet* GetExtensionSet(MessageLite* msg, int64 extension_offset) { - if (extension_offset == -1) { - return NULL; - } - - return Raw(msg, extension_offset); -} - -template -inline Type* AddField(MessageLite* msg, int64 offset) { - static_assert(std::is_pod::value, - "Do not assign"); - - RepeatedField* repeated = Raw>(msg, offset); - return repeated->Add(); -} - -template <> -inline std::string* AddField(MessageLite* msg, int64 offset) { - RepeatedPtrField* repeated = - Raw>(msg, offset); - return repeated->Add(); -} - - -template -inline void AddField(MessageLite* msg, int64 offset, Type value) { - static_assert(std::is_pod::value, - "Do not assign"); - *AddField(msg, offset) = value; -} - -inline void SetBit(uint32* has_bits, uint32 has_bit_index) { - GOOGLE_DCHECK(has_bits != nullptr); - - uint32 mask = static_cast(1u) << (has_bit_index % 32); - has_bits[has_bit_index / 32u] |= mask; -} - -template -inline Type* MutableField(MessageLite* msg, uint32* has_bits, - uint32 has_bit_index, int64 offset) { - SetBit(has_bits, has_bit_index); - return Raw(msg, offset); -} - -template -inline void SetField(MessageLite* msg, uint32* has_bits, uint32 has_bit_index, - int64 offset, Type value) { - static_assert(std::is_pod::value, - "Do not assign"); - *MutableField(msg, has_bits, has_bit_index, offset) = value; -} - -template -inline void SetOneofField(MessageLite* msg, uint32* oneof_case, - uint32 oneof_case_index, int64 offset, - int field_number, Type value) { - oneof_case[oneof_case_index] = field_number; - *Raw(msg, offset) = value; -} - -// Clears a oneof field. The field argument should correspond to the particular -// field that is currently set in the oneof. -inline void ClearOneofField(const ParseTableField& field, Arena* arena, - MessageLite* msg) { - switch (field.processing_type & kTypeMask) { - case WireFormatLite::TYPE_MESSAGE: - if (arena == NULL) { - delete *Raw(msg, field.offset); - } - break; - - case WireFormatLite::TYPE_STRING: - case WireFormatLite::TYPE_BYTES: - Raw(msg, field.offset) - ->Destroy(ArenaStringPtr::EmptyDefault{}, arena); - break; - - default: - // No cleanup needed. - break; - } -} - -// Clears and reinitializes a oneof field as necessary, in preparation for -// parsing a new value with type field_type and field number field_number. -// -// Note: the oneof_case argument should point directly to the _oneof_case_ -// element corresponding to this particular oneof, not to the beginning of the -// _oneof_case_ array. -template -inline void ResetOneofField(const ParseTable& table, int field_number, - Arena* arena, MessageLite* msg, uint32* oneof_case, - int64 offset, const void* default_ptr) { - if (*oneof_case == field_number) { - // The oneof is already set to the right type, so there is no need to clear - // it. - return; - } - - if (*oneof_case != 0) { - ClearOneofField(table.fields[*oneof_case], arena, msg); - } - *oneof_case = field_number; - - switch (field_type) { - case ProcessingType_STRING: - Raw(msg, offset) - ->UnsafeSetDefault(static_cast(default_ptr)); - break; - case ProcessingType_MESSAGE: - MessageLite** submessage = Raw(msg, offset); - const MessageLite* prototype = - table.aux[field_number].messages.default_message(); - *submessage = prototype->New(arena); - break; - } -} - -template -static inline bool HandleString(io::CodedInputStream* input, MessageLite* msg, - Arena* arena, uint32* has_bits, - uint32 has_bit_index, int64 offset, - const void* default_ptr, - const char* field_name) { - StringPiece utf8_string_data; -#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - constexpr bool kValidateUtf8 = is_string_type; -#else - constexpr bool kValidateUtf8 = false; -#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - - switch (ctype) { - case StringType_STRING: { - switch (cardinality) { - case Cardinality_SINGULAR: { - ArenaStringPtr* field = MutableField( - msg, has_bits, has_bit_index, offset); - std::string* value = field->MutableNoCopy( - static_cast(default_ptr), arena); - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadString(input, value))) { - return false; - } - utf8_string_data = field->Get(); - } break; - case Cardinality_REPEATED: { - std::string* value = AddField(msg, offset); - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadString(input, value))) { - return false; - } - utf8_string_data = *value; - } break; - case Cardinality_ONEOF: { - ArenaStringPtr* field = Raw(msg, offset); - std::string* value = field->MutableNoCopy( - static_cast(default_ptr), arena); - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadString(input, value))) { - return false; - } - utf8_string_data = field->Get(); - } break; - default: - PROTOBUF_ASSUME(false); - } - break; - } - default: - PROTOBUF_ASSUME(false); - } - - if (kValidateUtf8) { - // TODO(b/118759213): fail if proto3 - WireFormatLite::VerifyUtf8String(utf8_string_data.data(), - utf8_string_data.length(), - WireFormatLite::PARSE, field_name); - } - return true; -} - -template -inline bool HandleEnum(const ParseTable& table, io::CodedInputStream* input, - MessageLite* msg, uint32* presence, - uint32 presence_index, int64 offset, uint32 tag, - int field_number) { - int value; - if (PROTOBUF_PREDICT_FALSE( - (!WireFormatLite::ReadPrimitive( - input, &value)))) { - return false; - } - - AuxiliaryParseTableField::EnumValidator validator = - table.aux[field_number].enums.validator; - if (validator == nullptr || validator(value)) { - switch (cardinality) { - case Cardinality_SINGULAR: - SetField(msg, presence, presence_index, offset, value); - break; - case Cardinality_REPEATED: - AddField(msg, offset, value); - break; - case Cardinality_ONEOF: - ClearOneofField(table.fields[presence[presence_index]], msg->GetArena(), - msg); - SetOneofField(msg, presence, presence_index, offset, field_number, - value); - break; - default: - PROTOBUF_ASSUME(false); - } - } else { - UnknownFieldHandler::Varint(msg, table, tag, value); - } - - return true; -} - -// RepeatedMessageTypeHandler allows us to operate on RepeatedPtrField fields -// without instantiating the specific template. -class RepeatedMessageTypeHandler { - public: - typedef MessageLite Type; - typedef MessageLite WeakType; - static Arena* GetArena(Type* t) { return t->GetArena(); } - static void* GetMaybeArenaPointer(Type* t) { - return t->GetMaybeArenaPointer(); - } - static inline Type* NewFromPrototype(const Type* prototype, - Arena* arena = NULL) { - return prototype->New(arena); - } - static void Delete(Type* t, Arena* arena = NULL) { - if (arena == NULL) { - delete t; - } - } -}; - -class MergePartialFromCodedStreamHelper { - public: - static MessageLite* Add(RepeatedPtrFieldBase* field, - const MessageLite* prototype) { - return field->Add( - const_cast(prototype)); - } -}; - -template -bool MergePartialFromCodedStreamInlined(MessageLite* msg, - const ParseTable& table, - io::CodedInputStream* input) { - // We require that has_bits are present, as to avoid having to check for them - // for every field. - // - // TODO(ckennelly): Make this a compile-time parameter with templates. - GOOGLE_DCHECK_GE(table.has_bits_offset, 0); - uint32* has_bits = Raw(msg, table.has_bits_offset); - GOOGLE_DCHECK(has_bits != NULL); - - while (true) { - uint32 tag = input->ReadTagWithCutoffNoLastTag(kMaxTag).first; - const WireFormatLite::WireType wire_type = - WireFormatLite::GetTagWireType(tag); - const int field_number = WireFormatLite::GetTagFieldNumber(tag); - - if (PROTOBUF_PREDICT_FALSE(field_number > table.max_field_number)) { - // check for possible extensions - if (UnknownFieldHandler::ParseExtension(msg, table, input, tag)) { - // successfully parsed - continue; - } - - if (PROTOBUF_PREDICT_FALSE( - !UnknownFieldHandler::Skip(msg, table, input, tag))) { - return false; - } - - continue; - } - - // We implicitly verify that data points to a valid field as we check the - // wire types. Entries in table.fields[i] that do not correspond to valid - // field numbers have their normal_wiretype and packed_wiretype fields set - // with the kInvalidMask value. As wire_type cannot take on that value, we - // will never match. - const ParseTableField* data = table.fields + field_number; - - // TODO(ckennelly): Avoid sign extension - const int64 presence_index = data->presence_index; - const int64 offset = data->offset; - const unsigned char processing_type = data->processing_type; - - if (data->normal_wiretype == static_cast(wire_type)) { - switch (processing_type) { -#define HANDLE_TYPE(TYPE, CPPTYPE) \ - case (WireFormatLite::TYPE_##TYPE): { \ - CPPTYPE value; \ - if (PROTOBUF_PREDICT_FALSE( \ - (!WireFormatLite::ReadPrimitive< \ - CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)))) { \ - return false; \ - } \ - SetField(msg, has_bits, presence_index, offset, value); \ - break; \ - } \ - case (WireFormatLite::TYPE_##TYPE) | kRepeatedMask: { \ - RepeatedField* values = Raw>(msg, offset); \ - if (PROTOBUF_PREDICT_FALSE((!WireFormatLite::ReadRepeatedPrimitive< \ - CPPTYPE, WireFormatLite::TYPE_##TYPE>( \ - data->tag_size, tag, input, values)))) { \ - return false; \ - } \ - break; \ - } \ - case (WireFormatLite::TYPE_##TYPE) | kOneofMask: { \ - uint32* oneof_case = Raw(msg, table.oneof_case_offset); \ - CPPTYPE value; \ - if (PROTOBUF_PREDICT_FALSE( \ - (!WireFormatLite::ReadPrimitive< \ - CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)))) { \ - return false; \ - } \ - ClearOneofField(table.fields[oneof_case[presence_index]], msg->GetArena(), \ - msg); \ - SetOneofField(msg, oneof_case, presence_index, offset, field_number, \ - value); \ - break; \ - } - - HANDLE_TYPE(INT32, int32) - HANDLE_TYPE(INT64, int64) - HANDLE_TYPE(SINT32, int32) - HANDLE_TYPE(SINT64, int64) - HANDLE_TYPE(UINT32, uint32) - HANDLE_TYPE(UINT64, uint64) - - HANDLE_TYPE(FIXED32, uint32) - HANDLE_TYPE(FIXED64, uint64) - HANDLE_TYPE(SFIXED32, int32) - HANDLE_TYPE(SFIXED64, int64) - - HANDLE_TYPE(FLOAT, float) - HANDLE_TYPE(DOUBLE, double) - - HANDLE_TYPE(BOOL, bool) -#undef HANDLE_TYPE - case WireFormatLite::TYPE_BYTES: -#ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - case WireFormatLite::TYPE_STRING: -#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - { - Arena* const arena = msg->GetArena(); - const void* default_ptr = table.aux[field_number].strings.default_ptr; - - if (PROTOBUF_PREDICT_FALSE( - (!HandleString( - input, msg, arena, has_bits, presence_index, offset, - default_ptr, nullptr)))) { - return false; - } - break; - } - case WireFormatLite::TYPE_BYTES | kOneofMask: -#ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - case WireFormatLite::TYPE_STRING | kOneofMask: -#endif // !GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - { - Arena* const arena = msg->GetArena(); - uint32* oneof_case = Raw(msg, table.oneof_case_offset); - const void* default_ptr = table.aux[field_number].strings.default_ptr; - - ResetOneofField( - table, field_number, arena, msg, oneof_case + presence_index, - offset, default_ptr); - - if (PROTOBUF_PREDICT_FALSE( - (!HandleString(input, msg, arena, has_bits, - presence_index, offset, - default_ptr, nullptr)))) { - return false; - } - break; - } - case (WireFormatLite::TYPE_BYTES) | kRepeatedMask: -#ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - case (WireFormatLite::TYPE_STRING) | kRepeatedMask: -#endif // !GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - { - Arena* const arena = msg->GetArena(); - const void* default_ptr = table.aux[field_number].strings.default_ptr; - - if (PROTOBUF_PREDICT_FALSE( - (!HandleString( - input, msg, arena, has_bits, presence_index, offset, - default_ptr, nullptr)))) { - return false; - } - break; - } -#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - case (WireFormatLite::TYPE_STRING): { - Arena* const arena = msg->GetArena(); - const void* default_ptr = table.aux[field_number].strings.default_ptr; - const char* field_name = table.aux[field_number].strings.field_name; - - if (PROTOBUF_PREDICT_FALSE( - (!HandleString( - input, msg, arena, has_bits, presence_index, offset, - default_ptr, field_name)))) { - return false; - } - break; - } - case (WireFormatLite::TYPE_STRING) | kRepeatedMask: { - Arena* const arena = msg->GetArena(); - const void* default_ptr = table.aux[field_number].strings.default_ptr; - const char* field_name = table.aux[field_number].strings.field_name; - - if (PROTOBUF_PREDICT_FALSE( - (!HandleString( - input, msg, arena, has_bits, presence_index, offset, - default_ptr, field_name)))) { - return false; - } - break; - } - case (WireFormatLite::TYPE_STRING) | kOneofMask: { - Arena* const arena = msg->GetArena(); - uint32* oneof_case = Raw(msg, table.oneof_case_offset); - const void* default_ptr = table.aux[field_number].strings.default_ptr; - const char* field_name = table.aux[field_number].strings.field_name; - - ResetOneofField( - table, field_number, arena, msg, oneof_case + presence_index, - offset, default_ptr); - - if (PROTOBUF_PREDICT_FALSE( - (!HandleString( - input, msg, arena, has_bits, presence_index, offset, - default_ptr, field_name)))) { - return false; - } - break; - } -#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED - case WireFormatLite::TYPE_ENUM: { - if (PROTOBUF_PREDICT_FALSE( - (!HandleEnum( - table, input, msg, has_bits, presence_index, offset, tag, - field_number)))) { - return false; - } - break; - } - case WireFormatLite::TYPE_ENUM | kRepeatedMask: { - if (PROTOBUF_PREDICT_FALSE( - (!HandleEnum( - table, input, msg, has_bits, presence_index, offset, tag, - field_number)))) { - return false; - } - break; - } - case WireFormatLite::TYPE_ENUM | kOneofMask: { - uint32* oneof_case = Raw(msg, table.oneof_case_offset); - if (PROTOBUF_PREDICT_FALSE( - (!HandleEnum( - table, input, msg, oneof_case, presence_index, offset, - tag, field_number)))) { - return false; - } - break; - } - case WireFormatLite::TYPE_GROUP: { - MessageLite** submsg_holder = - MutableField(msg, has_bits, presence_index, offset); - MessageLite* submsg = *submsg_holder; - - if (submsg == NULL) { - Arena* const arena = msg->GetArena(); - const MessageLite* prototype = - table.aux[field_number].messages.default_message(); - submsg = prototype->New(arena); - *submsg_holder = submsg; - } - - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadGroup(field_number, input, submsg))) { - return false; - } - - break; - } - case WireFormatLite::TYPE_GROUP | kRepeatedMask: { - RepeatedPtrFieldBase* field = Raw(msg, offset); - const MessageLite* prototype = - table.aux[field_number].messages.default_message(); - GOOGLE_DCHECK(prototype != NULL); - - MessageLite* submsg = - MergePartialFromCodedStreamHelper::Add(field, prototype); - - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadGroup(field_number, input, submsg))) { - return false; - } - - break; - } - case WireFormatLite::TYPE_MESSAGE: { - MessageLite** submsg_holder = - MutableField(msg, has_bits, presence_index, offset); - MessageLite* submsg = *submsg_holder; - - if (submsg == NULL) { - Arena* const arena = msg->GetArena(); - const MessageLite* prototype = - table.aux[field_number].messages.default_message(); - if (prototype == NULL) { - prototype = ImplicitWeakMessage::default_instance(); - } - submsg = prototype->New(arena); - *submsg_holder = submsg; - } - - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadMessage(input, submsg))) { - return false; - } - - break; - } - // TODO(ckennelly): Adapt ReadMessageNoVirtualNoRecursionDepth and - // manage input->IncrementRecursionDepth() here. - case WireFormatLite::TYPE_MESSAGE | kRepeatedMask: { - RepeatedPtrFieldBase* field = Raw(msg, offset); - const MessageLite* prototype = - table.aux[field_number].messages.default_message(); - if (prototype == NULL) { - prototype = ImplicitWeakMessage::default_instance(); - } - - MessageLite* submsg = - MergePartialFromCodedStreamHelper::Add(field, prototype); - - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadMessage(input, submsg))) { - return false; - } - - break; - } - case WireFormatLite::TYPE_MESSAGE | kOneofMask: { - Arena* const arena = msg->GetArena(); - uint32* oneof_case = Raw(msg, table.oneof_case_offset); - MessageLite** submsg_holder = Raw(msg, offset); - ResetOneofField( - table, field_number, arena, msg, oneof_case + presence_index, - offset, NULL); - MessageLite* submsg = *submsg_holder; - - if (PROTOBUF_PREDICT_FALSE( - !WireFormatLite::ReadMessage(input, submsg))) { - return false; - } - - break; - } - case TYPE_MAP: { - if (PROTOBUF_PREDICT_FALSE(!(*table.aux[field_number].maps.parse_map)( - input, Raw(msg, offset)))) { - return false; - } - break; - } - case 0: { - // Done. - input->SetLastTag(tag); - return true; - } - default: - PROTOBUF_ASSUME(false); - } - } else if (data->packed_wiretype == static_cast(wire_type)) { - // Non-packable fields have their packed_wiretype masked with - // kNotPackedMask, which is impossible to match here. - GOOGLE_DCHECK(processing_type & kRepeatedMask); - GOOGLE_DCHECK_NE(processing_type, kRepeatedMask); - GOOGLE_DCHECK_EQ(0, processing_type & kOneofMask); - - - // Mask out kRepeatedMask bit, allowing the jump table to be smaller. - switch (static_cast(processing_type ^ - kRepeatedMask)) { -#define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \ - case WireFormatLite::TYPE_##TYPE: { \ - RepeatedField* values = Raw>(msg, offset); \ - if (PROTOBUF_PREDICT_FALSE( \ - (!WireFormatLite::ReadPackedPrimitive< \ - CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, values)))) { \ - return false; \ - } \ - break; \ - } - - HANDLE_PACKED_TYPE(INT32, int32, Int32) - HANDLE_PACKED_TYPE(INT64, int64, Int64) - HANDLE_PACKED_TYPE(SINT32, int32, Int32) - HANDLE_PACKED_TYPE(SINT64, int64, Int64) - HANDLE_PACKED_TYPE(UINT32, uint32, UInt32) - HANDLE_PACKED_TYPE(UINT64, uint64, UInt64) - - HANDLE_PACKED_TYPE(FIXED32, uint32, UInt32) - HANDLE_PACKED_TYPE(FIXED64, uint64, UInt64) - HANDLE_PACKED_TYPE(SFIXED32, int32, Int32) - HANDLE_PACKED_TYPE(SFIXED64, int64, Int64) - - HANDLE_PACKED_TYPE(FLOAT, float, Float) - HANDLE_PACKED_TYPE(DOUBLE, double, Double) - - HANDLE_PACKED_TYPE(BOOL, bool, Bool) -#undef HANDLE_PACKED_TYPE - case WireFormatLite::TYPE_ENUM: { - // To avoid unnecessarily calling MutableUnknownFields (which mutates - // InternalMetadata) when all inputs in the repeated series - // are valid, we implement our own parser rather than call - // WireFormat::ReadPackedEnumPreserveUnknowns. - uint32 length; - if (PROTOBUF_PREDICT_FALSE(!input->ReadVarint32(&length))) { - return false; - } - - AuxiliaryParseTableField::EnumValidator validator = - table.aux[field_number].enums.validator; - RepeatedField* values = Raw>(msg, offset); - - io::CodedInputStream::Limit limit = input->PushLimit(length); - while (input->BytesUntilLimit() > 0) { - int value; - if (PROTOBUF_PREDICT_FALSE( - (!WireFormatLite::ReadPrimitive< - int, WireFormatLite::TYPE_ENUM>(input, &value)))) { - return false; - } - - if (validator == nullptr || validator(value)) { - values->Add(value); - } else { - // TODO(ckennelly): Consider caching here. - UnknownFieldHandler::Varint(msg, table, tag, value); - } - } - input->PopLimit(limit); - - break; - } - case WireFormatLite::TYPE_STRING: - case WireFormatLite::TYPE_GROUP: - case WireFormatLite::TYPE_MESSAGE: - case WireFormatLite::TYPE_BYTES: - GOOGLE_DCHECK(false); - return false; - default: - PROTOBUF_ASSUME(false); - } - } else { - if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) { - // Must be the end of the message. - input->SetLastTag(tag); - return true; - } - - // check for possible extensions - if (UnknownFieldHandler::ParseExtension(msg, table, input, tag)) { - // successfully parsed - continue; - } - - // process unknown field. - if (PROTOBUF_PREDICT_FALSE( - !UnknownFieldHandler::Skip(msg, table, input, tag))) { - return false; - } - } - } -} // NOLINT(readability/fn_size) - -template -bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table, - io::CodedInputStream* input) { - // The main beneficial cutoff values are 1 and 2 byte tags. - // Instantiate calls with the appropriate upper tag range - if (table.max_field_number <= (0x7F >> 3)) { - return MergePartialFromCodedStreamInlined( - msg, table, input); - } else if (table.max_field_number <= (0x3FFF >> 3)) { - return MergePartialFromCodedStreamInlined( - msg, table, input); - } else { - return MergePartialFromCodedStreamInlined< - UnknownFieldHandler, std::numeric_limits::max()>(msg, table, - input); - } -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_LITE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/generated_message_util.h b/third_party/protobuf-lite/google/protobuf/generated_message_util.h deleted file mode 100644 index bae0c1f76..000000000 --- a/third_party/protobuf-lite/google/protobuf/generated_message_util.h +++ /dev/null @@ -1,270 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This file contains miscellaneous helper code used by generated code -- -// including lite types -- but which should not be used directly by users. - -#ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ -#define GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ - -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include // Add direct dep on port for pb.cc -#include -#include -#include -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { - -class Arena; -class Message; - -namespace io { -class CodedInputStream; -} - -namespace internal { - -template -inline To DownCast(From* f) { - return PROTOBUF_NAMESPACE_ID::internal::down_cast(f); -} -template -inline To DownCast(From& f) { - return PROTOBUF_NAMESPACE_ID::internal::down_cast(f); -} - - -// This fastpath inlines a single branch instead of having to make the -// InitProtobufDefaults function call. -// It also generates less inlined code than a function-scope static initializer. -PROTOBUF_EXPORT extern std::atomic init_protobuf_defaults_state; -PROTOBUF_EXPORT void InitProtobufDefaultsSlow(); -PROTOBUF_EXPORT inline void InitProtobufDefaults() { - if (PROTOBUF_PREDICT_FALSE( - !init_protobuf_defaults_state.load(std::memory_order_acquire))) { - InitProtobufDefaultsSlow(); - } -} - -// This used by proto1 -PROTOBUF_EXPORT inline const std::string& GetEmptyString() { - InitProtobufDefaults(); - return GetEmptyStringAlreadyInited(); -} - - -// True if IsInitialized() is true for all elements of t. Type is expected -// to be a RepeatedPtrField. It's useful to have this -// helper here to keep the protobuf compiler from ever having to emit loops in -// IsInitialized() methods. We want the C++ compiler to inline this or not -// as it sees fit. -template -bool AllAreInitialized(const RepeatedPtrField& t) { - for (int i = t.size(); --i >= 0;) { - if (!t.Get(i).IsInitialized()) return false; - } - return true; -} - -// "Weak" variant of AllAreInitialized, used to implement implicit weak fields. -// This version operates on MessageLite to avoid introducing a dependency on the -// concrete message type. -template -bool AllAreInitializedWeak(const RepeatedPtrField& t) { - for (int i = t.size(); --i >= 0;) { - if (!reinterpret_cast(t) - .Get >(i) - .IsInitialized()) { - return false; - } - } - return true; -} - -inline bool IsPresent(const void* base, uint32 hasbit) { - const uint32* has_bits_array = static_cast(base); - return (has_bits_array[hasbit / 32] & (1u << (hasbit & 31))) != 0; -} - -inline bool IsOneofPresent(const void* base, uint32 offset, uint32 tag) { - const uint32* oneof = - reinterpret_cast(static_cast(base) + offset); - return *oneof == tag >> 3; -} - -typedef void (*SpecialSerializer)(const uint8* base, uint32 offset, uint32 tag, - uint32 has_offset, - io::CodedOutputStream* output); - -PROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset, - uint32 tag, uint32 has_offset, - io::CodedOutputStream* output); -PROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base, - uint32 offset, uint32 tag, - uint32 has_offset, - io::CodedOutputStream* output); - -PROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message); -PROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena, - MessageLite* submessage, - Arena* submessage_arena); -PROTOBUF_EXPORT void GenericSwap(MessageLite* m1, MessageLite* m2); -// We specialize GenericSwap for non-lite messages to benefit from reflection. -PROTOBUF_EXPORT void GenericSwap(Message* m1, Message* m2); - -template -T* DuplicateIfNonNull(T* message) { - // The casts must be reinterpret_cast<> because T might be a forward-declared - // type that the compiler doesn't know is related to MessageLite. - return reinterpret_cast( - DuplicateIfNonNullInternal(reinterpret_cast(message))); -} - -template -T* GetOwnedMessage(Arena* message_arena, T* submessage, - Arena* submessage_arena) { - // The casts must be reinterpret_cast<> because T might be a forward-declared - // type that the compiler doesn't know is related to MessageLite. - return reinterpret_cast(GetOwnedMessageInternal( - message_arena, reinterpret_cast(submessage), - submessage_arena)); -} - -// Hide atomic from the public header and allow easy change to regular int -// on platforms where the atomic might have a perf impact. -class PROTOBUF_EXPORT CachedSize { - public: - int Get() const { return size_.load(std::memory_order_relaxed); } - void Set(int size) { size_.store(size, std::memory_order_relaxed); } - - private: - std::atomic size_{0}; -}; - -// SCCInfo represents information of a strongly connected component of -// mutual dependent messages. -struct PROTOBUF_EXPORT SCCInfoBase { - // We use 0 for the Initialized state, because test eax,eax, jnz is smaller - // and is subject to macro fusion. - enum { - kInitialized = 0, // final state - kRunning = 1, - kUninitialized = -1, // initial state - }; -#if defined(_MSC_VER) && !defined(__clang__) - // MSVC doesn't make std::atomic constant initialized. This union trick - // makes it so. - union { - int visit_status_to_make_linker_init; - std::atomic visit_status; - }; -#else - std::atomic visit_status; -#endif - int num_deps; - int num_implicit_weak_deps; - void (*init_func)(); - // This is followed by an array of num_deps - // const SCCInfoBase* deps[]; -}; - -// Zero-length arrays are a language extension available in GCC and Clang but -// not MSVC. -#ifdef __GNUC__ -#define PROTOBUF_ARRAY_SIZE(n) (n) -#else -#define PROTOBUF_ARRAY_SIZE(n) ((n) ? (n) : 1) -#endif - -template -struct SCCInfo { - SCCInfoBase base; - // Semantically this is const SCCInfo* which is is a templated type. - // The obvious inheriting from SCCInfoBase mucks with struct initialization. - // Attempts showed the compiler was generating dynamic initialization code. - // This deps array consists of base.num_deps pointers to SCCInfoBase followed - // by base.num_implicit_weak_deps pointers to SCCInfoBase*. We need the extra - // pointer indirection for implicit weak fields. We cannot use a union type - // here, since that would prevent the array from being linker-initialized. - void* deps[PROTOBUF_ARRAY_SIZE(N)]; -}; - -#undef PROTOBUF_ARRAY_SIZE - -PROTOBUF_EXPORT void InitSCCImpl(SCCInfoBase* scc); - -inline void InitSCC(SCCInfoBase* scc) { - auto status = scc->visit_status.load(std::memory_order_acquire); - if (PROTOBUF_PREDICT_FALSE(status != SCCInfoBase::kInitialized)) - InitSCCImpl(scc); -} - -PROTOBUF_EXPORT void DestroyMessage(const void* message); -PROTOBUF_EXPORT void DestroyString(const void* s); -// Destroy (not delete) the message -inline void OnShutdownDestroyMessage(const void* ptr) { - OnShutdownRun(DestroyMessage, ptr); -} -// Destroy the string (call std::string destructor) -inline void OnShutdownDestroyString(const std::string* ptr) { - OnShutdownRun(DestroyString, ptr); -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ diff --git a/third_party/protobuf-lite/google/protobuf/has_bits.h b/third_party/protobuf-lite/google/protobuf/has_bits.h deleted file mode 100644 index 1144b9f46..000000000 --- a/third_party/protobuf-lite/google/protobuf/has_bits.h +++ /dev/null @@ -1,116 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_HAS_BITS_H__ -#define GOOGLE_PROTOBUF_HAS_BITS_H__ - -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { -namespace internal { - -template -class HasBits { - public: - constexpr HasBits() PROTOBUF_ALWAYS_INLINE : has_bits_{} {} - - void Clear() PROTOBUF_ALWAYS_INLINE { - memset(has_bits_, 0, sizeof(has_bits_)); - } - - uint32& operator[](int index) PROTOBUF_ALWAYS_INLINE { - return has_bits_[index]; - } - - const uint32& operator[](int index) const PROTOBUF_ALWAYS_INLINE { - return has_bits_[index]; - } - - bool operator==(const HasBits& rhs) const { - return memcmp(has_bits_, rhs.has_bits_, sizeof(has_bits_)) == 0; - } - - bool operator!=(const HasBits& rhs) const { - return !(*this == rhs); - } - - void Or(const HasBits& rhs) { - for (size_t i = 0; i < doublewords; i++) has_bits_[i] |= rhs[i]; - } - - bool empty() const; - - private: - uint32 has_bits_[doublewords]; -}; - -template <> -inline bool HasBits<1>::empty() const { - return !has_bits_[0]; -} - -template <> -inline bool HasBits<2>::empty() const { - return !(has_bits_[0] | has_bits_[1]); -} - -template <> -inline bool HasBits<3>::empty() const { - return !(has_bits_[0] | has_bits_[1] | has_bits_[2]); -} - -template <> -inline bool HasBits<4>::empty() const { - return !(has_bits_[0] | has_bits_[1] | has_bits_[2] | has_bits_[3]); -} - -template -inline bool HasBits::empty() const { - for (size_t i = 0; i < doublewords; ++i) { - if (has_bits_[i]) return false; - } - return true; -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_HAS_BITS_H__ diff --git a/third_party/protobuf-lite/google/protobuf/implicit_weak_message.h b/third_party/protobuf-lite/google/protobuf/implicit_weak_message.h deleted file mode 100644 index bfa6a813b..000000000 --- a/third_party/protobuf-lite/google/protobuf/implicit_weak_message.h +++ /dev/null @@ -1,190 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__ -#define GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__ - -#include - -#include -#include -#include -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -#include - -// This file is logically internal-only and should only be used by protobuf -// generated code. - -namespace google { -namespace protobuf { -namespace internal { - -// An implementation of MessageLite that treats all data as unknown. This type -// acts as a placeholder for an implicit weak field in the case where the true -// message type does not get linked into the binary. -class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite { - public: - ImplicitWeakMessage() {} - explicit ImplicitWeakMessage(Arena* arena) : MessageLite(arena) {} - - static const ImplicitWeakMessage* default_instance(); - - std::string GetTypeName() const override { return ""; } - - MessageLite* New() const override { return new ImplicitWeakMessage; } - MessageLite* New(Arena* arena) const override { - return Arena::CreateMessage(arena); - } - - void Clear() override { data_.clear(); } - - bool IsInitialized() const override { return true; } - - void CheckTypeAndMergeFrom(const MessageLite& other) override { - data_.append(static_cast(other).data_); - } - - const char* _InternalParse(const char* ptr, ParseContext* ctx) final; - - size_t ByteSizeLong() const override { return data_.size(); } - - uint8* _InternalSerialize(uint8* target, - io::EpsCopyOutputStream* stream) const final { - return stream->WriteRaw(data_.data(), static_cast(data_.size()), - target); - } - - int GetCachedSize() const override { return static_cast(data_.size()); } - - typedef void InternalArenaConstructable_; - - private: - std::string data_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage); -}; - -// A type handler for use with implicit weak repeated message fields. -template -class ImplicitWeakTypeHandler { - public: - typedef MessageLite Type; - static constexpr bool Moveable = false; - - static inline MessageLite* NewFromPrototype(const MessageLite* prototype, - Arena* arena = NULL) { - return prototype->New(arena); - } - - static inline void Delete(MessageLite* value, Arena* arena) { - if (arena == NULL) { - delete value; - } - } - static inline Arena* GetArena(MessageLite* value) { - return value->GetArena(); - } - static inline void* GetMaybeArenaPointer(MessageLite* value) { - return value->GetArena(); - } - static inline void Clear(MessageLite* value) { value->Clear(); } - static void Merge(const MessageLite& from, MessageLite* to) { - to->CheckTypeAndMergeFrom(from); - } -}; - -} // namespace internal - -template -struct WeakRepeatedPtrField { - using TypeHandler = internal::ImplicitWeakTypeHandler; - constexpr WeakRepeatedPtrField() : weak() {} - explicit WeakRepeatedPtrField(Arena* arena) : weak(arena) {} - ~WeakRepeatedPtrField() { weak.template Destroy(); } - - typedef internal::RepeatedPtrIterator iterator; - typedef internal::RepeatedPtrIterator const_iterator; - typedef internal::RepeatedPtrOverPtrsIterator - pointer_iterator; - typedef internal::RepeatedPtrOverPtrsIterator - const_pointer_iterator; - - iterator begin() { return iterator(base().raw_data()); } - const_iterator begin() const { return iterator(base().raw_data()); } - const_iterator cbegin() const { return begin(); } - iterator end() { return begin() + base().size(); } - const_iterator end() const { return begin() + base().size(); } - const_iterator cend() const { return end(); } - pointer_iterator pointer_begin() { - return pointer_iterator(base().raw_mutable_data()); - } - const_pointer_iterator pointer_begin() const { - return const_pointer_iterator(base().raw_mutable_data()); - } - pointer_iterator pointer_end() { - return pointer_iterator(base().raw_mutable_data() + base().size()); - } - const_pointer_iterator pointer_end() const { - return const_pointer_iterator(base().raw_mutable_data() + base().size()); - } - - MessageLite* AddWeak(const MessageLite* prototype) { - return base().AddWeak(prototype); - } - T* Add() { return weak.Add(); } - void Clear() { base().template Clear(); } - void MergeFrom(const WeakRepeatedPtrField& other) { - base().template MergeFrom(other.base()); - } - void InternalSwap(WeakRepeatedPtrField* other) { - base().InternalSwap(&other->base()); - } - - const internal::RepeatedPtrFieldBase& base() const { return weak; } - internal::RepeatedPtrFieldBase& base() { return weak; } - // Union disables running the destructor. Which would create a strong link. - // Instead we explicitly destroy the underlying base through the virtual - // destructor. - union { - RepeatedPtrField weak; - }; -}; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/io/coded_stream.h b/third_party/protobuf-lite/google/protobuf/io/coded_stream.h deleted file mode 100644 index 0fff1782c..000000000 --- a/third_party/protobuf-lite/google/protobuf/io/coded_stream.h +++ /dev/null @@ -1,1714 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This file contains the CodedInputStream and CodedOutputStream classes, -// which wrap a ZeroCopyInputStream or ZeroCopyOutputStream, respectively, -// and allow you to read or write individual pieces of data in various -// formats. In particular, these implement the varint encoding for -// integers, a simple variable-length encoding in which smaller numbers -// take fewer bytes. -// -// Typically these classes will only be used internally by the protocol -// buffer library in order to encode and decode protocol buffers. Clients -// of the library only need to know about this class if they wish to write -// custom message parsing or serialization procedures. -// -// CodedOutputStream example: -// // Write some data to "myfile". First we write a 4-byte "magic number" -// // to identify the file type, then write a length-delimited string. The -// // string is composed of a varint giving the length followed by the raw -// // bytes. -// int fd = open("myfile", O_CREAT | O_WRONLY); -// ZeroCopyOutputStream* raw_output = new FileOutputStream(fd); -// CodedOutputStream* coded_output = new CodedOutputStream(raw_output); -// -// int magic_number = 1234; -// char text[] = "Hello world!"; -// coded_output->WriteLittleEndian32(magic_number); -// coded_output->WriteVarint32(strlen(text)); -// coded_output->WriteRaw(text, strlen(text)); -// -// delete coded_output; -// delete raw_output; -// close(fd); -// -// CodedInputStream example: -// // Read a file created by the above code. -// int fd = open("myfile", O_RDONLY); -// ZeroCopyInputStream* raw_input = new FileInputStream(fd); -// CodedInputStream* coded_input = new CodedInputStream(raw_input); -// -// coded_input->ReadLittleEndian32(&magic_number); -// if (magic_number != 1234) { -// cerr << "File not in expected format." << endl; -// return; -// } -// -// uint32 size; -// coded_input->ReadVarint32(&size); -// -// char* text = new char[size + 1]; -// coded_input->ReadRaw(buffer, size); -// text[size] = '\0'; -// -// delete coded_input; -// delete raw_input; -// close(fd); -// -// cout << "Text is: " << text << endl; -// delete [] text; -// -// For those who are interested, varint encoding is defined as follows: -// -// The encoding operates on unsigned integers of up to 64 bits in length. -// Each byte of the encoded value has the format: -// * bits 0-6: Seven bits of the number being encoded. -// * bit 7: Zero if this is the last byte in the encoding (in which -// case all remaining bits of the number are zero) or 1 if -// more bytes follow. -// The first byte contains the least-significant 7 bits of the number, the -// second byte (if present) contains the next-least-significant 7 bits, -// and so on. So, the binary number 1011000101011 would be encoded in two -// bytes as "10101011 00101100". -// -// In theory, varint could be used to encode integers of any length. -// However, for practicality we set a limit at 64 bits. The maximum encoded -// length of a number is thus 10 bytes. - -#ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_H__ -#define GOOGLE_PROTOBUF_IO_CODED_STREAM_H__ - - -#include - -#include -#include -#include -#include -#include -#include -#include - -#ifdef _MSC_VER -// Assuming windows is always little-endian. -#if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) -#define PROTOBUF_LITTLE_ENDIAN 1 -#endif -#if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) -// If MSVC has "/RTCc" set, it will complain about truncating casts at -// runtime. This file contains some intentional truncating casts. -#pragma runtime_checks("c", off) -#endif -#else -#include // __BYTE_ORDER -#if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ - (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \ - !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) -#define PROTOBUF_LITTLE_ENDIAN 1 -#endif -#endif -#include -#include -#include -#include -#include - - -#include - -namespace google { -namespace protobuf { - -class DescriptorPool; -class MessageFactory; -class ZeroCopyCodedInputStream; - -namespace internal { -void MapTestForceDeterministic(); -class EpsCopyByteStream; -} // namespace internal - -namespace io { - -// Defined in this file. -class CodedInputStream; -class CodedOutputStream; - -// Defined in other files. -class ZeroCopyInputStream; // zero_copy_stream.h -class ZeroCopyOutputStream; // zero_copy_stream.h - -// Class which reads and decodes binary data which is composed of varint- -// encoded integers and fixed-width pieces. Wraps a ZeroCopyInputStream. -// Most users will not need to deal with CodedInputStream. -// -// Most methods of CodedInputStream that return a bool return false if an -// underlying I/O error occurs or if the data is malformed. Once such a -// failure occurs, the CodedInputStream is broken and is no longer useful. -// After a failure, callers also should assume writes to "out" args may have -// occurred, though nothing useful can be determined from those writes. -class PROTOBUF_EXPORT CodedInputStream { - public: - // Create a CodedInputStream that reads from the given ZeroCopyInputStream. - explicit CodedInputStream(ZeroCopyInputStream* input); - - // Create a CodedInputStream that reads from the given flat array. This is - // faster than using an ArrayInputStream. PushLimit(size) is implied by - // this constructor. - explicit CodedInputStream(const uint8* buffer, int size); - - // Destroy the CodedInputStream and position the underlying - // ZeroCopyInputStream at the first unread byte. If an error occurred while - // reading (causing a method to return false), then the exact position of - // the input stream may be anywhere between the last value that was read - // successfully and the stream's byte limit. - ~CodedInputStream(); - - // Return true if this CodedInputStream reads from a flat array instead of - // a ZeroCopyInputStream. - inline bool IsFlat() const; - - // Skips a number of bytes. Returns false if an underlying read error - // occurs. - inline bool Skip(int count); - - // Sets *data to point directly at the unread part of the CodedInputStream's - // underlying buffer, and *size to the size of that buffer, but does not - // advance the stream's current position. This will always either produce - // a non-empty buffer or return false. If the caller consumes any of - // this data, it should then call Skip() to skip over the consumed bytes. - // This may be useful for implementing external fast parsing routines for - // types of data not covered by the CodedInputStream interface. - bool GetDirectBufferPointer(const void** data, int* size); - - // Like GetDirectBufferPointer, but this method is inlined, and does not - // attempt to Refresh() if the buffer is currently empty. - PROTOBUF_ALWAYS_INLINE - void GetDirectBufferPointerInline(const void** data, int* size); - - // Read raw bytes, copying them into the given buffer. - bool ReadRaw(void* buffer, int size); - - // Like ReadRaw, but reads into a string. - bool ReadString(std::string* buffer, int size); - - - // Read a 32-bit little-endian integer. - bool ReadLittleEndian32(uint32* value); - // Read a 64-bit little-endian integer. - bool ReadLittleEndian64(uint64* value); - - // These methods read from an externally provided buffer. The caller is - // responsible for ensuring that the buffer has sufficient space. - // Read a 32-bit little-endian integer. - static const uint8* ReadLittleEndian32FromArray(const uint8* buffer, - uint32* value); - // Read a 64-bit little-endian integer. - static const uint8* ReadLittleEndian64FromArray(const uint8* buffer, - uint64* value); - - // Read an unsigned integer with Varint encoding, truncating to 32 bits. - // Reading a 32-bit value is equivalent to reading a 64-bit one and casting - // it to uint32, but may be more efficient. - bool ReadVarint32(uint32* value); - // Read an unsigned integer with Varint encoding. - bool ReadVarint64(uint64* value); - - // Reads a varint off the wire into an "int". This should be used for reading - // sizes off the wire (sizes of strings, submessages, bytes fields, etc). - // - // The value from the wire is interpreted as unsigned. If its value exceeds - // the representable value of an integer on this platform, instead of - // truncating we return false. Truncating (as performed by ReadVarint32() - // above) is an acceptable approach for fields representing an integer, but - // when we are parsing a size from the wire, truncating the value would result - // in us misparsing the payload. - bool ReadVarintSizeAsInt(int* value); - - // Read a tag. This calls ReadVarint32() and returns the result, or returns - // zero (which is not a valid tag) if ReadVarint32() fails. Also, ReadTag - // (but not ReadTagNoLastTag) updates the last tag value, which can be checked - // with LastTagWas(). - // - // Always inline because this is only called in one place per parse loop - // but it is called for every iteration of said loop, so it should be fast. - // GCC doesn't want to inline this by default. - PROTOBUF_ALWAYS_INLINE uint32 ReadTag() { - return last_tag_ = ReadTagNoLastTag(); - } - - PROTOBUF_ALWAYS_INLINE uint32 ReadTagNoLastTag(); - - // This usually a faster alternative to ReadTag() when cutoff is a manifest - // constant. It does particularly well for cutoff >= 127. The first part - // of the return value is the tag that was read, though it can also be 0 in - // the cases where ReadTag() would return 0. If the second part is true - // then the tag is known to be in [0, cutoff]. If not, the tag either is - // above cutoff or is 0. (There's intentional wiggle room when tag is 0, - // because that can arise in several ways, and for best performance we want - // to avoid an extra "is tag == 0?" check here.) - PROTOBUF_ALWAYS_INLINE - std::pair ReadTagWithCutoff(uint32 cutoff) { - std::pair result = ReadTagWithCutoffNoLastTag(cutoff); - last_tag_ = result.first; - return result; - } - - PROTOBUF_ALWAYS_INLINE - std::pair ReadTagWithCutoffNoLastTag(uint32 cutoff); - - // Usually returns true if calling ReadVarint32() now would produce the given - // value. Will always return false if ReadVarint32() would not return the - // given value. If ExpectTag() returns true, it also advances past - // the varint. For best performance, use a compile-time constant as the - // parameter. - // Always inline because this collapses to a small number of instructions - // when given a constant parameter, but GCC doesn't want to inline by default. - PROTOBUF_ALWAYS_INLINE bool ExpectTag(uint32 expected); - - // Like above, except this reads from the specified buffer. The caller is - // responsible for ensuring that the buffer is large enough to read a varint - // of the expected size. For best performance, use a compile-time constant as - // the expected tag parameter. - // - // Returns a pointer beyond the expected tag if it was found, or NULL if it - // was not. - PROTOBUF_ALWAYS_INLINE - static const uint8* ExpectTagFromArray(const uint8* buffer, uint32 expected); - - // Usually returns true if no more bytes can be read. Always returns false - // if more bytes can be read. If ExpectAtEnd() returns true, a subsequent - // call to LastTagWas() will act as if ReadTag() had been called and returned - // zero, and ConsumedEntireMessage() will return true. - bool ExpectAtEnd(); - - // If the last call to ReadTag() or ReadTagWithCutoff() returned the given - // value, returns true. Otherwise, returns false. - // ReadTagNoLastTag/ReadTagWithCutoffNoLastTag do not preserve the last - // returned value. - // - // This is needed because parsers for some types of embedded messages - // (with field type TYPE_GROUP) don't actually know that they've reached the - // end of a message until they see an ENDGROUP tag, which was actually part - // of the enclosing message. The enclosing message would like to check that - // tag to make sure it had the right number, so it calls LastTagWas() on - // return from the embedded parser to check. - bool LastTagWas(uint32 expected); - void SetLastTag(uint32 tag) { last_tag_ = tag; } - - // When parsing message (but NOT a group), this method must be called - // immediately after MergeFromCodedStream() returns (if it returns true) - // to further verify that the message ended in a legitimate way. For - // example, this verifies that parsing did not end on an end-group tag. - // It also checks for some cases where, due to optimizations, - // MergeFromCodedStream() can incorrectly return true. - bool ConsumedEntireMessage(); - void SetConsumed() { legitimate_message_end_ = true; } - - // Limits ---------------------------------------------------------- - // Limits are used when parsing length-delimited embedded messages. - // After the message's length is read, PushLimit() is used to prevent - // the CodedInputStream from reading beyond that length. Once the - // embedded message has been parsed, PopLimit() is called to undo the - // limit. - - // Opaque type used with PushLimit() and PopLimit(). Do not modify - // values of this type yourself. The only reason that this isn't a - // struct with private internals is for efficiency. - typedef int Limit; - - // Places a limit on the number of bytes that the stream may read, - // starting from the current position. Once the stream hits this limit, - // it will act like the end of the input has been reached until PopLimit() - // is called. - // - // As the names imply, the stream conceptually has a stack of limits. The - // shortest limit on the stack is always enforced, even if it is not the - // top limit. - // - // The value returned by PushLimit() is opaque to the caller, and must - // be passed unchanged to the corresponding call to PopLimit(). - Limit PushLimit(int byte_limit); - - // Pops the last limit pushed by PushLimit(). The input must be the value - // returned by that call to PushLimit(). - void PopLimit(Limit limit); - - // Returns the number of bytes left until the nearest limit on the - // stack is hit, or -1 if no limits are in place. - int BytesUntilLimit() const; - - // Returns current position relative to the beginning of the input stream. - int CurrentPosition() const; - - // Total Bytes Limit ----------------------------------------------- - // To prevent malicious users from sending excessively large messages - // and causing memory exhaustion, CodedInputStream imposes a hard limit on - // the total number of bytes it will read. - - // Sets the maximum number of bytes that this CodedInputStream will read - // before refusing to continue. To prevent servers from allocating enormous - // amounts of memory to hold parsed messages, the maximum message length - // should be limited to the shortest length that will not harm usability. - // The default limit is INT_MAX (~2GB) and apps should set shorter limits - // if possible. An error will always be printed to stderr if the limit is - // reached. - // - // Note: setting a limit less than the current read position is interpreted - // as a limit on the current position. - // - // This is unrelated to PushLimit()/PopLimit(). - void SetTotalBytesLimit(int total_bytes_limit); - - PROTOBUF_DEPRECATED_MSG( - "Please use the single parameter version of SetTotalBytesLimit(). The " - "second parameter is ignored.") - void SetTotalBytesLimit(int total_bytes_limit, int) { - SetTotalBytesLimit(total_bytes_limit); - } - - // The Total Bytes Limit minus the Current Position, or -1 if the total bytes - // limit is INT_MAX. - int BytesUntilTotalBytesLimit() const; - - // Recursion Limit ------------------------------------------------- - // To prevent corrupt or malicious messages from causing stack overflows, - // we must keep track of the depth of recursion when parsing embedded - // messages and groups. CodedInputStream keeps track of this because it - // is the only object that is passed down the stack during parsing. - - // Sets the maximum recursion depth. The default is 100. - void SetRecursionLimit(int limit); - int RecursionBudget() { return recursion_budget_; } - - static int GetDefaultRecursionLimit() { return default_recursion_limit_; } - - // Increments the current recursion depth. Returns true if the depth is - // under the limit, false if it has gone over. - bool IncrementRecursionDepth(); - - // Decrements the recursion depth if possible. - void DecrementRecursionDepth(); - - // Decrements the recursion depth blindly. This is faster than - // DecrementRecursionDepth(). It should be used only if all previous - // increments to recursion depth were successful. - void UnsafeDecrementRecursionDepth(); - - // Shorthand for make_pair(PushLimit(byte_limit), --recursion_budget_). - // Using this can reduce code size and complexity in some cases. The caller - // is expected to check that the second part of the result is non-negative (to - // bail out if the depth of recursion is too high) and, if all is well, to - // later pass the first part of the result to PopLimit() or similar. - std::pair IncrementRecursionDepthAndPushLimit( - int byte_limit); - - // Shorthand for PushLimit(ReadVarint32(&length) ? length : 0). - Limit ReadLengthAndPushLimit(); - - // Helper that is equivalent to: { - // bool result = ConsumedEntireMessage(); - // PopLimit(limit); - // UnsafeDecrementRecursionDepth(); - // return result; } - // Using this can reduce code size and complexity in some cases. - // Do not use unless the current recursion depth is greater than zero. - bool DecrementRecursionDepthAndPopLimit(Limit limit); - - // Helper that is equivalent to: { - // bool result = ConsumedEntireMessage(); - // PopLimit(limit); - // return result; } - // Using this can reduce code size and complexity in some cases. - bool CheckEntireMessageConsumedAndPopLimit(Limit limit); - - // Extension Registry ---------------------------------------------- - // ADVANCED USAGE: 99.9% of people can ignore this section. - // - // By default, when parsing extensions, the parser looks for extension - // definitions in the pool which owns the outer message's Descriptor. - // However, you may call SetExtensionRegistry() to provide an alternative - // pool instead. This makes it possible, for example, to parse a message - // using a generated class, but represent some extensions using - // DynamicMessage. - - // Set the pool used to look up extensions. Most users do not need to call - // this as the correct pool will be chosen automatically. - // - // WARNING: It is very easy to misuse this. Carefully read the requirements - // below. Do not use this unless you are sure you need it. Almost no one - // does. - // - // Let's say you are parsing a message into message object m, and you want - // to take advantage of SetExtensionRegistry(). You must follow these - // requirements: - // - // The given DescriptorPool must contain m->GetDescriptor(). It is not - // sufficient for it to simply contain a descriptor that has the same name - // and content -- it must be the *exact object*. In other words: - // assert(pool->FindMessageTypeByName(m->GetDescriptor()->full_name()) == - // m->GetDescriptor()); - // There are two ways to satisfy this requirement: - // 1) Use m->GetDescriptor()->pool() as the pool. This is generally useless - // because this is the pool that would be used anyway if you didn't call - // SetExtensionRegistry() at all. - // 2) Use a DescriptorPool which has m->GetDescriptor()->pool() as an - // "underlay". Read the documentation for DescriptorPool for more - // information about underlays. - // - // You must also provide a MessageFactory. This factory will be used to - // construct Message objects representing extensions. The factory's - // GetPrototype() MUST return non-NULL for any Descriptor which can be found - // through the provided pool. - // - // If the provided factory might return instances of protocol-compiler- - // generated (i.e. compiled-in) types, or if the outer message object m is - // a generated type, then the given factory MUST have this property: If - // GetPrototype() is given a Descriptor which resides in - // DescriptorPool::generated_pool(), the factory MUST return the same - // prototype which MessageFactory::generated_factory() would return. That - // is, given a descriptor for a generated type, the factory must return an - // instance of the generated class (NOT DynamicMessage). However, when - // given a descriptor for a type that is NOT in generated_pool, the factory - // is free to return any implementation. - // - // The reason for this requirement is that generated sub-objects may be - // accessed via the standard (non-reflection) extension accessor methods, - // and these methods will down-cast the object to the generated class type. - // If the object is not actually of that type, the results would be undefined. - // On the other hand, if an extension is not compiled in, then there is no - // way the code could end up accessing it via the standard accessors -- the - // only way to access the extension is via reflection. When using reflection, - // DynamicMessage and generated messages are indistinguishable, so it's fine - // if these objects are represented using DynamicMessage. - // - // Using DynamicMessageFactory on which you have called - // SetDelegateToGeneratedFactory(true) should be sufficient to satisfy the - // above requirement. - // - // If either pool or factory is NULL, both must be NULL. - // - // Note that this feature is ignored when parsing "lite" messages as they do - // not have descriptors. - void SetExtensionRegistry(const DescriptorPool* pool, - MessageFactory* factory); - - // Get the DescriptorPool set via SetExtensionRegistry(), or NULL if no pool - // has been provided. - const DescriptorPool* GetExtensionPool(); - - // Get the MessageFactory set via SetExtensionRegistry(), or NULL if no - // factory has been provided. - MessageFactory* GetExtensionFactory(); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream); - - const uint8* buffer_; - const uint8* buffer_end_; // pointer to the end of the buffer. - ZeroCopyInputStream* input_; - int total_bytes_read_; // total bytes read from input_, including - // the current buffer - - // If total_bytes_read_ surpasses INT_MAX, we record the extra bytes here - // so that we can BackUp() on destruction. - int overflow_bytes_; - - // LastTagWas() stuff. - uint32 last_tag_; // result of last ReadTag() or ReadTagWithCutoff(). - - // This is set true by ReadTag{Fallback/Slow}() if it is called when exactly - // at EOF, or by ExpectAtEnd() when it returns true. This happens when we - // reach the end of a message and attempt to read another tag. - bool legitimate_message_end_; - - // See EnableAliasing(). - bool aliasing_enabled_; - - // Limits - Limit current_limit_; // if position = -1, no limit is applied - - // For simplicity, if the current buffer crosses a limit (either a normal - // limit created by PushLimit() or the total bytes limit), buffer_size_ - // only tracks the number of bytes before that limit. This field - // contains the number of bytes after it. Note that this implies that if - // buffer_size_ == 0 and buffer_size_after_limit_ > 0, we know we've - // hit a limit. However, if both are zero, it doesn't necessarily mean - // we aren't at a limit -- the buffer may have ended exactly at the limit. - int buffer_size_after_limit_; - - // Maximum number of bytes to read, period. This is unrelated to - // current_limit_. Set using SetTotalBytesLimit(). - int total_bytes_limit_; - - // Current recursion budget, controlled by IncrementRecursionDepth() and - // similar. Starts at recursion_limit_ and goes down: if this reaches - // -1 we are over budget. - int recursion_budget_; - // Recursion depth limit, set by SetRecursionLimit(). - int recursion_limit_; - - // See SetExtensionRegistry(). - const DescriptorPool* extension_pool_; - MessageFactory* extension_factory_; - - // Private member functions. - - // Fallback when Skip() goes past the end of the current buffer. - bool SkipFallback(int count, int original_buffer_size); - - // Advance the buffer by a given number of bytes. - void Advance(int amount); - - // Back up input_ to the current buffer position. - void BackUpInputToCurrentPosition(); - - // Recomputes the value of buffer_size_after_limit_. Must be called after - // current_limit_ or total_bytes_limit_ changes. - void RecomputeBufferLimits(); - - // Writes an error message saying that we hit total_bytes_limit_. - void PrintTotalBytesLimitError(); - - // Called when the buffer runs out to request more data. Implies an - // Advance(BufferSize()). - bool Refresh(); - - // When parsing varints, we optimize for the common case of small values, and - // then optimize for the case when the varint fits within the current buffer - // piece. The Fallback method is used when we can't use the one-byte - // optimization. The Slow method is yet another fallback when the buffer is - // not large enough. Making the slow path out-of-line speeds up the common - // case by 10-15%. The slow path is fairly uncommon: it only triggers when a - // message crosses multiple buffers. Note: ReadVarint32Fallback() and - // ReadVarint64Fallback() are called frequently and generally not inlined, so - // they have been optimized to avoid "out" parameters. The former returns -1 - // if it fails and the uint32 it read otherwise. The latter has a bool - // indicating success or failure as part of its return type. - int64 ReadVarint32Fallback(uint32 first_byte_or_zero); - int ReadVarintSizeAsIntFallback(); - std::pair ReadVarint64Fallback(); - bool ReadVarint32Slow(uint32* value); - bool ReadVarint64Slow(uint64* value); - int ReadVarintSizeAsIntSlow(); - bool ReadLittleEndian32Fallback(uint32* value); - bool ReadLittleEndian64Fallback(uint64* value); - - // Fallback/slow methods for reading tags. These do not update last_tag_, - // but will set legitimate_message_end_ if we are at the end of the input - // stream. - uint32 ReadTagFallback(uint32 first_byte_or_zero); - uint32 ReadTagSlow(); - bool ReadStringFallback(std::string* buffer, int size); - - // Return the size of the buffer. - int BufferSize() const; - - static const int kDefaultTotalBytesLimit = INT_MAX; - - static int default_recursion_limit_; // 100 by default. - - friend class google::protobuf::ZeroCopyCodedInputStream; - friend class google::protobuf::internal::EpsCopyByteStream; -}; - -// EpsCopyOutputStream wraps a ZeroCopyOutputStream and exposes a new stream, -// which has the property you can write kSlopBytes (16 bytes) from the current -// position without bounds checks. The cursor into the stream is managed by -// the user of the class and is an explicit parameter in the methods. Careful -// use of this class, ie. keep ptr a local variable, eliminates the need to -// for the compiler to sync the ptr value between register and memory. -class PROTOBUF_EXPORT EpsCopyOutputStream { - public: - enum { kSlopBytes = 16 }; - - // Initialize from a stream. - EpsCopyOutputStream(ZeroCopyOutputStream* stream, bool deterministic, - uint8** pp) - : end_(buffer_), - stream_(stream), - is_serialization_deterministic_(deterministic) { - *pp = buffer_; - } - - // Only for array serialization. No overflow protection, end_ will be the - // pointed to the end of the array. When using this the total size is already - // known, so no need to maintain the slop region. - EpsCopyOutputStream(void* data, int size, bool deterministic) - : end_(static_cast(data) + size), - buffer_end_(nullptr), - stream_(nullptr), - is_serialization_deterministic_(deterministic) {} - - // Initialize from stream but with the first buffer already given (eager). - EpsCopyOutputStream(void* data, int size, ZeroCopyOutputStream* stream, - bool deterministic, uint8** pp) - : stream_(stream), is_serialization_deterministic_(deterministic) { - *pp = SetInitialBuffer(data, size); - } - - // Flush everything that's written into the underlying ZeroCopyOutputStream - // and trims the underlying stream to the location of ptr. - uint8* Trim(uint8* ptr); - - // After this it's guaranteed you can safely write kSlopBytes to ptr. This - // will never fail! The underlying stream can produce an error. Use HadError - // to check for errors. - PROTOBUF_MUST_USE_RESULT uint8* EnsureSpace(uint8* ptr) { - if (PROTOBUF_PREDICT_FALSE(ptr >= end_)) { - return EnsureSpaceFallback(ptr); - } - return ptr; - } - - uint8* WriteRaw(const void* data, int size, uint8* ptr) { - if (PROTOBUF_PREDICT_FALSE(end_ - ptr < size)) { - return WriteRawFallback(data, size, ptr); - } - std::memcpy(ptr, data, size); - return ptr + size; - } - // Writes the buffer specified by data, size to the stream. Possibly by - // aliasing the buffer (ie. not copying the data). The caller is responsible - // to make sure the buffer is alive for the duration of the - // ZeroCopyOutputStream. - uint8* WriteRawMaybeAliased(const void* data, int size, uint8* ptr) { - if (aliasing_enabled_) { - return WriteAliasedRaw(data, size, ptr); - } else { - return WriteRaw(data, size, ptr); - } - } - - - uint8* WriteStringMaybeAliased(uint32 num, const std::string& s, uint8* ptr) { - std::ptrdiff_t size = s.size(); - if (PROTOBUF_PREDICT_FALSE( - size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) { - return WriteStringMaybeAliasedOutline(num, s, ptr); - } - ptr = UnsafeVarint((num << 3) | 2, ptr); - *ptr++ = static_cast(size); - std::memcpy(ptr, s.data(), size); - return ptr + size; - } - uint8* WriteBytesMaybeAliased(uint32 num, const std::string& s, uint8* ptr) { - return WriteStringMaybeAliased(num, s, ptr); - } - - template - PROTOBUF_ALWAYS_INLINE uint8* WriteString(uint32 num, const T& s, - uint8* ptr) { - std::ptrdiff_t size = s.size(); - if (PROTOBUF_PREDICT_FALSE( - size >= 128 || end_ - ptr + 16 - TagSize(num << 3) - 1 < size)) { - return WriteStringOutline(num, s, ptr); - } - ptr = UnsafeVarint((num << 3) | 2, ptr); - *ptr++ = static_cast(size); - std::memcpy(ptr, s.data(), size); - return ptr + size; - } - template - uint8* WriteBytes(uint32 num, const T& s, uint8* ptr) { - return WriteString(num, s, ptr); - } - - template - PROTOBUF_ALWAYS_INLINE uint8* WriteInt32Packed(int num, const T& r, int size, - uint8* ptr) { - return WriteVarintPacked(num, r, size, ptr, Encode64); - } - template - PROTOBUF_ALWAYS_INLINE uint8* WriteUInt32Packed(int num, const T& r, int size, - uint8* ptr) { - return WriteVarintPacked(num, r, size, ptr, Encode32); - } - template - PROTOBUF_ALWAYS_INLINE uint8* WriteSInt32Packed(int num, const T& r, int size, - uint8* ptr) { - return WriteVarintPacked(num, r, size, ptr, ZigZagEncode32); - } - template - PROTOBUF_ALWAYS_INLINE uint8* WriteInt64Packed(int num, const T& r, int size, - uint8* ptr) { - return WriteVarintPacked(num, r, size, ptr, Encode64); - } - template - PROTOBUF_ALWAYS_INLINE uint8* WriteUInt64Packed(int num, const T& r, int size, - uint8* ptr) { - return WriteVarintPacked(num, r, size, ptr, Encode64); - } - template - PROTOBUF_ALWAYS_INLINE uint8* WriteSInt64Packed(int num, const T& r, int size, - uint8* ptr) { - return WriteVarintPacked(num, r, size, ptr, ZigZagEncode64); - } - template - PROTOBUF_ALWAYS_INLINE uint8* WriteEnumPacked(int num, const T& r, int size, - uint8* ptr) { - return WriteVarintPacked(num, r, size, ptr, Encode64); - } - - template - PROTOBUF_ALWAYS_INLINE uint8* WriteFixedPacked(int num, const T& r, - uint8* ptr) { - ptr = EnsureSpace(ptr); - constexpr auto element_size = sizeof(typename T::value_type); - auto size = r.size() * element_size; - ptr = WriteLengthDelim(num, size, ptr); - return WriteRawLittleEndian(r.data(), static_cast(size), - ptr); - } - - // Returns true if there was an underlying I/O error since this object was - // created. - bool HadError() const { return had_error_; } - - // Instructs the EpsCopyOutputStream to allow the underlying - // ZeroCopyOutputStream to hold pointers to the original structure instead of - // copying, if it supports it (i.e. output->AllowsAliasing() is true). If the - // underlying stream does not support aliasing, then enabling it has no - // affect. For now, this only affects the behavior of - // WriteRawMaybeAliased(). - // - // NOTE: It is caller's responsibility to ensure that the chunk of memory - // remains live until all of the data has been consumed from the stream. - void EnableAliasing(bool enabled); - - // See documentation on CodedOutputStream::SetSerializationDeterministic. - void SetSerializationDeterministic(bool value) { - is_serialization_deterministic_ = value; - } - - // See documentation on CodedOutputStream::IsSerializationDeterministic. - bool IsSerializationDeterministic() const { - return is_serialization_deterministic_; - } - - // The number of bytes written to the stream at position ptr, relative to the - // stream's overall position. - int64 ByteCount(uint8* ptr) const; - - - private: - uint8* end_; - uint8* buffer_end_ = buffer_; - uint8 buffer_[2 * kSlopBytes]; - ZeroCopyOutputStream* stream_; - bool had_error_ = false; - bool aliasing_enabled_ = false; // See EnableAliasing(). - bool is_serialization_deterministic_; - - uint8* EnsureSpaceFallback(uint8* ptr); - inline uint8* Next(); - int Flush(uint8* ptr); - std::ptrdiff_t GetSize(uint8* ptr) const { - GOOGLE_DCHECK(ptr <= end_ + kSlopBytes); // NOLINT - return end_ + kSlopBytes - ptr; - } - - uint8* Error() { - had_error_ = true; - // We use the patch buffer to always guarantee space to write to. - end_ = buffer_ + kSlopBytes; - return buffer_; - } - - static constexpr int TagSize(uint32 tag) { - return (tag < (1 << 7)) - ? 1 - : (tag < (1 << 14)) - ? 2 - : (tag < (1 << 21)) ? 3 : (tag < (1 << 28)) ? 4 : 5; - } - - PROTOBUF_ALWAYS_INLINE uint8* WriteTag(uint32 num, uint32 wt, uint8* ptr) { - GOOGLE_DCHECK(ptr < end_); // NOLINT - return UnsafeVarint((num << 3) | wt, ptr); - } - - PROTOBUF_ALWAYS_INLINE uint8* WriteLengthDelim(int num, uint32 size, - uint8* ptr) { - ptr = WriteTag(num, 2, ptr); - return UnsafeWriteSize(size, ptr); - } - - uint8* WriteRawFallback(const void* data, int size, uint8* ptr); - - uint8* WriteAliasedRaw(const void* data, int size, uint8* ptr); - - uint8* WriteStringMaybeAliasedOutline(uint32 num, const std::string& s, - uint8* ptr); - uint8* WriteStringOutline(uint32 num, const std::string& s, uint8* ptr); - - template - PROTOBUF_ALWAYS_INLINE uint8* WriteVarintPacked(int num, const T& r, int size, - uint8* ptr, const E& encode) { - ptr = EnsureSpace(ptr); - ptr = WriteLengthDelim(num, size, ptr); - auto it = r.data(); - auto end = it + r.size(); - do { - ptr = EnsureSpace(ptr); - ptr = UnsafeVarint(encode(*it++), ptr); - } while (it < end); - return ptr; - } - - static uint32 Encode32(uint32 v) { return v; } - static uint64 Encode64(uint64 v) { return v; } - static uint32 ZigZagEncode32(int32 v) { - return (static_cast(v) << 1) ^ static_cast(v >> 31); - } - static uint64 ZigZagEncode64(int64 v) { - return (static_cast(v) << 1) ^ static_cast(v >> 63); - } - - template - PROTOBUF_ALWAYS_INLINE static uint8* UnsafeVarint(T value, uint8* ptr) { - static_assert(std::is_unsigned::value, - "Varint serialization must be unsigned"); - if (value < 0x80) { - ptr[0] = static_cast(value); - return ptr + 1; - } - ptr[0] = static_cast(value | 0x80); - value >>= 7; - if (value < 0x80) { - ptr[1] = static_cast(value); - return ptr + 2; - } - ptr++; - do { - *ptr = static_cast(value | 0x80); - value >>= 7; - ++ptr; - } while (PROTOBUF_PREDICT_FALSE(value >= 0x80)); - *ptr++ = static_cast(value); - return ptr; - } - - PROTOBUF_ALWAYS_INLINE static uint8* UnsafeWriteSize(uint32 value, - uint8* ptr) { - while (PROTOBUF_PREDICT_FALSE(value >= 0x80)) { - *ptr = static_cast(value | 0x80); - value >>= 7; - ++ptr; - } - *ptr++ = static_cast(value); - return ptr; - } - - template - uint8* WriteRawLittleEndian(const void* data, int size, uint8* ptr); -#ifndef PROTOBUF_LITTLE_ENDIAN - uint8* WriteRawLittleEndian32(const void* data, int size, uint8* ptr); - uint8* WriteRawLittleEndian64(const void* data, int size, uint8* ptr); -#endif - - // These methods are for CodedOutputStream. Ideally they should be private - // but to match current behavior of CodedOutputStream as close as possible - // we allow it some functionality. - public: - uint8* SetInitialBuffer(void* data, int size) { - auto ptr = static_cast(data); - if (size > kSlopBytes) { - end_ = ptr + size - kSlopBytes; - buffer_end_ = nullptr; - return ptr; - } else { - end_ = buffer_ + size; - buffer_end_ = ptr; - return buffer_; - } - } - - private: - // Needed by CodedOutputStream HadError. HadError needs to flush the patch - // buffers to ensure there is no error as of yet. - uint8* FlushAndResetBuffer(uint8*); - - // The following functions mimic the old CodedOutputStream behavior as close - // as possible. They flush the current state to the stream, behave as - // the old CodedOutputStream and then return to normal operation. - bool Skip(int count, uint8** pp); - bool GetDirectBufferPointer(void** data, int* size, uint8** pp); - uint8* GetDirectBufferForNBytesAndAdvance(int size, uint8** pp); - - friend class CodedOutputStream; -}; - -template <> -inline uint8* EpsCopyOutputStream::WriteRawLittleEndian<1>(const void* data, - int size, - uint8* ptr) { - return WriteRaw(data, size, ptr); -} -template <> -inline uint8* EpsCopyOutputStream::WriteRawLittleEndian<4>(const void* data, - int size, - uint8* ptr) { -#ifdef PROTOBUF_LITTLE_ENDIAN - return WriteRaw(data, size, ptr); -#else - return WriteRawLittleEndian32(data, size, ptr); -#endif -} -template <> -inline uint8* EpsCopyOutputStream::WriteRawLittleEndian<8>(const void* data, - int size, - uint8* ptr) { -#ifdef PROTOBUF_LITTLE_ENDIAN - return WriteRaw(data, size, ptr); -#else - return WriteRawLittleEndian64(data, size, ptr); -#endif -} - -// Class which encodes and writes binary data which is composed of varint- -// encoded integers and fixed-width pieces. Wraps a ZeroCopyOutputStream. -// Most users will not need to deal with CodedOutputStream. -// -// Most methods of CodedOutputStream which return a bool return false if an -// underlying I/O error occurs. Once such a failure occurs, the -// CodedOutputStream is broken and is no longer useful. The Write* methods do -// not return the stream status, but will invalidate the stream if an error -// occurs. The client can probe HadError() to determine the status. -// -// Note that every method of CodedOutputStream which writes some data has -// a corresponding static "ToArray" version. These versions write directly -// to the provided buffer, returning a pointer past the last written byte. -// They require that the buffer has sufficient capacity for the encoded data. -// This allows an optimization where we check if an output stream has enough -// space for an entire message before we start writing and, if there is, we -// call only the ToArray methods to avoid doing bound checks for each -// individual value. -// i.e., in the example above: -// -// CodedOutputStream* coded_output = new CodedOutputStream(raw_output); -// int magic_number = 1234; -// char text[] = "Hello world!"; -// -// int coded_size = sizeof(magic_number) + -// CodedOutputStream::VarintSize32(strlen(text)) + -// strlen(text); -// -// uint8* buffer = -// coded_output->GetDirectBufferForNBytesAndAdvance(coded_size); -// if (buffer != nullptr) { -// // The output stream has enough space in the buffer: write directly to -// // the array. -// buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number, -// buffer); -// buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer); -// buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer); -// } else { -// // Make bound-checked writes, which will ask the underlying stream for -// // more space as needed. -// coded_output->WriteLittleEndian32(magic_number); -// coded_output->WriteVarint32(strlen(text)); -// coded_output->WriteRaw(text, strlen(text)); -// } -// -// delete coded_output; -class PROTOBUF_EXPORT CodedOutputStream { - public: - // Create an CodedOutputStream that writes to the given ZeroCopyOutputStream. - explicit CodedOutputStream(ZeroCopyOutputStream* stream) - : CodedOutputStream(stream, true) {} - CodedOutputStream(ZeroCopyOutputStream* stream, bool do_eager_refresh); - - // Destroy the CodedOutputStream and position the underlying - // ZeroCopyOutputStream immediately after the last byte written. - ~CodedOutputStream(); - - // Returns true if there was an underlying I/O error since this object was - // created. On should call Trim before this function in order to catch all - // errors. - bool HadError() { - cur_ = impl_.FlushAndResetBuffer(cur_); - GOOGLE_DCHECK(cur_); - return impl_.HadError(); - } - - // Trims any unused space in the underlying buffer so that its size matches - // the number of bytes written by this stream. The underlying buffer will - // automatically be trimmed when this stream is destroyed; this call is only - // necessary if the underlying buffer is accessed *before* the stream is - // destroyed. - void Trim() { cur_ = impl_.Trim(cur_); } - - // Skips a number of bytes, leaving the bytes unmodified in the underlying - // buffer. Returns false if an underlying write error occurs. This is - // mainly useful with GetDirectBufferPointer(). - // Note of caution, the skipped bytes may contain uninitialized data. The - // caller must make sure that the skipped bytes are properly initialized, - // otherwise you might leak bytes from your heap. - bool Skip(int count) { return impl_.Skip(count, &cur_); } - - // Sets *data to point directly at the unwritten part of the - // CodedOutputStream's underlying buffer, and *size to the size of that - // buffer, but does not advance the stream's current position. This will - // always either produce a non-empty buffer or return false. If the caller - // writes any data to this buffer, it should then call Skip() to skip over - // the consumed bytes. This may be useful for implementing external fast - // serialization routines for types of data not covered by the - // CodedOutputStream interface. - bool GetDirectBufferPointer(void** data, int* size) { - return impl_.GetDirectBufferPointer(data, size, &cur_); - } - - // If there are at least "size" bytes available in the current buffer, - // returns a pointer directly into the buffer and advances over these bytes. - // The caller may then write directly into this buffer (e.g. using the - // *ToArray static methods) rather than go through CodedOutputStream. If - // there are not enough bytes available, returns NULL. The return pointer is - // invalidated as soon as any other non-const method of CodedOutputStream - // is called. - inline uint8* GetDirectBufferForNBytesAndAdvance(int size) { - return impl_.GetDirectBufferForNBytesAndAdvance(size, &cur_); - } - - // Write raw bytes, copying them from the given buffer. - void WriteRaw(const void* buffer, int size) { - cur_ = impl_.WriteRaw(buffer, size, cur_); - } - // Like WriteRaw() but will try to write aliased data if aliasing is - // turned on. - void WriteRawMaybeAliased(const void* data, int size); - // Like WriteRaw() but writing directly to the target array. - // This is _not_ inlined, as the compiler often optimizes memcpy into inline - // copy loops. Since this gets called by every field with string or bytes - // type, inlining may lead to a significant amount of code bloat, with only a - // minor performance gain. - static uint8* WriteRawToArray(const void* buffer, int size, uint8* target); - - // Equivalent to WriteRaw(str.data(), str.size()). - void WriteString(const std::string& str); - // Like WriteString() but writing directly to the target array. - static uint8* WriteStringToArray(const std::string& str, uint8* target); - // Write the varint-encoded size of str followed by str. - static uint8* WriteStringWithSizeToArray(const std::string& str, - uint8* target); - - - // Write a 32-bit little-endian integer. - void WriteLittleEndian32(uint32 value) { - cur_ = impl_.EnsureSpace(cur_); - SetCur(WriteLittleEndian32ToArray(value, Cur())); - } - // Like WriteLittleEndian32() but writing directly to the target array. - static uint8* WriteLittleEndian32ToArray(uint32 value, uint8* target); - // Write a 64-bit little-endian integer. - void WriteLittleEndian64(uint64 value) { - cur_ = impl_.EnsureSpace(cur_); - SetCur(WriteLittleEndian64ToArray(value, Cur())); - } - // Like WriteLittleEndian64() but writing directly to the target array. - static uint8* WriteLittleEndian64ToArray(uint64 value, uint8* target); - - // Write an unsigned integer with Varint encoding. Writing a 32-bit value - // is equivalent to casting it to uint64 and writing it as a 64-bit value, - // but may be more efficient. - void WriteVarint32(uint32 value); - // Like WriteVarint32() but writing directly to the target array. - static uint8* WriteVarint32ToArray(uint32 value, uint8* target); - // Write an unsigned integer with Varint encoding. - void WriteVarint64(uint64 value); - // Like WriteVarint64() but writing directly to the target array. - static uint8* WriteVarint64ToArray(uint64 value, uint8* target); - - // Equivalent to WriteVarint32() except when the value is negative, - // in which case it must be sign-extended to a full 10 bytes. - void WriteVarint32SignExtended(int32 value); - // Like WriteVarint32SignExtended() but writing directly to the target array. - static uint8* WriteVarint32SignExtendedToArray(int32 value, uint8* target); - - // This is identical to WriteVarint32(), but optimized for writing tags. - // In particular, if the input is a compile-time constant, this method - // compiles down to a couple instructions. - // Always inline because otherwise the aforementioned optimization can't work, - // but GCC by default doesn't want to inline this. - void WriteTag(uint32 value); - // Like WriteTag() but writing directly to the target array. - PROTOBUF_ALWAYS_INLINE - static uint8* WriteTagToArray(uint32 value, uint8* target); - - // Returns the number of bytes needed to encode the given value as a varint. - static size_t VarintSize32(uint32 value); - // Returns the number of bytes needed to encode the given value as a varint. - static size_t VarintSize64(uint64 value); - - // If negative, 10 bytes. Otherwise, same as VarintSize32(). - static size_t VarintSize32SignExtended(int32 value); - - // Compile-time equivalent of VarintSize32(). - template - struct StaticVarintSize32 { - static const size_t value = - (Value < (1 << 7)) - ? 1 - : (Value < (1 << 14)) - ? 2 - : (Value < (1 << 21)) ? 3 : (Value < (1 << 28)) ? 4 : 5; - }; - - // Returns the total number of bytes written since this object was created. - int ByteCount() const { - return static_cast(impl_.ByteCount(cur_) - start_count_); - } - - // Instructs the CodedOutputStream to allow the underlying - // ZeroCopyOutputStream to hold pointers to the original structure instead of - // copying, if it supports it (i.e. output->AllowsAliasing() is true). If the - // underlying stream does not support aliasing, then enabling it has no - // affect. For now, this only affects the behavior of - // WriteRawMaybeAliased(). - // - // NOTE: It is caller's responsibility to ensure that the chunk of memory - // remains live until all of the data has been consumed from the stream. - void EnableAliasing(bool enabled) { impl_.EnableAliasing(enabled); } - - // Indicate to the serializer whether the user wants derministic - // serialization. The default when this is not called comes from the global - // default, controlled by SetDefaultSerializationDeterministic. - // - // What deterministic serialization means is entirely up to the driver of the - // serialization process (i.e. the caller of methods like WriteVarint32). In - // the case of serializing a proto buffer message using one of the methods of - // MessageLite, this means that for a given binary equal messages will always - // be serialized to the same bytes. This implies: - // - // * Repeated serialization of a message will return the same bytes. - // - // * Different processes running the same binary (including on different - // machines) will serialize equal messages to the same bytes. - // - // Note that this is *not* canonical across languages. It is also unstable - // across different builds with intervening message definition changes, due to - // unknown fields. Users who need canonical serialization (e.g. persistent - // storage in a canonical form, fingerprinting) should define their own - // canonicalization specification and implement the serializer using - // reflection APIs rather than relying on this API. - void SetSerializationDeterministic(bool value) { - impl_.SetSerializationDeterministic(value); - } - - // Return whether the user wants deterministic serialization. See above. - bool IsSerializationDeterministic() const { - return impl_.IsSerializationDeterministic(); - } - - static bool IsDefaultSerializationDeterministic() { - return default_serialization_deterministic_.load( - std::memory_order_relaxed) != 0; - } - - template - void Serialize(const Func& func); - - uint8* Cur() const { return cur_; } - void SetCur(uint8* ptr) { cur_ = ptr; } - EpsCopyOutputStream* EpsCopy() { return &impl_; } - - private: - EpsCopyOutputStream impl_; - uint8* cur_; - int64 start_count_; - static std::atomic default_serialization_deterministic_; - - // See above. Other projects may use "friend" to allow them to call this. - // After SetDefaultSerializationDeterministic() completes, all protocol - // buffer serializations will be deterministic by default. Thread safe. - // However, the meaning of "after" is subtle here: to be safe, each thread - // that wants deterministic serialization by default needs to call - // SetDefaultSerializationDeterministic() or ensure on its own that another - // thread has done so. - friend void internal::MapTestForceDeterministic(); - static void SetDefaultSerializationDeterministic() { - default_serialization_deterministic_.store(true, std::memory_order_relaxed); - } - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedOutputStream); -}; - -// inline methods ==================================================== -// The vast majority of varints are only one byte. These inline -// methods optimize for that case. - -inline bool CodedInputStream::ReadVarint32(uint32* value) { - uint32 v = 0; - if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) { - v = *buffer_; - if (v < 0x80) { - *value = v; - Advance(1); - return true; - } - } - int64 result = ReadVarint32Fallback(v); - *value = static_cast(result); - return result >= 0; -} - -inline bool CodedInputStream::ReadVarint64(uint64* value) { - if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && *buffer_ < 0x80) { - *value = *buffer_; - Advance(1); - return true; - } - std::pair p = ReadVarint64Fallback(); - *value = p.first; - return p.second; -} - -inline bool CodedInputStream::ReadVarintSizeAsInt(int* value) { - if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) { - int v = *buffer_; - if (v < 0x80) { - *value = v; - Advance(1); - return true; - } - } - *value = ReadVarintSizeAsIntFallback(); - return *value >= 0; -} - -// static -inline const uint8* CodedInputStream::ReadLittleEndian32FromArray( - const uint8* buffer, uint32* value) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - memcpy(value, buffer, sizeof(*value)); - return buffer + sizeof(*value); -#else - *value = (static_cast(buffer[0])) | - (static_cast(buffer[1]) << 8) | - (static_cast(buffer[2]) << 16) | - (static_cast(buffer[3]) << 24); - return buffer + sizeof(*value); -#endif -} -// static -inline const uint8* CodedInputStream::ReadLittleEndian64FromArray( - const uint8* buffer, uint64* value) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - memcpy(value, buffer, sizeof(*value)); - return buffer + sizeof(*value); -#else - uint32 part0 = (static_cast(buffer[0])) | - (static_cast(buffer[1]) << 8) | - (static_cast(buffer[2]) << 16) | - (static_cast(buffer[3]) << 24); - uint32 part1 = (static_cast(buffer[4])) | - (static_cast(buffer[5]) << 8) | - (static_cast(buffer[6]) << 16) | - (static_cast(buffer[7]) << 24); - *value = static_cast(part0) | (static_cast(part1) << 32); - return buffer + sizeof(*value); -#endif -} - -inline bool CodedInputStream::ReadLittleEndian32(uint32* value) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast(sizeof(*value)))) { - buffer_ = ReadLittleEndian32FromArray(buffer_, value); - return true; - } else { - return ReadLittleEndian32Fallback(value); - } -#else - return ReadLittleEndian32Fallback(value); -#endif -} - -inline bool CodedInputStream::ReadLittleEndian64(uint64* value) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - if (PROTOBUF_PREDICT_TRUE(BufferSize() >= static_cast(sizeof(*value)))) { - buffer_ = ReadLittleEndian64FromArray(buffer_, value); - return true; - } else { - return ReadLittleEndian64Fallback(value); - } -#else - return ReadLittleEndian64Fallback(value); -#endif -} - -inline uint32 CodedInputStream::ReadTagNoLastTag() { - uint32 v = 0; - if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) { - v = *buffer_; - if (v < 0x80) { - Advance(1); - return v; - } - } - v = ReadTagFallback(v); - return v; -} - -inline std::pair CodedInputStream::ReadTagWithCutoffNoLastTag( - uint32 cutoff) { - // In performance-sensitive code we can expect cutoff to be a compile-time - // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at - // compile time. - uint32 first_byte_or_zero = 0; - if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_)) { - // Hot case: buffer_ non_empty, buffer_[0] in [1, 128). - // TODO(gpike): Is it worth rearranging this? E.g., if the number of fields - // is large enough then is it better to check for the two-byte case first? - first_byte_or_zero = buffer_[0]; - if (static_cast(buffer_[0]) > 0) { - const uint32 kMax1ByteVarint = 0x7f; - uint32 tag = buffer_[0]; - Advance(1); - return std::make_pair(tag, cutoff >= kMax1ByteVarint || tag <= cutoff); - } - // Other hot case: cutoff >= 0x80, buffer_ has at least two bytes available, - // and tag is two bytes. The latter is tested by bitwise-and-not of the - // first byte and the second byte. - if (cutoff >= 0x80 && PROTOBUF_PREDICT_TRUE(buffer_ + 1 < buffer_end_) && - PROTOBUF_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) { - const uint32 kMax2ByteVarint = (0x7f << 7) + 0x7f; - uint32 tag = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80); - Advance(2); - // It might make sense to test for tag == 0 now, but it is so rare that - // that we don't bother. A varint-encoded 0 should be one byte unless - // the encoder lost its mind. The second part of the return value of - // this function is allowed to be either true or false if the tag is 0, - // so we don't have to check for tag == 0. We may need to check whether - // it exceeds cutoff. - bool at_or_below_cutoff = cutoff >= kMax2ByteVarint || tag <= cutoff; - return std::make_pair(tag, at_or_below_cutoff); - } - } - // Slow path - const uint32 tag = ReadTagFallback(first_byte_or_zero); - return std::make_pair(tag, static_cast(tag - 1) < cutoff); -} - -inline bool CodedInputStream::LastTagWas(uint32 expected) { - return last_tag_ == expected; -} - -inline bool CodedInputStream::ConsumedEntireMessage() { - return legitimate_message_end_; -} - -inline bool CodedInputStream::ExpectTag(uint32 expected) { - if (expected < (1 << 7)) { - if (PROTOBUF_PREDICT_TRUE(buffer_ < buffer_end_) && - buffer_[0] == expected) { - Advance(1); - return true; - } else { - return false; - } - } else if (expected < (1 << 14)) { - if (PROTOBUF_PREDICT_TRUE(BufferSize() >= 2) && - buffer_[0] == static_cast(expected | 0x80) && - buffer_[1] == static_cast(expected >> 7)) { - Advance(2); - return true; - } else { - return false; - } - } else { - // Don't bother optimizing for larger values. - return false; - } -} - -inline const uint8* CodedInputStream::ExpectTagFromArray(const uint8* buffer, - uint32 expected) { - if (expected < (1 << 7)) { - if (buffer[0] == expected) { - return buffer + 1; - } - } else if (expected < (1 << 14)) { - if (buffer[0] == static_cast(expected | 0x80) && - buffer[1] == static_cast(expected >> 7)) { - return buffer + 2; - } - } - return nullptr; -} - -inline void CodedInputStream::GetDirectBufferPointerInline(const void** data, - int* size) { - *data = buffer_; - *size = static_cast(buffer_end_ - buffer_); -} - -inline bool CodedInputStream::ExpectAtEnd() { - // If we are at a limit we know no more bytes can be read. Otherwise, it's - // hard to say without calling Refresh(), and we'd rather not do that. - - if (buffer_ == buffer_end_ && ((buffer_size_after_limit_ != 0) || - (total_bytes_read_ == current_limit_))) { - last_tag_ = 0; // Pretend we called ReadTag()... - legitimate_message_end_ = true; // ... and it hit EOF. - return true; - } else { - return false; - } -} - -inline int CodedInputStream::CurrentPosition() const { - return total_bytes_read_ - (BufferSize() + buffer_size_after_limit_); -} - -inline void CodedInputStream::Advance(int amount) { buffer_ += amount; } - -inline void CodedInputStream::SetRecursionLimit(int limit) { - recursion_budget_ += limit - recursion_limit_; - recursion_limit_ = limit; -} - -inline bool CodedInputStream::IncrementRecursionDepth() { - --recursion_budget_; - return recursion_budget_ >= 0; -} - -inline void CodedInputStream::DecrementRecursionDepth() { - if (recursion_budget_ < recursion_limit_) ++recursion_budget_; -} - -inline void CodedInputStream::UnsafeDecrementRecursionDepth() { - assert(recursion_budget_ < recursion_limit_); - ++recursion_budget_; -} - -inline void CodedInputStream::SetExtensionRegistry(const DescriptorPool* pool, - MessageFactory* factory) { - extension_pool_ = pool; - extension_factory_ = factory; -} - -inline const DescriptorPool* CodedInputStream::GetExtensionPool() { - return extension_pool_; -} - -inline MessageFactory* CodedInputStream::GetExtensionFactory() { - return extension_factory_; -} - -inline int CodedInputStream::BufferSize() const { - return static_cast(buffer_end_ - buffer_); -} - -inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input) - : buffer_(nullptr), - buffer_end_(nullptr), - input_(input), - total_bytes_read_(0), - overflow_bytes_(0), - last_tag_(0), - legitimate_message_end_(false), - aliasing_enabled_(false), - current_limit_(kint32max), - buffer_size_after_limit_(0), - total_bytes_limit_(kDefaultTotalBytesLimit), - recursion_budget_(default_recursion_limit_), - recursion_limit_(default_recursion_limit_), - extension_pool_(nullptr), - extension_factory_(nullptr) { - // Eagerly Refresh() so buffer space is immediately available. - Refresh(); -} - -inline CodedInputStream::CodedInputStream(const uint8* buffer, int size) - : buffer_(buffer), - buffer_end_(buffer + size), - input_(nullptr), - total_bytes_read_(size), - overflow_bytes_(0), - last_tag_(0), - legitimate_message_end_(false), - aliasing_enabled_(false), - current_limit_(size), - buffer_size_after_limit_(0), - total_bytes_limit_(kDefaultTotalBytesLimit), - recursion_budget_(default_recursion_limit_), - recursion_limit_(default_recursion_limit_), - extension_pool_(nullptr), - extension_factory_(nullptr) { - // Note that setting current_limit_ == size is important to prevent some - // code paths from trying to access input_ and segfaulting. -} - -inline bool CodedInputStream::IsFlat() const { return input_ == nullptr; } - -inline bool CodedInputStream::Skip(int count) { - if (count < 0) return false; // security: count is often user-supplied - - const int original_buffer_size = BufferSize(); - - if (count <= original_buffer_size) { - // Just skipping within the current buffer. Easy. - Advance(count); - return true; - } - - return SkipFallback(count, original_buffer_size); -} - -inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value, - uint8* target) { - return EpsCopyOutputStream::UnsafeVarint(value, target); -} - -inline uint8* CodedOutputStream::WriteVarint64ToArray(uint64 value, - uint8* target) { - return EpsCopyOutputStream::UnsafeVarint(value, target); -} - -inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) { - WriteVarint64(static_cast(value)); -} - -inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray( - int32 value, uint8* target) { - return WriteVarint64ToArray(static_cast(value), target); -} - -inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value, - uint8* target) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - memcpy(target, &value, sizeof(value)); -#else - target[0] = static_cast(value); - target[1] = static_cast(value >> 8); - target[2] = static_cast(value >> 16); - target[3] = static_cast(value >> 24); -#endif - return target + sizeof(value); -} - -inline uint8* CodedOutputStream::WriteLittleEndian64ToArray(uint64 value, - uint8* target) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - memcpy(target, &value, sizeof(value)); -#else - uint32 part0 = static_cast(value); - uint32 part1 = static_cast(value >> 32); - - target[0] = static_cast(part0); - target[1] = static_cast(part0 >> 8); - target[2] = static_cast(part0 >> 16); - target[3] = static_cast(part0 >> 24); - target[4] = static_cast(part1); - target[5] = static_cast(part1 >> 8); - target[6] = static_cast(part1 >> 16); - target[7] = static_cast(part1 >> 24); -#endif - return target + sizeof(value); -} - -inline void CodedOutputStream::WriteVarint32(uint32 value) { - cur_ = impl_.EnsureSpace(cur_); - SetCur(WriteVarint32ToArray(value, Cur())); -} - -inline void CodedOutputStream::WriteVarint64(uint64 value) { - cur_ = impl_.EnsureSpace(cur_); - SetCur(WriteVarint64ToArray(value, Cur())); -} - -inline void CodedOutputStream::WriteTag(uint32 value) { WriteVarint32(value); } - -inline uint8* CodedOutputStream::WriteTagToArray(uint32 value, uint8* target) { - return WriteVarint32ToArray(value, target); -} - -inline size_t CodedOutputStream::VarintSize32(uint32 value) { - // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1 - // Use an explicit multiplication to implement the divide of - // a number in the 1..31 range. - // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is - // undefined. - uint32 log2value = Bits::Log2FloorNonZero(value | 0x1); - return static_cast((log2value * 9 + 73) / 64); -} - -inline size_t CodedOutputStream::VarintSize64(uint64 value) { - // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1 - // Use an explicit multiplication to implement the divide of - // a number in the 1..63 range. - // Explicit OR 0x1 to avoid calling Bits::Log2FloorNonZero(0), which is - // undefined. - uint32 log2value = Bits::Log2FloorNonZero64(value | 0x1); - return static_cast((log2value * 9 + 73) / 64); -} - -inline size_t CodedOutputStream::VarintSize32SignExtended(int32 value) { - if (value < 0) { - return 10; // TODO(kenton): Make this a symbolic constant. - } else { - return VarintSize32(static_cast(value)); - } -} - -inline void CodedOutputStream::WriteString(const std::string& str) { - WriteRaw(str.data(), static_cast(str.size())); -} - -inline void CodedOutputStream::WriteRawMaybeAliased(const void* data, - int size) { - cur_ = impl_.WriteRawMaybeAliased(data, size, cur_); -} - -inline uint8* CodedOutputStream::WriteRawToArray(const void* data, int size, - uint8* target) { - memcpy(target, data, size); - return target + size; -} - -inline uint8* CodedOutputStream::WriteStringToArray(const std::string& str, - uint8* target) { - return WriteRawToArray(str.data(), static_cast(str.size()), target); -} - -} // namespace io -} // namespace protobuf -} // namespace google - -#if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) -#pragma runtime_checks("c", restore) -#endif // _MSC_VER && !defined(__INTEL_COMPILER) - -#include - -#endif // GOOGLE_PROTOBUF_IO_CODED_STREAM_H__ diff --git a/third_party/protobuf-lite/google/protobuf/io/io_win32.h b/third_party/protobuf-lite/google/protobuf/io/io_win32.h deleted file mode 100644 index 7d11dc2b3..000000000 --- a/third_party/protobuf-lite/google/protobuf/io/io_win32.h +++ /dev/null @@ -1,139 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: laszlocsomor@google.com (Laszlo Csomor) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -// This file contains the declarations for Windows implementations of -// commonly used POSIX functions such as open(2) and access(2), as well -// as macro definitions for flags of these functions. -// -// By including this file you'll redefine open/access/etc. to -// ::google::protobuf::io::win32::{open/access/etc.}. -// Make sure you don't include a header that attempts to redeclare or -// redefine these functions, that'll lead to confusing compilation -// errors. It's best to #include this file as the last one to ensure that. -// -// This file is only used on Windows, it's empty on other platforms. - -#ifndef GOOGLE_PROTOBUF_IO_IO_WIN32_H__ -#define GOOGLE_PROTOBUF_IO_IO_WIN32_H__ - -#if defined(_WIN32) - -#include -#include - -#include -#include - -// Compilers on Windows other than MSVC (e.g. Cygwin, MinGW32) define the -// following functions already, except for mkdir. -namespace google { -namespace protobuf { -namespace io { -namespace win32 { - -PROTOBUF_EXPORT FILE* fopen(const char* path, const char* mode); -PROTOBUF_EXPORT int access(const char* path, int mode); -PROTOBUF_EXPORT int chdir(const char* path); -PROTOBUF_EXPORT int close(int fd); -PROTOBUF_EXPORT int dup(int fd); -PROTOBUF_EXPORT int dup2(int fd1, int fd2); -PROTOBUF_EXPORT int mkdir(const char* path, int _mode); -PROTOBUF_EXPORT int open(const char* path, int flags, int mode = 0); -PROTOBUF_EXPORT int read(int fd, void* buffer, size_t size); -PROTOBUF_EXPORT int setmode(int fd, int mode); -PROTOBUF_EXPORT int stat(const char* path, struct _stat* buffer); -PROTOBUF_EXPORT int write(int fd, const void* buffer, size_t size); -PROTOBUF_EXPORT std::wstring testonly_utf8_to_winpath(const char* path); - -enum class ExpandWildcardsResult { - kSuccess = 0, - kErrorNoMatchingFile = 1, - kErrorInputPathConversion = 2, - kErrorOutputPathConversion = 3, -}; - -// Expand wildcards in a path pattern, feed the result to a consumer function. -// -// `path` must be a valid, Windows-style path. It may be absolute, or relative -// to the current working directory, and it may contain wildcards ("*" and "?") -// in the last path segment. This function passes all matching file names to -// `consume`. The resulting paths may not be absolute nor normalized. -// -// The function returns a value from `ExpandWildcardsResult`. -PROTOBUF_EXPORT ExpandWildcardsResult ExpandWildcards( - const std::string& path, std::function consume); - -namespace strings { - -// Convert from UTF-16 to Active-Code-Page-encoded or to UTF-8-encoded text. -PROTOBUF_EXPORT bool wcs_to_mbs(const wchar_t* s, std::string* out, - bool outUtf8); - -// Convert from Active-Code-Page-encoded or UTF-8-encoded text to UTF-16. -PROTOBUF_EXPORT bool mbs_to_wcs(const char* s, std::wstring* out, bool inUtf8); - -// Convert from UTF-8-encoded text to UTF-16. -PROTOBUF_EXPORT bool utf8_to_wcs(const char* input, std::wstring* out); - -// Convert from UTF-16-encoded text to UTF-8. -PROTOBUF_EXPORT bool wcs_to_utf8(const wchar_t* input, std::string* out); - -} // namespace strings - -} // namespace win32 -} // namespace io -} // namespace protobuf -} // namespace google - -#ifndef W_OK -#define W_OK 02 // not defined by MSVC for whatever reason -#endif - -#ifndef F_OK -#define F_OK 00 // not defined by MSVC for whatever reason -#endif - -#ifndef STDIN_FILENO -#define STDIN_FILENO 0 -#endif - -#ifndef STDOUT_FILENO -#define STDOUT_FILENO 1 -#endif - -#include - -#endif // defined(_WIN32) - -#endif // GOOGLE_PROTOBUF_IO_IO_WIN32_H__ diff --git a/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream.h b/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream.h deleted file mode 100644 index d3bd6daed..000000000 --- a/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream.h +++ /dev/null @@ -1,253 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This file contains the ZeroCopyInputStream and ZeroCopyOutputStream -// interfaces, which represent abstract I/O streams to and from which -// protocol buffers can be read and written. For a few simple -// implementations of these interfaces, see zero_copy_stream_impl.h. -// -// These interfaces are different from classic I/O streams in that they -// try to minimize the amount of data copying that needs to be done. -// To accomplish this, responsibility for allocating buffers is moved to -// the stream object, rather than being the responsibility of the caller. -// So, the stream can return a buffer which actually points directly into -// the final data structure where the bytes are to be stored, and the caller -// can interact directly with that buffer, eliminating an intermediate copy -// operation. -// -// As an example, consider the common case in which you are reading bytes -// from an array that is already in memory (or perhaps an mmap()ed file). -// With classic I/O streams, you would do something like: -// char buffer[BUFFER_SIZE]; -// input->Read(buffer, BUFFER_SIZE); -// DoSomething(buffer, BUFFER_SIZE); -// Then, the stream basically just calls memcpy() to copy the data from -// the array into your buffer. With a ZeroCopyInputStream, you would do -// this instead: -// const void* buffer; -// int size; -// input->Next(&buffer, &size); -// DoSomething(buffer, size); -// Here, no copy is performed. The input stream returns a pointer directly -// into the backing array, and the caller ends up reading directly from it. -// -// If you want to be able to read the old-fashion way, you can create -// a CodedInputStream or CodedOutputStream wrapping these objects and use -// their ReadRaw()/WriteRaw() methods. These will, of course, add a copy -// step, but Coded*Stream will handle buffering so at least it will be -// reasonably efficient. -// -// ZeroCopyInputStream example: -// // Read in a file and print its contents to stdout. -// int fd = open("myfile", O_RDONLY); -// ZeroCopyInputStream* input = new FileInputStream(fd); -// -// const void* buffer; -// int size; -// while (input->Next(&buffer, &size)) { -// cout.write(buffer, size); -// } -// -// delete input; -// close(fd); -// -// ZeroCopyOutputStream example: -// // Copy the contents of "infile" to "outfile", using plain read() for -// // "infile" but a ZeroCopyOutputStream for "outfile". -// int infd = open("infile", O_RDONLY); -// int outfd = open("outfile", O_WRONLY); -// ZeroCopyOutputStream* output = new FileOutputStream(outfd); -// -// void* buffer; -// int size; -// while (output->Next(&buffer, &size)) { -// int bytes = read(infd, buffer, size); -// if (bytes < size) { -// // Reached EOF. -// output->BackUp(size - bytes); -// break; -// } -// } -// -// delete output; -// close(infd); -// close(outfd); - -#ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_H__ -#define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_H__ - - -#include - -#include -#include - - -namespace google { -namespace protobuf { -namespace io { - -// Defined in this file. -class ZeroCopyInputStream; -class ZeroCopyOutputStream; - -// Abstract interface similar to an input stream but designed to minimize -// copying. -class PROTOBUF_EXPORT ZeroCopyInputStream { - public: - ZeroCopyInputStream() {} - virtual ~ZeroCopyInputStream() {} - - // Obtains a chunk of data from the stream. - // - // Preconditions: - // * "size" and "data" are not NULL. - // - // Postconditions: - // * If the returned value is false, there is no more data to return or - // an error occurred. All errors are permanent. - // * Otherwise, "size" points to the actual number of bytes read and "data" - // points to a pointer to a buffer containing these bytes. - // * Ownership of this buffer remains with the stream, and the buffer - // remains valid only until some other method of the stream is called - // or the stream is destroyed. - // * It is legal for the returned buffer to have zero size, as long - // as repeatedly calling Next() eventually yields a buffer with non-zero - // size. - virtual bool Next(const void** data, int* size) = 0; - - // Backs up a number of bytes, so that the next call to Next() returns - // data again that was already returned by the last call to Next(). This - // is useful when writing procedures that are only supposed to read up - // to a certain point in the input, then return. If Next() returns a - // buffer that goes beyond what you wanted to read, you can use BackUp() - // to return to the point where you intended to finish. - // - // Preconditions: - // * The last method called must have been Next(). - // * count must be less than or equal to the size of the last buffer - // returned by Next(). - // - // Postconditions: - // * The last "count" bytes of the last buffer returned by Next() will be - // pushed back into the stream. Subsequent calls to Next() will return - // the same data again before producing new data. - virtual void BackUp(int count) = 0; - - // Skips a number of bytes. Returns false if the end of the stream is - // reached or some input error occurred. In the end-of-stream case, the - // stream is advanced to the end of the stream (so ByteCount() will return - // the total size of the stream). - virtual bool Skip(int count) = 0; - - // Returns the total number of bytes read since this object was created. - virtual int64_t ByteCount() const = 0; - - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyInputStream); -}; - -// Abstract interface similar to an output stream but designed to minimize -// copying. -class PROTOBUF_EXPORT ZeroCopyOutputStream { - public: - ZeroCopyOutputStream() {} - virtual ~ZeroCopyOutputStream() {} - - // Obtains a buffer into which data can be written. Any data written - // into this buffer will eventually (maybe instantly, maybe later on) - // be written to the output. - // - // Preconditions: - // * "size" and "data" are not NULL. - // - // Postconditions: - // * If the returned value is false, an error occurred. All errors are - // permanent. - // * Otherwise, "size" points to the actual number of bytes in the buffer - // and "data" points to the buffer. - // * Ownership of this buffer remains with the stream, and the buffer - // remains valid only until some other method of the stream is called - // or the stream is destroyed. - // * Any data which the caller stores in this buffer will eventually be - // written to the output (unless BackUp() is called). - // * It is legal for the returned buffer to have zero size, as long - // as repeatedly calling Next() eventually yields a buffer with non-zero - // size. - virtual bool Next(void** data, int* size) = 0; - - // Backs up a number of bytes, so that the end of the last buffer returned - // by Next() is not actually written. This is needed when you finish - // writing all the data you want to write, but the last buffer was bigger - // than you needed. You don't want to write a bunch of garbage after the - // end of your data, so you use BackUp() to back up. - // - // Preconditions: - // * The last method called must have been Next(). - // * count must be less than or equal to the size of the last buffer - // returned by Next(). - // * The caller must not have written anything to the last "count" bytes - // of that buffer. - // - // Postconditions: - // * The last "count" bytes of the last buffer returned by Next() will be - // ignored. - virtual void BackUp(int count) = 0; - - // Returns the total number of bytes written since this object was created. - virtual int64_t ByteCount() const = 0; - - // Write a given chunk of data to the output. Some output streams may - // implement this in a way that avoids copying. Check AllowsAliasing() before - // calling WriteAliasedRaw(). It will GOOGLE_CHECK fail if WriteAliasedRaw() is - // called on a stream that does not allow aliasing. - // - // NOTE: It is caller's responsibility to ensure that the chunk of memory - // remains live until all of the data has been consumed from the stream. - virtual bool WriteAliasedRaw(const void* data, int size); - virtual bool AllowsAliasing() const { return false; } - - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyOutputStream); -}; - -} // namespace io -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_H__ diff --git a/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl.h b/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl.h deleted file mode 100644 index 0206e3887..000000000 --- a/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl.h +++ /dev/null @@ -1,327 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This file contains common implementations of the interfaces defined in -// zero_copy_stream.h which are only included in the full (non-lite) -// protobuf library. These implementations include Unix file descriptors -// and C++ iostreams. See also: zero_copy_stream_impl_lite.h - -#ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ -#define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ - - -#include -#include - -#include -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace io { - -// =================================================================== - -// A ZeroCopyInputStream which reads from a file descriptor. -// -// FileInputStream is preferred over using an ifstream with IstreamInputStream. -// The latter will introduce an extra layer of buffering, harming performance. -// Also, it's conceivable that FileInputStream could someday be enhanced -// to use zero-copy file descriptors on OSs which support them. -class PROTOBUF_EXPORT FileInputStream : public ZeroCopyInputStream { - public: - // Creates a stream that reads from the given Unix file descriptor. - // If a block_size is given, it specifies the number of bytes that - // should be read and returned with each call to Next(). Otherwise, - // a reasonable default is used. - explicit FileInputStream(int file_descriptor, int block_size = -1); - - // Flushes any buffers and closes the underlying file. Returns false if - // an error occurs during the process; use GetErrno() to examine the error. - // Even if an error occurs, the file descriptor is closed when this returns. - bool Close(); - - // By default, the file descriptor is not closed when the stream is - // destroyed. Call SetCloseOnDelete(true) to change that. WARNING: - // This leaves no way for the caller to detect if close() fails. If - // detecting close() errors is important to you, you should arrange - // to close the descriptor yourself. - void SetCloseOnDelete(bool value) { copying_input_.SetCloseOnDelete(value); } - - // If an I/O error has occurred on this file descriptor, this is the - // errno from that error. Otherwise, this is zero. Once an error - // occurs, the stream is broken and all subsequent operations will - // fail. - int GetErrno() const { return copying_input_.GetErrno(); } - - // implements ZeroCopyInputStream ---------------------------------- - bool Next(const void** data, int* size) override; - void BackUp(int count) override; - bool Skip(int count) override; - int64_t ByteCount() const override; - - private: - class PROTOBUF_EXPORT CopyingFileInputStream : public CopyingInputStream { - public: - CopyingFileInputStream(int file_descriptor); - ~CopyingFileInputStream() override; - - bool Close(); - void SetCloseOnDelete(bool value) { close_on_delete_ = value; } - int GetErrno() const { return errno_; } - - // implements CopyingInputStream --------------------------------- - int Read(void* buffer, int size) override; - int Skip(int count) override; - - private: - // The file descriptor. - const int file_; - bool close_on_delete_; - bool is_closed_; - - // The errno of the I/O error, if one has occurred. Otherwise, zero. - int errno_; - - // Did we try to seek once and fail? If so, we assume this file descriptor - // doesn't support seeking and won't try again. - bool previous_seek_failed_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingFileInputStream); - }; - - CopyingFileInputStream copying_input_; - CopyingInputStreamAdaptor impl_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileInputStream); -}; - -// =================================================================== - -// A ZeroCopyOutputStream which writes to a file descriptor. -// -// FileOutputStream is preferred over using an ofstream with -// OstreamOutputStream. The latter will introduce an extra layer of buffering, -// harming performance. Also, it's conceivable that FileOutputStream could -// someday be enhanced to use zero-copy file descriptors on OSs which -// support them. -class PROTOBUF_EXPORT FileOutputStream : public CopyingOutputStreamAdaptor { - public: - // Creates a stream that writes to the given Unix file descriptor. - // If a block_size is given, it specifies the size of the buffers - // that should be returned by Next(). Otherwise, a reasonable default - // is used. - explicit FileOutputStream(int file_descriptor, int block_size = -1); - - ~FileOutputStream() override; - - // Flushes any buffers and closes the underlying file. Returns false if - // an error occurs during the process; use GetErrno() to examine the error. - // Even if an error occurs, the file descriptor is closed when this returns. - bool Close(); - - // By default, the file descriptor is not closed when the stream is - // destroyed. Call SetCloseOnDelete(true) to change that. WARNING: - // This leaves no way for the caller to detect if close() fails. If - // detecting close() errors is important to you, you should arrange - // to close the descriptor yourself. - void SetCloseOnDelete(bool value) { copying_output_.SetCloseOnDelete(value); } - - // If an I/O error has occurred on this file descriptor, this is the - // errno from that error. Otherwise, this is zero. Once an error - // occurs, the stream is broken and all subsequent operations will - // fail. - int GetErrno() const { return copying_output_.GetErrno(); } - - private: - class PROTOBUF_EXPORT CopyingFileOutputStream : public CopyingOutputStream { - public: - CopyingFileOutputStream(int file_descriptor); - ~CopyingFileOutputStream() override; - - bool Close(); - void SetCloseOnDelete(bool value) { close_on_delete_ = value; } - int GetErrno() const { return errno_; } - - // implements CopyingOutputStream -------------------------------- - bool Write(const void* buffer, int size) override; - - private: - // The file descriptor. - const int file_; - bool close_on_delete_; - bool is_closed_; - - // The errno of the I/O error, if one has occurred. Otherwise, zero. - int errno_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingFileOutputStream); - }; - - CopyingFileOutputStream copying_output_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileOutputStream); -}; - -// =================================================================== - -// A ZeroCopyInputStream which reads from a C++ istream. -// -// Note that for reading files (or anything represented by a file descriptor), -// FileInputStream is more efficient. -class PROTOBUF_EXPORT IstreamInputStream : public ZeroCopyInputStream { - public: - // Creates a stream that reads from the given C++ istream. - // If a block_size is given, it specifies the number of bytes that - // should be read and returned with each call to Next(). Otherwise, - // a reasonable default is used. - explicit IstreamInputStream(std::istream* stream, int block_size = -1); - - // implements ZeroCopyInputStream ---------------------------------- - bool Next(const void** data, int* size) override; - void BackUp(int count) override; - bool Skip(int count) override; - int64_t ByteCount() const override; - - private: - class PROTOBUF_EXPORT CopyingIstreamInputStream : public CopyingInputStream { - public: - CopyingIstreamInputStream(std::istream* input); - ~CopyingIstreamInputStream() override; - - // implements CopyingInputStream --------------------------------- - int Read(void* buffer, int size) override; - // (We use the default implementation of Skip().) - - private: - // The stream. - std::istream* input_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingIstreamInputStream); - }; - - CopyingIstreamInputStream copying_input_; - CopyingInputStreamAdaptor impl_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(IstreamInputStream); -}; - -// =================================================================== - -// A ZeroCopyOutputStream which writes to a C++ ostream. -// -// Note that for writing files (or anything represented by a file descriptor), -// FileOutputStream is more efficient. -class PROTOBUF_EXPORT OstreamOutputStream : public ZeroCopyOutputStream { - public: - // Creates a stream that writes to the given C++ ostream. - // If a block_size is given, it specifies the size of the buffers - // that should be returned by Next(). Otherwise, a reasonable default - // is used. - explicit OstreamOutputStream(std::ostream* stream, int block_size = -1); - ~OstreamOutputStream() override; - - // implements ZeroCopyOutputStream --------------------------------- - bool Next(void** data, int* size) override; - void BackUp(int count) override; - int64_t ByteCount() const override; - - private: - class PROTOBUF_EXPORT CopyingOstreamOutputStream - : public CopyingOutputStream { - public: - CopyingOstreamOutputStream(std::ostream* output); - ~CopyingOstreamOutputStream() override; - - // implements CopyingOutputStream -------------------------------- - bool Write(const void* buffer, int size) override; - - private: - // The stream. - std::ostream* output_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOstreamOutputStream); - }; - - CopyingOstreamOutputStream copying_output_; - CopyingOutputStreamAdaptor impl_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OstreamOutputStream); -}; - -// =================================================================== - -// A ZeroCopyInputStream which reads from several other streams in sequence. -// ConcatenatingInputStream is unable to distinguish between end-of-stream -// and read errors in the underlying streams, so it assumes any errors mean -// end-of-stream. So, if the underlying streams fail for any other reason, -// ConcatenatingInputStream may do odd things. It is suggested that you do -// not use ConcatenatingInputStream on streams that might produce read errors -// other than end-of-stream. -class PROTOBUF_EXPORT ConcatenatingInputStream : public ZeroCopyInputStream { - public: - // All streams passed in as well as the array itself must remain valid - // until the ConcatenatingInputStream is destroyed. - ConcatenatingInputStream(ZeroCopyInputStream* const streams[], int count); - ~ConcatenatingInputStream() override = default; - - // implements ZeroCopyInputStream ---------------------------------- - bool Next(const void** data, int* size) override; - void BackUp(int count) override; - bool Skip(int count) override; - int64_t ByteCount() const override; - - - private: - // As streams are retired, streams_ is incremented and count_ is - // decremented. - ZeroCopyInputStream* const* streams_; - int stream_count_; - int64 bytes_retired_; // Bytes read from previous streams. - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ConcatenatingInputStream); -}; - -// =================================================================== - -} // namespace io -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__ diff --git a/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl_lite.h b/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl_lite.h deleted file mode 100644 index cfe81d2cc..000000000 --- a/third_party/protobuf-lite/google/protobuf/io/zero_copy_stream_impl_lite.h +++ /dev/null @@ -1,408 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This file contains common implementations of the interfaces defined in -// zero_copy_stream.h which are included in the "lite" protobuf library. -// These implementations cover I/O on raw arrays and strings, as well as -// adaptors which make it easy to implement streams based on traditional -// streams. Of course, many users will probably want to write their own -// implementations of these interfaces specific to the particular I/O -// abstractions they prefer to use, but these should cover the most common -// cases. - -#ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__ -#define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__ - - -#include -#include -#include - -#include -#include -#include -#include - - -#include - -namespace google { -namespace protobuf { -namespace io { - -// =================================================================== - -// A ZeroCopyInputStream backed by an in-memory array of bytes. -class PROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream { - public: - // Create an InputStream that returns the bytes pointed to by "data". - // "data" remains the property of the caller but must remain valid until - // the stream is destroyed. If a block_size is given, calls to Next() - // will return data blocks no larger than the given size. Otherwise, the - // first call to Next() returns the entire array. block_size is mainly - // useful for testing; in production you would probably never want to set - // it. - ArrayInputStream(const void* data, int size, int block_size = -1); - ~ArrayInputStream() override = default; - - // implements ZeroCopyInputStream ---------------------------------- - bool Next(const void** data, int* size) override; - void BackUp(int count) override; - bool Skip(int count) override; - int64_t ByteCount() const override; - - - private: - const uint8* const data_; // The byte array. - const int size_; // Total size of the array. - const int block_size_; // How many bytes to return at a time. - - int position_; - int last_returned_size_; // How many bytes we returned last time Next() - // was called (used for error checking only). - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream); -}; - -// =================================================================== - -// A ZeroCopyOutputStream backed by an in-memory array of bytes. -class PROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream { - public: - // Create an OutputStream that writes to the bytes pointed to by "data". - // "data" remains the property of the caller but must remain valid until - // the stream is destroyed. If a block_size is given, calls to Next() - // will return data blocks no larger than the given size. Otherwise, the - // first call to Next() returns the entire array. block_size is mainly - // useful for testing; in production you would probably never want to set - // it. - ArrayOutputStream(void* data, int size, int block_size = -1); - ~ArrayOutputStream() override = default; - - // implements ZeroCopyOutputStream --------------------------------- - bool Next(void** data, int* size) override; - void BackUp(int count) override; - int64_t ByteCount() const override; - - private: - uint8* const data_; // The byte array. - const int size_; // Total size of the array. - const int block_size_; // How many bytes to return at a time. - - int position_; - int last_returned_size_; // How many bytes we returned last time Next() - // was called (used for error checking only). - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream); -}; - -// =================================================================== - -// A ZeroCopyOutputStream which appends bytes to a string. -class PROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream { - public: - // Create a StringOutputStream which appends bytes to the given string. - // The string remains property of the caller, but it is mutated in arbitrary - // ways and MUST NOT be accessed in any way until you're done with the - // stream. Either be sure there's no further usage, or (safest) destroy the - // stream before using the contents. - // - // Hint: If you call target->reserve(n) before creating the stream, - // the first call to Next() will return at least n bytes of buffer - // space. - explicit StringOutputStream(std::string* target); - ~StringOutputStream() override = default; - - // implements ZeroCopyOutputStream --------------------------------- - bool Next(void** data, int* size) override; - void BackUp(int count) override; - int64_t ByteCount() const override; - - private: - static constexpr size_t kMinimumSize = 16; - - std::string* target_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream); -}; - -// Note: There is no StringInputStream. Instead, just create an -// ArrayInputStream as follows: -// ArrayInputStream input(str.data(), str.size()); - -// =================================================================== - -// A generic traditional input stream interface. -// -// Lots of traditional input streams (e.g. file descriptors, C stdio -// streams, and C++ iostreams) expose an interface where every read -// involves copying bytes into a buffer. If you want to take such an -// interface and make a ZeroCopyInputStream based on it, simply implement -// CopyingInputStream and then use CopyingInputStreamAdaptor. -// -// CopyingInputStream implementations should avoid buffering if possible. -// CopyingInputStreamAdaptor does its own buffering and will read data -// in large blocks. -class PROTOBUF_EXPORT CopyingInputStream { - public: - virtual ~CopyingInputStream() {} - - // Reads up to "size" bytes into the given buffer. Returns the number of - // bytes read. Read() waits until at least one byte is available, or - // returns zero if no bytes will ever become available (EOF), or -1 if a - // permanent read error occurred. - virtual int Read(void* buffer, int size) = 0; - - // Skips the next "count" bytes of input. Returns the number of bytes - // actually skipped. This will always be exactly equal to "count" unless - // EOF was reached or a permanent read error occurred. - // - // The default implementation just repeatedly calls Read() into a scratch - // buffer. - virtual int Skip(int count); -}; - -// A ZeroCopyInputStream which reads from a CopyingInputStream. This is -// useful for implementing ZeroCopyInputStreams that read from traditional -// streams. Note that this class is not really zero-copy. -// -// If you want to read from file descriptors or C++ istreams, this is -// already implemented for you: use FileInputStream or IstreamInputStream -// respectively. -class PROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream { - public: - // Creates a stream that reads from the given CopyingInputStream. - // If a block_size is given, it specifies the number of bytes that - // should be read and returned with each call to Next(). Otherwise, - // a reasonable default is used. The caller retains ownership of - // copying_stream unless SetOwnsCopyingStream(true) is called. - explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream, - int block_size = -1); - ~CopyingInputStreamAdaptor() override; - - // Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to - // delete the underlying CopyingInputStream when it is destroyed. - void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; } - - // implements ZeroCopyInputStream ---------------------------------- - bool Next(const void** data, int* size) override; - void BackUp(int count) override; - bool Skip(int count) override; - int64_t ByteCount() const override; - - private: - // Insures that buffer_ is not NULL. - void AllocateBufferIfNeeded(); - // Frees the buffer and resets buffer_used_. - void FreeBuffer(); - - // The underlying copying stream. - CopyingInputStream* copying_stream_; - bool owns_copying_stream_; - - // True if we have seen a permanent error from the underlying stream. - bool failed_; - - // The current position of copying_stream_, relative to the point where - // we started reading. - int64 position_; - - // Data is read into this buffer. It may be NULL if no buffer is currently - // in use. Otherwise, it points to an array of size buffer_size_. - std::unique_ptr buffer_; - const int buffer_size_; - - // Number of valid bytes currently in the buffer (i.e. the size last - // returned by Next()). 0 <= buffer_used_ <= buffer_size_. - int buffer_used_; - - // Number of bytes in the buffer which were backed up over by a call to - // BackUp(). These need to be returned again. - // 0 <= backup_bytes_ <= buffer_used_ - int backup_bytes_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingInputStreamAdaptor); -}; - -// =================================================================== - -// A generic traditional output stream interface. -// -// Lots of traditional output streams (e.g. file descriptors, C stdio -// streams, and C++ iostreams) expose an interface where every write -// involves copying bytes from a buffer. If you want to take such an -// interface and make a ZeroCopyOutputStream based on it, simply implement -// CopyingOutputStream and then use CopyingOutputStreamAdaptor. -// -// CopyingOutputStream implementations should avoid buffering if possible. -// CopyingOutputStreamAdaptor does its own buffering and will write data -// in large blocks. -class PROTOBUF_EXPORT CopyingOutputStream { - public: - virtual ~CopyingOutputStream() {} - - // Writes "size" bytes from the given buffer to the output. Returns true - // if successful, false on a write error. - virtual bool Write(const void* buffer, int size) = 0; -}; - -// A ZeroCopyOutputStream which writes to a CopyingOutputStream. This is -// useful for implementing ZeroCopyOutputStreams that write to traditional -// streams. Note that this class is not really zero-copy. -// -// If you want to write to file descriptors or C++ ostreams, this is -// already implemented for you: use FileOutputStream or OstreamOutputStream -// respectively. -class PROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream { - public: - // Creates a stream that writes to the given Unix file descriptor. - // If a block_size is given, it specifies the size of the buffers - // that should be returned by Next(). Otherwise, a reasonable default - // is used. - explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream, - int block_size = -1); - ~CopyingOutputStreamAdaptor() override; - - // Writes all pending data to the underlying stream. Returns false if a - // write error occurred on the underlying stream. (The underlying - // stream itself is not necessarily flushed.) - bool Flush(); - - // Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to - // delete the underlying CopyingOutputStream when it is destroyed. - void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; } - - // implements ZeroCopyOutputStream --------------------------------- - bool Next(void** data, int* size) override; - void BackUp(int count) override; - int64_t ByteCount() const override; - bool WriteAliasedRaw(const void* data, int size) override; - bool AllowsAliasing() const override { return true; } - - private: - // Write the current buffer, if it is present. - bool WriteBuffer(); - // Insures that buffer_ is not NULL. - void AllocateBufferIfNeeded(); - // Frees the buffer. - void FreeBuffer(); - - // The underlying copying stream. - CopyingOutputStream* copying_stream_; - bool owns_copying_stream_; - - // True if we have seen a permanent error from the underlying stream. - bool failed_; - - // The current position of copying_stream_, relative to the point where - // we started writing. - int64 position_; - - // Data is written from this buffer. It may be NULL if no buffer is - // currently in use. Otherwise, it points to an array of size buffer_size_. - std::unique_ptr buffer_; - const int buffer_size_; - - // Number of valid bytes currently in the buffer (i.e. the size last - // returned by Next()). When BackUp() is called, we just reduce this. - // 0 <= buffer_used_ <= buffer_size_. - int buffer_used_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOutputStreamAdaptor); -}; - -// =================================================================== - -// A ZeroCopyInputStream which wraps some other stream and limits it to -// a particular byte count. -class PROTOBUF_EXPORT LimitingInputStream : public ZeroCopyInputStream { - public: - LimitingInputStream(ZeroCopyInputStream* input, int64 limit); - ~LimitingInputStream() override; - - // implements ZeroCopyInputStream ---------------------------------- - bool Next(const void** data, int* size) override; - void BackUp(int count) override; - bool Skip(int count) override; - int64_t ByteCount() const override; - - - private: - ZeroCopyInputStream* input_; - int64 limit_; // Decreases as we go, becomes negative if we overshoot. - int64 prior_bytes_read_; // Bytes read on underlying stream at construction - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LimitingInputStream); -}; - - -// =================================================================== - -// mutable_string_data() and as_string_data() are workarounds to improve -// the performance of writing new data to an existing string. Unfortunately -// the methods provided by the string class are suboptimal, and using memcpy() -// is mildly annoying because it requires its pointer args to be non-NULL even -// if we ask it to copy 0 bytes. Furthermore, string_as_array() has the -// property that it always returns NULL if its arg is the empty string, exactly -// what we want to avoid if we're using it in conjunction with memcpy()! -// With C++11, the desired memcpy() boils down to memcpy(..., &(*s)[0], size), -// where s is a string*. Without C++11, &(*s)[0] is not guaranteed to be safe, -// so we use string_as_array(), and live with the extra logic that tests whether -// *s is empty. - -// Return a pointer to mutable characters underlying the given string. The -// return value is valid until the next time the string is resized. We -// trust the caller to treat the return value as an array of length s->size(). -inline char* mutable_string_data(std::string* s) { - // This should be simpler & faster than string_as_array() because the latter - // is guaranteed to return NULL when *s is empty, so it has to check for that. - return &(*s)[0]; -} - -// as_string_data(s) is equivalent to -// ({ char* p = mutable_string_data(s); make_pair(p, p != NULL); }) -// Sometimes it's faster: in some scenarios p cannot be NULL, and then the -// code can avoid that check. -inline std::pair as_string_data(std::string* s) { - char* p = mutable_string_data(s); - return std::make_pair(p, true); -} - -} // namespace io -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/map.h b/third_party/protobuf-lite/google/protobuf/map.h deleted file mode 100644 index 2453246d2..000000000 --- a/third_party/protobuf-lite/google/protobuf/map.h +++ /dev/null @@ -1,1362 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// This file defines the map container and its helpers to support protobuf maps. -// -// The Map and MapIterator types are provided by this header file. -// Please avoid using other types defined here, unless they are public -// types within Map or MapIterator, such as Map::value_type. - -#ifndef GOOGLE_PROTOBUF_MAP_H__ -#define GOOGLE_PROTOBUF_MAP_H__ - -#include -#include -#include -#include // To support Visual Studio 2008 -#include -#include -#include -#include - -#if defined(__cpp_lib_string_view) -#include -#endif // defined(__cpp_lib_string_view) - -#include -#include -#include -#include -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -#include - -namespace google { -namespace protobuf { - -template -class Map; - -class MapIterator; - -template -struct is_proto_enum; - -namespace internal { -template -class MapFieldLite; - -template -class MapField; - -template -class TypeDefinedMapFieldBase; - -class DynamicMapField; - -class GeneratedMessageReflection; - -// re-implement std::allocator to use arena allocator for memory allocation. -// Used for Map implementation. Users should not use this class -// directly. -template -class MapAllocator { - public: - using value_type = U; - using pointer = value_type*; - using const_pointer = const value_type*; - using reference = value_type&; - using const_reference = const value_type&; - using size_type = size_t; - using difference_type = ptrdiff_t; - - constexpr MapAllocator() : arena_(nullptr) {} - explicit constexpr MapAllocator(Arena* arena) : arena_(arena) {} - template - MapAllocator(const MapAllocator& allocator) // NOLINT(runtime/explicit) - : arena_(allocator.arena()) {} - - pointer allocate(size_type n, const void* /* hint */ = nullptr) { - // If arena is not given, malloc needs to be called which doesn't - // construct element object. - if (arena_ == nullptr) { - return static_cast(::operator new(n * sizeof(value_type))); - } else { - return reinterpret_cast( - Arena::CreateArray(arena_, n * sizeof(value_type))); - } - } - - void deallocate(pointer p, size_type n) { - if (arena_ == nullptr) { -#if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) - ::operator delete(p, n * sizeof(value_type)); -#else - (void)n; - ::operator delete(p); -#endif - } - } - -#if !defined(GOOGLE_PROTOBUF_OS_APPLE) && !defined(GOOGLE_PROTOBUF_OS_NACL) && \ - !defined(GOOGLE_PROTOBUF_OS_EMSCRIPTEN) - template - void construct(NodeType* p, Args&&... args) { - // Clang 3.6 doesn't compile static casting to void* directly. (Issue - // #1266) According C++ standard 5.2.9/1: "The static_cast operator shall - // not cast away constness". So first the maybe const pointer is casted to - // const void* and after the const void* is const casted. - new (const_cast(static_cast(p))) - NodeType(std::forward(args)...); - } - - template - void destroy(NodeType* p) { - p->~NodeType(); - } -#else - void construct(pointer p, const_reference t) { new (p) value_type(t); } - - void destroy(pointer p) { p->~value_type(); } -#endif - - template - struct rebind { - using other = MapAllocator; - }; - - template - bool operator==(const MapAllocator& other) const { - return arena_ == other.arena_; - } - - template - bool operator!=(const MapAllocator& other) const { - return arena_ != other.arena_; - } - - // To support Visual Studio 2008 - size_type max_size() const { - // parentheses around (std::...:max) prevents macro warning of max() - return (std::numeric_limits::max)(); - } - - // To support gcc-4.4, which does not properly - // support templated friend classes - Arena* arena() const { return arena_; } - - private: - using DestructorSkippable_ = void; - Arena* arena_; -}; - -template -using KeyForTree = - typename std::conditional::value, T, - std::reference_wrapper>::type; - -// Default case: Not transparent. -// We use std::hash/std::less and all the lookup functions -// only accept `key_type`. -template -struct TransparentSupport { - using hash = std::hash; - using less = std::less; - - static bool Equals(const key_type& a, const key_type& b) { return a == b; } - - template - using key_arg = key_type; -}; - -#if defined(__cpp_lib_string_view) -// If std::string_view is available, we add transparent support for std::string -// keys. We use std::hash as it supports the input types we -// care about. The lookup functions accept arbitrary `K`. This will include any -// key type that is convertible to std::string_view. -template <> -struct TransparentSupport { - static std::string_view ImplicitConvert(std::string_view str) { return str; } - // If the element is not convertible to std::string_view, try to convert to - // std::string first. - // The template makes this overload lose resolution when both have the same - // rank otherwise. - template - static std::string_view ImplicitConvert(const std::string& str) { - return str; - } - - struct hash : private std::hash { - using is_transparent = void; - - template - size_t operator()(const T& str) const { - return base()(ImplicitConvert(str)); - } - - private: - const std::hash& base() const { return *this; } - }; - struct less { - using is_transparent = void; - - template - bool operator()(const T& t, const U& u) const { - return ImplicitConvert(t) < ImplicitConvert(u); - } - }; - - template - static bool Equals(const T& t, const U& u) { - return ImplicitConvert(t) == ImplicitConvert(u); - } - - template - using key_arg = K; -}; -#endif // defined(__cpp_lib_string_view) - -template -using TreeForMap = - std::map, void*, typename TransparentSupport::less, - MapAllocator, void*>>>; - -inline bool TableEntryIsEmpty(void* const* table, size_t b) { - return table[b] == nullptr; -} -inline bool TableEntryIsNonEmptyList(void* const* table, size_t b) { - return table[b] != nullptr && table[b] != table[b ^ 1]; -} -inline bool TableEntryIsTree(void* const* table, size_t b) { - return !TableEntryIsEmpty(table, b) && !TableEntryIsNonEmptyList(table, b); -} -inline bool TableEntryIsList(void* const* table, size_t b) { - return !TableEntryIsTree(table, b); -} - -// This captures all numeric types. -inline size_t MapValueSpaceUsedExcludingSelfLong(bool) { return 0; } -inline size_t MapValueSpaceUsedExcludingSelfLong(const std::string& str) { - return StringSpaceUsedExcludingSelfLong(str); -} -template ().SpaceUsedLong())> -size_t MapValueSpaceUsedExcludingSelfLong(const T& message) { - return message.SpaceUsedLong() - sizeof(T); -} - -constexpr size_t kGlobalEmptyTableSize = 1; -PROTOBUF_EXPORT extern void* const kGlobalEmptyTable[kGlobalEmptyTableSize]; - -// Space used for the table, trees, and nodes. -// Does not include the indirect space used. Eg the data of a std::string. -template -PROTOBUF_NOINLINE size_t SpaceUsedInTable(void** table, size_t num_buckets, - size_t num_elements, - size_t sizeof_node) { - size_t size = 0; - // The size of the table. - size += sizeof(void*) * num_buckets; - // All the nodes. - size += sizeof_node * num_elements; - // For each tree, count the overhead of the those nodes. - // Two buckets at a time because we only care about trees. - for (size_t b = 0; b < num_buckets; b += 2) { - if (internal::TableEntryIsTree(table, b)) { - using Tree = TreeForMap; - Tree* tree = static_cast(table[b]); - // Estimated cost of the red-black tree nodes, 3 pointers plus a - // bool (plus alignment, so 4 pointers). - size += tree->size() * - (sizeof(typename Tree::value_type) + sizeof(void*) * 4); - } - } - return size; -} - -template ::value || - !std::is_scalar::value>::type> -size_t SpaceUsedInValues(const Map* map) { - size_t size = 0; - for (const auto& v : *map) { - size += internal::MapValueSpaceUsedExcludingSelfLong(v.first) + - internal::MapValueSpaceUsedExcludingSelfLong(v.second); - } - return size; -} - -inline size_t SpaceUsedInValues(const void*) { return 0; } - -} // namespace internal - -// This is the class for Map's internal value_type. Instead of using -// std::pair as value_type, we use this class which provides us more control of -// its process of construction and destruction. -template -struct MapPair { - using first_type = const Key; - using second_type = T; - - MapPair(const Key& other_first, const T& other_second) - : first(other_first), second(other_second) {} - explicit MapPair(const Key& other_first) : first(other_first), second() {} - explicit MapPair(Key&& other_first) - : first(std::move(other_first)), second() {} - MapPair(const MapPair& other) : first(other.first), second(other.second) {} - - ~MapPair() {} - - // Implicitly convertible to std::pair of compatible types. - template - operator std::pair() const { // NOLINT(runtime/explicit) - return std::pair(first, second); - } - - const Key first; - T second; - - private: - friend class Arena; - friend class Map; -}; - -// Map is an associative container type used to store protobuf map -// fields. Each Map instance may or may not use a different hash function, a -// different iteration order, and so on. E.g., please don't examine -// implementation details to decide if the following would work: -// Map m0, m1; -// m0[0] = m1[0] = m0[1] = m1[1] = 0; -// assert(m0.begin()->first == m1.begin()->first); // Bug! -// -// Map's interface is similar to std::unordered_map, except that Map is not -// designed to play well with exceptions. -template -class Map { - public: - using key_type = Key; - using mapped_type = T; - using value_type = MapPair; - - using pointer = value_type*; - using const_pointer = const value_type*; - using reference = value_type&; - using const_reference = const value_type&; - - using size_type = size_t; - using hasher = typename internal::TransparentSupport::hash; - - constexpr Map() : elements_(nullptr) {} - explicit Map(Arena* arena) : elements_(arena) {} - - Map(const Map& other) : Map() { insert(other.begin(), other.end()); } - - Map(Map&& other) noexcept : Map() { - if (other.arena() != nullptr) { - *this = other; - } else { - swap(other); - } - } - - Map& operator=(Map&& other) noexcept { - if (this != &other) { - if (arena() != other.arena()) { - *this = other; - } else { - swap(other); - } - } - return *this; - } - - template - Map(const InputIt& first, const InputIt& last) : Map() { - insert(first, last); - } - - ~Map() {} - - private: - using Allocator = internal::MapAllocator; - - // InnerMap is a generic hash-based map. It doesn't contain any - // protocol-buffer-specific logic. It is a chaining hash map with the - // additional feature that some buckets can be converted to use an ordered - // container. This ensures O(lg n) bounds on find, insert, and erase, while - // avoiding the overheads of ordered containers most of the time. - // - // The implementation doesn't need the full generality of unordered_map, - // and it doesn't have it. More bells and whistles can be added as needed. - // Some implementation details: - // 1. The hash function has type hasher and the equality function - // equal_to. We inherit from hasher to save space - // (empty-base-class optimization). - // 2. The number of buckets is a power of two. - // 3. Buckets are converted to trees in pairs: if we convert bucket b then - // buckets b and b^1 will share a tree. Invariant: buckets b and b^1 have - // the same non-null value iff they are sharing a tree. (An alternative - // implementation strategy would be to have a tag bit per bucket.) - // 4. As is typical for hash_map and such, the Keys and Values are always - // stored in linked list nodes. Pointers to elements are never invalidated - // until the element is deleted. - // 5. The trees' payload type is pointer to linked-list node. Tree-converting - // a bucket doesn't copy Key-Value pairs. - // 6. Once we've tree-converted a bucket, it is never converted back. However, - // the items a tree contains may wind up assigned to trees or lists upon a - // rehash. - // 7. The code requires no C++ features from C++14 or later. - // 8. Mutations to a map do not invalidate the map's iterators, pointers to - // elements, or references to elements. - // 9. Except for erase(iterator), any non-const method can reorder iterators. - // 10. InnerMap uses KeyForTree when using the Tree representation, which - // is either `Key`, if Key is a scalar, or `reference_wrapper` - // otherwise. This avoids unnecessary copies of string keys, for example. - class InnerMap : private hasher { - public: - explicit constexpr InnerMap(Arena* arena) - : hasher(), - num_elements_(0), - num_buckets_(internal::kGlobalEmptyTableSize), - seed_(0), - index_of_first_non_null_(internal::kGlobalEmptyTableSize), - table_(const_cast(internal::kGlobalEmptyTable)), - alloc_(arena) {} - - ~InnerMap() { - if (alloc_.arena() == nullptr && - num_buckets_ != internal::kGlobalEmptyTableSize) { - clear(); - Dealloc(table_, num_buckets_); - } - } - - private: - enum { kMinTableSize = 8 }; - - // Linked-list nodes, as one would expect for a chaining hash table. - struct Node { - value_type kv; - Node* next; - }; - - // Trees. The payload type is a copy of Key, so that we can query the tree - // with Keys that are not in any particular data structure. - // The value is a void* pointing to Node. We use void* instead of Node* to - // avoid code bloat. That way there is only one instantiation of the tree - // class per key type. - using Tree = internal::TreeForMap; - using TreeIterator = typename Tree::iterator; - - static Node* NodeFromTreeIterator(TreeIterator it) { - return static_cast(it->second); - } - - // iterator and const_iterator are instantiations of iterator_base. - template - class iterator_base { - public: - using reference = KeyValueType&; - using pointer = KeyValueType*; - - // Invariants: - // node_ is always correct. This is handy because the most common - // operations are operator* and operator-> and they only use node_. - // When node_ is set to a non-null value, all the other non-const fields - // are updated to be correct also, but those fields can become stale - // if the underlying map is modified. When those fields are needed they - // are rechecked, and updated if necessary. - iterator_base() : node_(nullptr), m_(nullptr), bucket_index_(0) {} - - explicit iterator_base(const InnerMap* m) : m_(m) { - SearchFrom(m->index_of_first_non_null_); - } - - // Any iterator_base can convert to any other. This is overkill, and we - // rely on the enclosing class to use it wisely. The standard "iterator - // can convert to const_iterator" is OK but the reverse direction is not. - template - explicit iterator_base(const iterator_base& it) - : node_(it.node_), m_(it.m_), bucket_index_(it.bucket_index_) {} - - iterator_base(Node* n, const InnerMap* m, size_type index) - : node_(n), m_(m), bucket_index_(index) {} - - iterator_base(TreeIterator tree_it, const InnerMap* m, size_type index) - : node_(NodeFromTreeIterator(tree_it)), m_(m), bucket_index_(index) { - // Invariant: iterators that use buckets with trees have an even - // bucket_index_. - GOOGLE_DCHECK_EQ(bucket_index_ % 2, 0u); - } - - // Advance through buckets, looking for the first that isn't empty. - // If nothing non-empty is found then leave node_ == nullptr. - void SearchFrom(size_type start_bucket) { - GOOGLE_DCHECK(m_->index_of_first_non_null_ == m_->num_buckets_ || - m_->table_[m_->index_of_first_non_null_] != nullptr); - node_ = nullptr; - for (bucket_index_ = start_bucket; bucket_index_ < m_->num_buckets_; - bucket_index_++) { - if (m_->TableEntryIsNonEmptyList(bucket_index_)) { - node_ = static_cast(m_->table_[bucket_index_]); - break; - } else if (m_->TableEntryIsTree(bucket_index_)) { - Tree* tree = static_cast(m_->table_[bucket_index_]); - GOOGLE_DCHECK(!tree->empty()); - node_ = NodeFromTreeIterator(tree->begin()); - break; - } - } - } - - reference operator*() const { return node_->kv; } - pointer operator->() const { return &(operator*()); } - - friend bool operator==(const iterator_base& a, const iterator_base& b) { - return a.node_ == b.node_; - } - friend bool operator!=(const iterator_base& a, const iterator_base& b) { - return a.node_ != b.node_; - } - - iterator_base& operator++() { - if (node_->next == nullptr) { - TreeIterator tree_it; - const bool is_list = revalidate_if_necessary(&tree_it); - if (is_list) { - SearchFrom(bucket_index_ + 1); - } else { - GOOGLE_DCHECK_EQ(bucket_index_ & 1, 0u); - Tree* tree = static_cast(m_->table_[bucket_index_]); - if (++tree_it == tree->end()) { - SearchFrom(bucket_index_ + 2); - } else { - node_ = NodeFromTreeIterator(tree_it); - } - } - } else { - node_ = node_->next; - } - return *this; - } - - iterator_base operator++(int /* unused */) { - iterator_base tmp = *this; - ++*this; - return tmp; - } - - // Assumes node_ and m_ are correct and non-null, but other fields may be - // stale. Fix them as needed. Then return true iff node_ points to a - // Node in a list. If false is returned then *it is modified to be - // a valid iterator for node_. - bool revalidate_if_necessary(TreeIterator* it) { - GOOGLE_DCHECK(node_ != nullptr && m_ != nullptr); - // Force bucket_index_ to be in range. - bucket_index_ &= (m_->num_buckets_ - 1); - // Common case: the bucket we think is relevant points to node_. - if (m_->table_[bucket_index_] == static_cast(node_)) return true; - // Less common: the bucket is a linked list with node_ somewhere in it, - // but not at the head. - if (m_->TableEntryIsNonEmptyList(bucket_index_)) { - Node* l = static_cast(m_->table_[bucket_index_]); - while ((l = l->next) != nullptr) { - if (l == node_) { - return true; - } - } - } - // Well, bucket_index_ still might be correct, but probably - // not. Revalidate just to be sure. This case is rare enough that we - // don't worry about potential optimizations, such as having a custom - // find-like method that compares Node* instead of the key. - iterator_base i(m_->find(node_->kv.first, it)); - bucket_index_ = i.bucket_index_; - return m_->TableEntryIsList(bucket_index_); - } - - Node* node_; - const InnerMap* m_; - size_type bucket_index_; - }; - - public: - using iterator = iterator_base; - using const_iterator = iterator_base; - - Arena* arena() const { return alloc_.arena(); } - - void Swap(InnerMap* other) { - std::swap(num_elements_, other->num_elements_); - std::swap(num_buckets_, other->num_buckets_); - std::swap(seed_, other->seed_); - std::swap(index_of_first_non_null_, other->index_of_first_non_null_); - std::swap(table_, other->table_); - std::swap(alloc_, other->alloc_); - } - - iterator begin() { return iterator(this); } - iterator end() { return iterator(); } - const_iterator begin() const { return const_iterator(this); } - const_iterator end() const { return const_iterator(); } - - void clear() { - for (size_type b = 0; b < num_buckets_; b++) { - if (TableEntryIsNonEmptyList(b)) { - Node* node = static_cast(table_[b]); - table_[b] = nullptr; - do { - Node* next = node->next; - DestroyNode(node); - node = next; - } while (node != nullptr); - } else if (TableEntryIsTree(b)) { - Tree* tree = static_cast(table_[b]); - GOOGLE_DCHECK(table_[b] == table_[b + 1] && (b & 1) == 0); - table_[b] = table_[b + 1] = nullptr; - typename Tree::iterator tree_it = tree->begin(); - do { - Node* node = NodeFromTreeIterator(tree_it); - typename Tree::iterator next = tree_it; - ++next; - tree->erase(tree_it); - DestroyNode(node); - tree_it = next; - } while (tree_it != tree->end()); - DestroyTree(tree); - b++; - } - } - num_elements_ = 0; - index_of_first_non_null_ = num_buckets_; - } - - const hasher& hash_function() const { return *this; } - - static size_type max_size() { - return static_cast(1) << (sizeof(void**) >= 8 ? 60 : 28); - } - size_type size() const { return num_elements_; } - bool empty() const { return size() == 0; } - - template - iterator find(const K& k) { - return iterator(FindHelper(k).first); - } - - template - const_iterator find(const K& k) const { - return FindHelper(k).first; - } - - // Insert the key into the map, if not present. In that case, the value will - // be value initialized. - template - std::pair insert(K&& k) { - std::pair p = FindHelper(k); - // Case 1: key was already present. - if (p.first.node_ != nullptr) - return std::make_pair(iterator(p.first), false); - // Case 2: insert. - if (ResizeIfLoadIsOutOfRange(num_elements_ + 1)) { - p = FindHelper(k); - } - const size_type b = p.second; // bucket number - Node* node; - // If K is not key_type, make the conversion to key_type explicit. - using TypeToInit = typename std::conditional< - std::is_same::type, key_type>::value, K&&, - key_type>::type; - if (alloc_.arena() == nullptr) { - node = new Node{value_type(static_cast(std::forward(k))), - nullptr}; - } else { - node = Alloc(1); - Arena::CreateInArenaStorage( - const_cast(&node->kv.first), alloc_.arena(), - static_cast(std::forward(k))); - Arena::CreateInArenaStorage(&node->kv.second, alloc_.arena()); - } - - iterator result = InsertUnique(b, node); - ++num_elements_; - return std::make_pair(result, true); - } - - template - value_type& operator[](K&& k) { - return *insert(std::forward(k)).first; - } - - void erase(iterator it) { - GOOGLE_DCHECK_EQ(it.m_, this); - typename Tree::iterator tree_it; - const bool is_list = it.revalidate_if_necessary(&tree_it); - size_type b = it.bucket_index_; - Node* const item = it.node_; - if (is_list) { - GOOGLE_DCHECK(TableEntryIsNonEmptyList(b)); - Node* head = static_cast(table_[b]); - head = EraseFromLinkedList(item, head); - table_[b] = static_cast(head); - } else { - GOOGLE_DCHECK(TableEntryIsTree(b)); - Tree* tree = static_cast(table_[b]); - tree->erase(tree_it); - if (tree->empty()) { - // Force b to be the minimum of b and b ^ 1. This is important - // only because we want index_of_first_non_null_ to be correct. - b &= ~static_cast(1); - DestroyTree(tree); - table_[b] = table_[b + 1] = nullptr; - } - } - DestroyNode(item); - --num_elements_; - if (PROTOBUF_PREDICT_FALSE(b == index_of_first_non_null_)) { - while (index_of_first_non_null_ < num_buckets_ && - table_[index_of_first_non_null_] == nullptr) { - ++index_of_first_non_null_; - } - } - } - - size_t SpaceUsedInternal() const { - return internal::SpaceUsedInTable(table_, num_buckets_, - num_elements_, sizeof(Node)); - } - - private: - const_iterator find(const Key& k, TreeIterator* it) const { - return FindHelper(k, it).first; - } - template - std::pair FindHelper(const K& k) const { - return FindHelper(k, nullptr); - } - template - std::pair FindHelper(const K& k, - TreeIterator* it) const { - size_type b = BucketNumber(k); - if (TableEntryIsNonEmptyList(b)) { - Node* node = static_cast(table_[b]); - do { - if (internal::TransparentSupport::Equals(node->kv.first, k)) { - return std::make_pair(const_iterator(node, this, b), b); - } else { - node = node->next; - } - } while (node != nullptr); - } else if (TableEntryIsTree(b)) { - GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]); - b &= ~static_cast(1); - Tree* tree = static_cast(table_[b]); - auto tree_it = tree->find(k); - if (tree_it != tree->end()) { - if (it != nullptr) *it = tree_it; - return std::make_pair(const_iterator(tree_it, this, b), b); - } - } - return std::make_pair(end(), b); - } - - // Insert the given Node in bucket b. If that would make bucket b too big, - // and bucket b is not a tree, create a tree for buckets b and b^1 to share. - // Requires count(*KeyPtrFromNodePtr(node)) == 0 and that b is the correct - // bucket. num_elements_ is not modified. - iterator InsertUnique(size_type b, Node* node) { - GOOGLE_DCHECK(index_of_first_non_null_ == num_buckets_ || - table_[index_of_first_non_null_] != nullptr); - // In practice, the code that led to this point may have already - // determined whether we are inserting into an empty list, a short list, - // or whatever. But it's probably cheap enough to recompute that here; - // it's likely that we're inserting into an empty or short list. - iterator result; - GOOGLE_DCHECK(find(node->kv.first) == end()); - if (TableEntryIsEmpty(b)) { - result = InsertUniqueInList(b, node); - } else if (TableEntryIsNonEmptyList(b)) { - if (PROTOBUF_PREDICT_FALSE(TableEntryIsTooLong(b))) { - TreeConvert(b); - result = InsertUniqueInTree(b, node); - GOOGLE_DCHECK_EQ(result.bucket_index_, b & ~static_cast(1)); - } else { - // Insert into a pre-existing list. This case cannot modify - // index_of_first_non_null_, so we skip the code to update it. - return InsertUniqueInList(b, node); - } - } else { - // Insert into a pre-existing tree. This case cannot modify - // index_of_first_non_null_, so we skip the code to update it. - return InsertUniqueInTree(b, node); - } - // parentheses around (std::min) prevents macro expansion of min(...) - index_of_first_non_null_ = - (std::min)(index_of_first_non_null_, result.bucket_index_); - return result; - } - - // Returns whether we should insert after the head of the list. For - // non-optimized builds, we randomly decide whether to insert right at the - // head of the list or just after the head. This helps add a little bit of - // non-determinism to the map ordering. - bool ShouldInsertAfterHead(void* node) { -#ifdef NDEBUG - (void) node; - return false; -#else - // Doing modulo with a prime mixes the bits more. - return (reinterpret_cast(node) ^ seed_) % 13 > 6; -#endif - } - - // Helper for InsertUnique. Handles the case where bucket b is a - // not-too-long linked list. - iterator InsertUniqueInList(size_type b, Node* node) { - if (table_[b] != nullptr && ShouldInsertAfterHead(node)) { - Node* first = static_cast(table_[b]); - node->next = first->next; - first->next = node; - return iterator(node, this, b); - } - - node->next = static_cast(table_[b]); - table_[b] = static_cast(node); - return iterator(node, this, b); - } - - // Helper for InsertUnique. Handles the case where bucket b points to a - // Tree. - iterator InsertUniqueInTree(size_type b, Node* node) { - GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]); - // Maintain the invariant that node->next is null for all Nodes in Trees. - node->next = nullptr; - return iterator( - static_cast(table_[b])->insert({node->kv.first, node}).first, - this, b & ~static_cast(1)); - } - - // Returns whether it did resize. Currently this is only used when - // num_elements_ increases, though it could be used in other situations. - // It checks for load too low as well as load too high: because any number - // of erases can occur between inserts, the load could be as low as 0 here. - // Resizing to a lower size is not always helpful, but failing to do so can - // destroy the expected big-O bounds for some operations. By having the - // policy that sometimes we resize down as well as up, clients can easily - // keep O(size()) = O(number of buckets) if they want that. - bool ResizeIfLoadIsOutOfRange(size_type new_size) { - const size_type kMaxMapLoadTimes16 = 12; // controls RAM vs CPU tradeoff - const size_type hi_cutoff = num_buckets_ * kMaxMapLoadTimes16 / 16; - const size_type lo_cutoff = hi_cutoff / 4; - // We don't care how many elements are in trees. If a lot are, - // we may resize even though there are many empty buckets. In - // practice, this seems fine. - if (PROTOBUF_PREDICT_FALSE(new_size >= hi_cutoff)) { - if (num_buckets_ <= max_size() / 2) { - Resize(num_buckets_ * 2); - return true; - } - } else if (PROTOBUF_PREDICT_FALSE(new_size <= lo_cutoff && - num_buckets_ > kMinTableSize)) { - size_type lg2_of_size_reduction_factor = 1; - // It's possible we want to shrink a lot here... size() could even be 0. - // So, estimate how much to shrink by making sure we don't shrink so - // much that we would need to grow the table after a few inserts. - const size_type hypothetical_size = new_size * 5 / 4 + 1; - while ((hypothetical_size << lg2_of_size_reduction_factor) < - hi_cutoff) { - ++lg2_of_size_reduction_factor; - } - size_type new_num_buckets = std::max( - kMinTableSize, num_buckets_ >> lg2_of_size_reduction_factor); - if (new_num_buckets != num_buckets_) { - Resize(new_num_buckets); - return true; - } - } - return false; - } - - // Resize to the given number of buckets. - void Resize(size_t new_num_buckets) { - if (num_buckets_ == internal::kGlobalEmptyTableSize) { - // This is the global empty array. - // Just overwrite with a new one. No need to transfer or free anything. - num_buckets_ = index_of_first_non_null_ = kMinTableSize; - table_ = CreateEmptyTable(num_buckets_); - seed_ = Seed(); - return; - } - - GOOGLE_DCHECK_GE(new_num_buckets, kMinTableSize); - void** const old_table = table_; - const size_type old_table_size = num_buckets_; - num_buckets_ = new_num_buckets; - table_ = CreateEmptyTable(num_buckets_); - const size_type start = index_of_first_non_null_; - index_of_first_non_null_ = num_buckets_; - for (size_type i = start; i < old_table_size; i++) { - if (internal::TableEntryIsNonEmptyList(old_table, i)) { - TransferList(old_table, i); - } else if (internal::TableEntryIsTree(old_table, i)) { - TransferTree(old_table, i++); - } - } - Dealloc(old_table, old_table_size); - } - - void TransferList(void* const* table, size_type index) { - Node* node = static_cast(table[index]); - do { - Node* next = node->next; - InsertUnique(BucketNumber(node->kv.first), node); - node = next; - } while (node != nullptr); - } - - void TransferTree(void* const* table, size_type index) { - Tree* tree = static_cast(table[index]); - typename Tree::iterator tree_it = tree->begin(); - do { - InsertUnique(BucketNumber(std::cref(tree_it->first).get()), - NodeFromTreeIterator(tree_it)); - } while (++tree_it != tree->end()); - DestroyTree(tree); - } - - Node* EraseFromLinkedList(Node* item, Node* head) { - if (head == item) { - return head->next; - } else { - head->next = EraseFromLinkedList(item, head->next); - return head; - } - } - - bool TableEntryIsEmpty(size_type b) const { - return internal::TableEntryIsEmpty(table_, b); - } - bool TableEntryIsNonEmptyList(size_type b) const { - return internal::TableEntryIsNonEmptyList(table_, b); - } - bool TableEntryIsTree(size_type b) const { - return internal::TableEntryIsTree(table_, b); - } - bool TableEntryIsList(size_type b) const { - return internal::TableEntryIsList(table_, b); - } - - void TreeConvert(size_type b) { - GOOGLE_DCHECK(!TableEntryIsTree(b) && !TableEntryIsTree(b ^ 1)); - Tree* tree = - Arena::Create(alloc_.arena(), typename Tree::key_compare(), - typename Tree::allocator_type(alloc_)); - size_type count = CopyListToTree(b, tree) + CopyListToTree(b ^ 1, tree); - GOOGLE_DCHECK_EQ(count, tree->size()); - table_[b] = table_[b ^ 1] = static_cast(tree); - } - - // Copy a linked list in the given bucket to a tree. - // Returns the number of things it copied. - size_type CopyListToTree(size_type b, Tree* tree) { - size_type count = 0; - Node* node = static_cast(table_[b]); - while (node != nullptr) { - tree->insert({node->kv.first, node}); - ++count; - Node* next = node->next; - node->next = nullptr; - node = next; - } - return count; - } - - // Return whether table_[b] is a linked list that seems awfully long. - // Requires table_[b] to point to a non-empty linked list. - bool TableEntryIsTooLong(size_type b) { - const size_type kMaxLength = 8; - size_type count = 0; - Node* node = static_cast(table_[b]); - do { - ++count; - node = node->next; - } while (node != nullptr); - // Invariant: no linked list ever is more than kMaxLength in length. - GOOGLE_DCHECK_LE(count, kMaxLength); - return count >= kMaxLength; - } - - template - size_type BucketNumber(const K& k) const { - // We xor the hash value against the random seed so that we effectively - // have a random hash function. - uint64 h = hash_function()(k) ^ seed_; - - // We use the multiplication method to determine the bucket number from - // the hash value. The constant kPhi (suggested by Knuth) is roughly - // (sqrt(5) - 1) / 2 * 2^64. - constexpr uint64 kPhi = uint64{0x9e3779b97f4a7c15}; - return ((kPhi * h) >> 32) & (num_buckets_ - 1); - } - - // Return a power of two no less than max(kMinTableSize, n). - // Assumes either n < kMinTableSize or n is a power of two. - size_type TableSize(size_type n) { - return n < static_cast(kMinTableSize) - ? static_cast(kMinTableSize) - : n; - } - - // Use alloc_ to allocate an array of n objects of type U. - template - U* Alloc(size_type n) { - using alloc_type = typename Allocator::template rebind::other; - return alloc_type(alloc_).allocate(n); - } - - // Use alloc_ to deallocate an array of n objects of type U. - template - void Dealloc(U* t, size_type n) { - using alloc_type = typename Allocator::template rebind::other; - alloc_type(alloc_).deallocate(t, n); - } - - void DestroyNode(Node* node) { - if (alloc_.arena() == nullptr) { - delete node; - } - } - - void DestroyTree(Tree* tree) { - if (alloc_.arena() == nullptr) { - delete tree; - } - } - - void** CreateEmptyTable(size_type n) { - GOOGLE_DCHECK(n >= kMinTableSize); - GOOGLE_DCHECK_EQ(n & (n - 1), 0); - void** result = Alloc(n); - memset(result, 0, n * sizeof(result[0])); - return result; - } - - // Return a randomish value. - size_type Seed() const { - // We get a little bit of randomness from the address of the map. The - // lower bits are not very random, due to alignment, so we discard them - // and shift the higher bits into their place. - size_type s = reinterpret_cast(this) >> 12; -#if defined(__x86_64__) && defined(__GNUC__) && \ - !defined(GOOGLE_PROTOBUF_NO_RDTSC) - uint32 hi, lo; - asm volatile("rdtsc" : "=a"(lo), "=d"(hi)); - s += ((static_cast(hi) << 32) | lo); -#endif - return s; - } - - friend class Arena; - using InternalArenaConstructable_ = void; - using DestructorSkippable_ = void; - - size_type num_elements_; - size_type num_buckets_; - size_type seed_; - size_type index_of_first_non_null_; - void** table_; // an array with num_buckets_ entries - Allocator alloc_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(InnerMap); - }; // end of class InnerMap - - template - using key_arg = typename internal::TransparentSupport< - key_type>::template key_arg; - - public: - // Iterators - class const_iterator { - using InnerIt = typename InnerMap::const_iterator; - - public: - using iterator_category = std::forward_iterator_tag; - using value_type = typename Map::value_type; - using difference_type = ptrdiff_t; - using pointer = const value_type*; - using reference = const value_type&; - - const_iterator() {} - explicit const_iterator(const InnerIt& it) : it_(it) {} - - const_reference operator*() const { return *it_; } - const_pointer operator->() const { return &(operator*()); } - - const_iterator& operator++() { - ++it_; - return *this; - } - const_iterator operator++(int) { return const_iterator(it_++); } - - friend bool operator==(const const_iterator& a, const const_iterator& b) { - return a.it_ == b.it_; - } - friend bool operator!=(const const_iterator& a, const const_iterator& b) { - return !(a == b); - } - - private: - InnerIt it_; - }; - - class iterator { - using InnerIt = typename InnerMap::iterator; - - public: - using iterator_category = std::forward_iterator_tag; - using value_type = typename Map::value_type; - using difference_type = ptrdiff_t; - using pointer = value_type*; - using reference = value_type&; - - iterator() {} - explicit iterator(const InnerIt& it) : it_(it) {} - - reference operator*() const { return *it_; } - pointer operator->() const { return &(operator*()); } - - iterator& operator++() { - ++it_; - return *this; - } - iterator operator++(int) { return iterator(it_++); } - - // Allow implicit conversion to const_iterator. - operator const_iterator() const { // NOLINT(runtime/explicit) - return const_iterator(typename InnerMap::const_iterator(it_)); - } - - friend bool operator==(const iterator& a, const iterator& b) { - return a.it_ == b.it_; - } - friend bool operator!=(const iterator& a, const iterator& b) { - return !(a == b); - } - - private: - friend class Map; - - InnerIt it_; - }; - - iterator begin() { return iterator(elements_.begin()); } - iterator end() { return iterator(elements_.end()); } - const_iterator begin() const { return const_iterator(elements_.begin()); } - const_iterator end() const { return const_iterator(elements_.end()); } - const_iterator cbegin() const { return begin(); } - const_iterator cend() const { return end(); } - - // Capacity - size_type size() const { return elements_.size(); } - bool empty() const { return size() == 0; } - - // Element access - template - T& operator[](const key_arg& key) { - return elements_[key].second; - } - template < - typename K = key_type, - // Disable for integral types to reduce code bloat. - typename = typename std::enable_if::value>::type> - T& operator[](key_arg&& key) { - return elements_[std::forward(key)].second; - } - - template - const T& at(const key_arg& key) const { - const_iterator it = find(key); - GOOGLE_CHECK(it != end()) << "key not found: " << static_cast(key); - return it->second; - } - - template - T& at(const key_arg& key) { - iterator it = find(key); - GOOGLE_CHECK(it != end()) << "key not found: " << static_cast(key); - return it->second; - } - - // Lookup - template - size_type count(const key_arg& key) const { - return find(key) == end() ? 0 : 1; - } - - template - const_iterator find(const key_arg& key) const { - return const_iterator(elements_.find(key)); - } - template - iterator find(const key_arg& key) { - return iterator(elements_.find(key)); - } - - template - bool contains(const key_arg& key) const { - return find(key) != end(); - } - - template - std::pair equal_range( - const key_arg& key) const { - const_iterator it = find(key); - if (it == end()) { - return std::pair(it, it); - } else { - const_iterator begin = it++; - return std::pair(begin, it); - } - } - - template - std::pair equal_range(const key_arg& key) { - iterator it = find(key); - if (it == end()) { - return std::pair(it, it); - } else { - iterator begin = it++; - return std::pair(begin, it); - } - } - - // insert - std::pair insert(const value_type& value) { - std::pair p = - elements_.insert(value.first); - if (p.second) { - p.first->second = value.second; - } - return std::pair(iterator(p.first), p.second); - } - template - void insert(InputIt first, InputIt last) { - for (InputIt it = first; it != last; ++it) { - iterator exist_it = find(it->first); - if (exist_it == end()) { - operator[](it->first) = it->second; - } - } - } - void insert(std::initializer_list values) { - insert(values.begin(), values.end()); - } - - // Erase and clear - template - size_type erase(const key_arg& key) { - iterator it = find(key); - if (it == end()) { - return 0; - } else { - erase(it); - return 1; - } - } - iterator erase(iterator pos) { - iterator i = pos++; - elements_.erase(i.it_); - return pos; - } - void erase(iterator first, iterator last) { - while (first != last) { - first = erase(first); - } - } - void clear() { elements_.clear(); } - - // Assign - Map& operator=(const Map& other) { - if (this != &other) { - clear(); - insert(other.begin(), other.end()); - } - return *this; - } - - void swap(Map& other) { - if (arena() == other.arena()) { - elements_.Swap(&other.elements_); - } else { - // TODO(zuguang): optimize this. The temporary copy can be allocated - // in the same arena as the other message, and the "other = copy" can - // be replaced with the fast-path swap above. - Map copy = *this; - *this = other; - other = copy; - } - } - - // Access to hasher. Currently this returns a copy, but it may - // be modified to return a const reference in the future. - hasher hash_function() const { return elements_.hash_function(); } - - size_t SpaceUsedExcludingSelfLong() const { - if (empty()) return 0; - return elements_.SpaceUsedInternal() + internal::SpaceUsedInValues(this); - } - - private: - Arena* arena() const { return elements_.arena(); } - InnerMap elements_; - - friend class Arena; - using InternalArenaConstructable_ = void; - using DestructorSkippable_ = void; - template - friend class internal::MapFieldLite; -}; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_MAP_H__ diff --git a/third_party/protobuf-lite/google/protobuf/map_entry_lite.h b/third_party/protobuf-lite/google/protobuf/map_entry_lite.h deleted file mode 100644 index 1caf59dfe..000000000 --- a/third_party/protobuf-lite/google/protobuf/map_entry_lite.h +++ /dev/null @@ -1,653 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__ -#define GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__ - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { -namespace internal { -template -class MapEntry; -template -class MapFieldLite; -} // namespace internal -} // namespace protobuf -} // namespace google - -namespace google { -namespace protobuf { -namespace internal { - -// MoveHelper::Move is used to set *dest. It copies *src, or moves it (in -// the C++11 sense), or swaps it. *src is left in a sane state for -// subsequent destruction, but shouldn't be used for anything. -template -struct MoveHelper { // primitives - static void Move(T* src, T* dest) { *dest = *src; } -}; - -template -struct MoveHelper { // enums - static void Move(T* src, T* dest) { *dest = *src; } - // T is an enum here, so allow conversions to and from int. - static void Move(T* src, int* dest) { *dest = static_cast(*src); } - static void Move(int* src, T* dest) { *dest = static_cast(*src); } -}; - -template -struct MoveHelper { // messages - static void Move(T* src, T* dest) { dest->Swap(src); } -}; - -template -struct MoveHelper { // strings and similar - static void Move(T* src, T* dest) { - *dest = std::move(*src); - } -}; - -// Functions for operating on a map entry. Does not contain any representation -// (this class is not intended to be instantiated). -template -struct MapEntryFuncs { - typedef MapTypeHandler KeyTypeHandler; - typedef MapTypeHandler ValueTypeHandler; - static const int kKeyFieldNumber = 1; - static const int kValueFieldNumber = 2; - - static uint8* InternalSerialize(int field_number, const Key& key, - const Value& value, uint8* ptr, - io::EpsCopyOutputStream* stream) { - ptr = stream->EnsureSpace(ptr); - ptr = WireFormatLite::WriteTagToArray( - field_number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, ptr); - ptr = io::CodedOutputStream::WriteVarint32ToArray(GetCachedSize(key, value), - ptr); - - ptr = KeyTypeHandler::Write(kKeyFieldNumber, key, ptr, stream); - return ValueTypeHandler::Write(kValueFieldNumber, value, ptr, stream); - } - - static size_t ByteSizeLong(const Key& key, const Value& value) { - // Tags for key and value will both be one byte (field numbers 1 and 2). - size_t inner_length = - 2 + KeyTypeHandler::ByteSize(key) + ValueTypeHandler::ByteSize(value); - return inner_length + io::CodedOutputStream::VarintSize32( - static_cast(inner_length)); - } - - static int GetCachedSize(const Key& key, const Value& value) { - // Tags for key and value will both be one byte (field numbers 1 and 2). - return 2 + KeyTypeHandler::GetCachedSize(key) + - ValueTypeHandler::GetCachedSize(value); - } -}; - -// MapEntryImpl is used to implement parsing and serialization of map entries. -// It uses Curious Recursive Template Pattern (CRTP) to provide the type of -// the eventual code to the template code. -template -class MapEntryImpl : public Base { - public: - typedef MapEntryFuncs Funcs; - - protected: - // Provide utilities to parse/serialize key/value. Provide utilities to - // manipulate internal stored type. - typedef MapTypeHandler KeyTypeHandler; - typedef MapTypeHandler ValueTypeHandler; - - // Define internal memory layout. Strings and messages are stored as - // pointers, while other types are stored as values. - typedef typename KeyTypeHandler::TypeOnMemory KeyOnMemory; - typedef typename ValueTypeHandler::TypeOnMemory ValueOnMemory; - - // Enum type cannot be used for MapTypeHandler::Read. Define a type - // which will replace Enum with int. - typedef typename KeyTypeHandler::MapEntryAccessorType KeyMapEntryAccessorType; - typedef - typename ValueTypeHandler::MapEntryAccessorType ValueMapEntryAccessorType; - - // Constants for field number. - static const int kKeyFieldNumber = 1; - static const int kValueFieldNumber = 2; - - // Constants for field tag. - static const uint8 kKeyTag = - GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kKeyFieldNumber, KeyTypeHandler::kWireType); - static const uint8 kValueTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( - kValueFieldNumber, ValueTypeHandler::kWireType); - static const size_t kTagSize = 1; - - public: - // Work-around for a compiler bug (see repeated_field.h). - typedef void MapEntryHasMergeTypeTrait; - typedef Derived EntryType; - typedef Key EntryKeyType; - typedef Value EntryValueType; - static const WireFormatLite::FieldType kEntryKeyFieldType = kKeyFieldType; - static const WireFormatLite::FieldType kEntryValueFieldType = kValueFieldType; - - constexpr MapEntryImpl() - : key_(KeyTypeHandler::Constinit()), - value_(ValueTypeHandler::Constinit()), - _has_bits_{} {} - - explicit MapEntryImpl(Arena* arena) - : Base(arena), - key_(KeyTypeHandler::Constinit()), - value_(ValueTypeHandler::Constinit()), - _has_bits_{} {} - - ~MapEntryImpl() { - if (Base::GetArena() != NULL) return; - KeyTypeHandler::DeleteNoArena(key_); - ValueTypeHandler::DeleteNoArena(value_); - } - - // accessors ====================================================== - - virtual inline const KeyMapEntryAccessorType& key() const { - return KeyTypeHandler::GetExternalReference(key_); - } - virtual inline const ValueMapEntryAccessorType& value() const { - return ValueTypeHandler::DefaultIfNotInitialized(value_); - } - inline KeyMapEntryAccessorType* mutable_key() { - set_has_key(); - return KeyTypeHandler::EnsureMutable(&key_, Base::GetArena()); - } - inline ValueMapEntryAccessorType* mutable_value() { - set_has_value(); - return ValueTypeHandler::EnsureMutable(&value_, Base::GetArena()); - } - - // implements MessageLite ========================================= - - // MapEntryImpl is for implementation only and this function isn't called - // anywhere. Just provide a fake implementation here for MessageLite. - std::string GetTypeName() const override { return ""; } - - void CheckTypeAndMergeFrom(const MessageLite& other) override { - MergeFromInternal(*::google::protobuf::internal::DownCast(&other)); - } - - const char* _InternalParse(const char* ptr, ParseContext* ctx) final { - while (!ctx->Done(&ptr)) { - uint32 tag; - ptr = ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (tag == kKeyTag) { - set_has_key(); - KeyMapEntryAccessorType* key = mutable_key(); - ptr = KeyTypeHandler::Read(ptr, ctx, key); - if (!Derived::ValidateKey(key)) return nullptr; - } else if (tag == kValueTag) { - set_has_value(); - ValueMapEntryAccessorType* value = mutable_value(); - ptr = ValueTypeHandler::Read(ptr, ctx, value); - if (!Derived::ValidateValue(value)) return nullptr; - } else { - if (tag == 0 || WireFormatLite::GetTagWireType(tag) == - WireFormatLite::WIRETYPE_END_GROUP) { - ctx->SetLastTag(tag); - return ptr; - } - ptr = UnknownFieldParse(tag, static_cast(nullptr), ptr, - ctx); - } - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - } - return ptr; - } - - size_t ByteSizeLong() const override { - size_t size = 0; - size += kTagSize + static_cast(KeyTypeHandler::ByteSize(key())); - size += kTagSize + static_cast(ValueTypeHandler::ByteSize(value())); - return size; - } - - ::google::protobuf::uint8* _InternalSerialize(::google::protobuf::uint8* ptr, - io::EpsCopyOutputStream* stream) const override { - ptr = KeyTypeHandler::Write(kKeyFieldNumber, key(), ptr, stream); - return ValueTypeHandler::Write(kValueFieldNumber, value(), ptr, stream); - } - - // Don't override SerializeWithCachedSizesToArray. Use MessageLite's. - - int GetCachedSize() const override { - int size = 0; - size += has_key() ? static_cast(kTagSize) + - KeyTypeHandler::GetCachedSize(key()) - : 0; - size += has_value() ? static_cast(kTagSize) + - ValueTypeHandler::GetCachedSize(value()) - : 0; - return size; - } - - bool IsInitialized() const override { - return ValueTypeHandler::IsInitialized(value_); - } - - Base* New() const override { - Derived* entry = new Derived; - return entry; - } - - Base* New(Arena* arena) const override { - Derived* entry = Arena::CreateMessage(arena); - return entry; - } - - protected: - // We can't declare this function directly here as it would hide the other - // overload (const Message&). - void MergeFromInternal(const MapEntryImpl& from) { - if (from._has_bits_[0]) { - if (from.has_key()) { - KeyTypeHandler::EnsureMutable(&key_, Base::GetArena()); - KeyTypeHandler::Merge(from.key(), &key_, Base::GetArena()); - set_has_key(); - } - if (from.has_value()) { - ValueTypeHandler::EnsureMutable(&value_, Base::GetArena()); - ValueTypeHandler::Merge(from.value(), &value_, Base::GetArena()); - set_has_value(); - } - } - } - - public: - void Clear() override { - KeyTypeHandler::Clear(&key_, Base::GetArena()); - ValueTypeHandler::Clear(&value_, Base::GetArena()); - clear_has_key(); - clear_has_value(); - } - - // Parsing using MergePartialFromCodedStream, above, is not as - // efficient as it could be. This helper class provides a speedier way. - template - class Parser { - public: - explicit Parser(MapField* mf) : mf_(mf), map_(mf->MutableMap()) {} - ~Parser() { - if (entry_ != nullptr && entry_->GetArena() == nullptr) delete entry_; - } - - // This does what the typical MergePartialFromCodedStream() is expected to - // do, with the additional side-effect that if successful (i.e., if true is - // going to be its return value) it inserts the key-value pair into map_. - bool MergePartialFromCodedStream(io::CodedInputStream* input) { - // Look for the expected thing: a key and then a value. If it fails, - // invoke the enclosing class's MergePartialFromCodedStream, or return - // false if that would be pointless. - if (input->ExpectTag(kKeyTag)) { - if (!KeyTypeHandler::Read(input, &key_)) { - return false; - } - // Peek at the next byte to see if it is kValueTag. If not, bail out. - const void* data; - int size; - input->GetDirectBufferPointerInline(&data, &size); - // We could use memcmp here, but we don't bother. The tag is one byte. - static_assert(kTagSize == 1, "tag size must be 1"); - if (size > 0 && *reinterpret_cast(data) == kValueTag) { - typename Map::size_type map_size = map_->size(); - value_ptr_ = &(*map_)[key_]; - if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) { - // We created a new key-value pair. Fill in the value. - typedef - typename MapIf::type T; - input->Skip(kTagSize); // Skip kValueTag. - if (!ValueTypeHandler::Read(input, - reinterpret_cast(value_ptr_))) { - map_->erase(key_); // Failure! Undo insertion. - return false; - } - if (input->ExpectAtEnd()) return true; - return ReadBeyondKeyValuePair(input); - } - } - } else { - key_ = Key(); - } - - NewEntry(); - *entry_->mutable_key() = key_; - const bool result = entry_->MergePartialFromCodedStream(input); - if (result) UseKeyAndValueFromEntry(); - return result; - } - - const char* _InternalParse(const char* ptr, ParseContext* ctx) { - if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kKeyTag)) { - ptr = KeyTypeHandler::Read(ptr + 1, ctx, &key_); - if (PROTOBUF_PREDICT_FALSE(!ptr || !Derived::ValidateKey(&key_))) { - return nullptr; - } - if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kValueTag)) { - typename Map::size_type map_size = map_->size(); - value_ptr_ = &(*map_)[key_]; - if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) { - using T = - typename MapIf::type; - ptr = ValueTypeHandler::Read(ptr + 1, ctx, - reinterpret_cast(value_ptr_)); - if (PROTOBUF_PREDICT_FALSE(!ptr || - !Derived::ValidateValue(value_ptr_))) { - map_->erase(key_); // Failure! Undo insertion. - return nullptr; - } - if (PROTOBUF_PREDICT_TRUE(ctx->Done(&ptr))) return ptr; - if (!ptr) return nullptr; - NewEntry(); - ValueMover::Move(value_ptr_, entry_->mutable_value()); - map_->erase(key_); - goto move_key; - } - } else { - if (!ptr) return nullptr; - } - NewEntry(); - move_key: - KeyMover::Move(&key_, entry_->mutable_key()); - } else { - if (!ptr) return nullptr; - NewEntry(); - } - ptr = entry_->_InternalParse(ptr, ctx); - if (ptr) UseKeyAndValueFromEntry(); - return ptr; - } - - template - const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx, - bool (*is_valid)(int), uint32 field_num, - InternalMetadata* metadata) { - auto entry = NewEntry(); - ptr = entry->_InternalParse(ptr, ctx); - if (!ptr) return nullptr; - if (is_valid(entry->value())) { - UseKeyAndValueFromEntry(); - } else { - WriteLengthDelimited(field_num, entry->SerializeAsString(), - metadata->mutable_unknown_fields()); - } - return ptr; - } - - MapEntryImpl* NewEntry() { return entry_ = mf_->NewEntry(); } - - const Key& key() const { return key_; } - const Value& value() const { return *value_ptr_; } - - const Key& entry_key() const { return entry_->key(); } - const Value& entry_value() const { return entry_->value(); } - - private: - void UseKeyAndValueFromEntry() { - // Update key_ in case we need it later (because key() is called). - // This is potentially inefficient, especially if the key is - // expensive to copy (e.g., a long string), but this is a cold - // path, so it's not a big deal. - key_ = entry_->key(); - value_ptr_ = &(*map_)[key_]; - ValueMover::Move(entry_->mutable_value(), value_ptr_); - } - - // After reading a key and value successfully, and inserting that data - // into map_, we are not at the end of the input. This is unusual, but - // allowed by the spec. - bool ReadBeyondKeyValuePair(io::CodedInputStream* input) PROTOBUF_COLD { - NewEntry(); - ValueMover::Move(value_ptr_, entry_->mutable_value()); - map_->erase(key_); - KeyMover::Move(&key_, entry_->mutable_key()); - const bool result = entry_->MergePartialFromCodedStream(input); - if (result) UseKeyAndValueFromEntry(); - return result; - } - - typedef MoveHelper - KeyMover; - typedef MoveHelper - ValueMover; - - MapField* const mf_; - Map* const map_; - Key key_; - Value* value_ptr_; - MapEntryImpl* entry_ = nullptr; - }; - - protected: - void set_has_key() { _has_bits_[0] |= 0x00000001u; } - bool has_key() const { return (_has_bits_[0] & 0x00000001u) != 0; } - void clear_has_key() { _has_bits_[0] &= ~0x00000001u; } - void set_has_value() { _has_bits_[0] |= 0x00000002u; } - bool has_value() const { return (_has_bits_[0] & 0x00000002u) != 0; } - void clear_has_value() { _has_bits_[0] &= ~0x00000002u; } - - public: - inline Arena* GetArena() const { return Base::GetArena(); } - - public: // Needed for constructing tables - KeyOnMemory key_; - ValueOnMemory value_; - uint32 _has_bits_[1]; - - private: - friend class ::PROTOBUF_NAMESPACE_ID::Arena; - typedef void InternalArenaConstructable_; - typedef void DestructorSkippable_; - template - friend class internal::MapEntry; - template - friend class internal::MapFieldLite; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntryImpl); -}; - -template -class MapEntryLite : public MapEntryImpl { - public: - typedef MapEntryImpl - SuperType; - constexpr MapEntryLite() {} - explicit MapEntryLite(Arena* arena) : SuperType(arena) {} - ~MapEntryLite() { MessageLite::_internal_metadata_.Delete(); } - void MergeFrom(const MapEntryLite& other) { MergeFromInternal(other); } - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntryLite); -}; -// The completely unprincipled and unwieldy use of template parameters in -// the map code necessitates wrappers to make the code a little bit more -// manageable. -template -struct DeconstructMapEntry; - -template -struct DeconstructMapEntry > { - typedef K Key; - typedef V Value; - static const WireFormatLite::FieldType kKeyFieldType = key; - static const WireFormatLite::FieldType kValueFieldType = value; -}; - -// Helpers for deterministic serialization ============================= - -// This struct can be used with any generic sorting algorithm. If the Key -// type is relatively small and easy to copy then copying Keys into an -// array of SortItems can be beneficial. Then all the data the sorting -// algorithm needs to touch is in that one array. -template -struct SortItem { - SortItem() {} - explicit SortItem(PtrToKeyValuePair p) : first(p->first), second(p) {} - - Key first; - PtrToKeyValuePair second; -}; - -template -struct CompareByFirstField { - bool operator()(const T& a, const T& b) const { return a.first < b.first; } -}; - -template -struct CompareByDerefFirst { - bool operator()(const T& a, const T& b) const { return a->first < b->first; } -}; - -// Helper for table driven serialization - -template -struct FromHelper { - template - static const T& From(const T& x) { - return x; - } -}; - -template <> -struct FromHelper { - static ArenaStringPtr From(const std::string& x) { - ArenaStringPtr res; - TaggedPtr ptr; - ptr.Set(const_cast(&x)); - res.UnsafeSetTaggedPointer(ptr); - return res; - } -}; -template <> -struct FromHelper { - static ArenaStringPtr From(const std::string& x) { - ArenaStringPtr res; - TaggedPtr ptr; - ptr.Set(const_cast(&x)); - res.UnsafeSetTaggedPointer(ptr); - return res; - } -}; -template <> -struct FromHelper { - template - static T* From(const T& x) { - return const_cast(&x); - } -}; - -template -struct MapEntryHelper; - -template -struct MapEntryHelper< - MapEntryLite > { - // Provide utilities to parse/serialize key/value. Provide utilities to - // manipulate internal stored type. - typedef MapTypeHandler KeyTypeHandler; - typedef MapTypeHandler ValueTypeHandler; - - // Define internal memory layout. Strings and messages are stored as - // pointers, while other types are stored as values. - typedef typename KeyTypeHandler::TypeOnMemory KeyOnMemory; - typedef typename ValueTypeHandler::TypeOnMemory ValueOnMemory; - - explicit MapEntryHelper(const MapPair& map_pair) - : _has_bits_(3), - _cached_size_(2 + KeyTypeHandler::GetCachedSize(map_pair.first) + - ValueTypeHandler::GetCachedSize(map_pair.second)), - key_(FromHelper::From(map_pair.first)), - value_(FromHelper::From(map_pair.second)) {} - - // Purposely not following the style guide naming. These are the names - // the proto compiler would generate given the map entry descriptor. - // The proto compiler generates the offsets in this struct as if this was - // a regular message. This way the table driven code barely notices it's - // dealing with a map field. - uint32 _has_bits_; // NOLINT - uint32 _cached_size_; // NOLINT - KeyOnMemory key_; // NOLINT - ValueOnMemory value_; // NOLINT -}; - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/map_field_lite.h b/third_party/protobuf-lite/google/protobuf/map_field_lite.h deleted file mode 100644 index 665cb0eeb..000000000 --- a/third_party/protobuf-lite/google/protobuf/map_field_lite.h +++ /dev/null @@ -1,183 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__ -#define GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__ - -#include -#include -#include -#include -#include -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { -namespace internal { - -// This class provides access to map field using generated api. It is used for -// internal generated message implementation only. Users should never use this -// directly. -template -class MapFieldLite { - // Define message type for internal repeated field. - typedef Derived EntryType; - - public: - typedef Map MapType; - typedef EntryType EntryTypeTrait; - - constexpr MapFieldLite() {} - - explicit MapFieldLite(Arena* arena) : map_(arena) {} - - // Accessors - const Map& GetMap() const { return map_; } - Map* MutableMap() { return &map_; } - - // Convenient methods for generated message implementation. - int size() const { return static_cast(map_.size()); } - void Clear() { return map_.clear(); } - void MergeFrom(const MapFieldLite& other) { - for (typename Map::const_iterator it = other.map_.begin(); - it != other.map_.end(); ++it) { - map_[it->first] = it->second; - } - } - void Swap(MapFieldLite* other) { map_.swap(other->map_); } - - // Used in the implementation of parsing. Caller should take the ownership iff - // arena_ is NULL. - EntryType* NewEntry() const { - return Arena::CreateMessage(map_.arena()); - } - // Used in the implementation of serializing enum value type. Caller should - // take the ownership iff arena_ is NULL. - EntryType* NewEnumEntryWrapper(const Key& key, const T t) const { - return EntryType::EnumWrap(key, t, map_.arena_); - } - // Used in the implementation of serializing other value types. Caller should - // take the ownership iff arena_ is NULL. - EntryType* NewEntryWrapper(const Key& key, const T& t) const { - return EntryType::Wrap(key, t, map_.arena_); - } - - const char* _InternalParse(const char* ptr, ParseContext* ctx) { - typename Derived::template Parser> parser(this); - return parser._InternalParse(ptr, ctx); - } - - template - const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx, - bool (*is_valid)(int), uint32 field_num, - InternalMetadata* metadata) { - typename Derived::template Parser> parser(this); - return parser.template ParseWithEnumValidation( - ptr, ctx, is_valid, field_num, metadata); - } - - private: - typedef void DestructorSkippable_; - - Map map_; - - friend class ::PROTOBUF_NAMESPACE_ID::Arena; -}; - -template -struct EnumParseWrapper { - const char* _InternalParse(const char* ptr, ParseContext* ctx) { - return map_field->template ParseWithEnumValidation( - ptr, ctx, is_valid, field_num, metadata); - } - T* map_field; - bool (*is_valid)(int); - uint32 field_num; - InternalMetadata* metadata; -}; - -// Helper function because the typenames of maps are horrendous to print. This -// leverages compiler type deduction, to keep all type data out of the -// generated code -template -EnumParseWrapper InitEnumParseWrapper( - T* map_field, bool (*is_valid)(int), uint32 field_num, - InternalMetadata* metadata) { - return EnumParseWrapper{map_field, is_valid, field_num, - metadata}; -} - -// True if IsInitialized() is true for value field in all elements of t. T is -// expected to be message. It's useful to have this helper here to keep the -// protobuf compiler from ever having to emit loops in IsInitialized() methods. -// We want the C++ compiler to inline this or not as it sees fit. -template -bool AllAreInitialized(const MapFieldLite& field) { - const auto& t = field.GetMap(); - for (typename Map::const_iterator it = t.begin(); it != t.end(); - ++it) { - if (!it->second.IsInitialized()) return false; - } - return true; -} - -template -struct MapEntryToMapField : MapEntryToMapField {}; - -template -struct MapEntryToMapField< - MapEntryLite> { - typedef MapFieldLite< - MapEntryLite, Key, Value, - kKeyFieldType, kValueFieldType> - MapFieldType; -}; - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/map_type_handler.h b/third_party/protobuf-lite/google/protobuf/map_type_handler.h deleted file mode 100644 index 8b8fd14c2..000000000 --- a/third_party/protobuf-lite/google/protobuf/map_type_handler.h +++ /dev/null @@ -1,688 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_TYPE_HANDLER_H__ -#define GOOGLE_PROTOBUF_TYPE_HANDLER_H__ - -#include -#include -#include -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { -namespace internal { - -// Used for compile time type selection. MapIf::type will be TrueType if Flag is -// true and FalseType otherwise. -template -struct MapIf; - -template -struct MapIf { - typedef TrueType type; -}; - -template -struct MapIf { - typedef FalseType type; -}; - -template -class MapArenaMessageCreator { - public: - // Use arena to create message if Type is arena constructable. Otherwise, - // create the message on heap. - static inline Type* CreateMessage(Arena* arena); -}; -template -class MapArenaMessageCreator { - public: - static inline Type* CreateMessage(Arena* arena) { - return Arena::CreateMessage(arena); - } -}; -template -class MapArenaMessageCreator { - public: - static inline Type* CreateMessage(Arena* arena) { - return Arena::Create(arena); - } -}; - -// Define constants for given wire field type -template -class MapWireFieldTypeTraits {}; - -#define TYPE_TRAITS(FieldType, CType, WireFormatType, IsMessage, IsEnum) \ - template \ - class MapWireFieldTypeTraits { \ - public: \ - static const bool kIsMessage = IsMessage; \ - static const bool kIsEnum = IsEnum; \ - typedef typename MapIf::type TypeOnMemory; \ - typedef typename MapIf::type MapEntryAccessorType; \ - static const WireFormatLite::WireType kWireType = \ - WireFormatLite::WIRETYPE_##WireFormatType; \ - }; - -TYPE_TRAITS(MESSAGE, Type, LENGTH_DELIMITED, true, false) -TYPE_TRAITS(STRING, ArenaStringPtr, LENGTH_DELIMITED, false, false) -TYPE_TRAITS(BYTES, ArenaStringPtr, LENGTH_DELIMITED, false, false) -TYPE_TRAITS(INT64, int64, VARINT, false, false) -TYPE_TRAITS(UINT64, uint64, VARINT, false, false) -TYPE_TRAITS(INT32, int32, VARINT, false, false) -TYPE_TRAITS(UINT32, uint32, VARINT, false, false) -TYPE_TRAITS(SINT64, int64, VARINT, false, false) -TYPE_TRAITS(SINT32, int32, VARINT, false, false) -TYPE_TRAITS(ENUM, int, VARINT, false, true) -TYPE_TRAITS(DOUBLE, double, FIXED64, false, false) -TYPE_TRAITS(FLOAT, float, FIXED32, false, false) -TYPE_TRAITS(FIXED64, uint64, FIXED64, false, false) -TYPE_TRAITS(FIXED32, uint32, FIXED32, false, false) -TYPE_TRAITS(SFIXED64, int64, FIXED64, false, false) -TYPE_TRAITS(SFIXED32, int32, FIXED32, false, false) -TYPE_TRAITS(BOOL, bool, VARINT, false, false) - -#undef TYPE_TRAITS - -template -class MapTypeHandler {}; - -template -class MapTypeHandler { - public: - // Enum type cannot be used for MapTypeHandler::Read. Define a type which will - // replace Enum with int. - typedef typename MapWireFieldTypeTraits::MapEntryAccessorType - MapEntryAccessorType; - // Internal stored type in MapEntryLite for given wire field type. - typedef typename MapWireFieldTypeTraits::TypeOnMemory TypeOnMemory; - // Corresponding wire type for field type. - static constexpr WireFormatLite::WireType kWireType = - MapWireFieldTypeTraits::kWireType; - // Whether wire type is for message. - static constexpr bool kIsMessage = - MapWireFieldTypeTraits::kIsMessage; - // Whether wire type is for enum. - static constexpr bool kIsEnum = - MapWireFieldTypeTraits::kIsEnum; - - // Functions used in parsing and serialization. =================== - static inline size_t ByteSize(const MapEntryAccessorType& value); - static inline int GetCachedSize(const MapEntryAccessorType& value); - static inline bool Read(io::CodedInputStream* input, - MapEntryAccessorType* value); - static inline const char* Read(const char* ptr, ParseContext* ctx, - MapEntryAccessorType* value); - - static inline uint8* Write(int field, const MapEntryAccessorType& value, - uint8* ptr, io::EpsCopyOutputStream* stream); - - // Functions to manipulate data on memory. ======================== - static inline const Type& GetExternalReference(const Type* value); - static inline void DeleteNoArena(const Type* x); - static inline void Merge(const Type& from, Type** to, Arena* arena); - static inline void Clear(Type** value, Arena* arena); - static constexpr TypeOnMemory Constinit(); - - static inline Type* EnsureMutable(Type** value, Arena* arena); - // SpaceUsedInMapEntry: Return bytes used by value in MapEntry, excluding - // those already calculate in sizeof(MapField). - static inline size_t SpaceUsedInMapEntryLong(const Type* value); - // Return default instance if value is not initialized when calling const - // reference accessor. - static inline const Type& DefaultIfNotInitialized(const Type* value); - // Check if all required fields have values set. - static inline bool IsInitialized(Type* value); -}; - -#define MAP_HANDLER(FieldType) \ - template \ - class MapTypeHandler { \ - public: \ - typedef typename MapWireFieldTypeTraits::MapEntryAccessorType \ - MapEntryAccessorType; \ - typedef typename MapWireFieldTypeTraits::TypeOnMemory TypeOnMemory; \ - static const WireFormatLite::WireType kWireType = \ - MapWireFieldTypeTraits::kWireType; \ - static const bool kIsMessage = \ - MapWireFieldTypeTraits::kIsMessage; \ - static const bool kIsEnum = \ - MapWireFieldTypeTraits::kIsEnum; \ - static inline int ByteSize(const MapEntryAccessorType& value); \ - static inline int GetCachedSize(const MapEntryAccessorType& value); \ - static inline bool Read(io::CodedInputStream* input, \ - MapEntryAccessorType* value); \ - static inline const char* Read(const char* begin, ParseContext* ctx, \ - MapEntryAccessorType* value); \ - static inline uint8* Write(int field, const MapEntryAccessorType& value, \ - uint8* ptr, io::EpsCopyOutputStream* stream); \ - static inline const MapEntryAccessorType& GetExternalReference( \ - const TypeOnMemory& value); \ - static inline void DeleteNoArena(const TypeOnMemory& x); \ - static inline void Merge(const MapEntryAccessorType& from, \ - TypeOnMemory* to, Arena* arena); \ - static inline void Clear(TypeOnMemory* value, Arena* arena); \ - static inline size_t SpaceUsedInMapEntryLong(const TypeOnMemory& value); \ - static inline const MapEntryAccessorType& DefaultIfNotInitialized( \ - const TypeOnMemory& value); \ - static inline bool IsInitialized(const TypeOnMemory& value); \ - static void DeleteNoArena(TypeOnMemory& value); \ - static constexpr TypeOnMemory Constinit(); \ - static inline MapEntryAccessorType* EnsureMutable(TypeOnMemory* value, \ - Arena* arena); \ - }; -MAP_HANDLER(STRING) -MAP_HANDLER(BYTES) -MAP_HANDLER(INT64) -MAP_HANDLER(UINT64) -MAP_HANDLER(INT32) -MAP_HANDLER(UINT32) -MAP_HANDLER(SINT64) -MAP_HANDLER(SINT32) -MAP_HANDLER(ENUM) -MAP_HANDLER(DOUBLE) -MAP_HANDLER(FLOAT) -MAP_HANDLER(FIXED64) -MAP_HANDLER(FIXED32) -MAP_HANDLER(SFIXED64) -MAP_HANDLER(SFIXED32) -MAP_HANDLER(BOOL) -#undef MAP_HANDLER - -template -inline size_t MapTypeHandler::ByteSize( - const MapEntryAccessorType& value) { - return WireFormatLite::MessageSizeNoVirtual(value); -} - -#define GOOGLE_PROTOBUF_BYTE_SIZE(FieldType, DeclaredType) \ - template \ - inline int MapTypeHandler::ByteSize( \ - const MapEntryAccessorType& value) { \ - return static_cast(WireFormatLite::DeclaredType##Size(value)); \ - } - -GOOGLE_PROTOBUF_BYTE_SIZE(STRING, String) -GOOGLE_PROTOBUF_BYTE_SIZE(BYTES, Bytes) -GOOGLE_PROTOBUF_BYTE_SIZE(INT64, Int64) -GOOGLE_PROTOBUF_BYTE_SIZE(UINT64, UInt64) -GOOGLE_PROTOBUF_BYTE_SIZE(INT32, Int32) -GOOGLE_PROTOBUF_BYTE_SIZE(UINT32, UInt32) -GOOGLE_PROTOBUF_BYTE_SIZE(SINT64, SInt64) -GOOGLE_PROTOBUF_BYTE_SIZE(SINT32, SInt32) -GOOGLE_PROTOBUF_BYTE_SIZE(ENUM, Enum) - -#undef GOOGLE_PROTOBUF_BYTE_SIZE - -#define FIXED_BYTE_SIZE(FieldType, DeclaredType) \ - template \ - inline int MapTypeHandler::ByteSize( \ - const MapEntryAccessorType& /* value */) { \ - return WireFormatLite::k##DeclaredType##Size; \ - } - -FIXED_BYTE_SIZE(DOUBLE, Double) -FIXED_BYTE_SIZE(FLOAT, Float) -FIXED_BYTE_SIZE(FIXED64, Fixed64) -FIXED_BYTE_SIZE(FIXED32, Fixed32) -FIXED_BYTE_SIZE(SFIXED64, SFixed64) -FIXED_BYTE_SIZE(SFIXED32, SFixed32) -FIXED_BYTE_SIZE(BOOL, Bool) - -#undef FIXED_BYTE_SIZE - -template -inline int MapTypeHandler::GetCachedSize( - const MapEntryAccessorType& value) { - return static_cast(WireFormatLite::LengthDelimitedSize( - static_cast(value.GetCachedSize()))); -} - -#define GET_CACHED_SIZE(FieldType, DeclaredType) \ - template \ - inline int \ - MapTypeHandler::GetCachedSize( \ - const MapEntryAccessorType& value) { \ - return static_cast(WireFormatLite::DeclaredType##Size(value)); \ - } - -GET_CACHED_SIZE(STRING, String) -GET_CACHED_SIZE(BYTES, Bytes) -GET_CACHED_SIZE(INT64, Int64) -GET_CACHED_SIZE(UINT64, UInt64) -GET_CACHED_SIZE(INT32, Int32) -GET_CACHED_SIZE(UINT32, UInt32) -GET_CACHED_SIZE(SINT64, SInt64) -GET_CACHED_SIZE(SINT32, SInt32) -GET_CACHED_SIZE(ENUM, Enum) - -#undef GET_CACHED_SIZE - -#define GET_FIXED_CACHED_SIZE(FieldType, DeclaredType) \ - template \ - inline int \ - MapTypeHandler::GetCachedSize( \ - const MapEntryAccessorType& /* value */) { \ - return WireFormatLite::k##DeclaredType##Size; \ - } - -GET_FIXED_CACHED_SIZE(DOUBLE, Double) -GET_FIXED_CACHED_SIZE(FLOAT, Float) -GET_FIXED_CACHED_SIZE(FIXED64, Fixed64) -GET_FIXED_CACHED_SIZE(FIXED32, Fixed32) -GET_FIXED_CACHED_SIZE(SFIXED64, SFixed64) -GET_FIXED_CACHED_SIZE(SFIXED32, SFixed32) -GET_FIXED_CACHED_SIZE(BOOL, Bool) - -#undef GET_FIXED_CACHED_SIZE - -template -inline uint8* MapTypeHandler::Write( - int field, const MapEntryAccessorType& value, uint8* ptr, - io::EpsCopyOutputStream* stream) { - ptr = stream->EnsureSpace(ptr); - return WireFormatLite::InternalWriteMessage(field, value, ptr, stream); -} - -#define WRITE_METHOD(FieldType, DeclaredType) \ - template \ - inline uint8* MapTypeHandler::Write( \ - int field, const MapEntryAccessorType& value, uint8* ptr, \ - io::EpsCopyOutputStream* stream) { \ - ptr = stream->EnsureSpace(ptr); \ - return stream->Write##DeclaredType(field, value, ptr); \ - } - -WRITE_METHOD(STRING, String) -WRITE_METHOD(BYTES, Bytes) - -#undef WRITE_METHOD -#define WRITE_METHOD(FieldType, DeclaredType) \ - template \ - inline uint8* MapTypeHandler::Write( \ - int field, const MapEntryAccessorType& value, uint8* ptr, \ - io::EpsCopyOutputStream* stream) { \ - ptr = stream->EnsureSpace(ptr); \ - return WireFormatLite::Write##DeclaredType##ToArray(field, value, ptr); \ - } - -WRITE_METHOD(INT64, Int64) -WRITE_METHOD(UINT64, UInt64) -WRITE_METHOD(INT32, Int32) -WRITE_METHOD(UINT32, UInt32) -WRITE_METHOD(SINT64, SInt64) -WRITE_METHOD(SINT32, SInt32) -WRITE_METHOD(ENUM, Enum) -WRITE_METHOD(DOUBLE, Double) -WRITE_METHOD(FLOAT, Float) -WRITE_METHOD(FIXED64, Fixed64) -WRITE_METHOD(FIXED32, Fixed32) -WRITE_METHOD(SFIXED64, SFixed64) -WRITE_METHOD(SFIXED32, SFixed32) -WRITE_METHOD(BOOL, Bool) - -#undef WRITE_METHOD - -template -inline bool MapTypeHandler::Read( - io::CodedInputStream* input, MapEntryAccessorType* value) { - return WireFormatLite::ReadMessageNoVirtual(input, value); -} - -template -inline bool MapTypeHandler::Read( - io::CodedInputStream* input, MapEntryAccessorType* value) { - return WireFormatLite::ReadString(input, value); -} - -template -inline bool MapTypeHandler::Read( - io::CodedInputStream* input, MapEntryAccessorType* value) { - return WireFormatLite::ReadBytes(input, value); -} - -template -const char* MapTypeHandler::Read( - const char* ptr, ParseContext* ctx, MapEntryAccessorType* value) { - return ctx->ParseMessage(value, ptr); -} - -template -const char* MapTypeHandler::Read( - const char* ptr, ParseContext* ctx, MapEntryAccessorType* value) { - int size = ReadSize(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - return ctx->ReadString(ptr, size, value); -} - -template -const char* MapTypeHandler::Read( - const char* ptr, ParseContext* ctx, MapEntryAccessorType* value) { - int size = ReadSize(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - return ctx->ReadString(ptr, size, value); -} - -inline const char* ReadINT64(const char* ptr, int64* value) { - return VarintParse(ptr, reinterpret_cast(value)); -} -inline const char* ReadUINT64(const char* ptr, uint64* value) { - return VarintParse(ptr, value); -} -inline const char* ReadINT32(const char* ptr, int32* value) { - return VarintParse(ptr, reinterpret_cast(value)); -} -inline const char* ReadUINT32(const char* ptr, uint32* value) { - return VarintParse(ptr, value); -} -inline const char* ReadSINT64(const char* ptr, int64* value) { - *value = ReadVarintZigZag64(&ptr); - return ptr; -} -inline const char* ReadSINT32(const char* ptr, int32* value) { - *value = ReadVarintZigZag32(&ptr); - return ptr; -} -template -inline const char* ReadENUM(const char* ptr, E* value) { - *value = static_cast(ReadVarint32(&ptr)); - return ptr; -} -inline const char* ReadBOOL(const char* ptr, bool* value) { - *value = static_cast(ReadVarint32(&ptr)); - return ptr; -} - -template -inline const char* ReadUnaligned(const char* ptr, F* value) { - *value = UnalignedLoad(ptr); - return ptr + sizeof(F); -} -inline const char* ReadFLOAT(const char* ptr, float* value) { - return ReadUnaligned(ptr, value); -} -inline const char* ReadDOUBLE(const char* ptr, double* value) { - return ReadUnaligned(ptr, value); -} -inline const char* ReadFIXED64(const char* ptr, uint64* value) { - return ReadUnaligned(ptr, value); -} -inline const char* ReadFIXED32(const char* ptr, uint32* value) { - return ReadUnaligned(ptr, value); -} -inline const char* ReadSFIXED64(const char* ptr, int64* value) { - return ReadUnaligned(ptr, value); -} -inline const char* ReadSFIXED32(const char* ptr, int32* value) { - return ReadUnaligned(ptr, value); -} - -#define READ_METHOD(FieldType) \ - template \ - inline bool MapTypeHandler::Read( \ - io::CodedInputStream* input, MapEntryAccessorType* value) { \ - return WireFormatLite::ReadPrimitive( \ - input, value); \ - } \ - template \ - const char* MapTypeHandler::Read( \ - const char* begin, ParseContext* ctx, MapEntryAccessorType* value) { \ - (void)ctx; \ - return Read##FieldType(begin, value); \ - } - -READ_METHOD(INT64) -READ_METHOD(UINT64) -READ_METHOD(INT32) -READ_METHOD(UINT32) -READ_METHOD(SINT64) -READ_METHOD(SINT32) -READ_METHOD(ENUM) -READ_METHOD(DOUBLE) -READ_METHOD(FLOAT) -READ_METHOD(FIXED64) -READ_METHOD(FIXED32) -READ_METHOD(SFIXED64) -READ_METHOD(SFIXED32) -READ_METHOD(BOOL) - -#undef READ_METHOD - -// Definition for message handler - -template -inline const Type& -MapTypeHandler::GetExternalReference( - const Type* value) { - return *value; -} - -template -inline size_t MapTypeHandler::SpaceUsedInMapEntryLong(const Type* value) { - return value->SpaceUsedLong(); -} - -template -inline void MapTypeHandler::Clear( - Type** value, Arena* /* arena */) { - if (*value != NULL) (*value)->Clear(); -} -template -inline void MapTypeHandler::Merge( - const Type& from, Type** to, Arena* /* arena */) { - (*to)->MergeFrom(from); -} - -template -void MapTypeHandler::DeleteNoArena( - const Type* ptr) { - delete ptr; -} - -template -constexpr auto MapTypeHandler::Constinit() - -> TypeOnMemory { - return nullptr; -} - -template -inline Type* MapTypeHandler::EnsureMutable( - Type** value, Arena* arena) { - if (*value == NULL) { - *value = MapArenaMessageCreator< - Type, - Arena::is_arena_constructable::type::value>::CreateMessage(arena); - } - return *value; -} - -template -inline const Type& -MapTypeHandler::DefaultIfNotInitialized( - const Type* value) { - return value != NULL ? *value : *Type::internal_default_instance(); -} - -template -inline bool MapTypeHandler::IsInitialized( - Type* value) { - return value ? value->IsInitialized() : false; -} - -// Definition for string/bytes handler - -#define STRING_OR_BYTES_HANDLER_FUNCTIONS(FieldType) \ - template \ - inline const typename MapTypeHandler::MapEntryAccessorType& \ - MapTypeHandler::GetExternalReference(const TypeOnMemory& value) { \ - return value.Get(); \ - } \ - template \ - inline size_t \ - MapTypeHandler::SpaceUsedInMapEntryLong(const TypeOnMemory& value) { \ - return sizeof(value); \ - } \ - template \ - inline void MapTypeHandler::Clear( \ - TypeOnMemory* value, Arena* arena) { \ - value->ClearToEmpty(); \ - } \ - template \ - inline void MapTypeHandler::Merge( \ - const MapEntryAccessorType& from, TypeOnMemory* to, Arena* arena) { \ - to->Set(&internal::GetEmptyStringAlreadyInited(), from, arena); \ - } \ - template \ - void MapTypeHandler::DeleteNoArena( \ - TypeOnMemory& value) { \ - value.DestroyNoArena(&internal::GetEmptyStringAlreadyInited()); \ - } \ - template \ - constexpr auto \ - MapTypeHandler::Constinit() \ - ->TypeOnMemory { \ - return TypeOnMemory(&internal::GetEmptyStringAlreadyInited()); \ - } \ - template \ - inline typename MapTypeHandler::MapEntryAccessorType* \ - MapTypeHandler::EnsureMutable( \ - TypeOnMemory* value, Arena* arena) { \ - return value->Mutable(ArenaStringPtr::EmptyDefault{}, arena); \ - } \ - template \ - inline const typename MapTypeHandler::MapEntryAccessorType& \ - MapTypeHandler::DefaultIfNotInitialized(const TypeOnMemory& value) { \ - return value.Get(); \ - } \ - template \ - inline bool \ - MapTypeHandler::IsInitialized( \ - const TypeOnMemory& /* value */) { \ - return true; \ - } -STRING_OR_BYTES_HANDLER_FUNCTIONS(STRING) -STRING_OR_BYTES_HANDLER_FUNCTIONS(BYTES) -#undef STRING_OR_BYTES_HANDLER_FUNCTIONS - -#define PRIMITIVE_HANDLER_FUNCTIONS(FieldType) \ - template \ - inline const typename MapTypeHandler::MapEntryAccessorType& \ - MapTypeHandler::GetExternalReference(const TypeOnMemory& value) { \ - return value; \ - } \ - template \ - inline size_t MapTypeHandler:: \ - SpaceUsedInMapEntryLong(const TypeOnMemory& /* value */) { \ - return 0; \ - } \ - template \ - inline void MapTypeHandler::Clear( \ - TypeOnMemory* value, Arena* /* arena */) { \ - *value = 0; \ - } \ - template \ - inline void MapTypeHandler::Merge( \ - const MapEntryAccessorType& from, TypeOnMemory* to, \ - Arena* /* arena */) { \ - *to = from; \ - } \ - template \ - inline void MapTypeHandler::DeleteNoArena(TypeOnMemory& /* x */) {} \ - template \ - constexpr auto \ - MapTypeHandler::Constinit() \ - ->TypeOnMemory { \ - return 0; \ - } \ - template \ - inline typename MapTypeHandler::MapEntryAccessorType* \ - MapTypeHandler::EnsureMutable( \ - TypeOnMemory* value, Arena* /* arena */) { \ - return value; \ - } \ - template \ - inline const typename MapTypeHandler::MapEntryAccessorType& \ - MapTypeHandler::DefaultIfNotInitialized(const TypeOnMemory& value) { \ - return value; \ - } \ - template \ - inline bool \ - MapTypeHandler::IsInitialized( \ - const TypeOnMemory& /* value */) { \ - return true; \ - } -PRIMITIVE_HANDLER_FUNCTIONS(INT64) -PRIMITIVE_HANDLER_FUNCTIONS(UINT64) -PRIMITIVE_HANDLER_FUNCTIONS(INT32) -PRIMITIVE_HANDLER_FUNCTIONS(UINT32) -PRIMITIVE_HANDLER_FUNCTIONS(SINT64) -PRIMITIVE_HANDLER_FUNCTIONS(SINT32) -PRIMITIVE_HANDLER_FUNCTIONS(ENUM) -PRIMITIVE_HANDLER_FUNCTIONS(DOUBLE) -PRIMITIVE_HANDLER_FUNCTIONS(FLOAT) -PRIMITIVE_HANDLER_FUNCTIONS(FIXED64) -PRIMITIVE_HANDLER_FUNCTIONS(FIXED32) -PRIMITIVE_HANDLER_FUNCTIONS(SFIXED64) -PRIMITIVE_HANDLER_FUNCTIONS(SFIXED32) -PRIMITIVE_HANDLER_FUNCTIONS(BOOL) -#undef PRIMITIVE_HANDLER_FUNCTIONS - -} // namespace internal -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_TYPE_HANDLER_H__ diff --git a/third_party/protobuf-lite/google/protobuf/message_lite.h b/third_party/protobuf-lite/google/protobuf/message_lite.h deleted file mode 100644 index a76c16e5a..000000000 --- a/third_party/protobuf-lite/google/protobuf/message_lite.h +++ /dev/null @@ -1,621 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Authors: wink@google.com (Wink Saville), -// kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// Defines MessageLite, the abstract interface implemented by all (lite -// and non-lite) protocol message objects. - -#ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__ -#define GOOGLE_PROTOBUF_MESSAGE_LITE_H__ - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { - -template -class RepeatedPtrField; - -namespace io { - -class CodedInputStream; -class CodedOutputStream; -class ZeroCopyInputStream; -class ZeroCopyOutputStream; - -} // namespace io -namespace internal { - -// Tag type used to invoke the constinit constructor overload of some classes. -// Such constructors are internal implementation details of the library. -struct ConstantInitialized { - explicit ConstantInitialized() = default; -}; - -// See parse_context.h for explanation -class ParseContext; - -class RepeatedPtrFieldBase; -class WireFormatLite; -class WeakFieldMap; - -// We compute sizes as size_t but cache them as int. This function converts a -// computed size to a cached size. Since we don't proceed with serialization -// if the total size was > INT_MAX, it is not important what this function -// returns for inputs > INT_MAX. However this case should not error or -// GOOGLE_CHECK-fail, because the full size_t resolution is still returned from -// ByteSizeLong() and checked against INT_MAX; we can catch the overflow -// there. -inline int ToCachedSize(size_t size) { return static_cast(size); } - -// We mainly calculate sizes in terms of size_t, but some functions that -// compute sizes return "int". These int sizes are expected to always be -// positive. This function is more efficient than casting an int to size_t -// directly on 64-bit platforms because it avoids making the compiler emit a -// sign extending instruction, which we don't want and don't want to pay for. -inline size_t FromIntSize(int size) { - // Convert to unsigned before widening so sign extension is not necessary. - return static_cast(size); -} - -// For cases where a legacy function returns an integer size. We GOOGLE_DCHECK() -// that the conversion will fit within an integer; if this is false then we -// are losing information. -inline int ToIntSize(size_t size) { - GOOGLE_DCHECK_LE(size, static_cast(INT_MAX)); - return static_cast(size); -} - -// This type wraps a variable whose constructor and destructor are explicitly -// called. It is particularly useful for a global variable, without its -// constructor and destructor run on start and end of the program lifetime. -// This circumvents the initial construction order fiasco, while keeping -// the address of the empty string a compile time constant. -// -// Pay special attention to the initialization state of the object. -// 1. The object is "uninitialized" to begin with. -// 2. Call Construct() or DefaultConstruct() only if the object is -// uninitialized. After the call, the object becomes "initialized". -// 3. Call get() and get_mutable() only if the object is initialized. -// 4. Call Destruct() only if the object is initialized. -// After the call, the object becomes uninitialized. -template -class ExplicitlyConstructed { - public: - void DefaultConstruct() { new (&union_) T(); } - - template - void Construct(Args&&... args) { - new (&union_) T(std::forward(args)...); - } - - void Destruct() { get_mutable()->~T(); } - - constexpr const T& get() const { return reinterpret_cast(union_); } - T* get_mutable() { return reinterpret_cast(&union_); } - - private: - // Prefer c++14 aligned_storage, but for compatibility this will do. - union AlignedUnion { - char space[sizeof(T)]; - int64 align_to_int64; - void* align_to_ptr; - } union_; -}; - -PROTOBUF_DISABLE_MSVC_UNION_WARNING -// We need a publicly accessible `value` object to allow constexpr -// support in C++11. -// A constexpr accessor does not work portably. -union EmptyString { - constexpr EmptyString() : dummy{} {} - ~EmptyString() {} - - // We need a dummy object for constant initialization. - std::false_type dummy; - std::string value; -}; -PROTOBUF_ENABLE_MSVC_UNION_WARNING - -// Default empty string object. Don't use this directly. Instead, call -// GetEmptyString() to get the reference. -PROTOBUF_EXPORT extern EmptyString fixed_address_empty_string; - - -PROTOBUF_EXPORT constexpr const std::string& GetEmptyStringAlreadyInited() { - return fixed_address_empty_string.value; -} - -PROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const std::string& str); - -} // namespace internal - -// Interface to light weight protocol messages. -// -// This interface is implemented by all protocol message objects. Non-lite -// messages additionally implement the Message interface, which is a -// subclass of MessageLite. Use MessageLite instead when you only need -// the subset of features which it supports -- namely, nothing that uses -// descriptors or reflection. You can instruct the protocol compiler -// to generate classes which implement only MessageLite, not the full -// Message interface, by adding the following line to the .proto file: -// -// option optimize_for = LITE_RUNTIME; -// -// This is particularly useful on resource-constrained systems where -// the full protocol buffers runtime library is too big. -// -// Note that on non-constrained systems (e.g. servers) when you need -// to link in lots of protocol definitions, a better way to reduce -// total code footprint is to use optimize_for = CODE_SIZE. This -// will make the generated code smaller while still supporting all the -// same features (at the expense of speed). optimize_for = LITE_RUNTIME -// is best when you only have a small number of message types linked -// into your binary, in which case the size of the protocol buffers -// runtime itself is the biggest problem. -// -// Users must not derive from this class. Only the protocol compiler and -// the internal library are allowed to create subclasses. -class PROTOBUF_EXPORT MessageLite { - public: - constexpr MessageLite() = default; - virtual ~MessageLite() = default; - - // Basic Operations ------------------------------------------------ - - // Get the name of this message type, e.g. "foo.bar.BazProto". - virtual std::string GetTypeName() const = 0; - - // Construct a new instance of the same type. Ownership is passed to the - // caller. - virtual MessageLite* New() const = 0; - - // Construct a new instance on the arena. Ownership is passed to the caller - // if arena is a NULL. Default implementation for backwards compatibility. - virtual MessageLite* New(Arena* arena) const; - - // Get the arena, if any, associated with this message. Virtual method - // required for generic operations but most arena-related operations should - // use the GetArena() generated-code method. Default implementation - // to reduce code size by avoiding the need for per-type implementations - // when types do not implement arena support. - Arena* GetArena() const { return _internal_metadata_.arena(); } - - // Get a pointer that may be equal to this message's arena, or may not be. - // If the value returned by this method is equal to some arena pointer, then - // this message is on that arena; however, if this message is on some arena, - // this method may or may not return that arena's pointer. As a tradeoff, - // this method may be more efficient than GetArena(). The intent is to allow - // underlying representations that use e.g. tagged pointers to sometimes - // store the arena pointer directly, and sometimes in a more indirect way, - // and allow a fastpath comparison against the arena pointer when it's easy - // to obtain. - void* GetMaybeArenaPointer() const { - return _internal_metadata_.raw_arena_ptr(); - } - - // Clear all fields of the message and set them to their default values. - // Clear() avoids freeing memory, assuming that any memory allocated - // to hold parts of the message will be needed again to hold the next - // message. If you actually want to free the memory used by a Message, - // you must delete it. - virtual void Clear() = 0; - - // Quickly check if all required fields have values set. - virtual bool IsInitialized() const = 0; - - // This is not implemented for Lite messages -- it just returns "(cannot - // determine missing fields for lite message)". However, it is implemented - // for full messages. See message.h. - virtual std::string InitializationErrorString() const; - - // If |other| is the exact same class as this, calls MergeFrom(). Otherwise, - // results are undefined (probably crash). - virtual void CheckTypeAndMergeFrom(const MessageLite& other) = 0; - - // These methods return a human-readable summary of the message. Note that - // since the MessageLite interface does not support reflection, there is very - // little information that these methods can provide. They are shadowed by - // methods of the same name on the Message interface which provide much more - // information. The methods here are intended primarily to facilitate code - // reuse for logic that needs to interoperate with both full and lite protos. - // - // The format of the returned string is subject to change, so please do not - // assume it will remain stable over time. - std::string DebugString() const; - std::string ShortDebugString() const { return DebugString(); } - // MessageLite::DebugString is already Utf8 Safe. This is to add compatibility - // with Message. - std::string Utf8DebugString() const { return DebugString(); } - - // Parsing --------------------------------------------------------- - // Methods for parsing in protocol buffer format. Most of these are - // just simple wrappers around MergeFromCodedStream(). Clear() will be - // called before merging the input. - - // Fill the message with a protocol buffer parsed from the given input - // stream. Returns false on a read error or if the input is in the wrong - // format. A successful return does not indicate the entire input is - // consumed, ensure you call ConsumedEntireMessage() to check that if - // applicable. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromCodedStream( - io::CodedInputStream* input); - // Like ParseFromCodedStream(), but accepts messages that are missing - // required fields. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCodedStream( - io::CodedInputStream* input); - // Read a protocol buffer from the given zero-copy input stream. If - // successful, the entire input will be consumed. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromZeroCopyStream( - io::ZeroCopyInputStream* input); - // Like ParseFromZeroCopyStream(), but accepts messages that are missing - // required fields. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromZeroCopyStream( - io::ZeroCopyInputStream* input); - // Parse a protocol buffer from a file descriptor. If successful, the entire - // input will be consumed. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromFileDescriptor( - int file_descriptor); - // Like ParseFromFileDescriptor(), but accepts messages that are missing - // required fields. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromFileDescriptor( - int file_descriptor); - // Parse a protocol buffer from a C++ istream. If successful, the entire - // input will be consumed. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromIstream(std::istream* input); - // Like ParseFromIstream(), but accepts messages that are missing - // required fields. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromIstream( - std::istream* input); - // Read a protocol buffer from the given zero-copy input stream, expecting - // the message to be exactly "size" bytes long. If successful, exactly - // this many bytes will have been consumed from the input. - bool MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, - int size); - // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are - // missing required fields. - bool MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size); - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromBoundedZeroCopyStream( - io::ZeroCopyInputStream* input, int size); - // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are - // missing required fields. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromBoundedZeroCopyStream( - io::ZeroCopyInputStream* input, int size); - // Parses a protocol buffer contained in a string. Returns true on success. - // This function takes a string in the (non-human-readable) binary wire - // format, matching the encoding output by MessageLite::SerializeToString(). - // If you'd like to convert a human-readable string into a protocol buffer - // object, see google::protobuf::TextFormat::ParseFromString(). - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromString(ConstStringParam data); - // Like ParseFromString(), but accepts messages that are missing - // required fields. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString( - ConstStringParam data); - // Parse a protocol buffer contained in an array of bytes. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data, - int size); - // Like ParseFromArray(), but accepts messages that are missing - // required fields. - PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromArray(const void* data, - int size); - - - // Reads a protocol buffer from the stream and merges it into this - // Message. Singular fields read from the what is - // already in the Message and repeated fields are appended to those - // already present. - // - // It is the responsibility of the caller to call input->LastTagWas() - // (for groups) or input->ConsumedEntireMessage() (for non-groups) after - // this returns to verify that the message's end was delimited correctly. - // - // ParseFromCodedStream() is implemented as Clear() followed by - // MergeFromCodedStream(). - bool MergeFromCodedStream(io::CodedInputStream* input); - - // Like MergeFromCodedStream(), but succeeds even if required fields are - // missing in the input. - // - // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream() - // followed by IsInitialized(). - bool MergePartialFromCodedStream(io::CodedInputStream* input); - - // Merge a protocol buffer contained in a string. - bool MergeFromString(ConstStringParam data); - - - // Serialization --------------------------------------------------- - // Methods for serializing in protocol buffer format. Most of these - // are just simple wrappers around ByteSize() and SerializeWithCachedSizes(). - - // Write a protocol buffer of this message to the given output. Returns - // false on a write error. If the message is missing required fields, - // this may GOOGLE_CHECK-fail. - bool SerializeToCodedStream(io::CodedOutputStream* output) const; - // Like SerializeToCodedStream(), but allows missing required fields. - bool SerializePartialToCodedStream(io::CodedOutputStream* output) const; - // Write the message to the given zero-copy output stream. All required - // fields must be set. - bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const; - // Like SerializeToZeroCopyStream(), but allows missing required fields. - bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const; - // Serialize the message and store it in the given string. All required - // fields must be set. - bool SerializeToString(std::string* output) const; - // Like SerializeToString(), but allows missing required fields. - bool SerializePartialToString(std::string* output) const; - // Serialize the message and store it in the given byte array. All required - // fields must be set. - bool SerializeToArray(void* data, int size) const; - // Like SerializeToArray(), but allows missing required fields. - bool SerializePartialToArray(void* data, int size) const; - - // Make a string encoding the message. Is equivalent to calling - // SerializeToString() on a string and using that. Returns the empty - // string if SerializeToString() would have returned an error. - // Note: If you intend to generate many such strings, you may - // reduce heap fragmentation by instead re-using the same string - // object with calls to SerializeToString(). - std::string SerializeAsString() const; - // Like SerializeAsString(), but allows missing required fields. - std::string SerializePartialAsString() const; - - // Serialize the message and write it to the given file descriptor. All - // required fields must be set. - bool SerializeToFileDescriptor(int file_descriptor) const; - // Like SerializeToFileDescriptor(), but allows missing required fields. - bool SerializePartialToFileDescriptor(int file_descriptor) const; - // Serialize the message and write it to the given C++ ostream. All - // required fields must be set. - bool SerializeToOstream(std::ostream* output) const; - // Like SerializeToOstream(), but allows missing required fields. - bool SerializePartialToOstream(std::ostream* output) const; - - // Like SerializeToString(), but appends to the data to the string's - // existing contents. All required fields must be set. - bool AppendToString(std::string* output) const; - // Like AppendToString(), but allows missing required fields. - bool AppendPartialToString(std::string* output) const; - - - // Computes the serialized size of the message. This recursively calls - // ByteSizeLong() on all embedded messages. - // - // ByteSizeLong() is generally linear in the number of fields defined for the - // proto. - virtual size_t ByteSizeLong() const = 0; - - // Legacy ByteSize() API. - PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead") - int ByteSize() const { return internal::ToIntSize(ByteSizeLong()); } - - // Serializes the message without recomputing the size. The message must not - // have changed since the last call to ByteSize(), and the value returned by - // ByteSize must be non-negative. Otherwise the results are undefined. - void SerializeWithCachedSizes(io::CodedOutputStream* output) const { - output->SetCur(_InternalSerialize(output->Cur(), output->EpsCopy())); - } - - // Functions below here are not part of the public interface. It isn't - // enforced, but they should be treated as private, and will be private - // at some future time. Unfortunately the implementation of the "friend" - // keyword in GCC is broken at the moment, but we expect it will be fixed. - - // Like SerializeWithCachedSizes, but writes directly to *target, returning - // a pointer to the byte immediately after the last byte written. "target" - // must point at a byte array of at least ByteSize() bytes. Whether to use - // deterministic serialization, e.g., maps in sorted order, is determined by - // CodedOutputStream::IsDefaultSerializationDeterministic(). - uint8* SerializeWithCachedSizesToArray(uint8* target) const; - - // Returns the result of the last call to ByteSize(). An embedded message's - // size is needed both to serialize it (because embedded messages are - // length-delimited) and to compute the outer message's size. Caching - // the size avoids computing it multiple times. - // - // ByteSize() does not automatically use the cached size when available - // because this would require invalidating it every time the message was - // modified, which would be too hard and expensive. (E.g. if a deeply-nested - // sub-message is changed, all of its parents' cached sizes would need to be - // invalidated, which is too much work for an otherwise inlined setter - // method.) - virtual int GetCachedSize() const = 0; - - virtual const char* _InternalParse(const char* /*ptr*/, - internal::ParseContext* /*ctx*/) { - return nullptr; - } - - protected: - template - static T* CreateMaybeMessage(Arena* arena) { - return Arena::CreateMaybeMessage(arena); - } - - inline explicit MessageLite(Arena* arena) : _internal_metadata_(arena) {} - - internal::InternalMetadata _internal_metadata_; - - public: - enum ParseFlags { - kMerge = 0, - kParse = 1, - kMergePartial = 2, - kParsePartial = 3, - kMergeWithAliasing = 4, - kParseWithAliasing = 5, - kMergePartialWithAliasing = 6, - kParsePartialWithAliasing = 7 - }; - - template - bool ParseFrom(const T& input); - - // Fast path when conditions match (ie. non-deterministic) - // uint8* _InternalSerialize(uint8* ptr) const; - virtual uint8* _InternalSerialize(uint8* ptr, - io::EpsCopyOutputStream* stream) const = 0; - - // Identical to IsInitialized() except that it logs an error message. - bool IsInitializedWithErrors() const { - if (IsInitialized()) return true; - LogInitializationErrorMessage(); - return false; - } - - private: - // TODO(gerbens) make this a pure abstract function - virtual const void* InternalGetTable() const { return NULL; } - - friend class internal::WireFormatLite; - friend class Message; - friend class internal::WeakFieldMap; - - void LogInitializationErrorMessage() const; - - bool MergeFromImpl(io::CodedInputStream* input, ParseFlags parse_flags); - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite); -}; - -namespace internal { - -template -bool MergeFromImpl(StringPiece input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(StringPiece input, - MessageLite* msg, - MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(StringPiece input, - MessageLite* msg, - MessageLite::ParseFlags parse_flags); - -template -bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(io::ZeroCopyInputStream* input, - MessageLite* msg, - MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(io::ZeroCopyInputStream* input, - MessageLite* msg, - MessageLite::ParseFlags parse_flags); - -struct BoundedZCIS { - io::ZeroCopyInputStream* zcis; - int limit; -}; - -template -bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); -extern template bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); - -template -struct SourceWrapper; - -template -bool MergeFromImpl(const SourceWrapper& input, MessageLite* msg, - MessageLite::ParseFlags parse_flags) { - return input.template MergeInto(msg, parse_flags); -} - -} // namespace internal - -template -bool MessageLite::ParseFrom(const T& input) { - if (flags & kParse) Clear(); - constexpr bool alias = (flags & kMergeWithAliasing) != 0; - return internal::MergeFromImpl(input, this, flags); -} - -// =================================================================== -// Shutdown support. - - -// Shut down the entire protocol buffers library, deleting all static-duration -// objects allocated by the library or by generated .pb.cc files. -// -// There are two reasons you might want to call this: -// * You use a draconian definition of "memory leak" in which you expect -// every single malloc() to have a corresponding free(), even for objects -// which live until program exit. -// * You are writing a dynamically-loaded library which needs to clean up -// after itself when the library is unloaded. -// -// It is safe to call this multiple times. However, it is not safe to use -// any other part of the protocol buffers library after -// ShutdownProtobufLibrary() has been called. Furthermore this call is not -// thread safe, user needs to synchronize multiple calls. -PROTOBUF_EXPORT void ShutdownProtobufLibrary(); - -namespace internal { - -// Register a function to be called when ShutdownProtocolBuffers() is called. -PROTOBUF_EXPORT void OnShutdown(void (*func)()); -// Run an arbitrary function on an arg -PROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg); - -template -T* OnShutdownDelete(T* p) { - OnShutdownRun([](const void* pp) { delete static_cast(pp); }, p); - return p; -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/metadata_lite.h b/third_party/protobuf-lite/google/protobuf/metadata_lite.h deleted file mode 100644 index ec5f92389..000000000 --- a/third_party/protobuf-lite/google/protobuf/metadata_lite.h +++ /dev/null @@ -1,248 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_METADATA_LITE_H__ -#define GOOGLE_PROTOBUF_METADATA_LITE_H__ - -#include -#include -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { -namespace internal { - -// This is the representation for messages that support arena allocation. It -// uses a tagged pointer to either store the Arena pointer, if there are no -// unknown fields, or a pointer to a block of memory with both the Arena pointer -// and the UnknownFieldSet, if there are unknown fields. This optimization -// allows for "zero-overhead" storage of the Arena pointer, relative to the -// above baseline implementation. -// -// The tagged pointer uses the LSB to disambiguate cases, and uses bit 0 == 0 to -// indicate an arena pointer and bit 0 == 1 to indicate a UFS+Arena-container -// pointer. -class InternalMetadata { - public: - constexpr InternalMetadata() : ptr_(nullptr) {} - explicit InternalMetadata(Arena* arena) : ptr_(arena) {} - - template - void Delete() { - // Note that Delete<> should be called not more than once. - if (have_unknown_fields() && arena() == NULL) { - delete PtrValue>(); - } - } - - PROTOBUF_ALWAYS_INLINE Arena* arena() const { - if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) { - return PtrValue()->arena; - } else { - return PtrValue(); - } - } - - PROTOBUF_ALWAYS_INLINE bool have_unknown_fields() const { - return PtrTag() == kTagContainer; - } - - PROTOBUF_ALWAYS_INLINE void* raw_arena_ptr() const { return ptr_; } - - template - PROTOBUF_ALWAYS_INLINE const T& unknown_fields( - const T& (*default_instance)()) const { - if (PROTOBUF_PREDICT_FALSE(have_unknown_fields())) { - return PtrValue>()->unknown_fields; - } else { - return default_instance(); - } - } - - template - PROTOBUF_ALWAYS_INLINE T* mutable_unknown_fields() { - if (PROTOBUF_PREDICT_TRUE(have_unknown_fields())) { - return &PtrValue>()->unknown_fields; - } else { - return mutable_unknown_fields_slow(); - } - } - - template - PROTOBUF_ALWAYS_INLINE void Swap(InternalMetadata* other) { - // Semantics here are that we swap only the unknown fields, not the arena - // pointer. We cannot simply swap ptr_ with other->ptr_ because we need to - // maintain our own arena ptr. Also, our ptr_ and other's ptr_ may be in - // different states (direct arena pointer vs. container with UFS) so we - // cannot simply swap ptr_ and then restore the arena pointers. We reuse - // UFS's swap implementation instead. - if (have_unknown_fields() || other->have_unknown_fields()) { - DoSwap(other->mutable_unknown_fields()); - } - } - - template - PROTOBUF_ALWAYS_INLINE void MergeFrom(const InternalMetadata& other) { - if (other.have_unknown_fields()) { - DoMergeFrom(other.unknown_fields(nullptr)); - } - } - - template - PROTOBUF_ALWAYS_INLINE void Clear() { - if (have_unknown_fields()) { - DoClear(); - } - } - - private: - void* ptr_; - - // Tagged pointer implementation. - enum { - // ptr_ is an Arena*. - kTagArena = 0, - // ptr_ is a Container*. - kTagContainer = 1, - }; - static constexpr intptr_t kPtrTagMask = 1; - static constexpr intptr_t kPtrValueMask = ~kPtrTagMask; - - // Accessors for pointer tag and pointer value. - PROTOBUF_ALWAYS_INLINE int PtrTag() const { - return reinterpret_cast(ptr_) & kPtrTagMask; - } - - template - U* PtrValue() const { - return reinterpret_cast(reinterpret_cast(ptr_) & - kPtrValueMask); - } - - // If ptr_'s tag is kTagContainer, it points to an instance of this struct. - struct ContainerBase { - Arena* arena; - }; - - template - struct Container : public ContainerBase { - T unknown_fields; - }; - - template - PROTOBUF_NOINLINE T* mutable_unknown_fields_slow() { - Arena* my_arena = arena(); - Container* container = Arena::Create>(my_arena); - // Two-step assignment works around a bug in clang's static analyzer: - // https://bugs.llvm.org/show_bug.cgi?id=34198. - ptr_ = container; - ptr_ = reinterpret_cast(reinterpret_cast(ptr_) | - kTagContainer); - container->arena = my_arena; - return &(container->unknown_fields); - } - - // Templated functions. - - template - void DoClear() { - mutable_unknown_fields()->Clear(); - } - - template - void DoMergeFrom(const T& other) { - mutable_unknown_fields()->MergeFrom(other); - } - - template - void DoSwap(T* other) { - mutable_unknown_fields()->Swap(other); - } -}; - -// String Template specializations. - -template <> -inline void InternalMetadata::DoClear() { - mutable_unknown_fields()->clear(); -} - -template <> -inline void InternalMetadata::DoMergeFrom( - const std::string& other) { - mutable_unknown_fields()->append(other); -} - -template <> -inline void InternalMetadata::DoSwap(std::string* other) { - mutable_unknown_fields()->swap(*other); -} - -// This helper RAII class is needed to efficiently parse unknown fields. We -// should only call mutable_unknown_fields if there are actual unknown fields. -// The obvious thing to just use a stack string and swap it at the end of -// the parse won't work, because the destructor of StringOutputStream needs to -// be called before we can modify the string (it check-fails). Using -// LiteUnknownFieldSetter setter(&_internal_metadata_); -// StringOutputStream stream(setter.buffer()); -// guarantees that the string is only swapped after stream is destroyed. -class PROTOBUF_EXPORT LiteUnknownFieldSetter { - public: - explicit LiteUnknownFieldSetter(InternalMetadata* metadata) - : metadata_(metadata) { - if (metadata->have_unknown_fields()) { - buffer_.swap(*metadata->mutable_unknown_fields()); - } - } - ~LiteUnknownFieldSetter() { - if (!buffer_.empty()) - metadata_->mutable_unknown_fields()->swap(buffer_); - } - std::string* buffer() { return &buffer_; } - - private: - InternalMetadata* metadata_; - std::string buffer_; -}; - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_METADATA_LITE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/parse_context.h b/third_party/protobuf-lite/google/protobuf/parse_context.h deleted file mode 100644 index 661008589..000000000 --- a/third_party/protobuf-lite/google/protobuf/parse_context.h +++ /dev/null @@ -1,869 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_PARSE_CONTEXT_H__ -#define GOOGLE_PROTOBUF_PARSE_CONTEXT_H__ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - - -namespace google { -namespace protobuf { - -class UnknownFieldSet; -class DescriptorPool; -class MessageFactory; - -namespace internal { - -// Template code below needs to know about the existence of these functions. -PROTOBUF_EXPORT void WriteVarint(uint32 num, uint64 val, std::string* s); -PROTOBUF_EXPORT void WriteLengthDelimited(uint32 num, StringPiece val, - std::string* s); -// Inline because it is just forwarding to s->WriteVarint -inline void WriteVarint(uint32 num, uint64 val, UnknownFieldSet* s); -inline void WriteLengthDelimited(uint32 num, StringPiece val, - UnknownFieldSet* s); - - -// The basic abstraction the parser is designed for is a slight modification -// of the ZeroCopyInputStream (ZCIS) abstraction. A ZCIS presents a serialized -// stream as a series of buffers that concatenate to the full stream. -// Pictorially a ZCIS presents a stream in chunks like so -// [---------------------------------------------------------------] -// [---------------------] chunk 1 -// [----------------------------] chunk 2 -// chunk 3 [--------------] -// -// Where the '-' represent the bytes which are vertically lined up with the -// bytes of the stream. The proto parser requires its input to be presented -// similarly with the extra -// property that each chunk has kSlopBytes past its end that overlaps with the -// first kSlopBytes of the next chunk, or if there is no next chunk at least its -// still valid to read those bytes. Again, pictorially, we now have -// -// [---------------------------------------------------------------] -// [-------------------....] chunk 1 -// [------------------------....] chunk 2 -// chunk 3 [------------------..**] -// chunk 4 [--****] -// Here '-' mean the bytes of the stream or chunk and '.' means bytes past the -// chunk that match up with the start of the next chunk. Above each chunk has -// 4 '.' after the chunk. In the case these 'overflow' bytes represents bytes -// past the stream, indicated by '*' above, their values are unspecified. It is -// still legal to read them (ie. should not segfault). Reading past the -// end should be detected by the user and indicated as an error. -// -// The reason for this, admittedly, unconventional invariant is to ruthlessly -// optimize the protobuf parser. Having an overlap helps in two important ways. -// Firstly it alleviates having to performing bounds checks if a piece of code -// is guaranteed to not read more than kSlopBytes. Secondly, and more -// importantly, the protobuf wireformat is such that reading a key/value pair is -// always less than 16 bytes. This removes the need to change to next buffer in -// the middle of reading primitive values. Hence there is no need to store and -// load the current position. - -class PROTOBUF_EXPORT EpsCopyInputStream { - public: - enum { kSlopBytes = 16, kMaxCordBytesToCopy = 512 }; - - explicit EpsCopyInputStream(bool enable_aliasing) - : aliasing_(enable_aliasing ? kOnPatch : kNoAliasing) {} - - void BackUp(const char* ptr) { - GOOGLE_DCHECK(ptr <= buffer_end_ + kSlopBytes); - int count; - if (next_chunk_ == buffer_) { - count = static_cast(buffer_end_ + kSlopBytes - ptr); - } else { - count = size_ + static_cast(buffer_end_ - ptr); - } - if (count > 0) StreamBackUp(count); - } - - // If return value is negative it's an error - PROTOBUF_MUST_USE_RESULT int PushLimit(const char* ptr, int limit) { - GOOGLE_DCHECK(limit >= 0 && limit <= INT_MAX - kSlopBytes); - // This add is safe due to the invariant above, because - // ptr - buffer_end_ <= kSlopBytes. - limit += static_cast(ptr - buffer_end_); - limit_end_ = buffer_end_ + (std::min)(0, limit); - auto old_limit = limit_; - limit_ = limit; - return old_limit - limit; - } - - PROTOBUF_MUST_USE_RESULT bool PopLimit(int delta) { - if (PROTOBUF_PREDICT_FALSE(!EndedAtLimit())) return false; - limit_ = limit_ + delta; - // TODO(gerbens) We could remove this line and hoist the code to - // DoneFallback. Study the perf/bin-size effects. - limit_end_ = buffer_end_ + (std::min)(0, limit_); - return true; - } - - PROTOBUF_MUST_USE_RESULT const char* Skip(const char* ptr, int size) { - if (size <= buffer_end_ + kSlopBytes - ptr) { - return ptr + size; - } - return SkipFallback(ptr, size); - } - PROTOBUF_MUST_USE_RESULT const char* ReadString(const char* ptr, int size, - std::string* s) { - if (size <= buffer_end_ + kSlopBytes - ptr) { - s->assign(ptr, size); - return ptr + size; - } - return ReadStringFallback(ptr, size, s); - } - PROTOBUF_MUST_USE_RESULT const char* AppendString(const char* ptr, int size, - std::string* s) { - if (size <= buffer_end_ + kSlopBytes - ptr) { - s->append(ptr, size); - return ptr + size; - } - return AppendStringFallback(ptr, size, s); - } - - template - PROTOBUF_MUST_USE_RESULT const char* ReadRepeatedFixed(const char* ptr, - Tag expected_tag, - RepeatedField* out); - - template - PROTOBUF_MUST_USE_RESULT const char* ReadPackedFixed(const char* ptr, - int size, - RepeatedField* out); - template - PROTOBUF_MUST_USE_RESULT const char* ReadPackedVarint(const char* ptr, - Add add); - - uint32 LastTag() const { return last_tag_minus_1_ + 1; } - bool ConsumeEndGroup(uint32 start_tag) { - bool res = last_tag_minus_1_ == start_tag; - last_tag_minus_1_ = 0; - return res; - } - bool EndedAtLimit() const { return last_tag_minus_1_ == 0; } - bool EndedAtEndOfStream() const { return last_tag_minus_1_ == 1; } - void SetLastTag(uint32 tag) { last_tag_minus_1_ = tag - 1; } - void SetEndOfStream() { last_tag_minus_1_ = 1; } - bool IsExceedingLimit(const char* ptr) { - return ptr > limit_end_ && - (next_chunk_ == nullptr || ptr - buffer_end_ > limit_); - } - int BytesUntilLimit(const char* ptr) const { - return limit_ + static_cast(buffer_end_ - ptr); - } - // Returns true if more data is available, if false is returned one has to - // call Done for further checks. - bool DataAvailable(const char* ptr) { return ptr < limit_end_; } - - protected: - // Returns true is limit (either an explicit limit or end of stream) is - // reached. It aligns *ptr across buffer seams. - // If limit is exceeded it returns true and ptr is set to null. - bool DoneWithCheck(const char** ptr, int d) { - GOOGLE_DCHECK(*ptr); - if (PROTOBUF_PREDICT_TRUE(*ptr < limit_end_)) return false; - int overrun = *ptr - buffer_end_; - GOOGLE_DCHECK_LE(overrun, kSlopBytes); // Guaranteed by parse loop. - if (overrun == - limit_) { // No need to flip buffers if we ended on a limit. - // If we actually overrun the buffer and next_chunk_ is null. It means - // the stream ended and we passed the stream end. - if (overrun > 0 && next_chunk_ == nullptr) *ptr = nullptr; - return true; - } - auto res = DoneFallback(overrun, d); - *ptr = res.first; - return res.second; - } - - const char* InitFrom(StringPiece flat) { - overall_limit_ = 0; - if (flat.size() > kSlopBytes) { - limit_ = kSlopBytes; - limit_end_ = buffer_end_ = flat.data() + flat.size() - kSlopBytes; - next_chunk_ = buffer_; - if (aliasing_ == kOnPatch) aliasing_ = kNoDelta; - return flat.data(); - } else { - std::memcpy(buffer_, flat.data(), flat.size()); - limit_ = 0; - limit_end_ = buffer_end_ = buffer_ + flat.size(); - next_chunk_ = nullptr; - if (aliasing_ == kOnPatch) { - aliasing_ = reinterpret_cast(flat.data()) - - reinterpret_cast(buffer_); - } - return buffer_; - } - } - - const char* InitFrom(io::ZeroCopyInputStream* zcis); - - const char* InitFrom(io::ZeroCopyInputStream* zcis, int limit) { - if (limit == -1) return InitFrom(zcis); - overall_limit_ = limit; - auto res = InitFrom(zcis); - limit_ = limit - static_cast(buffer_end_ - res); - limit_end_ = buffer_end_ + (std::min)(0, limit_); - return res; - } - - private: - const char* limit_end_; // buffer_end_ + min(limit_, 0) - const char* buffer_end_; - const char* next_chunk_; - int size_; - int limit_; // relative to buffer_end_; - io::ZeroCopyInputStream* zcis_ = nullptr; - char buffer_[2 * kSlopBytes] = {}; - enum { kNoAliasing = 0, kOnPatch = 1, kNoDelta = 2 }; - std::uintptr_t aliasing_ = kNoAliasing; - // This variable is used to communicate how the parse ended, in order to - // completely verify the parsed data. A wire-format parse can end because of - // one of the following conditions: - // 1) A parse can end on a pushed limit. - // 2) A parse can end on End Of Stream (EOS). - // 3) A parse can end on 0 tag (only valid for toplevel message). - // 4) A parse can end on an end-group tag. - // This variable should always be set to 0, which indicates case 1. If the - // parse terminated due to EOS (case 2), it's set to 1. In case the parse - // ended due to a terminating tag (case 3 and 4) it's set to (tag - 1). - // This var doesn't really belong in EpsCopyInputStream and should be part of - // the ParseContext, but case 2 is most easily and optimally implemented in - // DoneFallback. - uint32 last_tag_minus_1_ = 0; - int overall_limit_ = INT_MAX; // Overall limit independent of pushed limits. - // Pretty random large number that seems like a safe allocation on most - // systems. TODO(gerbens) do we need to set this as build flag? - enum { kSafeStringSize = 50000000 }; - - // Advances to next buffer chunk returns a pointer to the same logical place - // in the stream as set by overrun. Overrun indicates the position in the slop - // region the parse was left (0 <= overrun <= kSlopBytes). Returns true if at - // limit, at which point the returned pointer maybe null if there was an - // error. The invariant of this function is that it's guaranteed that - // kSlopBytes bytes can be accessed from the returned ptr. This function might - // advance more buffers than one in the underlying ZeroCopyInputStream. - std::pair DoneFallback(int overrun, int depth); - // Advances to the next buffer, at most one call to Next() on the underlying - // ZeroCopyInputStream is made. This function DOES NOT match the returned - // pointer to where in the slop region the parse ends, hence no overrun - // parameter. This is useful for string operations where you always copy - // to the end of the buffer (including the slop region). - const char* Next(); - // overrun is the location in the slop region the stream currently is - // (0 <= overrun <= kSlopBytes). To prevent flipping to the next buffer of - // the ZeroCopyInputStream in the case the parse will end in the last - // kSlopBytes of the current buffer. depth is the current depth of nested - // groups (or negative if the use case does not need careful tracking). - inline const char* NextBuffer(int overrun, int depth); - const char* SkipFallback(const char* ptr, int size); - const char* AppendStringFallback(const char* ptr, int size, std::string* str); - const char* ReadStringFallback(const char* ptr, int size, std::string* str); - bool StreamNext(const void** data) { - bool res = zcis_->Next(data, &size_); - if (res) overall_limit_ -= size_; - return res; - } - void StreamBackUp(int count) { - zcis_->BackUp(count); - overall_limit_ += count; - } - - template - const char* AppendSize(const char* ptr, int size, const A& append) { - int chunk_size = buffer_end_ + kSlopBytes - ptr; - do { - GOOGLE_DCHECK(size > chunk_size); - if (next_chunk_ == nullptr) return nullptr; - append(ptr, chunk_size); - ptr += chunk_size; - size -= chunk_size; - // TODO(gerbens) Next calls NextBuffer which generates buffers with - // overlap and thus incurs cost of copying the slop regions. This is not - // necessary for reading strings. We should just call Next buffers. - if (limit_ <= kSlopBytes) return nullptr; - ptr = Next(); - if (ptr == nullptr) return nullptr; // passed the limit - ptr += kSlopBytes; - chunk_size = buffer_end_ + kSlopBytes - ptr; - } while (size > chunk_size); - append(ptr, size); - return ptr + size; - } - - // AppendUntilEnd appends data until a limit (either a PushLimit or end of - // stream. Normal payloads are from length delimited fields which have an - // explicit size. Reading until limit only comes when the string takes - // the place of a protobuf, ie RawMessage/StringRawMessage, lazy fields and - // implicit weak messages. We keep these methods private and friend them. - template - const char* AppendUntilEnd(const char* ptr, const A& append) { - if (ptr - buffer_end_ > limit_) return nullptr; - while (limit_ > kSlopBytes) { - int chunk_size = buffer_end_ + kSlopBytes - ptr; - GOOGLE_DCHECK_GE(chunk_size, 0); - append(ptr, chunk_size); - ptr = Next(); - if (ptr == nullptr) return limit_end_; - ptr += kSlopBytes; - } - auto end = buffer_end_ + limit_; - GOOGLE_DCHECK(end >= ptr); - append(ptr, end - ptr); - return end; - } - - PROTOBUF_MUST_USE_RESULT const char* AppendString(const char* ptr, - std::string* str) { - return AppendUntilEnd( - ptr, [str](const char* p, ptrdiff_t s) { str->append(p, s); }); - } - friend class ImplicitWeakMessage; -}; - -// ParseContext holds all data that is global to the entire parse. Most -// importantly it contains the input stream, but also recursion depth and also -// stores the end group tag, in case a parser ended on a endgroup, to verify -// matching start/end group tags. -class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream { - public: - struct Data { - const DescriptorPool* pool = nullptr; - MessageFactory* factory = nullptr; - }; - - template - ParseContext(int depth, bool aliasing, const char** start, T&&... args) - : EpsCopyInputStream(aliasing), depth_(depth) { - *start = InitFrom(std::forward(args)...); - } - - void TrackCorrectEnding() { group_depth_ = 0; } - - bool Done(const char** ptr) { return DoneWithCheck(ptr, group_depth_); } - - int depth() const { return depth_; } - - Data& data() { return data_; } - const Data& data() const { return data_; } - - template - PROTOBUF_MUST_USE_RESULT const char* ParseMessage(T* msg, const char* ptr); - // We outline when the type is generic and we go through a virtual - const char* ParseMessage(MessageLite* msg, const char* ptr); - const char* ParseMessage(Message* msg, const char* ptr); - - template - PROTOBUF_MUST_USE_RESULT PROTOBUF_ALWAYS_INLINE const char* ParseGroup( - T* msg, const char* ptr, uint32 tag) { - if (--depth_ < 0) return nullptr; - group_depth_++; - ptr = msg->_InternalParse(ptr, this); - group_depth_--; - depth_++; - if (PROTOBUF_PREDICT_FALSE(!ConsumeEndGroup(tag))) return nullptr; - return ptr; - } - - private: - // The context keeps an internal stack to keep track of the recursive - // part of the parse state. - // Current depth of the active parser, depth counts down. - // This is used to limit recursion depth (to prevent overflow on malicious - // data), but is also used to index in stack_ to store the current state. - int depth_; - // Unfortunately necessary for the fringe case of ending on 0 or end-group tag - // in the last kSlopBytes of a ZeroCopyInputStream chunk. - int group_depth_ = INT_MIN; - Data data_; -}; - -template -bool ExpectTag(const char* ptr) { - if (tag < 128) { - return *ptr == tag; - } else { - static_assert(tag < 128 * 128, "We only expect tags for 1 or 2 bytes"); - char buf[2] = {static_cast(tag | 0x80), static_cast(tag >> 7)}; - return std::memcmp(ptr, buf, 2) == 0; - } -} - -template -struct EndianHelper; - -template <> -struct EndianHelper<1> { - static uint8 Load(const void* p) { return *static_cast(p); } -}; - -template <> -struct EndianHelper<2> { - static uint16 Load(const void* p) { - uint16 tmp; - std::memcpy(&tmp, p, 2); -#ifndef PROTOBUF_LITTLE_ENDIAN - tmp = bswap_16(tmp); -#endif - return tmp; - } -}; - -template <> -struct EndianHelper<4> { - static uint32 Load(const void* p) { - uint32 tmp; - std::memcpy(&tmp, p, 4); -#ifndef PROTOBUF_LITTLE_ENDIAN - tmp = bswap_32(tmp); -#endif - return tmp; - } -}; - -template <> -struct EndianHelper<8> { - static uint64 Load(const void* p) { - uint64 tmp; - std::memcpy(&tmp, p, 8); -#ifndef PROTOBUF_LITTLE_ENDIAN - tmp = bswap_64(tmp); -#endif - return tmp; - } -}; - -template -T UnalignedLoad(const char* p) { - auto tmp = EndianHelper::Load(p); - T res; - memcpy(&res, &tmp, sizeof(T)); - return res; -} - -PROTOBUF_EXPORT -std::pair VarintParseSlow32(const char* p, uint32 res); -PROTOBUF_EXPORT -std::pair VarintParseSlow64(const char* p, uint32 res); - -inline const char* VarintParseSlow(const char* p, uint32 res, uint32* out) { - auto tmp = VarintParseSlow32(p, res); - *out = tmp.second; - return tmp.first; -} - -inline const char* VarintParseSlow(const char* p, uint32 res, uint64* out) { - auto tmp = VarintParseSlow64(p, res); - *out = tmp.second; - return tmp.first; -} - -template -PROTOBUF_MUST_USE_RESULT const char* VarintParse(const char* p, T* out) { - auto ptr = reinterpret_cast(p); - uint32 res = ptr[0]; - if (!(res & 0x80)) { - *out = res; - return p + 1; - } - uint32 byte = ptr[1]; - res += (byte - 1) << 7; - if (!(byte & 0x80)) { - *out = res; - return p + 2; - } - return VarintParseSlow(p, res, out); -} - -// Used for tags, could read up to 5 bytes which must be available. -// Caller must ensure its safe to call. - -PROTOBUF_EXPORT -std::pair ReadTagFallback(const char* p, uint32 res); - -// Same as ParseVarint but only accept 5 bytes at most. -inline const char* ReadTag(const char* p, uint32* out, uint32 /*max_tag*/ = 0) { - uint32 res = static_cast(p[0]); - if (res < 128) { - *out = res; - return p + 1; - } - uint32 second = static_cast(p[1]); - res += (second - 1) << 7; - if (second < 128) { - *out = res; - return p + 2; - } - auto tmp = ReadTagFallback(p, res); - *out = tmp.second; - return tmp.first; -} - -// Decode 2 consecutive bytes of a varint and returns the value, shifted left -// by 1. It simultaneous updates *ptr to *ptr + 1 or *ptr + 2 depending if the -// first byte's continuation bit is set. -// If bit 15 of return value is set (equivalent to the continuation bits of both -// bytes being set) the varint continues, otherwise the parse is done. On x86 -// movsx eax, dil -// add edi, eax -// adc [rsi], 1 -// add eax, eax -// and eax, edi -inline uint32 DecodeTwoBytes(const char** ptr) { - uint32 value = UnalignedLoad(*ptr); - // Sign extend the low byte continuation bit - uint32_t x = static_cast(value); - // This add is an amazing operation, it cancels the low byte continuation bit - // from y transferring it to the carry. Simultaneously it also shifts the 7 - // LSB left by one tightly against high byte varint bits. Hence value now - // contains the unpacked value shifted left by 1. - value += x; - // Use the carry to update the ptr appropriately. - *ptr += value < x ? 2 : 1; - return value & (x + x); // Mask out the high byte iff no continuation -} - -// More efficient varint parsing for big varints -inline const char* ParseBigVarint(const char* p, uint64* out) { - auto pnew = p; - auto tmp = DecodeTwoBytes(&pnew); - uint64 res = tmp >> 1; - if (PROTOBUF_PREDICT_TRUE(std::int16_t(tmp) >= 0)) { - *out = res; - return pnew; - } - for (std::uint32_t i = 1; i < 5; i++) { - pnew = p + 2 * i; - tmp = DecodeTwoBytes(&pnew); - res += (static_cast(tmp) - 2) << (14 * i - 1); - if (PROTOBUF_PREDICT_TRUE(std::int16_t(tmp) >= 0)) { - *out = res; - return pnew; - } - } - return nullptr; -} - -PROTOBUF_EXPORT -std::pair ReadSizeFallback(const char* p, uint32 first); -// Used for tags, could read up to 5 bytes which must be available. Additionally -// it makes sure the unsigned value fits a int32, otherwise returns nullptr. -// Caller must ensure its safe to call. -inline uint32 ReadSize(const char** pp) { - auto p = *pp; - uint32 res = static_cast(p[0]); - if (res < 128) { - *pp = p + 1; - return res; - } - auto x = ReadSizeFallback(p, res); - *pp = x.first; - return x.second; -} - -// Some convenience functions to simplify the generated parse loop code. -// Returning the value and updating the buffer pointer allows for nicer -// function composition. We rely on the compiler to inline this. -// Also in debug compiles having local scoped variables tend to generated -// stack frames that scale as O(num fields). -inline uint64 ReadVarint64(const char** p) { - uint64 tmp; - *p = VarintParse(*p, &tmp); - return tmp; -} - -inline uint32 ReadVarint32(const char** p) { - uint32 tmp; - *p = VarintParse(*p, &tmp); - return tmp; -} - -inline int64 ReadVarintZigZag64(const char** p) { - uint64 tmp; - *p = VarintParse(*p, &tmp); - return WireFormatLite::ZigZagDecode64(tmp); -} - -inline int32 ReadVarintZigZag32(const char** p) { - uint64 tmp; - *p = VarintParse(*p, &tmp); - return WireFormatLite::ZigZagDecode32(static_cast(tmp)); -} - -template -PROTOBUF_MUST_USE_RESULT const char* ParseContext::ParseMessage( - T* msg, const char* ptr) { - int size = ReadSize(&ptr); - if (!ptr) return nullptr; - auto old = PushLimit(ptr, size); - if (--depth_ < 0) return nullptr; - ptr = msg->_InternalParse(ptr, this); - if (PROTOBUF_PREDICT_FALSE(ptr == nullptr)) return nullptr; - depth_++; - if (!PopLimit(old)) return nullptr; - return ptr; -} - -template -const char* ReadPackedVarintArray(const char* ptr, const char* end, Add add) { - while (ptr < end) { - uint64 varint; - ptr = VarintParse(ptr, &varint); - if (ptr == nullptr) return nullptr; - add(varint); - } - return ptr; -} - -template -const char* EpsCopyInputStream::ReadPackedVarint(const char* ptr, Add add) { - int size = ReadSize(&ptr); - if (ptr == nullptr) return nullptr; - int chunk_size = buffer_end_ - ptr; - while (size > chunk_size) { - ptr = ReadPackedVarintArray(ptr, buffer_end_, add); - if (ptr == nullptr) return nullptr; - int overrun = ptr - buffer_end_; - GOOGLE_DCHECK(overrun >= 0 && overrun <= kSlopBytes); - if (size - chunk_size <= kSlopBytes) { - // The current buffer contains all the information needed, we don't need - // to flip buffers. However we must parse from a buffer with enough space - // so we are not prone to a buffer overflow. - char buf[kSlopBytes + 10] = {}; - std::memcpy(buf, buffer_end_, kSlopBytes); - GOOGLE_CHECK_LE(size - chunk_size, kSlopBytes); - auto end = buf + (size - chunk_size); - auto res = ReadPackedVarintArray(buf + overrun, end, add); - if (res == nullptr || res != end) return nullptr; - return buffer_end_ + (res - buf); - } - size -= overrun + chunk_size; - GOOGLE_DCHECK_GT(size, 0); - // We must flip buffers - if (limit_ <= kSlopBytes) return nullptr; - ptr = Next(); - if (ptr == nullptr) return nullptr; - ptr += overrun; - chunk_size = buffer_end_ - ptr; - } - auto end = ptr + size; - ptr = ReadPackedVarintArray(ptr, end, add); - return end == ptr ? ptr : nullptr; -} - -// Helper for verification of utf8 -PROTOBUF_EXPORT -bool VerifyUTF8(StringPiece s, const char* field_name); - -inline bool VerifyUTF8(const std::string* s, const char* field_name) { - return VerifyUTF8(*s, field_name); -} - -// All the string parsers with or without UTF checking and for all CTypes. -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* InlineGreedyStringParser( - std::string* s, const char* ptr, ParseContext* ctx); - - -// Add any of the following lines to debug which parse function is failing. - -#define GOOGLE_PROTOBUF_ASSERT_RETURN(predicate, ret) \ - if (!(predicate)) { \ - /* ::raise(SIGINT); */ \ - /* GOOGLE_LOG(ERROR) << "Parse failure"; */ \ - return ret; \ - } - -#define GOOGLE_PROTOBUF_PARSER_ASSERT(predicate) \ - GOOGLE_PROTOBUF_ASSERT_RETURN(predicate, nullptr) - -template -PROTOBUF_MUST_USE_RESULT const char* FieldParser(uint64 tag, T& field_parser, - const char* ptr, - ParseContext* ctx) { - uint32 number = tag >> 3; - GOOGLE_PROTOBUF_PARSER_ASSERT(number != 0); - using WireType = internal::WireFormatLite::WireType; - switch (tag & 7) { - case WireType::WIRETYPE_VARINT: { - uint64 value; - ptr = VarintParse(ptr, &value); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - field_parser.AddVarint(number, value); - break; - } - case WireType::WIRETYPE_FIXED64: { - uint64 value = UnalignedLoad(ptr); - ptr += 8; - field_parser.AddFixed64(number, value); - break; - } - case WireType::WIRETYPE_LENGTH_DELIMITED: { - ptr = field_parser.ParseLengthDelimited(number, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - break; - } - case WireType::WIRETYPE_START_GROUP: { - ptr = field_parser.ParseGroup(number, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - break; - } - case WireType::WIRETYPE_END_GROUP: { - GOOGLE_LOG(FATAL) << "Can't happen"; - break; - } - case WireType::WIRETYPE_FIXED32: { - uint32 value = UnalignedLoad(ptr); - ptr += 4; - field_parser.AddFixed32(number, value); - break; - } - default: - return nullptr; - } - return ptr; -} - -template -PROTOBUF_MUST_USE_RESULT const char* WireFormatParser(T& field_parser, - const char* ptr, - ParseContext* ctx) { - while (!ctx->Done(&ptr)) { - uint32 tag; - ptr = ReadTag(ptr, &tag); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); - if (tag == 0 || (tag & 7) == 4) { - ctx->SetLastTag(tag); - return ptr; - } - ptr = FieldParser(tag, field_parser, ptr, ctx); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr); - } - return ptr; -} - -// The packed parsers parse repeated numeric primitives directly into the -// corresponding field - -// These are packed varints -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedInt32Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedUInt32Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedInt64Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedUInt64Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedSInt32Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedSInt64Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedEnumParser( - void* object, const char* ptr, ParseContext* ctx); - -template -PROTOBUF_MUST_USE_RESULT const char* PackedEnumParser( - void* object, const char* ptr, ParseContext* ctx, bool (*is_valid)(int), - InternalMetadata* metadata, int field_num) { - return ctx->ReadPackedVarint( - ptr, [object, is_valid, metadata, field_num](uint64 val) { - if (is_valid(val)) { - static_cast*>(object)->Add(val); - } else { - WriteVarint(field_num, val, metadata->mutable_unknown_fields()); - } - }); -} - -template -PROTOBUF_MUST_USE_RESULT const char* PackedEnumParserArg( - void* object, const char* ptr, ParseContext* ctx, - bool (*is_valid)(const void*, int), const void* data, - InternalMetadata* metadata, int field_num) { - return ctx->ReadPackedVarint( - ptr, [object, is_valid, data, metadata, field_num](uint64 val) { - if (is_valid(data, val)) { - static_cast*>(object)->Add(val); - } else { - WriteVarint(field_num, val, metadata->mutable_unknown_fields()); - } - }); -} - -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedBoolParser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedFixed32Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedSFixed32Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedFixed64Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedSFixed64Parser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedFloatParser( - void* object, const char* ptr, ParseContext* ctx); -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* PackedDoubleParser( - void* object, const char* ptr, ParseContext* ctx); - -// This is the only recursive parser. -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* UnknownGroupLiteParse( - std::string* unknown, const char* ptr, ParseContext* ctx); -// This is a helper to for the UnknownGroupLiteParse but is actually also -// useful in the generated code. It uses overload on std::string* vs -// UnknownFieldSet* to make the generated code isomorphic between full and lite. -PROTOBUF_EXPORT PROTOBUF_MUST_USE_RESULT const char* UnknownFieldParse( - uint32 tag, std::string* unknown, const char* ptr, ParseContext* ctx); - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_PARSE_CONTEXT_H__ diff --git a/third_party/protobuf-lite/google/protobuf/port.h b/third_party/protobuf-lite/google/protobuf/port.h deleted file mode 100644 index 555fd4ebc..000000000 --- a/third_party/protobuf-lite/google/protobuf/port.h +++ /dev/null @@ -1,43 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// A common header that is included across all protobuf headers. We do our best -// to avoid #defining any macros here; instead we generally put macros in -// port_def.inc and port_undef.inc so they are not visible from outside of -// protobuf. - -#ifndef GOOGLE_PROTOBUF_PORT_H__ -#define GOOGLE_PROTOBUF_PORT_H__ - - -#include - - -#endif // GOOGLE_PROTOBUF_PORT_H__ diff --git a/third_party/protobuf-lite/google/protobuf/port_def.inc b/third_party/protobuf-lite/google/protobuf/port_def.inc deleted file mode 100644 index 320e888ec..000000000 --- a/third_party/protobuf-lite/google/protobuf/port_def.inc +++ /dev/null @@ -1,606 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// This file defines common macros that are used in protobuf. -// -// To hide these definitions from the outside world (and to prevent collisions -// if more than one version of protobuf is #included in the same project) you -// must follow this pattern when #including port_def.inc in a header file: -// -// #include "other_header.h" -// #include "message.h" -// // etc. -// -// #include "port_def.inc" // MUST be last header included -// -// // Definitions for this header. -// -// #include "port_undef.inc" -// -// This is a textual header with no include guard, because we want to -// detect/prohibit anytime it is #included twice without a corresponding -// #undef. - -// These macros are private and should always be -// ::util::RetrieveErrorSpace(*this) headers. If any of these errors fire, you -// should either properly #include port_undef.h at the end of your header that -// #includes port.h, or don't #include port.h twice in a .cc file. -#ifdef PROTOBUF_NAMESPACE -#error PROTOBUF_NAMESPACE was previously defined -#endif -#ifdef PROTOBUF_NAMESPACE_ID -#error PROTOBUF_NAMESPACE_ID was previously defined -#endif -#ifdef PROTOBUF_ALWAYS_INLINE -#error PROTOBUF_ALWAYS_INLINE was previously defined -#endif -#ifdef PROTOBUF_COLD -#error PROTOBUF_COLD was previously defined -#endif -#ifdef PROTOBUF_NOINLINE -#error PROTOBUF_NOINLINE was previously defined -#endif -#ifdef PROTOBUF_SECTION_VARIABLE -#error PROTOBUF_SECTION_VARIABLE was previously defined -#endif -#ifdef PROTOBUF_DEPRECATED -#error PROTOBUF_DEPRECATED was previously defined -#endif -#ifdef PROTOBUF_DEPRECATED_MSG -#error PROTOBUF_DEPRECATED_MSG was previously defined -#endif -#ifdef PROTOBUF_FUNC_ALIGN -#error PROTOBUF_FUNC_ALIGN was previously defined -#endif -#ifdef PROTOBUF_RETURNS_NONNULL -#error PROTOBUF_RETURNS_NONNULL was previously defined -#endif -#ifdef PROTOBUF_ATTRIBUTE_REINITIALIZES -#error PROTOBUF_ATTRIBUTE_REINITIALIZES was previously defined -#endif -#ifdef PROTOBUF_RTTI -#error PROTOBUF_RTTI was previously defined -#endif -#ifdef PROTOBUF_VERSION -#error PROTOBUF_VERSION was previously defined -#endif -#ifdef PROTOBUF_VERSION_SUFFIX -#error PROTOBUF_VERSION_SUFFIX was previously defined -#endif -#ifdef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC -#error PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC was previously defined -#endif -#ifdef PROTOBUF_MIN_PROTOC_VERSION -#error PROTOBUF_MIN_PROTOC_VERSION was previously defined -#endif -#ifdef PROTOBUF_PREDICT_TRUE -#error PROTOBUF_PREDICT_TRUE was previously defined -#endif -#ifdef PROTOBUF_PREDICT_FALSE -#error PROTOBUF_PREDICT_FALSE was previously defined -#endif -#ifdef PROTOBUF_FIELD_OFFSET -#error PROTOBUF_FIELD_OFFSET was previously defined -#endif -#ifdef PROTOBUF_LL_FORMAT -#error PROTOBUF_LL_FORMAT was previously defined -#endif -#ifdef PROTOBUF_GUARDED_BY -#error PROTOBUF_GUARDED_BY was previously defined -#endif -#ifdef PROTOBUF_LONGLONG -#error PROTOBUF_LONGLONG was previously defined -#endif -#ifdef PROTOBUF_ULONGLONG -#error PROTOBUF_ULONGLONG was previously defined -#endif -#ifdef PROTOBUF_FALLTHROUGH_INTENDED -#error PROTOBUF_FALLTHROUGH_INTENDED was previously defined -#endif -#ifdef PROTOBUF_EXPORT -#error PROTOBUF_EXPORT was previously defined -#endif -#ifdef PROTOC_EXPORT -#error PROTOC_EXPORT was previously defined -#endif -#ifdef PROTOBUF_MUST_USE_RESULT -#error PROTOBUF_MUST_USE_RESULT was previously defined -#endif -#ifdef PROTOBUF_UNUSED -#error PROTOBUF_UNUSED was previously defined -#endif -#ifdef PROTOBUF_FINAL -#error PROTOBUF_FINAL was previously defined -#endif -#ifdef PROTOBUF_DISABLE_MSVC_UNION_WARNING -#error PROTOBUF_DISABLE_MSVC_UNION_WARNING was previously defined -#endif -#ifdef PROTOBUF_ENABLE_MSVC_UNION_WARNING -#error PROTOBUF_ENABLE_MSVC_UNION_WARNING was previously defined -#endif -#ifdef PROTOBUF_CONSTINIT -#error PROTOBUF_CONSTINIT was previously defined -#endif -#ifdef PROTOBUF_MAYBE_CONSTEXPR -#error PROTOBUF_MAYBE_CONSTEXPR was previously defined -#endif -#ifdef PROTOBUF_ATTRIBUTE_NO_DESTROY -#error PROTOBUF_ATTRIBUTE_NO_DESTROY was previously defined -#endif - - -#define PROTOBUF_NAMESPACE "google::protobuf" -#define PROTOBUF_NAMESPACE_ID google::protobuf -#define PROTOBUF_NAMESPACE_OPEN \ - namespace google { \ - namespace protobuf { -#define PROTOBUF_NAMESPACE_CLOSE \ - } /* namespace protobuf */ \ - } /* namespace google */ - -#if defined(__GNUC__) || defined(__clang__) -#define PROTOBUF_DEPRECATED __attribute__((deprecated)) -#define PROTOBUF_DEPRECATED_ENUM __attribute__((deprecated)) -#define PROTOBUF_DEPRECATED_MSG(msg) __attribute__((deprecated(msg))) -#elif defined(_MSC_VER) -#define PROTOBUF_DEPRECATED __declspec(deprecated) -#define PROTOBUF_DEPRECATED_ENUM -#define PROTOBUF_DEPRECATED_MSG(msg) __declspec(deprecated(msg)) -#endif - -#define PROTOBUF_SECTION_VARIABLE(x) -#define PROTOBUF_MUST_USE_RESULT - -// ---------------------------------------------------------------------------- -// Annotations: Some parts of the code have been annotated in ways that might -// be useful to some compilers or tools, but are not supported universally. -// You can #define these annotations yourself if the default implementation -// is not right for you. - -#ifdef GOOGLE_ATTRIBUTE_ALWAYS_INLINE -#define PROTOBUF_ALWAYS_INLINE GOOGLE_ATTRIBUTE_ALWAYS_INLINE -#else -#if defined(__GNUC__) && \ - (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) -// For functions we want to force inline. -// Introduced in gcc 3.1. -#define PROTOBUF_ALWAYS_INLINE __attribute__((always_inline)) -#else -// Other compilers will have to figure it out for themselves. -#define PROTOBUF_ALWAYS_INLINE -#endif -#endif - -#ifdef GOOGLE_ATTRIBUTE_NOINLINE -#define PROTOBUF_NOINLINE GOOGLE_ATTRIBUTE_NOINLINE -#else -#if defined(__GNUC__) && \ - (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) -// For functions we want to force not inline. -// Introduced in gcc 3.1. -#define PROTOBUF_NOINLINE __attribute__((noinline)) -#elif defined(_MSC_VER) && (_MSC_VER >= 1400) -// Seems to have been around since at least Visual Studio 2005 -#define PROTOBUF_NOINLINE __declspec(noinline) -#else -// Other compilers will have to figure it out for themselves. -#define PROTOBUF_NOINLINE -#endif -#endif - -#ifdef GOOGLE_ATTRIBUTE_FUNC_ALIGN -#define PROTOBUF_FUNC_ALIGN GOOGLE_ATTRIBUTE_FUNC_ALIGN -#else -#if defined(__clang__) || \ - defined(__GNUC__) && \ - (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) -// Function alignment attribute introduced in gcc 4.3 -#define PROTOBUF_FUNC_ALIGN(bytes) __attribute__((aligned(bytes))) -#else -#define PROTOBUF_FUNC_ALIGN(bytes) -#endif -#endif - -#ifdef GOOGLE_PREDICT_TRUE -#define PROTOBUF_PREDICT_TRUE GOOGLE_PREDICT_TRUE -#else -#ifdef __GNUC__ -// Provided at least since GCC 3.0. -#define PROTOBUF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) -#else -#define PROTOBUF_PREDICT_TRUE(x) (x) -#endif -#endif - -#ifdef GOOGLE_PREDICT_FALSE -#define PROTOBUF_PREDICT_FALSE GOOGLE_PREDICT_FALSE -#else -#ifdef __GNUC__ -// Provided at least since GCC 3.0. -#define PROTOBUF_PREDICT_FALSE(x) (__builtin_expect(x, 0)) -#else -#define PROTOBUF_PREDICT_FALSE(x) (x) -#endif -#endif - -#ifdef GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL -#define PROTOBUF_RETURNS_NONNULL GOOGLE_PROTOBUF_ATTRIBUTE_RETURNS_NONNULL -#else -#if defined(__has_attribute) -#if __has_attribute(returns_nonnull) -#define PROTOBUF_RETURNS_NONNULL __attribute__((returns_nonnull)) -#endif -#endif -#endif -#ifndef PROTOBUF_RETURNS_NONNULL -#define PROTOBUF_RETURNS_NONNULL -#endif - -#if defined(__has_cpp_attribute) -#if __has_cpp_attribute(clang::reinitializes) -#define PROTOBUF_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]] -#endif -#endif -#ifndef PROTOBUF_ATTRIBUTE_REINITIALIZES -#define PROTOBUF_ATTRIBUTE_REINITIALIZES -#endif - -#define PROTOBUF_GUARDED_BY(x) -#define PROTOBUF_COLD - -// Copied from ABSL. -#if defined(__clang__) && defined(__has_warning) -#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") -#define PROTOBUF_FALLTHROUGH_INTENDED [[clang::fallthrough]] -#endif -#elif defined(__GNUC__) && __GNUC__ >= 7 -#define PROTOBUF_FALLTHROUGH_INTENDED [[gnu::fallthrough]] -#endif - -#ifndef PROTOBUF_FALLTHROUGH_INTENDED -#define PROTOBUF_FALLTHROUGH_INTENDED -#endif - -#if defined(__has_cpp_attribute) -#define HAS_ATTRIBUTE(attr) __has_cpp_attribute(attr) -#else -#define HAS_ATTRIBUTE(attr) 0 -#endif - -#if HAS_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__)) -#define PROTOBUF_UNUSED __attribute__((__unused__)) -#else -#define PROTOBUF_UNUSED -#endif - -#undef HAS_ATTRIBUTE - -#ifdef _MSC_VER -#define PROTOBUF_LONGLONG(x) x##I64 -#define PROTOBUF_ULONGLONG(x) x##UI64 -#define PROTOBUF_LL_FORMAT "I64" // As in printf("%I64d", ...) -#else -// By long long, we actually mean int64. -#define PROTOBUF_LONGLONG(x) x##LL -#define PROTOBUF_ULONGLONG(x) x##ULL -// Used to format real long long integers. -#define PROTOBUF_LL_FORMAT \ - "ll" // As in "%lld". Note that "q" is poor form also. -#endif - - -// Shared google3/opensource definitions. ////////////////////////////////////// - -#define PROTOBUF_VERSION 3014000 -#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC 3014000 -#define PROTOBUF_MIN_PROTOC_VERSION 3014000 -#define PROTOBUF_VERSION_SUFFIX "" - -// The minimum library version which works with the current version of the -// headers. -#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3014000 - -#if defined(GOOGLE_PROTOBUF_NO_RTTI) && GOOGLE_PROTOBUF_NO_RTTI -#define PROTOBUF_RTTI 0 -#elif defined(__has_feature) -// https://clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension -#define PROTOBUF_RTTI __has_feature(cxx_rtti) -#elif !defined(__cxx_rtti) -// https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros#C.2B.2B98 -#define PROTOBUF_RTTI 0 -#elif defined(__GNUC__) && !defined(__GXX_RTTI) -#https: // gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html -#define PROTOBUF_RTTI 0 -#else -#define PROTOBUF_RTTI 1 -#endif - -// Returns the offset of the given field within the given aggregate type. -// This is equivalent to the ANSI C offsetof() macro. However, according -// to the C++ standard, offsetof() only works on POD types, and GCC -// enforces this requirement with a warning. In practice, this rule is -// unnecessarily strict; there is probably no compiler or platform on -// which the offsets of the direct fields of a class are non-constant. -// Fields inherited from superclasses *can* have non-constant offsets, -// but that's not what this macro will be used for. -#if defined(__clang__) -// For Clang we use __builtin_offsetof() and suppress the warning, -// to avoid Control Flow Integrity and UBSan vptr sanitizers from -// crashing while trying to validate the invalid reinterpet_casts. -#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Winvalid-offsetof\"") \ - __builtin_offsetof(TYPE, FIELD) \ - _Pragma("clang diagnostic pop") -#elif defined(__GNUC__) && \ - (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) -#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) __builtin_offsetof(TYPE, FIELD) -#else // defined(__clang__) -// Note that we calculate relative to the pointer value 16 here since if we -// just use zero, GCC complains about dereferencing a NULL pointer. We -// choose 16 rather than some other number just in case the compiler would -// be confused by an unaligned pointer. -#define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \ - static_cast< ::google::protobuf::uint32>(reinterpret_cast( \ - &reinterpret_cast(16)->FIELD) - \ - reinterpret_cast(16)) -#endif - -#if defined(PROTOBUF_USE_DLLS) -#if defined(_MSC_VER) -#ifdef LIBPROTOBUF_EXPORTS -#define PROTOBUF_EXPORT __declspec(dllexport) -#define PROTOBUF_EXPORT_TEMPLATE_DECLARE -#define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllexport) -#else -#define PROTOBUF_EXPORT __declspec(dllimport) -#define PROTOBUF_EXPORT_TEMPLATE_DECLARE -#define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllimport) -#endif -#ifdef LIBPROTOC_EXPORTS -#define PROTOC_EXPORT __declspec(dllexport) -#else -#define PROTOC_EXPORT __declspec(dllimport) -#endif -#else // defined(_MSC_VER) -#ifdef LIBPROTOBUF_EXPORTS -#define PROTOBUF_EXPORT __attribute__((visibility("default"))) -#define PROTOBUF_EXPORT_TEMPLATE_DECLARE __attribute__((visibility("default"))) -#define PROTOBUF_EXPORT_TEMPLATE_DEFINE -#else -#define PROTOBUF_EXPORT -#define PROTOBUF_EXPORT_TEMPLATE_DECLARE -#define PROTOBUF_EXPORT_TEMPLATE_DEFINE -#endif -#ifdef LIBPROTOC_EXPORTS -#define PROTOC_EXPORT __attribute__((visibility("default"))) -#else -#define PROTOC_EXPORT -#endif -#endif -#else // defined(PROTOBUF_USE_DLLS) -#define PROTOBUF_EXPORT -#define PROTOC_EXPORT -#define PROTOBUF_EXPORT_TEMPLATE_DECLARE -#define PROTOBUF_EXPORT_TEMPLATE_DEFINE -#endif - -// Windows declares several inconvenient macro names. We #undef them and then -// restore them in port_undef.inc. -#ifdef _MSC_VER -#pragma push_macro("CREATE_NEW") -#undef CREATE_NEW -#pragma push_macro("DOUBLE_CLICK") -#undef DOUBLE_CLICK -#pragma push_macro("ERROR") -#undef ERROR -#pragma push_macro("ERROR_BUSY") -#undef ERROR_BUSY -#pragma push_macro("ERROR_NOT_FOUND") -#undef ERROR_NOT_FOUND -#pragma push_macro("GetMessage") -#undef GetMessage -#pragma push_macro("IGNORE") -#undef IGNORE -#pragma push_macro("IN") -#undef IN -#pragma push_macro("INPUT_KEYBOARD") -#undef INPUT_KEYBOARD -#pragma push_macro("NO_ERROR") -#undef NO_ERROR -#pragma push_macro("OUT") -#undef OUT -#pragma push_macro("OPTIONAL") -#undef OPTIONAL -#pragma push_macro("min") -#undef min -#pragma push_macro("max") -#undef max -#pragma push_macro("NEAR") -#undef NEAR -#pragma push_macro("NO_DATA") -#undef NO_DATA -#pragma push_macro("REASON_UNKNOWN") -#undef REASON_UNKNOWN -#pragma push_macro("SERVICE_DISABLED") -#undef SERVICE_DISABLED -#pragma push_macro("SEVERITY_ERROR") -#undef SEVERITY_ERROR -#pragma push_macro("STRICT") -#undef STRICT -#pragma push_macro("timezone") -#undef timezone -#endif // _MSC_VER - -#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) -// Don't let Objective-C Macros interfere with proto identifiers with the same -// name. -#pragma push_macro("DEBUG") -#undef DEBUG -#pragma push_macro("TRUE") -#undef TRUE -#pragma push_macro("FALSE") -#undef FALSE -#endif // defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) - -#if defined(__clang__) -#pragma clang diagnostic push -// TODO(gerbens) ideally we cleanup the code. But a cursory try shows many -// violations. So let's ignore for now. -#pragma clang diagnostic ignored "-Wshorten-64-to-32" -#elif defined(__GNUC__) -// GCC does not allow disabling diagnostics within an expression: -// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60875, so we disable this one -// globally even though it's only used for PROTOBUF_FIELD_OFFSET. -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Winvalid-offsetof" -#endif - -// PROTOBUF_ASSUME(pred) tells the compiler that it can assume pred is true. To -// be safe, we also validate the assumption with a GOOGLE_DCHECK in unoptimized -// builds. The macro does not do anything useful if the compiler does not -// support __builtin_assume. -#ifdef __has_builtin -#if __has_builtin(__builtin_assume) -#define PROTOBUF_ASSUME(pred) \ - GOOGLE_DCHECK(pred); \ - __builtin_assume(pred) -#else -#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred) -#endif -#else -#define PROTOBUF_ASSUME(pred) GOOGLE_DCHECK(pred) -#endif - -// Specify memory alignment for structs, classes, etc. -// Use like: -// class PROTOBUF_ALIGNAS(16) MyClass { ... } -// PROTOBUF_ALIGNAS(16) int array[4]; -// -// In most places you can use the C++11 keyword "alignas", which is preferred. -// -// But compilers have trouble mixing __attribute__((...)) syntax with -// alignas(...) syntax. -// -// Doesn't work in clang or gcc: -// struct alignas(16) __attribute__((packed)) S { char c; }; -// Works in clang but not gcc: -// struct __attribute__((packed)) alignas(16) S2 { char c; }; -// Works in clang and gcc: -// struct alignas(16) S3 { char c; } __attribute__((packed)); -// -// There are also some attributes that must be specified *before* a class -// definition: visibility (used for exporting functions/classes) is one of -// these attributes. This means that it is not possible to use alignas() with a -// class that is marked as exported. -#if defined(_MSC_VER) -#define PROTOBUF_ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) -#elif defined(__GNUC__) -#define PROTOBUF_ALIGNAS(byte_alignment) \ - __attribute__((aligned(byte_alignment))) -#else -#define PROTOBUF_ALIGNAS(byte_alignment) alignas(byte_alignment) -#endif - -#define PROTOBUF_FINAL final - -#if defined(_MSC_VER) -#define PROTOBUF_THREAD_LOCAL __declspec(thread) -#else -#define PROTOBUF_THREAD_LOCAL __thread -#endif - -// For enabling message owned arena, one major blocker is semantic change from -// moving to copying when there is ownership transfer (e.g., move ctor, swap, -// set allocated, release). This change not only causes performance regression -// but also breaks users code (e.g., dangling reference). For top-level -// messages, since it owns the arena, we can mitigate the issue by transferring -// ownership of arena. However, we cannot do that for nested messages. In order -// to tell how many usages of nested messages affected by message owned arena, -// we need to simulate the arena ownership. -// This experiment is purely for the purpose of gathering data. All code guarded -// by this flag is supposed to be removed after this experiment. -// #define PROTOBUF_MESSAGE_OWNED_ARENA_EXPERIMENT - -#if defined(__cpp_constinit) -#define PROTOBUF_CONSTINIT constinit -#elif defined(__has_cpp_attribute) -#if __has_cpp_attribute(clang::require_constant_initialization) -#define PROTOBUF_CONSTINIT [[clang::require_constant_initialization]] -#endif -#endif -#ifndef PROTOBUF_CONSTINIT -#define PROTOBUF_CONSTINIT -#endif - -// Some constructors can't be constexpr under MSVC, but given that MSVC will not -// do constant initialization of globals anyway we can omit `constexpr` from -// them. These constructors are marked with PROTOBUF_MAYBE_CONSTEXPR -#if defined(_MSC_VER) -#define PROTOBUF_MAYBE_CONSTEXPR -#else -#define PROTOBUF_MAYBE_CONSTEXPR constexpr -#endif - -#if _MSC_VER -#define PROTOBUF_DISABLE_MSVC_UNION_WARNING \ - __pragma(warning(push)) \ - __pragma(warning(disable : 4582)) \ - __pragma(warning(disable : 4583)) - -#define PROTOBUF_ENABLE_MSVC_UNION_WARNING \ - __pragma(warning(pop)) -#else -#define PROTOBUF_DISABLE_MSVC_UNION_WARNING -#define PROTOBUF_ENABLE_MSVC_UNION_WARNING -#endif - -#if defined(__cpp_constinit) -#define PROTOBUF_CONSTINIT constinit -#elif defined(__has_cpp_attribute) -#if __has_cpp_attribute(clang::require_constant_initialization) -#define PROTOBUF_CONSTINIT [[clang::require_constant_initialization]] -#endif -#endif -#ifndef PROTOBUF_CONSTINIT -#define PROTOBUF_CONSTINIT -#endif - -// Some globals with an empty non-trivial destructor are annotated with -// no_destroy for performance reasons. It reduces the cost of these globals in -// non-opt mode and under sanitizers. -#if defined(__has_cpp_attribute) -#if __has_cpp_attribute(clang::no_destroy) -#define PROTOBUF_ATTRIBUTE_NO_DESTROY [[clang::no_destroy]] -#endif -#endif -#if !defined(PROTOBUF_ATTRIBUTE_NO_DESTROY) -#define PROTOBUF_ATTRIBUTE_NO_DESTROY -#endif diff --git a/third_party/protobuf-lite/google/protobuf/port_undef.inc b/third_party/protobuf-lite/google/protobuf/port_undef.inc deleted file mode 100644 index d1414285e..000000000 --- a/third_party/protobuf-lite/google/protobuf/port_undef.inc +++ /dev/null @@ -1,116 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// #undefs all macros defined in port_def.inc. See comments in port_def.inc -// for more info. - -#ifndef PROTOBUF_NAMESPACE -#error "port_undef.inc must be included after port_def.inc" -#endif -#undef PROTOBUF_NAMESPACE -#undef PROTOBUF_NAMESPACE_ID -#undef PROTOBUF_ALWAYS_INLINE -#undef PROTOBUF_COLD -#undef PROTOBUF_NOINLINE -#undef PROTOBUF_SECTION_VARIABLE -#undef PROTOBUF_DEPRECATED -#undef PROTOBUF_DEPRECATED_ENUM -#undef PROTOBUF_DEPRECATED_MSG -#undef PROTOBUF_FUNC_ALIGN -#undef PROTOBUF_RETURNS_NONNULL -#undef PROTOBUF_ATTRIBUTE_REINITIALIZES -#undef PROTOBUF_RTTI -#undef PROTOBUF_VERSION -#undef PROTOBUF_VERSION_SUFFIX -#undef PROTOBUF_FIELD_OFFSET -#undef PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC -#undef PROTOBUF_MIN_PROTOC_VERSION -#undef PROTOBUF_PREDICT_TRUE -#undef PROTOBUF_PREDICT_FALSE -#undef PROTOBUF_LONGLONG -#undef PROTOBUF_ULONGLONG -#undef PROTOBUF_LL_FORMAT -#undef PROTOBUF_GUARDED_BY -#undef PROTOBUF_FALLTHROUGH_INTENDED -#undef PROTOBUF_EXPORT -#undef PROTOC_EXPORT -#undef PROTOBUF_MUST_USE_RESULT -#undef PROTOBUF_NAMESPACE_OPEN -#undef PROTOBUF_NAMESPACE_CLOSE -#undef PROTOBUF_UNUSED -#undef PROTOBUF_ASSUME -#undef PROTOBUF_EXPORT_TEMPLATE_DECLARE -#undef PROTOBUF_EXPORT_TEMPLATE_DEFINE -#undef PROTOBUF_ALIGNAS -#undef PROTOBUF_FINAL -#undef PROTOBUF_THREAD_LOCAL -#undef PROTOBUF_MESSAGE_OWNED_ARENA_EXPERIMENT -#undef PROTOBUF_DISABLE_MSVC_UNION_WARNING -#undef PROTOBUF_ENABLE_MSVC_UNION_WARNING -#undef PROTOBUF_CONSTINIT -#undef PROTOBUF_MAYBE_CONSTEXPR -#undef PROTOBUF_ATTRIBUTE_NO_DESTROY - -// Restore macro that may have been #undef'd in port_def.inc. -#ifdef _MSC_VER -#pragma pop_macro("CREATE_NEW") -#pragma pop_macro("DOUBLE_CLICK") -#pragma pop_macro("ERROR") -#pragma pop_macro("ERROR_BUSY") -#pragma pop_macro("ERROR_NOT_FOUND") -#pragma pop_macro("GetMessage") -#pragma pop_macro("IGNORE") -#pragma pop_macro("IN") -#pragma pop_macro("INPUT_KEYBOARD") -#pragma pop_macro("OUT") -#pragma pop_macro("OPTIONAL") -#pragma pop_macro("min") -#pragma pop_macro("max") -#pragma pop_macro("NEAR") -#pragma pop_macro("NO_DATA") -#pragma pop_macro("NO_ERROR") -#pragma pop_macro("REASON_UNKNOWN") -#pragma pop_macro("SERVICE_DISABLED") -#pragma pop_macro("SEVERITY_ERROR") -#pragma pop_macro("STRICT") -#pragma pop_macro("timezone") -#endif - -#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) -#pragma pop_macro("DEBUG") -#pragma pop_macro("TRUE") -#pragma pop_macro("FALSE") -#endif // defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) - -#if defined(__clang__) -#pragma clang diagnostic pop -#elif defined(__GNUC__) -#pragma GCC diagnostic pop -#endif diff --git a/third_party/protobuf-lite/google/protobuf/repeated_field.h b/third_party/protobuf-lite/google/protobuf/repeated_field.h deleted file mode 100644 index 26d584733..000000000 --- a/third_party/protobuf-lite/google/protobuf/repeated_field.h +++ /dev/null @@ -1,2847 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// RepeatedField and RepeatedPtrField are used by generated protocol message -// classes to manipulate repeated fields. These classes are very similar to -// STL's vector, but include a number of optimizations found to be useful -// specifically in the case of Protocol Buffers. RepeatedPtrField is -// particularly different from STL vector as it manages ownership of the -// pointers that it contains. -// -// Typically, clients should not need to access RepeatedField objects directly, -// but should instead use the accessor functions generated automatically by the -// protocol compiler. - -#ifndef GOOGLE_PROTOBUF_REPEATED_FIELD_H__ -#define GOOGLE_PROTOBUF_REPEATED_FIELD_H__ - -#include -#ifdef _MSC_VER -// This is required for min/max on VS2013 only. -#include -#endif - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - - -// Must be included last. -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { - -class Message; -class Reflection; - -template -struct WeakRepeatedPtrField; - -namespace internal { - -class MergePartialFromCodedStreamHelper; - -// kRepeatedFieldLowerClampLimit is the smallest size that will be allocated -// when growing a repeated field. -constexpr int kRepeatedFieldLowerClampLimit = 4; - -// kRepeatedFieldUpperClampLimit is the lowest signed integer value that -// overflows when multiplied by 2 (which is undefined behavior). Sizes above -// this will clamp to the maximum int value instead of following exponential -// growth when growing a repeated field. -constexpr int kRepeatedFieldUpperClampLimit = - (std::numeric_limits::max() / 2) + 1; - -// A utility function for logging that doesn't need any template types. -void LogIndexOutOfBounds(int index, int size); - -template -inline int CalculateReserve(Iter begin, Iter end, std::forward_iterator_tag) { - return static_cast(std::distance(begin, end)); -} - -template -inline int CalculateReserve(Iter /*begin*/, Iter /*end*/, - std::input_iterator_tag /*unused*/) { - return -1; -} - -template -inline int CalculateReserve(Iter begin, Iter end) { - typedef typename std::iterator_traits::iterator_category Category; - return CalculateReserve(begin, end, Category()); -} - -// Swaps two blocks of memory of size sizeof(T). -template -inline void SwapBlock(char* p, char* q) { - T tmp; - memcpy(&tmp, p, sizeof(T)); - memcpy(p, q, sizeof(T)); - memcpy(q, &tmp, sizeof(T)); -} - -// Swaps two blocks of memory of size kSize: -// template void memswap(char* p, char* q); - -template -inline typename std::enable_if<(kSize == 0), void>::type memswap(char*, char*) { -} - -#define PROTO_MEMSWAP_DEF_SIZE(reg_type, max_size) \ - template \ - typename std::enable_if<(kSize >= sizeof(reg_type) && kSize < (max_size)), \ - void>::type \ - memswap(char* p, char* q) { \ - SwapBlock(p, q); \ - memswap(p + sizeof(reg_type), \ - q + sizeof(reg_type)); \ - } - -PROTO_MEMSWAP_DEF_SIZE(uint8, 2) -PROTO_MEMSWAP_DEF_SIZE(uint16, 4) -PROTO_MEMSWAP_DEF_SIZE(uint32, 8) - -#ifdef __SIZEOF_INT128__ -PROTO_MEMSWAP_DEF_SIZE(uint64, 16) -PROTO_MEMSWAP_DEF_SIZE(__uint128_t, (1u << 31)) -#else -PROTO_MEMSWAP_DEF_SIZE(uint64, (1u << 31)) -#endif - -#undef PROTO_MEMSWAP_DEF_SIZE - -} // namespace internal - -// RepeatedField is used to represent repeated fields of a primitive type (in -// other words, everything except strings and nested Messages). Most users will -// not ever use a RepeatedField directly; they will use the get-by-index, -// set-by-index, and add accessors that are generated for all repeated fields. -template -class RepeatedField final { - static_assert( - alignof(Arena) >= alignof(Element), - "We only support types that have an alignment smaller than Arena"); - - public: - constexpr RepeatedField(); - explicit RepeatedField(Arena* arena); - RepeatedField(const RepeatedField& other); - template - RepeatedField(Iter begin, const Iter& end); - ~RepeatedField(); - - RepeatedField& operator=(const RepeatedField& other); - - RepeatedField(RepeatedField&& other) noexcept; - RepeatedField& operator=(RepeatedField&& other) noexcept; - - bool empty() const; - int size() const; - - const Element& Get(int index) const; - Element* Mutable(int index); - - const Element& operator[](int index) const { return Get(index); } - Element& operator[](int index) { return *Mutable(index); } - - const Element& at(int index) const; - Element& at(int index); - - void Set(int index, const Element& value); - void Add(const Element& value); - // Appends a new element and return a pointer to it. - // The new element is uninitialized if |Element| is a POD type. - Element* Add(); - // Append elements in the range [begin, end) after reserving - // the appropriate number of elements. - template - void Add(Iter begin, Iter end); - - // Remove the last element in the array. - void RemoveLast(); - - // Extract elements with indices in "[start .. start+num-1]". - // Copy them into "elements[0 .. num-1]" if "elements" is not NULL. - // Caution: implementation also moves elements with indices [start+num ..]. - // Calling this routine inside a loop can cause quadratic behavior. - void ExtractSubrange(int start, int num, Element* elements); - - void Clear(); - void MergeFrom(const RepeatedField& other); - void CopyFrom(const RepeatedField& other); - - // Reserve space to expand the field to at least the given size. If the - // array is grown, it will always be at least doubled in size. - void Reserve(int new_size); - - // Resize the RepeatedField to a new, smaller size. This is O(1). - void Truncate(int new_size); - - void AddAlreadyReserved(const Element& value); - // Appends a new element and return a pointer to it. - // The new element is uninitialized if |Element| is a POD type. - // Should be called only if Capacity() > Size(). - Element* AddAlreadyReserved(); - Element* AddNAlreadyReserved(int elements); - int Capacity() const; - - // Like STL resize. Uses value to fill appended elements. - // Like Truncate() if new_size <= size(), otherwise this is - // O(new_size - size()). - void Resize(int new_size, const Element& value); - - // Gets the underlying array. This pointer is possibly invalidated by - // any add or remove operation. - Element* mutable_data(); - const Element* data() const; - - // Swap entire contents with "other". If they are separate arenas then, copies - // data between each other. - void Swap(RepeatedField* other); - - // Swap entire contents with "other". Should be called only if the caller can - // guarantee that both repeated fields are on the same arena or are on the - // heap. Swapping between different arenas is disallowed and caught by a - // GOOGLE_DCHECK (see API docs for details). - void UnsafeArenaSwap(RepeatedField* other); - - // Swap two elements. - void SwapElements(int index1, int index2); - - // STL-like iterator support - typedef Element* iterator; - typedef const Element* const_iterator; - typedef Element value_type; - typedef value_type& reference; - typedef const value_type& const_reference; - typedef value_type* pointer; - typedef const value_type* const_pointer; - typedef int size_type; - typedef ptrdiff_t difference_type; - - iterator begin(); - const_iterator begin() const; - const_iterator cbegin() const; - iterator end(); - const_iterator end() const; - const_iterator cend() const; - - // Reverse iterator support - typedef std::reverse_iterator const_reverse_iterator; - typedef std::reverse_iterator reverse_iterator; - reverse_iterator rbegin() { return reverse_iterator(end()); } - const_reverse_iterator rbegin() const { - return const_reverse_iterator(end()); - } - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rend() const { - return const_reverse_iterator(begin()); - } - - // Returns the number of bytes used by the repeated field, excluding - // sizeof(*this) - size_t SpaceUsedExcludingSelfLong() const; - - int SpaceUsedExcludingSelf() const { - return internal::ToIntSize(SpaceUsedExcludingSelfLong()); - } - - // Removes the element referenced by position. - // - // Returns an iterator to the element immediately following the removed - // element. - // - // Invalidates all iterators at or after the removed element, including end(). - iterator erase(const_iterator position); - - // Removes the elements in the range [first, last). - // - // Returns an iterator to the element immediately following the removed range. - // - // Invalidates all iterators at or after the removed range, including end(). - iterator erase(const_iterator first, const_iterator last); - - // Get the Arena on which this RepeatedField stores its elements. - inline Arena* GetArena() const { - return (total_size_ == 0) ? static_cast(arena_or_elements_) - : rep()->arena; - } - - // For internal use only. - // - // This is public due to it being called by generated code. - inline void InternalSwap(RepeatedField* other); - - private: - static constexpr int kInitialSize = 0; - // A note on the representation here (see also comment below for - // RepeatedPtrFieldBase's struct Rep): - // - // We maintain the same sizeof(RepeatedField) as before we added arena support - // so that we do not degrade performance by bloating memory usage. Directly - // adding an arena_ element to RepeatedField is quite costly. By using - // indirection in this way, we keep the same size when the RepeatedField is - // empty (common case), and add only an 8-byte header to the elements array - // when non-empty. We make sure to place the size fields directly in the - // RepeatedField class to avoid costly cache misses due to the indirection. - int current_size_; - int total_size_; - struct Rep { - Arena* arena; - // Here we declare a huge array as a way of approximating C's "flexible - // array member" feature without relying on undefined behavior. - Element elements[(std::numeric_limits::max() - 2 * sizeof(Arena*)) / - sizeof(Element)]; - }; - static constexpr size_t kRepHeaderSize = offsetof(Rep, elements); - - // If total_size_ == 0 this points to an Arena otherwise it points to the - // elements member of a Rep struct. Using this invariant allows the storage of - // the arena pointer without an extra allocation in the constructor. - void* arena_or_elements_; - - // Return pointer to elements array. - // pre-condition: the array must have been allocated. - Element* elements() const { - GOOGLE_DCHECK_GT(total_size_, 0); - // Because of above pre-condition this cast is safe. - return unsafe_elements(); - } - - // Return pointer to elements array if it exists otherwise either null or - // a invalid pointer is returned. This only happens for empty repeated fields, - // where you can't dereference this pointer anyway (it's empty). - Element* unsafe_elements() const { - return static_cast(arena_or_elements_); - } - - // Return pointer to the Rep struct. - // pre-condition: the Rep must have been allocated, ie elements() is safe. - Rep* rep() const { - char* addr = reinterpret_cast(elements()) - offsetof(Rep, elements); - return reinterpret_cast(addr); - } - - friend class Arena; - typedef void InternalArenaConstructable_; - - // Move the contents of |from| into |to|, possibly clobbering |from| in the - // process. For primitive types this is just a memcpy(), but it could be - // specialized for non-primitive types to, say, swap each element instead. - void MoveArray(Element* to, Element* from, int size); - - // Copy the elements of |from| into |to|. - void CopyArray(Element* to, const Element* from, int size); - - // Internal helper to delete all elements and deallocate the storage. - void InternalDeallocate(Rep* rep, int size) { - if (rep != NULL) { - Element* e = &rep->elements[0]; - if (!std::is_trivial::value) { - Element* limit = &rep->elements[size]; - for (; e < limit; e++) { - e->~Element(); - } - } - if (rep->arena == NULL) { -#if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) - const size_t bytes = size * sizeof(*e) + kRepHeaderSize; - ::operator delete(static_cast(rep), bytes); -#else - ::operator delete(static_cast(rep)); -#endif - } - } - } - - // This class is a performance wrapper around RepeatedField::Add(const T&) - // function. In general unless a RepeatedField is a local stack variable LLVM - // has a hard time optimizing Add. The machine code tends to be - // loop: - // mov %size, dword ptr [%repeated_field] // load - // cmp %size, dword ptr [%repeated_field + 4] - // jae fallback - // mov %buffer, qword ptr [%repeated_field + 8] - // mov dword [%buffer + %size * 4], %value - // inc %size // increment - // mov dword ptr [%repeated_field], %size // store - // jmp loop - // - // This puts a load/store in each iteration of the important loop variable - // size. It's a pretty bad compile that happens even in simple cases, but - // largely the presence of the fallback path disturbs the compilers mem-to-reg - // analysis. - // - // This class takes ownership of a repeated field for the duration of it's - // lifetime. The repeated field should not be accessed during this time, ie. - // only access through this class is allowed. This class should always be a - // function local stack variable. Intended use - // - // void AddSequence(const int* begin, const int* end, RepeatedField* out) - // { - // RepeatedFieldAdder adder(out); // Take ownership of out - // for (auto it = begin; it != end; ++it) { - // adder.Add(*it); - // } - // } - // - // Typically due to the fact adder is a local stack variable. The compiler - // will be successful in mem-to-reg transformation and the machine code will - // be loop: cmp %size, %capacity jae fallback mov dword ptr [%buffer + %size * - // 4], %val inc %size jmp loop - // - // The first version executes at 7 cycles per iteration while the second - // version near 1 or 2 cycles. - template ::value> - class FastAdderImpl { - public: - explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) { - index_ = repeated_field_->current_size_; - capacity_ = repeated_field_->total_size_; - buffer_ = repeated_field_->unsafe_elements(); - } - ~FastAdderImpl() { repeated_field_->current_size_ = index_; } - - void Add(Element val) { - if (index_ == capacity_) { - repeated_field_->current_size_ = index_; - repeated_field_->Reserve(index_ + 1); - capacity_ = repeated_field_->total_size_; - buffer_ = repeated_field_->unsafe_elements(); - } - buffer_[index_++] = val; - } - - private: - RepeatedField* repeated_field_; - int index_; - int capacity_; - Element* buffer_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl); - }; - - // FastAdder is a wrapper for adding fields. The specialization above handles - // POD types more efficiently than RepeatedField. - template - class FastAdderImpl { - public: - explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {} - void Add(const Element& val) { repeated_field_->Add(val); } - - private: - RepeatedField* repeated_field_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl); - }; - - using FastAdder = FastAdderImpl<>; - - friend class TestRepeatedFieldHelper; - friend class ::google::protobuf::internal::ParseContext; -}; - -namespace internal { -template -class RepeatedPtrIterator; -template -class RepeatedPtrOverPtrsIterator; -} // namespace internal - -namespace internal { - -// This is a helper template to copy an array of elements efficiently when they -// have a trivial copy constructor, and correctly otherwise. This really -// shouldn't be necessary, but our compiler doesn't optimize std::copy very -// effectively. -template ::value> -struct ElementCopier { - void operator()(Element* to, const Element* from, int array_size); -}; - -} // namespace internal - -namespace internal { - -// type-traits helper for RepeatedPtrFieldBase: we only want to invoke -// arena-related "copy if on different arena" behavior if the necessary methods -// exist on the contained type. In particular, we rely on MergeFrom() existing -// as a general proxy for the fact that a copy will work, and we also provide a -// specific override for std::string*. -template -struct TypeImplementsMergeBehaviorProbeForMergeFrom { - typedef char HasMerge; - typedef long HasNoMerge; - - // We accept either of: - // - void MergeFrom(const T& other) - // - bool MergeFrom(const T& other) - // - // We mangle these names a bit to avoid compatibility issues in 'unclean' - // include environments that may have, e.g., "#define test ..." (yes, this - // exists). - template - struct CheckType; - template - static HasMerge Check(CheckType*); - template - static HasMerge Check(CheckType*); - template - static HasNoMerge Check(...); - - // Resolves to either std::true_type or std::false_type. - typedef std::integral_constant(0)) == sizeof(HasMerge))> - type; -}; - -template -struct TypeImplementsMergeBehavior - : TypeImplementsMergeBehaviorProbeForMergeFrom {}; - - -template <> -struct TypeImplementsMergeBehavior { - typedef std::true_type type; -}; - -template -struct IsMovable - : std::integral_constant::value && - std::is_move_assignable::value> {}; - -// This is the common base class for RepeatedPtrFields. It deals only in void* -// pointers. Users should not use this interface directly. -// -// The methods of this interface correspond to the methods of RepeatedPtrField, -// but may have a template argument called TypeHandler. Its signature is: -// class TypeHandler { -// public: -// typedef MyType Type; -// static Type* New(); -// static Type* NewFromPrototype(const Type* prototype, -// Arena* arena); -// static void Delete(Type*); -// static void Clear(Type*); -// static void Merge(const Type& from, Type* to); -// -// // Only needs to be implemented if SpaceUsedExcludingSelf() is called. -// static int SpaceUsedLong(const Type&); -// }; -class PROTOBUF_EXPORT RepeatedPtrFieldBase { - protected: - constexpr RepeatedPtrFieldBase(); - explicit RepeatedPtrFieldBase(Arena* arena); - ~RepeatedPtrFieldBase() { -#ifndef NDEBUG - // Try to trigger segfault / asan failure in non-opt builds. If arena_ - // lifetime has ended before the destructor. - if (arena_) (void)arena_->SpaceAllocated(); -#endif - } - - public: - // Must be called from destructor. - template - void Destroy(); - - protected: - bool empty() const; - int size() const; - - template - const typename TypeHandler::Type& at(int index) const; - template - typename TypeHandler::Type& at(int index); - - template - typename TypeHandler::Type* Mutable(int index); - template - void Delete(int index); - template - typename TypeHandler::Type* Add(typename TypeHandler::Type* prototype = NULL); - - public: - // The next few methods are public so that they can be called from generated - // code when implicit weak fields are used, but they should never be called by - // application code. - - template - const typename TypeHandler::Type& Get(int index) const; - - // Creates and adds an element using the given prototype, without introducing - // a link-time dependency on the concrete message type. This method is used to - // implement implicit weak fields. The prototype may be NULL, in which case an - // ImplicitWeakMessage will be used as a placeholder. - MessageLite* AddWeak(const MessageLite* prototype); - - template - void Clear(); - - template - void MergeFrom(const RepeatedPtrFieldBase& other); - - inline void InternalSwap(RepeatedPtrFieldBase* other); - - protected: - template < - typename TypeHandler, - typename std::enable_if::type* = nullptr> - void Add(typename TypeHandler::Type&& value); - - template - void RemoveLast(); - template - void CopyFrom(const RepeatedPtrFieldBase& other); - - void CloseGap(int start, int num); - - void Reserve(int new_size); - - int Capacity() const; - - // Used for constructing iterators. - void* const* raw_data() const; - void** raw_mutable_data() const; - - template - typename TypeHandler::Type** mutable_data(); - template - const typename TypeHandler::Type* const* data() const; - - template - PROTOBUF_ALWAYS_INLINE void Swap(RepeatedPtrFieldBase* other); - - void SwapElements(int index1, int index2); - - template - size_t SpaceUsedExcludingSelfLong() const; - - // Advanced memory management -------------------------------------- - - // Like Add(), but if there are no cleared objects to use, returns NULL. - template - typename TypeHandler::Type* AddFromCleared(); - - template - void AddAllocated(typename TypeHandler::Type* value) { - typename TypeImplementsMergeBehavior::type t; - AddAllocatedInternal(value, t); - } - - template - void UnsafeArenaAddAllocated(typename TypeHandler::Type* value); - - template - typename TypeHandler::Type* ReleaseLast() { - typename TypeImplementsMergeBehavior::type t; - return ReleaseLastInternal(t); - } - - // Releases last element and returns it, but does not do out-of-arena copy. - // And just returns the raw pointer to the contained element in the arena. - template - typename TypeHandler::Type* UnsafeArenaReleaseLast(); - - int ClearedCount() const; - template - void AddCleared(typename TypeHandler::Type* value); - template - typename TypeHandler::Type* ReleaseCleared(); - - template - void AddAllocatedInternal(typename TypeHandler::Type* value, std::true_type); - template - void AddAllocatedInternal(typename TypeHandler::Type* value, std::false_type); - - template - PROTOBUF_NOINLINE void AddAllocatedSlowWithCopy( - typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena); - template - PROTOBUF_NOINLINE void AddAllocatedSlowWithoutCopy( - typename TypeHandler::Type* value); - - template - typename TypeHandler::Type* ReleaseLastInternal(std::true_type); - template - typename TypeHandler::Type* ReleaseLastInternal(std::false_type); - - template - PROTOBUF_NOINLINE void SwapFallback(RepeatedPtrFieldBase* other); - - inline Arena* GetArena() const { return arena_; } - - private: - static constexpr int kInitialSize = 0; - // A few notes on internal representation: - // - // We use an indirected approach, with struct Rep, to keep - // sizeof(RepeatedPtrFieldBase) equivalent to what it was before arena support - // was added, namely, 3 8-byte machine words on x86-64. An instance of Rep is - // allocated only when the repeated field is non-empty, and it is a - // dynamically-sized struct (the header is directly followed by elements[]). - // We place arena_ and current_size_ directly in the object to avoid cache - // misses due to the indirection, because these fields are checked frequently. - // Placing all fields directly in the RepeatedPtrFieldBase instance costs - // significant performance for memory-sensitive workloads. - Arena* arena_; - int current_size_; - int total_size_; - struct Rep { - int allocated_size; - // Here we declare a huge array as a way of approximating C's "flexible - // array member" feature without relying on undefined behavior. - void* elements[(std::numeric_limits::max() - 2 * sizeof(int)) / - sizeof(void*)]; - }; - static constexpr size_t kRepHeaderSize = offsetof(Rep, elements); - Rep* rep_; - - template - static inline typename TypeHandler::Type* cast(void* element) { - return reinterpret_cast(element); - } - template - static inline const typename TypeHandler::Type* cast(const void* element) { - return reinterpret_cast(element); - } - - // Non-templated inner function to avoid code duplication. Takes a function - // pointer to the type-specific (templated) inner allocate/merge loop. - void MergeFromInternal(const RepeatedPtrFieldBase& other, - void (RepeatedPtrFieldBase::*inner_loop)(void**, - void**, int, - int)); - - template - void MergeFromInnerLoop(void** our_elems, void** other_elems, int length, - int already_allocated); - - // Internal helper: extend array space if necessary to contain |extend_amount| - // more elements, and return a pointer to the element immediately following - // the old list of elements. This interface factors out common behavior from - // Reserve() and MergeFrom() to reduce code size. |extend_amount| must be > 0. - void** InternalExtend(int extend_amount); - - // The reflection implementation needs to call protected methods directly, - // reinterpreting pointers as being to Message instead of a specific Message - // subclass. - friend class ::PROTOBUF_NAMESPACE_ID::Reflection; - - // ExtensionSet stores repeated message extensions as - // RepeatedPtrField, but non-lite ExtensionSets need to implement - // SpaceUsedLong(), and thus need to call SpaceUsedExcludingSelfLong() - // reinterpreting MessageLite as Message. ExtensionSet also needs to make use - // of AddFromCleared(), which is not part of the public interface. - friend class ExtensionSet; - - // The MapFieldBase implementation needs to call protected methods directly, - // reinterpreting pointers as being to Message instead of a specific Message - // subclass. - friend class MapFieldBase; - - // The table-driven MergePartialFromCodedStream implementation needs to - // operate on RepeatedPtrField. - friend class MergePartialFromCodedStreamHelper; - friend class AccessorHelper; - template - friend struct google::protobuf::WeakRepeatedPtrField; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrFieldBase); -}; - -template -class GenericTypeHandler { - public: - typedef GenericType Type; - using Movable = IsMovable; - - static inline GenericType* New(Arena* arena) { - return Arena::CreateMaybeMessage(arena); - } - static inline GenericType* New(Arena* arena, GenericType&& value) { - return Arena::Create(arena, std::move(value)); - } - static inline GenericType* NewFromPrototype(const GenericType* prototype, - Arena* arena = NULL); - static inline void Delete(GenericType* value, Arena* arena) { - if (arena == NULL) { - delete value; - } - } - static inline Arena* GetArena(GenericType* value) { - return Arena::GetArena(value); - } - static inline void* GetMaybeArenaPointer(GenericType* value) { - return Arena::GetArena(value); - } - - static inline void Clear(GenericType* value) { value->Clear(); } - PROTOBUF_NOINLINE - static void Merge(const GenericType& from, GenericType* to); - static inline size_t SpaceUsedLong(const GenericType& value) { - return value.SpaceUsedLong(); - } -}; - -template -GenericType* GenericTypeHandler::NewFromPrototype( - const GenericType* /* prototype */, Arena* arena) { - return New(arena); -} -template -void GenericTypeHandler::Merge(const GenericType& from, - GenericType* to) { - to->MergeFrom(from); -} - -// NewFromPrototype() and Merge() are not defined inline here, as we will need -// to do a virtual function dispatch anyways to go from Message* to call -// New/Merge. -template <> -MessageLite* GenericTypeHandler::NewFromPrototype( - const MessageLite* prototype, Arena* arena); -template <> -inline Arena* GenericTypeHandler::GetArena(MessageLite* value) { - return value->GetArena(); -} -template <> -inline void* GenericTypeHandler::GetMaybeArenaPointer( - MessageLite* value) { - return value->GetMaybeArenaPointer(); -} -template <> -void GenericTypeHandler::Merge(const MessageLite& from, - MessageLite* to); -template <> -inline void GenericTypeHandler::Clear(std::string* value) { - value->clear(); -} -template <> -void GenericTypeHandler::Merge(const std::string& from, - std::string* to); - -// Message specialization bodies defined in message.cc. This split is necessary -// to allow proto2-lite (which includes this header) to be independent of -// Message. -template <> -PROTOBUF_EXPORT Message* GenericTypeHandler::NewFromPrototype( - const Message* prototype, Arena* arena); -template <> -PROTOBUF_EXPORT Arena* GenericTypeHandler::GetArena(Message* value); -template <> -PROTOBUF_EXPORT void* GenericTypeHandler::GetMaybeArenaPointer( - Message* value); - -class StringTypeHandler { - public: - typedef std::string Type; - using Movable = IsMovable; - - static inline std::string* New(Arena* arena) { - return Arena::Create(arena); - } - static inline std::string* New(Arena* arena, std::string&& value) { - return Arena::Create(arena, std::move(value)); - } - static inline std::string* NewFromPrototype(const std::string*, - Arena* arena) { - return New(arena); - } - static inline Arena* GetArena(std::string*) { return NULL; } - static inline void* GetMaybeArenaPointer(std::string* /* value */) { - return NULL; - } - static inline void Delete(std::string* value, Arena* arena) { - if (arena == NULL) { - delete value; - } - } - static inline void Clear(std::string* value) { value->clear(); } - static inline void Merge(const std::string& from, std::string* to) { - *to = from; - } - static size_t SpaceUsedLong(const std::string& value) { - return sizeof(value) + StringSpaceUsedExcludingSelfLong(value); - } -}; - -} // namespace internal - -// RepeatedPtrField is like RepeatedField, but used for repeated strings or -// Messages. -template -class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { - public: - constexpr RepeatedPtrField(); - explicit RepeatedPtrField(Arena* arena); - - RepeatedPtrField(const RepeatedPtrField& other); - template - RepeatedPtrField(Iter begin, const Iter& end); - ~RepeatedPtrField(); - - RepeatedPtrField& operator=(const RepeatedPtrField& other); - - RepeatedPtrField(RepeatedPtrField&& other) noexcept; - RepeatedPtrField& operator=(RepeatedPtrField&& other) noexcept; - - bool empty() const; - int size() const; - - const Element& Get(int index) const; - Element* Mutable(int index); - Element* Add(); - void Add(Element&& value); - - const Element& operator[](int index) const { return Get(index); } - Element& operator[](int index) { return *Mutable(index); } - - const Element& at(int index) const; - Element& at(int index); - - // Remove the last element in the array. - // Ownership of the element is retained by the array. - void RemoveLast(); - - // Delete elements with indices in the range [start .. start+num-1]. - // Caution: implementation moves all elements with indices [start+num .. ]. - // Calling this routine inside a loop can cause quadratic behavior. - void DeleteSubrange(int start, int num); - - void Clear(); - void MergeFrom(const RepeatedPtrField& other); - void CopyFrom(const RepeatedPtrField& other); - - // Reserve space to expand the field to at least the given size. This only - // resizes the pointer array; it doesn't allocate any objects. If the - // array is grown, it will always be at least doubled in size. - void Reserve(int new_size); - - int Capacity() const; - - // Gets the underlying array. This pointer is possibly invalidated by - // any add or remove operation. - Element** mutable_data(); - const Element* const* data() const; - - // Swap entire contents with "other". If they are on separate arenas, then - // copies data. - void Swap(RepeatedPtrField* other); - - // Swap entire contents with "other". Caller should guarantee that either both - // fields are on the same arena or both are on the heap. Swapping between - // different arenas with this function is disallowed and is caught via - // GOOGLE_DCHECK. - void UnsafeArenaSwap(RepeatedPtrField* other); - - // Swap two elements. - void SwapElements(int index1, int index2); - - // STL-like iterator support - typedef internal::RepeatedPtrIterator iterator; - typedef internal::RepeatedPtrIterator const_iterator; - typedef Element value_type; - typedef value_type& reference; - typedef const value_type& const_reference; - typedef value_type* pointer; - typedef const value_type* const_pointer; - typedef int size_type; - typedef ptrdiff_t difference_type; - - iterator begin(); - const_iterator begin() const; - const_iterator cbegin() const; - iterator end(); - const_iterator end() const; - const_iterator cend() const; - - // Reverse iterator support - typedef std::reverse_iterator const_reverse_iterator; - typedef std::reverse_iterator reverse_iterator; - reverse_iterator rbegin() { return reverse_iterator(end()); } - const_reverse_iterator rbegin() const { - return const_reverse_iterator(end()); - } - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rend() const { - return const_reverse_iterator(begin()); - } - - // Custom STL-like iterator that iterates over and returns the underlying - // pointers to Element rather than Element itself. - typedef internal::RepeatedPtrOverPtrsIterator - pointer_iterator; - typedef internal::RepeatedPtrOverPtrsIterator - const_pointer_iterator; - pointer_iterator pointer_begin(); - const_pointer_iterator pointer_begin() const; - pointer_iterator pointer_end(); - const_pointer_iterator pointer_end() const; - - // Returns (an estimate of) the number of bytes used by the repeated field, - // excluding sizeof(*this). - size_t SpaceUsedExcludingSelfLong() const; - - int SpaceUsedExcludingSelf() const { - return internal::ToIntSize(SpaceUsedExcludingSelfLong()); - } - - // Advanced memory management -------------------------------------- - // When hardcore memory management becomes necessary -- as it sometimes - // does here at Google -- the following methods may be useful. - - // Add an already-allocated object, passing ownership to the - // RepeatedPtrField. - // - // Note that some special behavior occurs with respect to arenas: - // - // (i) if this field holds submessages, the new submessage will be copied if - // the original is in an arena and this RepeatedPtrField is either in a - // different arena, or on the heap. - // (ii) if this field holds strings, the passed-in string *must* be - // heap-allocated, not arena-allocated. There is no way to dynamically check - // this at runtime, so User Beware. - void AddAllocated(Element* value); - - // Remove the last element and return it, passing ownership to the caller. - // Requires: size() > 0 - // - // If this RepeatedPtrField is on an arena, an object copy is required to pass - // ownership back to the user (for compatible semantics). Use - // UnsafeArenaReleaseLast() if this behavior is undesired. - Element* ReleaseLast(); - - // Add an already-allocated object, skipping arena-ownership checks. The user - // must guarantee that the given object is in the same arena as this - // RepeatedPtrField. - // It is also useful in legacy code that uses temporary ownership to avoid - // copies. Example: - // RepeatedPtrField temp_field; - // temp_field.AddAllocated(new T); - // ... // Do something with temp_field - // temp_field.ExtractSubrange(0, temp_field.size(), nullptr); - // If you put temp_field on the arena this fails, because the ownership - // transfers to the arena at the "AddAllocated" call and is not released - // anymore causing a double delete. UnsafeArenaAddAllocated prevents this. - void UnsafeArenaAddAllocated(Element* value); - - // Remove the last element and return it. Works only when operating on an - // arena. The returned pointer is to the original object in the arena, hence - // has the arena's lifetime. - // Requires: current_size_ > 0 - Element* UnsafeArenaReleaseLast(); - - // Extract elements with indices in the range "[start .. start+num-1]". - // The caller assumes ownership of the extracted elements and is responsible - // for deleting them when they are no longer needed. - // If "elements" is non-NULL, then pointers to the extracted elements - // are stored in "elements[0 .. num-1]" for the convenience of the caller. - // If "elements" is NULL, then the caller must use some other mechanism - // to perform any further operations (like deletion) on these elements. - // Caution: implementation also moves elements with indices [start+num ..]. - // Calling this routine inside a loop can cause quadratic behavior. - // - // Memory copying behavior is identical to ReleaseLast(), described above: if - // this RepeatedPtrField is on an arena, an object copy is performed for each - // returned element, so that all returned element pointers are to - // heap-allocated copies. If this copy is not desired, the user should call - // UnsafeArenaExtractSubrange(). - void ExtractSubrange(int start, int num, Element** elements); - - // Identical to ExtractSubrange() described above, except that when this - // repeated field is on an arena, no object copies are performed. Instead, the - // raw object pointers are returned. Thus, if on an arena, the returned - // objects must not be freed, because they will not be heap-allocated objects. - void UnsafeArenaExtractSubrange(int start, int num, Element** elements); - - // When elements are removed by calls to RemoveLast() or Clear(), they - // are not actually freed. Instead, they are cleared and kept so that - // they can be reused later. This can save lots of CPU time when - // repeatedly reusing a protocol message for similar purposes. - // - // Hardcore programs may choose to manipulate these cleared objects - // to better optimize memory management using the following routines. - - // Get the number of cleared objects that are currently being kept - // around for reuse. - int ClearedCount() const; - // Add an element to the pool of cleared objects, passing ownership to - // the RepeatedPtrField. The element must be cleared prior to calling - // this method. - // - // This method cannot be called when the repeated field is on an arena or when - // |value| is; both cases will trigger a GOOGLE_DCHECK-failure. - void AddCleared(Element* value); - // Remove a single element from the cleared pool and return it, passing - // ownership to the caller. The element is guaranteed to be cleared. - // Requires: ClearedCount() > 0 - // - // - // This method cannot be called when the repeated field is on an arena; doing - // so will trigger a GOOGLE_DCHECK-failure. - Element* ReleaseCleared(); - - // Removes the element referenced by position. - // - // Returns an iterator to the element immediately following the removed - // element. - // - // Invalidates all iterators at or after the removed element, including end(). - iterator erase(const_iterator position); - - // Removes the elements in the range [first, last). - // - // Returns an iterator to the element immediately following the removed range. - // - // Invalidates all iterators at or after the removed range, including end(). - iterator erase(const_iterator first, const_iterator last); - - // Gets the arena on which this RepeatedPtrField stores its elements. - inline Arena* GetArena() const; - - // For internal use only. - // - // This is public due to it being called by generated code. - void InternalSwap(RepeatedPtrField* other) { - internal::RepeatedPtrFieldBase::InternalSwap(other); - } - - private: - // Note: RepeatedPtrField SHOULD NOT be subclassed by users. - class TypeHandler; - - // Implementations for ExtractSubrange(). The copying behavior must be - // included only if the type supports the necessary operations (e.g., - // MergeFrom()), so we must resolve this at compile time. ExtractSubrange() - // uses SFINAE to choose one of the below implementations. - void ExtractSubrangeInternal(int start, int num, Element** elements, - std::true_type); - void ExtractSubrangeInternal(int start, int num, Element** elements, - std::false_type); - - friend class Arena; - - template - friend struct WeakRepeatedPtrField; - - typedef void InternalArenaConstructable_; - -}; - -// implementation ==================================================== - -template -constexpr RepeatedField::RepeatedField() - : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {} - -template -inline RepeatedField::RepeatedField(Arena* arena) - : current_size_(0), total_size_(0), arena_or_elements_(arena) {} - -template -inline RepeatedField::RepeatedField(const RepeatedField& other) - : current_size_(0), total_size_(0), arena_or_elements_(nullptr) { - if (other.current_size_ != 0) { - Reserve(other.size()); - AddNAlreadyReserved(other.size()); - CopyArray(Mutable(0), &other.Get(0), other.size()); - } -} - -template -template -RepeatedField::RepeatedField(Iter begin, const Iter& end) - : current_size_(0), total_size_(0), arena_or_elements_(nullptr) { - Add(begin, end); -} - -template -RepeatedField::~RepeatedField() { - if (total_size_ > 0) { - InternalDeallocate(rep(), total_size_); - } -} - -template -inline RepeatedField& RepeatedField::operator=( - const RepeatedField& other) { - if (this != &other) CopyFrom(other); - return *this; -} - -template -inline RepeatedField::RepeatedField(RepeatedField&& other) noexcept - : RepeatedField() { - // We don't just call Swap(&other) here because it would perform 3 copies if - // other is on an arena. This field can't be on an arena because arena - // construction always uses the Arena* accepting constructor. - if (other.GetArena()) { - CopyFrom(other); - } else { - InternalSwap(&other); - } -} - -template -inline RepeatedField& RepeatedField::operator=( - RepeatedField&& other) noexcept { - // We don't just call Swap(&other) here because it would perform 3 copies if - // the two fields are on different arenas. - if (this != &other) { - if (this->GetArena() != other.GetArena()) { - CopyFrom(other); - } else { - InternalSwap(&other); - } - } - return *this; -} - -template -inline bool RepeatedField::empty() const { - return current_size_ == 0; -} - -template -inline int RepeatedField::size() const { - return current_size_; -} - -template -inline int RepeatedField::Capacity() const { - return total_size_; -} - -template -inline void RepeatedField::AddAlreadyReserved(const Element& value) { - GOOGLE_DCHECK_LT(current_size_, total_size_); - elements()[current_size_++] = value; -} - -template -inline Element* RepeatedField::AddAlreadyReserved() { - GOOGLE_DCHECK_LT(current_size_, total_size_); - return &elements()[current_size_++]; -} - -template -inline Element* RepeatedField::AddNAlreadyReserved(int n) { - GOOGLE_DCHECK_GE(total_size_ - current_size_, n) - << total_size_ << ", " << current_size_; - // Warning: sometimes people call this when n == 0 and total_size_ == 0. In - // this case the return pointer points to a zero size array (n == 0). Hence - // we can just use unsafe_elements(), because the user cannot dereference the - // pointer anyway. - Element* ret = unsafe_elements() + current_size_; - current_size_ += n; - return ret; -} - -template -inline void RepeatedField::Resize(int new_size, const Element& value) { - GOOGLE_DCHECK_GE(new_size, 0); - if (new_size > current_size_) { - Reserve(new_size); - std::fill(&elements()[current_size_], &elements()[new_size], value); - } - current_size_ = new_size; -} - -template -inline const Element& RepeatedField::Get(int index) const { - GOOGLE_DCHECK_GE(index, 0); - GOOGLE_DCHECK_LT(index, current_size_); - return elements()[index]; -} - -template -inline const Element& RepeatedField::at(int index) const { - GOOGLE_CHECK_GE(index, 0); - GOOGLE_CHECK_LT(index, current_size_); - return elements()[index]; -} - -template -inline Element& RepeatedField::at(int index) { - GOOGLE_CHECK_GE(index, 0); - GOOGLE_CHECK_LT(index, current_size_); - return elements()[index]; -} - -template -inline Element* RepeatedField::Mutable(int index) { - GOOGLE_DCHECK_GE(index, 0); - GOOGLE_DCHECK_LT(index, current_size_); - return &elements()[index]; -} - -template -inline void RepeatedField::Set(int index, const Element& value) { - GOOGLE_DCHECK_GE(index, 0); - GOOGLE_DCHECK_LT(index, current_size_); - elements()[index] = value; -} - -template -inline void RepeatedField::Add(const Element& value) { - uint32 size = current_size_; - if (static_cast(size) == total_size_) { - // value could reference an element of the array. Reserving new space will - // invalidate the reference. So we must make a copy first. - auto tmp = value; - Reserve(total_size_ + 1); - elements()[size] = std::move(tmp); - } else { - elements()[size] = value; - } - current_size_ = size + 1; -} - -template -inline Element* RepeatedField::Add() { - uint32 size = current_size_; - if (static_cast(size) == total_size_) Reserve(total_size_ + 1); - auto ptr = &elements()[size]; - current_size_ = size + 1; - return ptr; -} - -template -template -inline void RepeatedField::Add(Iter begin, Iter end) { - int reserve = internal::CalculateReserve(begin, end); - if (reserve != -1) { - if (reserve == 0) { - return; - } - - Reserve(reserve + size()); - // TODO(ckennelly): The compiler loses track of the buffer freshly - // allocated by Reserve() by the time we call elements, so it cannot - // guarantee that elements does not alias [begin(), end()). - // - // If restrict is available, annotating the pointer obtained from elements() - // causes this to lower to memcpy instead of memmove. - std::copy(begin, end, elements() + size()); - current_size_ = reserve + size(); - } else { - FastAdder fast_adder(this); - for (; begin != end; ++begin) fast_adder.Add(*begin); - } -} - -template -inline void RepeatedField::RemoveLast() { - GOOGLE_DCHECK_GT(current_size_, 0); - current_size_--; -} - -template -void RepeatedField::ExtractSubrange(int start, int num, - Element* elements) { - GOOGLE_DCHECK_GE(start, 0); - GOOGLE_DCHECK_GE(num, 0); - GOOGLE_DCHECK_LE(start + num, this->current_size_); - - // Save the values of the removed elements if requested. - if (elements != NULL) { - for (int i = 0; i < num; ++i) elements[i] = this->Get(i + start); - } - - // Slide remaining elements down to fill the gap. - if (num > 0) { - for (int i = start + num; i < this->current_size_; ++i) - this->Set(i - num, this->Get(i)); - this->Truncate(this->current_size_ - num); - } -} - -template -inline void RepeatedField::Clear() { - current_size_ = 0; -} - -template -inline void RepeatedField::MergeFrom(const RepeatedField& other) { - GOOGLE_DCHECK_NE(&other, this); - if (other.current_size_ != 0) { - int existing_size = size(); - Reserve(existing_size + other.size()); - AddNAlreadyReserved(other.size()); - CopyArray(Mutable(existing_size), &other.Get(0), other.size()); - } -} - -template -inline void RepeatedField::CopyFrom(const RepeatedField& other) { - if (&other == this) return; - Clear(); - MergeFrom(other); -} - -template -inline typename RepeatedField::iterator RepeatedField::erase( - const_iterator position) { - return erase(position, position + 1); -} - -template -inline typename RepeatedField::iterator RepeatedField::erase( - const_iterator first, const_iterator last) { - size_type first_offset = first - cbegin(); - if (first != last) { - Truncate(std::copy(last, cend(), begin() + first_offset) - cbegin()); - } - return begin() + first_offset; -} - -template -inline Element* RepeatedField::mutable_data() { - return unsafe_elements(); -} - -template -inline const Element* RepeatedField::data() const { - return unsafe_elements(); -} - -template -inline void RepeatedField::InternalSwap(RepeatedField* other) { - GOOGLE_DCHECK(this != other); - GOOGLE_DCHECK(GetArena() == other->GetArena()); - - // Swap all fields at once. - static_assert(std::is_standard_layout>::value, - "offsetof() requires standard layout before c++17"); - internal::memswaparena_or_elements_) - - offsetof(RepeatedField, current_size_)>( - reinterpret_cast(this) + offsetof(RepeatedField, current_size_), - reinterpret_cast(other) + offsetof(RepeatedField, current_size_)); -} - -template -void RepeatedField::Swap(RepeatedField* other) { - if (this == other) return; - if (GetArena() == other->GetArena()) { - InternalSwap(other); - } else { - RepeatedField temp(other->GetArena()); - temp.MergeFrom(*this); - CopyFrom(*other); - other->UnsafeArenaSwap(&temp); - } -} - -template -void RepeatedField::UnsafeArenaSwap(RepeatedField* other) { - if (this == other) return; - InternalSwap(other); -} - -template -void RepeatedField::SwapElements(int index1, int index2) { - using std::swap; // enable ADL with fallback - swap(elements()[index1], elements()[index2]); -} - -template -inline typename RepeatedField::iterator -RepeatedField::begin() { - return unsafe_elements(); -} -template -inline typename RepeatedField::const_iterator -RepeatedField::begin() const { - return unsafe_elements(); -} -template -inline typename RepeatedField::const_iterator -RepeatedField::cbegin() const { - return unsafe_elements(); -} -template -inline typename RepeatedField::iterator RepeatedField::end() { - return unsafe_elements() + current_size_; -} -template -inline typename RepeatedField::const_iterator -RepeatedField::end() const { - return unsafe_elements() + current_size_; -} -template -inline typename RepeatedField::const_iterator -RepeatedField::cend() const { - return unsafe_elements() + current_size_; -} - -template -inline size_t RepeatedField::SpaceUsedExcludingSelfLong() const { - return total_size_ > 0 ? (total_size_ * sizeof(Element) + kRepHeaderSize) : 0; -} - -namespace internal { -// Returns the new size for a reserved field based on its 'total_size' and the -// requested 'new_size'. The result is clamped to the closed interval: -// [internal::kMinRepeatedFieldAllocationSize, -// std::numeric_limits::max()] -// Requires: -// new_size > total_size && -// (total_size == 0 || -// total_size >= kRepeatedFieldLowerClampLimit) -inline int CalculateReserveSize(int total_size, int new_size) { - if (new_size < kRepeatedFieldLowerClampLimit) { - // Clamp to smallest allowed size. - return kRepeatedFieldLowerClampLimit; - } - if (total_size < kRepeatedFieldUpperClampLimit) { - return std::max(total_size * 2, new_size); - } else { - // Clamp to largest allowed size. - GOOGLE_DCHECK_GT(new_size, kRepeatedFieldUpperClampLimit); - return std::numeric_limits::max(); - } -} -} // namespace internal - -// Avoid inlining of Reserve(): new, copy, and delete[] lead to a significant -// amount of code bloat. -template -void RepeatedField::Reserve(int new_size) { - if (total_size_ >= new_size) return; - Rep* old_rep = total_size_ > 0 ? rep() : NULL; - Rep* new_rep; - Arena* arena = GetArena(); - new_size = internal::CalculateReserveSize(total_size_, new_size); - GOOGLE_DCHECK_LE( - static_cast(new_size), - (std::numeric_limits::max() - kRepHeaderSize) / sizeof(Element)) - << "Requested size is too large to fit into size_t."; - size_t bytes = - kRepHeaderSize + sizeof(Element) * static_cast(new_size); - if (arena == NULL) { - new_rep = static_cast(::operator new(bytes)); - } else { - new_rep = reinterpret_cast(Arena::CreateArray(arena, bytes)); - } - new_rep->arena = arena; - int old_total_size = total_size_; - // Already known: new_size >= internal::kMinRepeatedFieldAllocationSize - // Maintain invariant: - // total_size_ == 0 || - // total_size_ >= internal::kMinRepeatedFieldAllocationSize - total_size_ = new_size; - arena_or_elements_ = new_rep->elements; - // Invoke placement-new on newly allocated elements. We shouldn't have to do - // this, since Element is supposed to be POD, but a previous version of this - // code allocated storage with "new Element[size]" and some code uses - // RepeatedField with non-POD types, relying on constructor invocation. If - // Element has a trivial constructor (e.g., int32), gcc (tested with -O2) - // completely removes this loop because the loop body is empty, so this has no - // effect unless its side-effects are required for correctness. - // Note that we do this before MoveArray() below because Element's copy - // assignment implementation will want an initialized instance first. - Element* e = &elements()[0]; - Element* limit = e + total_size_; - for (; e < limit; e++) { - new (e) Element; - } - if (current_size_ > 0) { - MoveArray(&elements()[0], old_rep->elements, current_size_); - } - - // Likewise, we need to invoke destructors on the old array. - InternalDeallocate(old_rep, old_total_size); - -} - -template -inline void RepeatedField::Truncate(int new_size) { - GOOGLE_DCHECK_LE(new_size, current_size_); - if (current_size_ > 0) { - current_size_ = new_size; - } -} - -template -inline void RepeatedField::MoveArray(Element* to, Element* from, - int array_size) { - CopyArray(to, from, array_size); -} - -template -inline void RepeatedField::CopyArray(Element* to, const Element* from, - int array_size) { - internal::ElementCopier()(to, from, array_size); -} - -namespace internal { - -template -void ElementCopier::operator()(Element* to, - const Element* from, - int array_size) { - std::copy(from, from + array_size, to); -} - -template -struct ElementCopier { - void operator()(Element* to, const Element* from, int array_size) { - memcpy(to, from, static_cast(array_size) * sizeof(Element)); - } -}; - -} // namespace internal - - -// ------------------------------------------------------------------- - -namespace internal { - -constexpr RepeatedPtrFieldBase::RepeatedPtrFieldBase() - : arena_(NULL), current_size_(0), total_size_(0), rep_(NULL) {} - -inline RepeatedPtrFieldBase::RepeatedPtrFieldBase(Arena* arena) - : arena_(arena), current_size_(0), total_size_(0), rep_(NULL) {} - -template -void RepeatedPtrFieldBase::Destroy() { - if (rep_ != NULL && arena_ == NULL) { - int n = rep_->allocated_size; - void* const* elements = rep_->elements; - for (int i = 0; i < n; i++) { - TypeHandler::Delete(cast(elements[i]), NULL); - } -#if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) - const size_t size = total_size_ * sizeof(elements[0]) + kRepHeaderSize; - ::operator delete(static_cast(rep_), size); -#else - ::operator delete(static_cast(rep_)); -#endif - } - rep_ = NULL; -} - -template -inline void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) { - if (other->GetArena() == GetArena()) { - InternalSwap(other); - } else { - SwapFallback(other); - } -} - -template -void RepeatedPtrFieldBase::SwapFallback(RepeatedPtrFieldBase* other) { - GOOGLE_DCHECK(other->GetArena() != GetArena()); - - // Copy semantics in this case. We try to improve efficiency by placing the - // temporary on |other|'s arena so that messages are copied twice rather than - // three times. - RepeatedPtrFieldBase temp(other->GetArena()); - temp.MergeFrom(*this); - this->Clear(); - this->MergeFrom(*other); - other->InternalSwap(&temp); - temp.Destroy(); // Frees rep_ if `other` had no arena. -} - -inline bool RepeatedPtrFieldBase::empty() const { return current_size_ == 0; } - -inline int RepeatedPtrFieldBase::size() const { return current_size_; } - -template -inline const typename TypeHandler::Type& RepeatedPtrFieldBase::Get( - int index) const { - GOOGLE_DCHECK_GE(index, 0); - GOOGLE_DCHECK_LT(index, current_size_); - return *cast(rep_->elements[index]); -} - -template -inline const typename TypeHandler::Type& RepeatedPtrFieldBase::at( - int index) const { - GOOGLE_CHECK_GE(index, 0); - GOOGLE_CHECK_LT(index, current_size_); - return *cast(rep_->elements[index]); -} - -template -inline typename TypeHandler::Type& RepeatedPtrFieldBase::at(int index) { - GOOGLE_CHECK_GE(index, 0); - GOOGLE_CHECK_LT(index, current_size_); - return *cast(rep_->elements[index]); -} - -template -inline typename TypeHandler::Type* RepeatedPtrFieldBase::Mutable(int index) { - GOOGLE_DCHECK_GE(index, 0); - GOOGLE_DCHECK_LT(index, current_size_); - return cast(rep_->elements[index]); -} - -template -inline void RepeatedPtrFieldBase::Delete(int index) { - GOOGLE_DCHECK_GE(index, 0); - GOOGLE_DCHECK_LT(index, current_size_); - TypeHandler::Delete(cast(rep_->elements[index]), arena_); -} - -template -inline typename TypeHandler::Type* RepeatedPtrFieldBase::Add( - typename TypeHandler::Type* prototype) { - if (rep_ != NULL && current_size_ < rep_->allocated_size) { - return cast(rep_->elements[current_size_++]); - } - if (!rep_ || rep_->allocated_size == total_size_) { - Reserve(total_size_ + 1); - } - ++rep_->allocated_size; - typename TypeHandler::Type* result = - TypeHandler::NewFromPrototype(prototype, arena_); - rep_->elements[current_size_++] = result; - return result; -} - -template ::type*> -inline void RepeatedPtrFieldBase::Add(typename TypeHandler::Type&& value) { - if (rep_ != NULL && current_size_ < rep_->allocated_size) { - *cast(rep_->elements[current_size_++]) = std::move(value); - return; - } - if (!rep_ || rep_->allocated_size == total_size_) { - Reserve(total_size_ + 1); - } - ++rep_->allocated_size; - typename TypeHandler::Type* result = - TypeHandler::New(arena_, std::move(value)); - rep_->elements[current_size_++] = result; -} - -template -inline void RepeatedPtrFieldBase::RemoveLast() { - GOOGLE_DCHECK_GT(current_size_, 0); - TypeHandler::Clear(cast(rep_->elements[--current_size_])); -} - -template -void RepeatedPtrFieldBase::Clear() { - const int n = current_size_; - GOOGLE_DCHECK_GE(n, 0); - if (n > 0) { - void* const* elements = rep_->elements; - int i = 0; - do { - TypeHandler::Clear(cast(elements[i++])); - } while (i < n); - current_size_ = 0; - } -} - -// To avoid unnecessary code duplication and reduce binary size, we use a -// layered approach to implementing MergeFrom(). The toplevel method is -// templated, so we get a small thunk per concrete message type in the binary. -// This calls a shared implementation with most of the logic, passing a function -// pointer to another type-specific piece of code that calls the object-allocate -// and merge handlers. -template -inline void RepeatedPtrFieldBase::MergeFrom(const RepeatedPtrFieldBase& other) { - GOOGLE_DCHECK_NE(&other, this); - if (other.current_size_ == 0) return; - MergeFromInternal(other, - &RepeatedPtrFieldBase::MergeFromInnerLoop); -} - -inline void RepeatedPtrFieldBase::MergeFromInternal( - const RepeatedPtrFieldBase& other, - void (RepeatedPtrFieldBase::*inner_loop)(void**, void**, int, int)) { - // Note: wrapper has already guaranteed that other.rep_ != NULL here. - int other_size = other.current_size_; - void** other_elements = other.rep_->elements; - void** new_elements = InternalExtend(other_size); - int allocated_elems = rep_->allocated_size - current_size_; - (this->*inner_loop)(new_elements, other_elements, other_size, - allocated_elems); - current_size_ += other_size; - if (rep_->allocated_size < current_size_) { - rep_->allocated_size = current_size_; - } -} - -// Merges other_elems to our_elems. -template -void RepeatedPtrFieldBase::MergeFromInnerLoop(void** our_elems, - void** other_elems, int length, - int already_allocated) { - // Split into two loops, over ranges [0, allocated) and [allocated, length), - // to avoid a branch within the loop. - for (int i = 0; i < already_allocated && i < length; i++) { - // Already allocated: use existing element. - typename TypeHandler::Type* other_elem = - reinterpret_cast(other_elems[i]); - typename TypeHandler::Type* new_elem = - reinterpret_cast(our_elems[i]); - TypeHandler::Merge(*other_elem, new_elem); - } - Arena* arena = GetArena(); - for (int i = already_allocated; i < length; i++) { - // Not allocated: alloc a new element first, then merge it. - typename TypeHandler::Type* other_elem = - reinterpret_cast(other_elems[i]); - typename TypeHandler::Type* new_elem = - TypeHandler::NewFromPrototype(other_elem, arena); - TypeHandler::Merge(*other_elem, new_elem); - our_elems[i] = new_elem; - } -} - -template -inline void RepeatedPtrFieldBase::CopyFrom(const RepeatedPtrFieldBase& other) { - if (&other == this) return; - RepeatedPtrFieldBase::Clear(); - RepeatedPtrFieldBase::MergeFrom(other); -} - -inline int RepeatedPtrFieldBase::Capacity() const { return total_size_; } - -inline void* const* RepeatedPtrFieldBase::raw_data() const { - return rep_ ? rep_->elements : NULL; -} - -inline void** RepeatedPtrFieldBase::raw_mutable_data() const { - return rep_ ? const_cast(rep_->elements) : NULL; -} - -template -inline typename TypeHandler::Type** RepeatedPtrFieldBase::mutable_data() { - // TODO(kenton): Breaks C++ aliasing rules. We should probably remove this - // method entirely. - return reinterpret_cast(raw_mutable_data()); -} - -template -inline const typename TypeHandler::Type* const* RepeatedPtrFieldBase::data() - const { - // TODO(kenton): Breaks C++ aliasing rules. We should probably remove this - // method entirely. - return reinterpret_cast(raw_data()); -} - -inline void RepeatedPtrFieldBase::SwapElements(int index1, int index2) { - using std::swap; // enable ADL with fallback - swap(rep_->elements[index1], rep_->elements[index2]); -} - -template -inline size_t RepeatedPtrFieldBase::SpaceUsedExcludingSelfLong() const { - size_t allocated_bytes = static_cast(total_size_) * sizeof(void*); - if (rep_ != NULL) { - for (int i = 0; i < rep_->allocated_size; ++i) { - allocated_bytes += - TypeHandler::SpaceUsedLong(*cast(rep_->elements[i])); - } - allocated_bytes += kRepHeaderSize; - } - return allocated_bytes; -} - -template -inline typename TypeHandler::Type* RepeatedPtrFieldBase::AddFromCleared() { - if (rep_ != NULL && current_size_ < rep_->allocated_size) { - return cast(rep_->elements[current_size_++]); - } else { - return NULL; - } -} - -// AddAllocated version that implements arena-safe copying behavior. -template -void RepeatedPtrFieldBase::AddAllocatedInternal( - typename TypeHandler::Type* value, std::true_type) { - Arena* element_arena = - reinterpret_cast(TypeHandler::GetMaybeArenaPointer(value)); - Arena* arena = GetArena(); - if (arena == element_arena && rep_ && rep_->allocated_size < total_size_) { - // Fast path: underlying arena representation (tagged pointer) is equal to - // our arena pointer, and we can add to array without resizing it (at least - // one slot that is not allocated). - void** elems = rep_->elements; - if (current_size_ < rep_->allocated_size) { - // Make space at [current] by moving first allocated element to end of - // allocated list. - elems[rep_->allocated_size] = elems[current_size_]; - } - elems[current_size_] = value; - current_size_ = current_size_ + 1; - rep_->allocated_size = rep_->allocated_size + 1; - } else { - AddAllocatedSlowWithCopy(value, TypeHandler::GetArena(value), - arena); - } -} - -// Slowpath handles all cases, copying if necessary. -template -void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy( - // Pass value_arena and my_arena to avoid duplicate virtual call (value) or - // load (mine). - typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena) { - // Ensure that either the value is in the same arena, or if not, we do the - // appropriate thing: Own() it (if it's on heap and we're in an arena) or copy - // it to our arena/heap (otherwise). - if (my_arena != NULL && value_arena == NULL) { - my_arena->Own(value); - } else if (my_arena != value_arena) { - typename TypeHandler::Type* new_value = - TypeHandler::NewFromPrototype(value, my_arena); - TypeHandler::Merge(*value, new_value); - TypeHandler::Delete(value, value_arena); - value = new_value; - } - - UnsafeArenaAddAllocated(value); -} - -// AddAllocated version that does not implement arena-safe copying behavior. -template -void RepeatedPtrFieldBase::AddAllocatedInternal( - typename TypeHandler::Type* value, std::false_type) { - if (rep_ && rep_->allocated_size < total_size_) { - // Fast path: underlying arena representation (tagged pointer) is equal to - // our arena pointer, and we can add to array without resizing it (at least - // one slot that is not allocated). - void** elems = rep_->elements; - if (current_size_ < rep_->allocated_size) { - // Make space at [current] by moving first allocated element to end of - // allocated list. - elems[rep_->allocated_size] = elems[current_size_]; - } - elems[current_size_] = value; - current_size_ = current_size_ + 1; - ++rep_->allocated_size; - } else { - UnsafeArenaAddAllocated(value); - } -} - -template -void RepeatedPtrFieldBase::UnsafeArenaAddAllocated( - typename TypeHandler::Type* value) { - // Make room for the new pointer. - if (!rep_ || current_size_ == total_size_) { - // The array is completely full with no cleared objects, so grow it. - Reserve(total_size_ + 1); - ++rep_->allocated_size; - } else if (rep_->allocated_size == total_size_) { - // There is no more space in the pointer array because it contains some - // cleared objects awaiting reuse. We don't want to grow the array in this - // case because otherwise a loop calling AddAllocated() followed by Clear() - // would leak memory. - TypeHandler::Delete(cast(rep_->elements[current_size_]), - arena_); - } else if (current_size_ < rep_->allocated_size) { - // We have some cleared objects. We don't care about their order, so we - // can just move the first one to the end to make space. - rep_->elements[rep_->allocated_size] = rep_->elements[current_size_]; - ++rep_->allocated_size; - } else { - // There are no cleared objects. - ++rep_->allocated_size; - } - - rep_->elements[current_size_++] = value; -} - -// ReleaseLast() for types that implement merge/copy behavior. -template -inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal( - std::true_type) { - // First, release an element. - typename TypeHandler::Type* result = UnsafeArenaReleaseLast(); - // Now perform a copy if we're on an arena. - Arena* arena = GetArena(); - if (arena == NULL) { - return result; - } else { - typename TypeHandler::Type* new_result = - TypeHandler::NewFromPrototype(result, NULL); - TypeHandler::Merge(*result, new_result); - return new_result; - } -} - -// ReleaseLast() for types that *do not* implement merge/copy behavior -- this -// is the same as UnsafeArenaReleaseLast(). Note that we GOOGLE_DCHECK-fail if we're on -// an arena, since the user really should implement the copy operation in this -// case. -template -inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal( - std::false_type) { - GOOGLE_DCHECK(GetArena() == NULL) - << "ReleaseLast() called on a RepeatedPtrField that is on an arena, " - << "with a type that does not implement MergeFrom. This is unsafe; " - << "please implement MergeFrom for your type."; - return UnsafeArenaReleaseLast(); -} - -template -inline typename TypeHandler::Type* -RepeatedPtrFieldBase::UnsafeArenaReleaseLast() { - GOOGLE_DCHECK_GT(current_size_, 0); - typename TypeHandler::Type* result = - cast(rep_->elements[--current_size_]); - --rep_->allocated_size; - if (current_size_ < rep_->allocated_size) { - // There are cleared elements on the end; replace the removed element - // with the last allocated element. - rep_->elements[current_size_] = rep_->elements[rep_->allocated_size]; - } - return result; -} - -inline int RepeatedPtrFieldBase::ClearedCount() const { - return rep_ ? (rep_->allocated_size - current_size_) : 0; -} - -template -inline void RepeatedPtrFieldBase::AddCleared( - typename TypeHandler::Type* value) { - GOOGLE_DCHECK(GetArena() == NULL) - << "AddCleared() can only be used on a RepeatedPtrField not on an arena."; - GOOGLE_DCHECK(TypeHandler::GetArena(value) == NULL) - << "AddCleared() can only accept values not on an arena."; - if (!rep_ || rep_->allocated_size == total_size_) { - Reserve(total_size_ + 1); - } - rep_->elements[rep_->allocated_size++] = value; -} - -template -inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseCleared() { - GOOGLE_DCHECK(GetArena() == NULL) - << "ReleaseCleared() can only be used on a RepeatedPtrField not on " - << "an arena."; - GOOGLE_DCHECK(GetArena() == NULL); - GOOGLE_DCHECK(rep_ != NULL); - GOOGLE_DCHECK_GT(rep_->allocated_size, current_size_); - return cast(rep_->elements[--rep_->allocated_size]); -} - -} // namespace internal - -// ------------------------------------------------------------------- - -template -class RepeatedPtrField::TypeHandler - : public internal::GenericTypeHandler {}; - -template <> -class RepeatedPtrField::TypeHandler - : public internal::StringTypeHandler {}; - -template -constexpr RepeatedPtrField::RepeatedPtrField() - : RepeatedPtrFieldBase() {} - -template -inline RepeatedPtrField::RepeatedPtrField(Arena* arena) - : RepeatedPtrFieldBase(arena) {} - -template -inline RepeatedPtrField::RepeatedPtrField( - const RepeatedPtrField& other) - : RepeatedPtrFieldBase() { - MergeFrom(other); -} - -template -template -inline RepeatedPtrField::RepeatedPtrField(Iter begin, - const Iter& end) { - int reserve = internal::CalculateReserve(begin, end); - if (reserve != -1) { - Reserve(reserve); - } - for (; begin != end; ++begin) { - *Add() = *begin; - } -} - -template -RepeatedPtrField::~RepeatedPtrField() { - Destroy(); -} - -template -inline RepeatedPtrField& RepeatedPtrField::operator=( - const RepeatedPtrField& other) { - if (this != &other) CopyFrom(other); - return *this; -} - -template -inline RepeatedPtrField::RepeatedPtrField( - RepeatedPtrField&& other) noexcept - : RepeatedPtrField() { - // We don't just call Swap(&other) here because it would perform 3 copies if - // other is on an arena. This field can't be on an arena because arena - // construction always uses the Arena* accepting constructor. - if (other.GetArena()) { - CopyFrom(other); - } else { - InternalSwap(&other); - } -} - -template -inline RepeatedPtrField& RepeatedPtrField::operator=( - RepeatedPtrField&& other) noexcept { - // We don't just call Swap(&other) here because it would perform 3 copies if - // the two fields are on different arenas. - if (this != &other) { - if (this->GetArena() != other.GetArena()) { - CopyFrom(other); - } else { - InternalSwap(&other); - } - } - return *this; -} - -template -inline bool RepeatedPtrField::empty() const { - return RepeatedPtrFieldBase::empty(); -} - -template -inline int RepeatedPtrField::size() const { - return RepeatedPtrFieldBase::size(); -} - -template -inline const Element& RepeatedPtrField::Get(int index) const { - return RepeatedPtrFieldBase::Get(index); -} - -template -inline const Element& RepeatedPtrField::at(int index) const { - return RepeatedPtrFieldBase::at(index); -} - -template -inline Element& RepeatedPtrField::at(int index) { - return RepeatedPtrFieldBase::at(index); -} - - -template -inline Element* RepeatedPtrField::Mutable(int index) { - return RepeatedPtrFieldBase::Mutable(index); -} - -template -inline Element* RepeatedPtrField::Add() { - return RepeatedPtrFieldBase::Add(); -} - -template -inline void RepeatedPtrField::Add(Element&& value) { - RepeatedPtrFieldBase::Add(std::move(value)); -} - -template -inline void RepeatedPtrField::RemoveLast() { - RepeatedPtrFieldBase::RemoveLast(); -} - -template -inline void RepeatedPtrField::DeleteSubrange(int start, int num) { - GOOGLE_DCHECK_GE(start, 0); - GOOGLE_DCHECK_GE(num, 0); - GOOGLE_DCHECK_LE(start + num, size()); - for (int i = 0; i < num; ++i) { - RepeatedPtrFieldBase::Delete(start + i); - } - ExtractSubrange(start, num, NULL); -} - -template -inline void RepeatedPtrField::ExtractSubrange(int start, int num, - Element** elements) { - typename internal::TypeImplementsMergeBehavior< - typename TypeHandler::Type>::type t; - ExtractSubrangeInternal(start, num, elements, t); -} - -// ExtractSubrange() implementation for types that implement merge/copy -// behavior. -template -inline void RepeatedPtrField::ExtractSubrangeInternal( - int start, int num, Element** elements, std::true_type) { - GOOGLE_DCHECK_GE(start, 0); - GOOGLE_DCHECK_GE(num, 0); - GOOGLE_DCHECK_LE(start + num, size()); - - if (num > 0) { - // Save the values of the removed elements if requested. - if (elements != NULL) { - if (GetArena() != NULL) { - // If we're on an arena, we perform a copy for each element so that the - // returned elements are heap-allocated. - for (int i = 0; i < num; ++i) { - Element* element = - RepeatedPtrFieldBase::Mutable(i + start); - typename TypeHandler::Type* new_value = - TypeHandler::NewFromPrototype(element, NULL); - TypeHandler::Merge(*element, new_value); - elements[i] = new_value; - } - } else { - for (int i = 0; i < num; ++i) { - elements[i] = RepeatedPtrFieldBase::Mutable(i + start); - } - } - } - CloseGap(start, num); - } -} - -// ExtractSubrange() implementation for types that do not implement merge/copy -// behavior. -template -inline void RepeatedPtrField::ExtractSubrangeInternal( - int start, int num, Element** elements, std::false_type) { - // This case is identical to UnsafeArenaExtractSubrange(). However, since - // ExtractSubrange() must return heap-allocated objects by contract, and we - // cannot fulfill this contract if we are an on arena, we must GOOGLE_DCHECK() that - // we are not on an arena. - GOOGLE_DCHECK(GetArena() == NULL) - << "ExtractSubrange() when arena is non-NULL is only supported when " - << "the Element type supplies a MergeFrom() operation to make copies."; - UnsafeArenaExtractSubrange(start, num, elements); -} - -template -inline void RepeatedPtrField::UnsafeArenaExtractSubrange( - int start, int num, Element** elements) { - GOOGLE_DCHECK_GE(start, 0); - GOOGLE_DCHECK_GE(num, 0); - GOOGLE_DCHECK_LE(start + num, size()); - - if (num > 0) { - // Save the values of the removed elements if requested. - if (elements != NULL) { - for (int i = 0; i < num; ++i) { - elements[i] = RepeatedPtrFieldBase::Mutable(i + start); - } - } - CloseGap(start, num); - } -} - -template -inline void RepeatedPtrField::Clear() { - RepeatedPtrFieldBase::Clear(); -} - -template -inline void RepeatedPtrField::MergeFrom( - const RepeatedPtrField& other) { - RepeatedPtrFieldBase::MergeFrom(other); -} - -template -inline void RepeatedPtrField::CopyFrom(const RepeatedPtrField& other) { - RepeatedPtrFieldBase::CopyFrom(other); -} - -template -inline typename RepeatedPtrField::iterator -RepeatedPtrField::erase(const_iterator position) { - return erase(position, position + 1); -} - -template -inline typename RepeatedPtrField::iterator -RepeatedPtrField::erase(const_iterator first, const_iterator last) { - size_type pos_offset = std::distance(cbegin(), first); - size_type last_offset = std::distance(cbegin(), last); - DeleteSubrange(pos_offset, last_offset - pos_offset); - return begin() + pos_offset; -} - -template -inline Element** RepeatedPtrField::mutable_data() { - return RepeatedPtrFieldBase::mutable_data(); -} - -template -inline const Element* const* RepeatedPtrField::data() const { - return RepeatedPtrFieldBase::data(); -} - -template -inline void RepeatedPtrField::Swap(RepeatedPtrField* other) { - if (this == other) return; - RepeatedPtrFieldBase::Swap(other); -} - -template -inline void RepeatedPtrField::UnsafeArenaSwap( - RepeatedPtrField* other) { - if (this == other) return; - RepeatedPtrFieldBase::InternalSwap(other); -} - -template -inline void RepeatedPtrField::SwapElements(int index1, int index2) { - RepeatedPtrFieldBase::SwapElements(index1, index2); -} - -template -inline Arena* RepeatedPtrField::GetArena() const { - return RepeatedPtrFieldBase::GetArena(); -} - -template -inline size_t RepeatedPtrField::SpaceUsedExcludingSelfLong() const { - return RepeatedPtrFieldBase::SpaceUsedExcludingSelfLong(); -} - -template -inline void RepeatedPtrField::AddAllocated(Element* value) { - RepeatedPtrFieldBase::AddAllocated(value); -} - -template -inline void RepeatedPtrField::UnsafeArenaAddAllocated(Element* value) { - RepeatedPtrFieldBase::UnsafeArenaAddAllocated(value); -} - -template -inline Element* RepeatedPtrField::ReleaseLast() { - return RepeatedPtrFieldBase::ReleaseLast(); -} - -template -inline Element* RepeatedPtrField::UnsafeArenaReleaseLast() { - return RepeatedPtrFieldBase::UnsafeArenaReleaseLast(); -} - -template -inline int RepeatedPtrField::ClearedCount() const { - return RepeatedPtrFieldBase::ClearedCount(); -} - -template -inline void RepeatedPtrField::AddCleared(Element* value) { - return RepeatedPtrFieldBase::AddCleared(value); -} - -template -inline Element* RepeatedPtrField::ReleaseCleared() { - return RepeatedPtrFieldBase::ReleaseCleared(); -} - -template -inline void RepeatedPtrField::Reserve(int new_size) { - return RepeatedPtrFieldBase::Reserve(new_size); -} - -template -inline int RepeatedPtrField::Capacity() const { - return RepeatedPtrFieldBase::Capacity(); -} - -// ------------------------------------------------------------------- - -namespace internal { - -// STL-like iterator implementation for RepeatedPtrField. You should not -// refer to this class directly; use RepeatedPtrField::iterator instead. -// -// The iterator for RepeatedPtrField, RepeatedPtrIterator, is -// very similar to iterator_ptr in util/gtl/iterator_adaptors.h, -// but adds random-access operators and is modified to wrap a void** base -// iterator (since RepeatedPtrField stores its array as a void* array and -// casting void** to T** would violate C++ aliasing rules). -// -// This code based on net/proto/proto-array-internal.h by Jeffrey Yasskin -// (jyasskin@google.com). -template -class RepeatedPtrIterator { - public: - using iterator = RepeatedPtrIterator; - using iterator_category = std::random_access_iterator_tag; - using value_type = typename std::remove_const::type; - using difference_type = std::ptrdiff_t; - using pointer = Element*; - using reference = Element&; - - RepeatedPtrIterator() : it_(NULL) {} - explicit RepeatedPtrIterator(void* const* it) : it_(it) {} - - // Allow "upcasting" from RepeatedPtrIterator to - // RepeatedPtrIterator. - template - RepeatedPtrIterator(const RepeatedPtrIterator& other) - : it_(other.it_) { - // Force a compiler error if the other type is not convertible to ours. - if (false) { - implicit_cast(static_cast(nullptr)); - } - } - - // dereferenceable - reference operator*() const { return *reinterpret_cast(*it_); } - pointer operator->() const { return &(operator*()); } - - // {inc,dec}rementable - iterator& operator++() { - ++it_; - return *this; - } - iterator operator++(int) { return iterator(it_++); } - iterator& operator--() { - --it_; - return *this; - } - iterator operator--(int) { return iterator(it_--); } - - // equality_comparable - bool operator==(const iterator& x) const { return it_ == x.it_; } - bool operator!=(const iterator& x) const { return it_ != x.it_; } - - // less_than_comparable - bool operator<(const iterator& x) const { return it_ < x.it_; } - bool operator<=(const iterator& x) const { return it_ <= x.it_; } - bool operator>(const iterator& x) const { return it_ > x.it_; } - bool operator>=(const iterator& x) const { return it_ >= x.it_; } - - // addable, subtractable - iterator& operator+=(difference_type d) { - it_ += d; - return *this; - } - friend iterator operator+(iterator it, const difference_type d) { - it += d; - return it; - } - friend iterator operator+(const difference_type d, iterator it) { - it += d; - return it; - } - iterator& operator-=(difference_type d) { - it_ -= d; - return *this; - } - friend iterator operator-(iterator it, difference_type d) { - it -= d; - return it; - } - - // indexable - reference operator[](difference_type d) const { return *(*this + d); } - - // random access iterator - difference_type operator-(const iterator& x) const { return it_ - x.it_; } - - private: - template - friend class RepeatedPtrIterator; - - // The internal iterator. - void* const* it_; -}; - -// Provide an iterator that operates on pointers to the underlying objects -// rather than the objects themselves as RepeatedPtrIterator does. -// Consider using this when working with stl algorithms that change -// the array. -// The VoidPtr template parameter holds the type-agnostic pointer value -// referenced by the iterator. It should either be "void *" for a mutable -// iterator, or "const void* const" for a constant iterator. -template -class RepeatedPtrOverPtrsIterator { - public: - using iterator = RepeatedPtrOverPtrsIterator; - using iterator_category = std::random_access_iterator_tag; - using value_type = typename std::remove_const::type; - using difference_type = std::ptrdiff_t; - using pointer = Element*; - using reference = Element&; - - RepeatedPtrOverPtrsIterator() : it_(NULL) {} - explicit RepeatedPtrOverPtrsIterator(VoidPtr* it) : it_(it) {} - - // dereferenceable - reference operator*() const { return *reinterpret_cast(it_); } - pointer operator->() const { return &(operator*()); } - - // {inc,dec}rementable - iterator& operator++() { - ++it_; - return *this; - } - iterator operator++(int) { return iterator(it_++); } - iterator& operator--() { - --it_; - return *this; - } - iterator operator--(int) { return iterator(it_--); } - - // equality_comparable - bool operator==(const iterator& x) const { return it_ == x.it_; } - bool operator!=(const iterator& x) const { return it_ != x.it_; } - - // less_than_comparable - bool operator<(const iterator& x) const { return it_ < x.it_; } - bool operator<=(const iterator& x) const { return it_ <= x.it_; } - bool operator>(const iterator& x) const { return it_ > x.it_; } - bool operator>=(const iterator& x) const { return it_ >= x.it_; } - - // addable, subtractable - iterator& operator+=(difference_type d) { - it_ += d; - return *this; - } - friend iterator operator+(iterator it, difference_type d) { - it += d; - return it; - } - friend iterator operator+(difference_type d, iterator it) { - it += d; - return it; - } - iterator& operator-=(difference_type d) { - it_ -= d; - return *this; - } - friend iterator operator-(iterator it, difference_type d) { - it -= d; - return it; - } - - // indexable - reference operator[](difference_type d) const { return *(*this + d); } - - // random access iterator - difference_type operator-(const iterator& x) const { return it_ - x.it_; } - - private: - template - friend class RepeatedPtrIterator; - - // The internal iterator. - VoidPtr* it_; -}; - -void RepeatedPtrFieldBase::InternalSwap(RepeatedPtrFieldBase* other) { - GOOGLE_DCHECK(this != other); - GOOGLE_DCHECK(GetArena() == other->GetArena()); - - // Swap all fields at once. - static_assert(std::is_standard_layout::value, - "offsetof() requires standard layout before c++17"); - internal::memswaprep_) - - offsetof(RepeatedPtrFieldBase, current_size_)>( - reinterpret_cast(this) + - offsetof(RepeatedPtrFieldBase, current_size_), - reinterpret_cast(other) + - offsetof(RepeatedPtrFieldBase, current_size_)); -} - -} // namespace internal - -template -inline typename RepeatedPtrField::iterator -RepeatedPtrField::begin() { - return iterator(raw_data()); -} -template -inline typename RepeatedPtrField::const_iterator -RepeatedPtrField::begin() const { - return iterator(raw_data()); -} -template -inline typename RepeatedPtrField::const_iterator -RepeatedPtrField::cbegin() const { - return begin(); -} -template -inline typename RepeatedPtrField::iterator -RepeatedPtrField::end() { - return iterator(raw_data() + size()); -} -template -inline typename RepeatedPtrField::const_iterator -RepeatedPtrField::end() const { - return iterator(raw_data() + size()); -} -template -inline typename RepeatedPtrField::const_iterator -RepeatedPtrField::cend() const { - return end(); -} - -template -inline typename RepeatedPtrField::pointer_iterator -RepeatedPtrField::pointer_begin() { - return pointer_iterator(raw_mutable_data()); -} -template -inline typename RepeatedPtrField::const_pointer_iterator -RepeatedPtrField::pointer_begin() const { - return const_pointer_iterator(const_cast(raw_data())); -} -template -inline typename RepeatedPtrField::pointer_iterator -RepeatedPtrField::pointer_end() { - return pointer_iterator(raw_mutable_data() + size()); -} -template -inline typename RepeatedPtrField::const_pointer_iterator -RepeatedPtrField::pointer_end() const { - return const_pointer_iterator( - const_cast(raw_data() + size())); -} - -// Iterators and helper functions that follow the spirit of the STL -// std::back_insert_iterator and std::back_inserter but are tailor-made -// for RepeatedField and RepeatedPtrField. Typical usage would be: -// -// std::copy(some_sequence.begin(), some_sequence.end(), -// RepeatedFieldBackInserter(proto.mutable_sequence())); -// -// Ported by johannes from util/gtl/proto-array-iterators.h - -namespace internal { -// A back inserter for RepeatedField objects. -template -class RepeatedFieldBackInsertIterator - : public std::iterator { - public: - explicit RepeatedFieldBackInsertIterator( - RepeatedField* const mutable_field) - : field_(mutable_field) {} - RepeatedFieldBackInsertIterator& operator=(const T& value) { - field_->Add(value); - return *this; - } - RepeatedFieldBackInsertIterator& operator*() { return *this; } - RepeatedFieldBackInsertIterator& operator++() { return *this; } - RepeatedFieldBackInsertIterator& operator++(int /* unused */) { - return *this; - } - - private: - RepeatedField* field_; -}; - -// A back inserter for RepeatedPtrField objects. -template -class RepeatedPtrFieldBackInsertIterator - : public std::iterator { - public: - RepeatedPtrFieldBackInsertIterator(RepeatedPtrField* const mutable_field) - : field_(mutable_field) {} - RepeatedPtrFieldBackInsertIterator& operator=(const T& value) { - *field_->Add() = value; - return *this; - } - RepeatedPtrFieldBackInsertIterator& operator=( - const T* const ptr_to_value) { - *field_->Add() = *ptr_to_value; - return *this; - } - RepeatedPtrFieldBackInsertIterator& operator=(T&& value) { - *field_->Add() = std::move(value); - return *this; - } - RepeatedPtrFieldBackInsertIterator& operator*() { return *this; } - RepeatedPtrFieldBackInsertIterator& operator++() { return *this; } - RepeatedPtrFieldBackInsertIterator& operator++(int /* unused */) { - return *this; - } - - private: - RepeatedPtrField* field_; -}; - -// A back inserter for RepeatedPtrFields that inserts by transferring ownership -// of a pointer. -template -class AllocatedRepeatedPtrFieldBackInsertIterator - : public std::iterator { - public: - explicit AllocatedRepeatedPtrFieldBackInsertIterator( - RepeatedPtrField* const mutable_field) - : field_(mutable_field) {} - AllocatedRepeatedPtrFieldBackInsertIterator& operator=( - T* const ptr_to_value) { - field_->AddAllocated(ptr_to_value); - return *this; - } - AllocatedRepeatedPtrFieldBackInsertIterator& operator*() { return *this; } - AllocatedRepeatedPtrFieldBackInsertIterator& operator++() { return *this; } - AllocatedRepeatedPtrFieldBackInsertIterator& operator++(int /* unused */) { - return *this; - } - - private: - RepeatedPtrField* field_; -}; - -// Almost identical to AllocatedRepeatedPtrFieldBackInsertIterator. This one -// uses the UnsafeArenaAddAllocated instead. -template -class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator - : public std::iterator { - public: - explicit UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator( - RepeatedPtrField* const mutable_field) - : field_(mutable_field) {} - UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator& operator=( - T const* const ptr_to_value) { - field_->UnsafeArenaAddAllocated(const_cast(ptr_to_value)); - return *this; - } - UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator& operator*() { - return *this; - } - UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator& operator++() { - return *this; - } - UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator& operator++( - int /* unused */) { - return *this; - } - - private: - RepeatedPtrField* field_; -}; - -} // namespace internal - -// Provides a back insert iterator for RepeatedField instances, -// similar to std::back_inserter(). -template -internal::RepeatedFieldBackInsertIterator RepeatedFieldBackInserter( - RepeatedField* const mutable_field) { - return internal::RepeatedFieldBackInsertIterator(mutable_field); -} - -// Provides a back insert iterator for RepeatedPtrField instances, -// similar to std::back_inserter(). -template -internal::RepeatedPtrFieldBackInsertIterator RepeatedPtrFieldBackInserter( - RepeatedPtrField* const mutable_field) { - return internal::RepeatedPtrFieldBackInsertIterator(mutable_field); -} - -// Special back insert iterator for RepeatedPtrField instances, just in -// case someone wants to write generic template code that can access both -// RepeatedFields and RepeatedPtrFields using a common name. -template -internal::RepeatedPtrFieldBackInsertIterator RepeatedFieldBackInserter( - RepeatedPtrField* const mutable_field) { - return internal::RepeatedPtrFieldBackInsertIterator(mutable_field); -} - -// Provides a back insert iterator for RepeatedPtrField instances -// similar to std::back_inserter() which transfers the ownership while -// copying elements. -template -internal::AllocatedRepeatedPtrFieldBackInsertIterator -AllocatedRepeatedPtrFieldBackInserter( - RepeatedPtrField* const mutable_field) { - return internal::AllocatedRepeatedPtrFieldBackInsertIterator( - mutable_field); -} - -// Similar to AllocatedRepeatedPtrFieldBackInserter, using -// UnsafeArenaAddAllocated instead of AddAllocated. -// This is slightly faster if that matters. It is also useful in legacy code -// that uses temporary ownership to avoid copies. Example: -// RepeatedPtrField temp_field; -// temp_field.AddAllocated(new T); -// ... // Do something with temp_field -// temp_field.ExtractSubrange(0, temp_field.size(), nullptr); -// If you put temp_field on the arena this fails, because the ownership -// transfers to the arena at the "AddAllocated" call and is not released anymore -// causing a double delete. Using UnsafeArenaAddAllocated prevents this. -template -internal::UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator -UnsafeArenaAllocatedRepeatedPtrFieldBackInserter( - RepeatedPtrField* const mutable_field) { - return internal::UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator( - mutable_field); -} - -// Extern declarations of common instantiations to reduce library bloat. -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField; -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField; -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField; -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField; -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField; -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField; -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField; -extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE - RepeatedPtrField; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_REPEATED_FIELD_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/bytestream.h b/third_party/protobuf-lite/google/protobuf/stubs/bytestream.h deleted file mode 100644 index c7a48dea5..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/bytestream.h +++ /dev/null @@ -1,351 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// This file declares the ByteSink and ByteSource abstract interfaces. These -// interfaces represent objects that consume (ByteSink) or produce (ByteSource) -// a sequence of bytes. Using these abstract interfaces in your APIs can help -// make your code work with a variety of input and output types. -// -// This file also declares the following commonly used implementations of these -// interfaces. -// -// ByteSink: -// UncheckedArrayByteSink Writes to an array, without bounds checking -// CheckedArrayByteSink Writes to an array, with bounds checking -// GrowingArrayByteSink Allocates and writes to a growable buffer -// StringByteSink Writes to an STL string -// NullByteSink Consumes a never-ending stream of bytes -// -// ByteSource: -// ArrayByteSource Reads from an array or string/StringPiece -// LimitedByteSource Limits the number of bytes read from an - -#ifndef GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_ -#define GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_ - -#include -#include - -#include -#include - -#include - -class CordByteSink; - -namespace google { -namespace protobuf { -namespace strings { - -// An abstract interface for an object that consumes a sequence of bytes. This -// interface offers a way to append data as well as a Flush() function. -// -// Example: -// -// string my_data; -// ... -// ByteSink* sink = ... -// sink->Append(my_data.data(), my_data.size()); -// sink->Flush(); -// -class PROTOBUF_EXPORT ByteSink { - public: - ByteSink() {} - virtual ~ByteSink() {} - - // Appends the "n" bytes starting at "bytes". - virtual void Append(const char* bytes, size_t n) = 0; - - // Flushes internal buffers. The default implementation does nothing. ByteSink - // subclasses may use internal buffers that require calling Flush() at the end - // of the stream. - virtual void Flush(); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ByteSink); -}; - -// An abstract interface for an object that produces a fixed-size sequence of -// bytes. -// -// Example: -// -// ByteSource* source = ... -// while (source->Available() > 0) { -// StringPiece data = source->Peek(); -// ... do something with "data" ... -// source->Skip(data.length()); -// } -// -class PROTOBUF_EXPORT ByteSource { - public: - ByteSource() {} - virtual ~ByteSource() {} - - // Returns the number of bytes left to read from the source. Available() - // should decrease by N each time Skip(N) is called. Available() may not - // increase. Available() returning 0 indicates that the ByteSource is - // exhausted. - // - // Note: Size() may have been a more appropriate name as it's more - // indicative of the fixed-size nature of a ByteSource. - virtual size_t Available() const = 0; - - // Returns a StringPiece of the next contiguous region of the source. Does not - // reposition the source. The returned region is empty iff Available() == 0. - // - // The returned region is valid until the next call to Skip() or until this - // object is destroyed, whichever occurs first. - // - // The length of the returned StringPiece will be <= Available(). - virtual StringPiece Peek() = 0; - - // Skips the next n bytes. Invalidates any StringPiece returned by a previous - // call to Peek(). - // - // REQUIRES: Available() >= n - virtual void Skip(size_t n) = 0; - - // Writes the next n bytes in this ByteSource to the given ByteSink, and - // advances this ByteSource past the copied bytes. The default implementation - // of this method just copies the bytes normally, but subclasses might - // override CopyTo to optimize certain cases. - // - // REQUIRES: Available() >= n - virtual void CopyTo(ByteSink* sink, size_t n); - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ByteSource); -}; - -// -// Some commonly used implementations of ByteSink -// - -// Implementation of ByteSink that writes to an unsized byte array. No -// bounds-checking is performed--it is the caller's responsibility to ensure -// that the destination array is large enough. -// -// Example: -// -// char buf[10]; -// UncheckedArrayByteSink sink(buf); -// sink.Append("hi", 2); // OK -// sink.Append(data, 100); // WOOPS! Overflows buf[10]. -// -class PROTOBUF_EXPORT UncheckedArrayByteSink : public ByteSink { - public: - explicit UncheckedArrayByteSink(char* dest) : dest_(dest) {} - virtual void Append(const char* data, size_t n) override; - - // Returns the current output pointer so that a caller can see how many bytes - // were produced. - // - // Note: this method is not part of the ByteSink interface. - char* CurrentDestination() const { return dest_; } - - private: - char* dest_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UncheckedArrayByteSink); -}; - -// Implementation of ByteSink that writes to a sized byte array. This sink will -// not write more than "capacity" bytes to outbuf. Once "capacity" bytes are -// appended, subsequent bytes will be ignored and Overflowed() will return true. -// Overflowed() does not cause a runtime error (i.e., it does not CHECK fail). -// -// Example: -// -// char buf[10]; -// CheckedArrayByteSink sink(buf, 10); -// sink.Append("hi", 2); // OK -// sink.Append(data, 100); // Will only write 8 more bytes -// -class PROTOBUF_EXPORT CheckedArrayByteSink : public ByteSink { - public: - CheckedArrayByteSink(char* outbuf, size_t capacity); - virtual void Append(const char* bytes, size_t n) override; - - // Returns the number of bytes actually written to the sink. - size_t NumberOfBytesWritten() const { return size_; } - - // Returns true if any bytes were discarded, i.e., if there was an - // attempt to write more than 'capacity' bytes. - bool Overflowed() const { return overflowed_; } - - private: - char* outbuf_; - const size_t capacity_; - size_t size_; - bool overflowed_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CheckedArrayByteSink); -}; - -// Implementation of ByteSink that allocates an internal buffer (a char array) -// and expands it as needed to accommodate appended data (similar to a string), -// and allows the caller to take ownership of the internal buffer via the -// GetBuffer() method. The buffer returned from GetBuffer() must be deleted by -// the caller with delete[]. GetBuffer() also sets the internal buffer to be -// empty, and subsequent appends to the sink will create a new buffer. The -// destructor will free the internal buffer if GetBuffer() was not called. -// -// Example: -// -// GrowingArrayByteSink sink(10); -// sink.Append("hi", 2); -// sink.Append(data, n); -// const char* buf = sink.GetBuffer(); // Ownership transferred -// delete[] buf; -// -class PROTOBUF_EXPORT GrowingArrayByteSink : public strings::ByteSink { - public: - explicit GrowingArrayByteSink(size_t estimated_size); - virtual ~GrowingArrayByteSink(); - virtual void Append(const char* bytes, size_t n) override; - - // Returns the allocated buffer, and sets nbytes to its size. The caller takes - // ownership of the buffer and must delete it with delete[]. - char* GetBuffer(size_t* nbytes); - - private: - void Expand(size_t amount); - void ShrinkToFit(); - - size_t capacity_; - char* buf_; - size_t size_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GrowingArrayByteSink); -}; - -// Implementation of ByteSink that appends to the given string. -// Existing contents of "dest" are not modified; new data is appended. -// -// Example: -// -// string dest = "Hello "; -// StringByteSink sink(&dest); -// sink.Append("World", 5); -// assert(dest == "Hello World"); -// -class PROTOBUF_EXPORT StringByteSink : public ByteSink { - public: - explicit StringByteSink(std::string* dest) : dest_(dest) {} - virtual void Append(const char* data, size_t n) override; - - private: - std::string* dest_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringByteSink); -}; - -// Implementation of ByteSink that discards all data. -// -// Example: -// -// NullByteSink sink; -// sink.Append(data, data.size()); // All data ignored. -// -class PROTOBUF_EXPORT NullByteSink : public ByteSink { - public: - NullByteSink() {} - void Append(const char* /*data*/, size_t /*n*/) override {} - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(NullByteSink); -}; - -// -// Some commonly used implementations of ByteSource -// - -// Implementation of ByteSource that reads from a StringPiece. -// -// Example: -// -// string data = "Hello"; -// ArrayByteSource source(data); -// assert(source.Available() == 5); -// assert(source.Peek() == "Hello"); -// -class PROTOBUF_EXPORT ArrayByteSource : public ByteSource { - public: - explicit ArrayByteSource(StringPiece s) : input_(s) {} - - virtual size_t Available() const override; - virtual StringPiece Peek() override; - virtual void Skip(size_t n) override; - - private: - StringPiece input_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayByteSource); -}; - -// Implementation of ByteSource that wraps another ByteSource, limiting the -// number of bytes returned. -// -// The caller maintains ownership of the underlying source, and may not use the -// underlying source while using the LimitByteSource object. The underlying -// source's pointer is advanced by n bytes every time this LimitByteSource -// object is advanced by n. -// -// Example: -// -// string data = "Hello World"; -// ArrayByteSource abs(data); -// assert(abs.Available() == data.size()); -// -// LimitByteSource limit(abs, 5); -// assert(limit.Available() == 5); -// assert(limit.Peek() == "Hello"); -// -class PROTOBUF_EXPORT LimitByteSource : public ByteSource { - public: - // Returns at most "limit" bytes from "source". - LimitByteSource(ByteSource* source, size_t limit); - - virtual size_t Available() const override; - virtual StringPiece Peek() override; - virtual void Skip(size_t n) override; - - // We override CopyTo so that we can forward to the underlying source, in - // case it has an efficient implementation of CopyTo. - virtual void CopyTo(ByteSink* sink, size_t n) override; - - private: - ByteSource* source_; - size_t limit_; -}; - -} // namespace strings -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_BYTESTREAM_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/callback.h b/third_party/protobuf-lite/google/protobuf/stubs/callback.h deleted file mode 100644 index 43d546d19..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/callback.h +++ /dev/null @@ -1,583 +0,0 @@ -#ifndef GOOGLE_PROTOBUF_STUBS_CALLBACK_H_ -#define GOOGLE_PROTOBUF_STUBS_CALLBACK_H_ - -#include - -#include - -#include - -// =================================================================== -// emulates google3/base/callback.h - -namespace google { -namespace protobuf { - -// Abstract interface for a callback. When calling an RPC, you must provide -// a Closure to call when the procedure completes. See the Service interface -// in service.h. -// -// To automatically construct a Closure which calls a particular function or -// method with a particular set of parameters, use the NewCallback() function. -// Example: -// void FooDone(const FooResponse* response) { -// ... -// } -// -// void CallFoo() { -// ... -// // When done, call FooDone() and pass it a pointer to the response. -// Closure* callback = NewCallback(&FooDone, response); -// // Make the call. -// service->Foo(controller, request, response, callback); -// } -// -// Example that calls a method: -// class Handler { -// public: -// ... -// -// void FooDone(const FooResponse* response) { -// ... -// } -// -// void CallFoo() { -// ... -// // When done, call FooDone() and pass it a pointer to the response. -// Closure* callback = NewCallback(this, &Handler::FooDone, response); -// // Make the call. -// service->Foo(controller, request, response, callback); -// } -// }; -// -// Currently NewCallback() supports binding zero, one, or two arguments. -// -// Callbacks created with NewCallback() automatically delete themselves when -// executed. They should be used when a callback is to be called exactly -// once (usually the case with RPC callbacks). If a callback may be called -// a different number of times (including zero), create it with -// NewPermanentCallback() instead. You are then responsible for deleting the -// callback (using the "delete" keyword as normal). -// -// Note that NewCallback() is a bit touchy regarding argument types. Generally, -// the values you provide for the parameter bindings must exactly match the -// types accepted by the callback function. For example: -// void Foo(std::string s); -// NewCallback(&Foo, "foo"); // WON'T WORK: const char* != string -// NewCallback(&Foo, std::string("foo")); // WORKS -// Also note that the arguments cannot be references: -// void Foo(const std::string& s); -// std::string my_str; -// NewCallback(&Foo, my_str); // WON'T WORK: Can't use references. -// However, correctly-typed pointers will work just fine. -class PROTOBUF_EXPORT Closure { - public: - Closure() {} - virtual ~Closure(); - - virtual void Run() = 0; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Closure); -}; - -template -class ResultCallback { - public: - ResultCallback() {} - virtual ~ResultCallback() {} - - virtual R Run() = 0; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback); -}; - -template -class PROTOBUF_EXPORT ResultCallback1 { - public: - ResultCallback1() {} - virtual ~ResultCallback1() {} - - virtual R Run(A1) = 0; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback1); -}; - -template -class PROTOBUF_EXPORT ResultCallback2 { - public: - ResultCallback2() {} - virtual ~ResultCallback2() {} - - virtual R Run(A1,A2) = 0; - - private: - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ResultCallback2); -}; - -namespace internal { - -class PROTOBUF_EXPORT FunctionClosure0 : public Closure { - public: - typedef void (*FunctionType)(); - - FunctionClosure0(FunctionType function, bool self_deleting) - : function_(function), self_deleting_(self_deleting) {} - ~FunctionClosure0(); - - void Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - function_(); - if (needs_delete) delete this; - } - - private: - FunctionType function_; - bool self_deleting_; -}; - -template -class MethodClosure0 : public Closure { - public: - typedef void (Class::*MethodType)(); - - MethodClosure0(Class* object, MethodType method, bool self_deleting) - : object_(object), method_(method), self_deleting_(self_deleting) {} - ~MethodClosure0() {} - - void Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - (object_->*method_)(); - if (needs_delete) delete this; - } - - private: - Class* object_; - MethodType method_; - bool self_deleting_; -}; - -template -class FunctionClosure1 : public Closure { - public: - typedef void (*FunctionType)(Arg1 arg1); - - FunctionClosure1(FunctionType function, bool self_deleting, - Arg1 arg1) - : function_(function), self_deleting_(self_deleting), - arg1_(arg1) {} - ~FunctionClosure1() {} - - void Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - function_(arg1_); - if (needs_delete) delete this; - } - - private: - FunctionType function_; - bool self_deleting_; - Arg1 arg1_; -}; - -template -class MethodClosure1 : public Closure { - public: - typedef void (Class::*MethodType)(Arg1 arg1); - - MethodClosure1(Class* object, MethodType method, bool self_deleting, - Arg1 arg1) - : object_(object), method_(method), self_deleting_(self_deleting), - arg1_(arg1) {} - ~MethodClosure1() {} - - void Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - (object_->*method_)(arg1_); - if (needs_delete) delete this; - } - - private: - Class* object_; - MethodType method_; - bool self_deleting_; - Arg1 arg1_; -}; - -template -class FunctionClosure2 : public Closure { - public: - typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2); - - FunctionClosure2(FunctionType function, bool self_deleting, - Arg1 arg1, Arg2 arg2) - : function_(function), self_deleting_(self_deleting), - arg1_(arg1), arg2_(arg2) {} - ~FunctionClosure2() {} - - void Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - function_(arg1_, arg2_); - if (needs_delete) delete this; - } - - private: - FunctionType function_; - bool self_deleting_; - Arg1 arg1_; - Arg2 arg2_; -}; - -template -class MethodClosure2 : public Closure { - public: - typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2); - - MethodClosure2(Class* object, MethodType method, bool self_deleting, - Arg1 arg1, Arg2 arg2) - : object_(object), method_(method), self_deleting_(self_deleting), - arg1_(arg1), arg2_(arg2) {} - ~MethodClosure2() {} - - void Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - (object_->*method_)(arg1_, arg2_); - if (needs_delete) delete this; - } - - private: - Class* object_; - MethodType method_; - bool self_deleting_; - Arg1 arg1_; - Arg2 arg2_; -}; - -template -class FunctionResultCallback_0_0 : public ResultCallback { - public: - typedef R (*FunctionType)(); - - FunctionResultCallback_0_0(FunctionType function, bool self_deleting) - : function_(function), self_deleting_(self_deleting) {} - ~FunctionResultCallback_0_0() {} - - R Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - R result = function_(); - if (needs_delete) delete this; - return result; - } - - private: - FunctionType function_; - bool self_deleting_; -}; - -template -class FunctionResultCallback_1_0 : public ResultCallback { - public: - typedef R (*FunctionType)(P1); - - FunctionResultCallback_1_0(FunctionType function, bool self_deleting, - P1 p1) - : function_(function), self_deleting_(self_deleting), p1_(p1) {} - ~FunctionResultCallback_1_0() {} - - R Run() override { - bool needs_delete = self_deleting_; // read in case callback deletes - R result = function_(p1_); - if (needs_delete) delete this; - return result; - } - - private: - FunctionType function_; - bool self_deleting_; - P1 p1_; -}; - -template -class FunctionResultCallback_0_1 : public ResultCallback1 { - public: - typedef R (*FunctionType)(Arg1 arg1); - - FunctionResultCallback_0_1(FunctionType function, bool self_deleting) - : function_(function), self_deleting_(self_deleting) {} - ~FunctionResultCallback_0_1() {} - - R Run(Arg1 a1) override { - bool needs_delete = self_deleting_; // read in case callback deletes - R result = function_(a1); - if (needs_delete) delete this; - return result; - } - - private: - FunctionType function_; - bool self_deleting_; -}; - -template -class FunctionResultCallback_1_1 : public ResultCallback1 { - public: - typedef R (*FunctionType)(P1, A1); - - FunctionResultCallback_1_1(FunctionType function, bool self_deleting, - P1 p1) - : function_(function), self_deleting_(self_deleting), p1_(p1) {} - ~FunctionResultCallback_1_1() {} - - R Run(A1 a1) override { - bool needs_delete = self_deleting_; // read in case callback deletes - R result = function_(p1_, a1); - if (needs_delete) delete this; - return result; - } - - private: - FunctionType function_; - bool self_deleting_; - P1 p1_; -}; - -template -struct InternalConstRef { - typedef typename std::remove_reference::type base_type; - typedef const base_type& type; -}; - -template -class MethodResultCallback_0_0 : public ResultCallback { - public: - typedef R (T::*MethodType)(); - MethodResultCallback_0_0(T* object, MethodType method, bool self_deleting) - : object_(object), - method_(method), - self_deleting_(self_deleting) {} - ~MethodResultCallback_0_0() {} - - R Run() { - bool needs_delete = self_deleting_; - R result = (object_->*method_)(); - if (needs_delete) delete this; - return result; - } - - private: - T* object_; - MethodType method_; - bool self_deleting_; -}; - -template -class MethodResultCallback_6_2 : public ResultCallback2 { - public: - typedef R (T::*MethodType)(P1, P2, P3, P4, P5, P6, A1, A2); - MethodResultCallback_6_2(T* object, MethodType method, bool self_deleting, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) - : object_(object), - method_(method), - self_deleting_(self_deleting), - p1_(p1), - p2_(p2), - p3_(p3), - p4_(p4), - p5_(p5), - p6_(p6) {} - ~MethodResultCallback_6_2() {} - - R Run(A1 a1, A2 a2) override { - bool needs_delete = self_deleting_; - R result = (object_->*method_)(p1_, p2_, p3_, p4_, p5_, p6_, a1, a2); - if (needs_delete) delete this; - return result; - } - - private: - T* object_; - MethodType method_; - bool self_deleting_; - typename std::remove_reference::type p1_; - typename std::remove_reference::type p2_; - typename std::remove_reference::type p3_; - typename std::remove_reference::type p4_; - typename std::remove_reference::type p5_; - typename std::remove_reference::type p6_; -}; - -} // namespace internal - -// See Closure. -inline Closure* NewCallback(void (*function)()) { - return new internal::FunctionClosure0(function, true); -} - -// See Closure. -inline Closure* NewPermanentCallback(void (*function)()) { - return new internal::FunctionClosure0(function, false); -} - -// See Closure. -template -inline Closure* NewCallback(Class* object, void (Class::*method)()) { - return new internal::MethodClosure0(object, method, true); -} - -// See Closure. -template -inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) { - return new internal::MethodClosure0(object, method, false); -} - -// See Closure. -template -inline Closure* NewCallback(void (*function)(Arg1), - Arg1 arg1) { - return new internal::FunctionClosure1(function, true, arg1); -} - -// See Closure. -template -inline Closure* NewPermanentCallback(void (*function)(Arg1), - Arg1 arg1) { - return new internal::FunctionClosure1(function, false, arg1); -} - -// See Closure. -template -inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1), - Arg1 arg1) { - return new internal::MethodClosure1(object, method, true, arg1); -} - -// See Closure. -template -inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1), - Arg1 arg1) { - return new internal::MethodClosure1(object, method, false, arg1); -} - -// See Closure. -template -inline Closure* NewCallback(void (*function)(Arg1, Arg2), - Arg1 arg1, Arg2 arg2) { - return new internal::FunctionClosure2( - function, true, arg1, arg2); -} - -// See Closure. -template -inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2), - Arg1 arg1, Arg2 arg2) { - return new internal::FunctionClosure2( - function, false, arg1, arg2); -} - -// See Closure. -template -inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2), - Arg1 arg1, Arg2 arg2) { - return new internal::MethodClosure2( - object, method, true, arg1, arg2); -} - -// See Closure. -template -inline Closure* NewPermanentCallback( - Class* object, void (Class::*method)(Arg1, Arg2), - Arg1 arg1, Arg2 arg2) { - return new internal::MethodClosure2( - object, method, false, arg1, arg2); -} - -// See ResultCallback -template -inline ResultCallback* NewCallback(R (*function)()) { - return new internal::FunctionResultCallback_0_0(function, true); -} - -// See ResultCallback -template -inline ResultCallback* NewPermanentCallback(R (*function)()) { - return new internal::FunctionResultCallback_0_0(function, false); -} - -// See ResultCallback -template -inline ResultCallback* NewCallback(R (*function)(P1), P1 p1) { - return new internal::FunctionResultCallback_1_0( - function, true, p1); -} - -// See ResultCallback -template -inline ResultCallback* NewPermanentCallback( - R (*function)(P1), P1 p1) { - return new internal::FunctionResultCallback_1_0( - function, false, p1); -} - -// See ResultCallback1 -template -inline ResultCallback1* NewCallback(R (*function)(A1)) { - return new internal::FunctionResultCallback_0_1(function, true); -} - -// See ResultCallback1 -template -inline ResultCallback1* NewPermanentCallback(R (*function)(A1)) { - return new internal::FunctionResultCallback_0_1(function, false); -} - -// See ResultCallback1 -template -inline ResultCallback1* NewCallback(R (*function)(P1, A1), P1 p1) { - return new internal::FunctionResultCallback_1_1( - function, true, p1); -} - -// See ResultCallback1 -template -inline ResultCallback1* NewPermanentCallback( - R (*function)(P1, A1), P1 p1) { - return new internal::FunctionResultCallback_1_1( - function, false, p1); -} - -// See MethodResultCallback_0_0 -template -inline ResultCallback* NewPermanentCallback( - T1* object, R (T2::*function)()) { - return new internal::MethodResultCallback_0_0(object, function, false); -} - -// See MethodResultCallback_6_2 -template -inline ResultCallback2* NewPermanentCallback( - T* object, R (T::*function)(P1, P2, P3, P4, P5, P6, A1, A2), - typename internal::InternalConstRef::type p1, - typename internal::InternalConstRef::type p2, - typename internal::InternalConstRef::type p3, - typename internal::InternalConstRef::type p4, - typename internal::InternalConstRef::type p5, - typename internal::InternalConstRef::type p6) { - return new internal::MethodResultCallback_6_2(object, function, false, - p1, p2, p3, p4, p5, p6); -} - -// A function which does nothing. Useful for creating no-op callbacks, e.g.: -// Closure* nothing = NewCallback(&DoNothing); -void PROTOBUF_EXPORT DoNothing(); - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_CALLBACK_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/casts.h b/third_party/protobuf-lite/google/protobuf/stubs/casts.h deleted file mode 100644 index d8a49cec3..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/casts.h +++ /dev/null @@ -1,139 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2014 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_CASTS_H__ -#define GOOGLE_PROTOBUF_CASTS_H__ - -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace internal { - -// Use implicit_cast as a safe version of static_cast or const_cast -// for upcasting in the type hierarchy (i.e. casting a pointer to Foo -// to a pointer to SuperclassOfFoo or casting a pointer to Foo to -// a const pointer to Foo). -// When you use implicit_cast, the compiler checks that the cast is safe. -// Such explicit implicit_casts are necessary in surprisingly many -// situations where C++ demands an exact type match instead of an -// argument type convertible to a target type. -// -// The From type can be inferred, so the preferred syntax for using -// implicit_cast is the same as for static_cast etc.: -// -// implicit_cast(expr) -// -// implicit_cast would have been part of the C++ standard library, -// but the proposal was submitted too late. It will probably make -// its way into the language in the future. -template -inline To implicit_cast(From const &f) { - return f; -} - -// When you upcast (that is, cast a pointer from type Foo to type -// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts -// always succeed. When you downcast (that is, cast a pointer from -// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because -// how do you know the pointer is really of type SubclassOfFoo? It -// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, -// when you downcast, you should use this macro. In debug mode, we -// use dynamic_cast<> to double-check the downcast is legal (we die -// if it's not). In normal mode, we do the efficient static_cast<> -// instead. Thus, it's important to test in debug mode to make sure -// the cast is legal! -// This is the only place in the code we should use dynamic_cast<>. -// In particular, you SHOULDN'T be using dynamic_cast<> in order to -// do RTTI (eg code like this: -// if (dynamic_cast(foo)) HandleASubclass1Object(foo); -// if (dynamic_cast(foo)) HandleASubclass2Object(foo); -// You should design the code some other way not to need this. - -template // use like this: down_cast(foo); -inline To down_cast(From* f) { // so we only accept pointers - // Ensures that To is a sub-type of From *. This test is here only - // for compile-time type checking, and has no overhead in an - // optimized build at run-time, as it will be optimized away - // completely. - if (false) { - implicit_cast(0); - } - -#if !defined(NDEBUG) && PROTOBUF_RTTI - assert(f == nullptr || dynamic_cast(f) != nullptr); // RTTI: debug mode only! -#endif - return static_cast(f); -} - -template // use like this: down_cast(foo); -inline To down_cast(From& f) { - typedef typename std::remove_reference::type* ToAsPointer; - // Ensures that To is a sub-type of From *. This test is here only - // for compile-time type checking, and has no overhead in an - // optimized build at run-time, as it will be optimized away - // completely. - if (false) { - implicit_cast(0); - } - -#if !defined(NDEBUG) && PROTOBUF_RTTI - // RTTI: debug mode only! - assert(dynamic_cast(&f) != nullptr); -#endif - return *static_cast(&f); -} - -template -inline To bit_cast(const From& from) { - GOOGLE_COMPILE_ASSERT(sizeof(From) == sizeof(To), - bit_cast_with_different_sizes); - To dest; - memcpy(&dest, &from, sizeof(dest)); - return dest; -} - -} // namespace internal - -// We made these internal so that they would show up as such in the docs, -// but we don't want to stick "internal::" in front of them everywhere. -using internal::implicit_cast; -using internal::down_cast; -using internal::bit_cast; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_CASTS_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/common.h b/third_party/protobuf-lite/google/protobuf/stubs/common.h deleted file mode 100644 index 2d210d727..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/common.h +++ /dev/null @@ -1,202 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) and others -// -// Contains basic types and utilities used by the rest of the library. - -#ifndef GOOGLE_PROTOBUF_COMMON_H__ -#define GOOGLE_PROTOBUF_COMMON_H__ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#ifndef PROTOBUF_USE_EXCEPTIONS -#if defined(_MSC_VER) && defined(_CPPUNWIND) - #define PROTOBUF_USE_EXCEPTIONS 1 -#elif defined(__EXCEPTIONS) - #define PROTOBUF_USE_EXCEPTIONS 1 -#else - #define PROTOBUF_USE_EXCEPTIONS 0 -#endif -#endif - -#if PROTOBUF_USE_EXCEPTIONS -#include -#endif -#if defined(__APPLE__) -#include // for TARGET_OS_IPHONE -#endif - -#if defined(__ANDROID__) || defined(GOOGLE_PROTOBUF_OS_ANDROID) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || defined(GOOGLE_PROTOBUF_OS_IPHONE) -#include -#endif - -#include - -namespace std {} - -namespace google { -namespace protobuf { -namespace internal { - -// Some of these constants are macros rather than const ints so that they can -// be used in #if directives. - -// The current version, represented as a single integer to make comparison -// easier: major * 10^6 + minor * 10^3 + micro -#define GOOGLE_PROTOBUF_VERSION 3014000 - -// A suffix string for alpha, beta or rc releases. Empty for stable releases. -#define GOOGLE_PROTOBUF_VERSION_SUFFIX "" - -// The minimum header version which works with the current version of -// the library. This constant should only be used by protoc's C++ code -// generator. -static const int kMinHeaderVersionForLibrary = 3014000; - -// The minimum protoc version which works with the current version of the -// headers. -#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3014000 - -// The minimum header version which works with the current version of -// protoc. This constant should only be used in VerifyVersion(). -static const int kMinHeaderVersionForProtoc = 3014000; - -// Verifies that the headers and libraries are compatible. Use the macro -// below to call this. -void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion, - const char* filename); - -// Converts a numeric version number to a string. -std::string PROTOBUF_EXPORT VersionString(int version); - -} // namespace internal - -// Place this macro in your main() function (or somewhere before you attempt -// to use the protobuf library) to verify that the version you link against -// matches the headers you compiled against. If a version mismatch is -// detected, the process will abort. -#define GOOGLE_PROTOBUF_VERIFY_VERSION \ - ::google::protobuf::internal::VerifyVersion( \ - GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION, \ - __FILE__) - - -// =================================================================== -// from google3/util/utf8/public/unilib.h - -class StringPiece; -namespace internal { - -// Checks if the buffer contains structurally-valid UTF-8. Implemented in -// structurally_valid.cc. -PROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len); - -inline bool IsStructurallyValidUTF8(StringPiece str) { - return IsStructurallyValidUTF8(str.data(), static_cast(str.length())); -} - -// Returns initial number of bytes of structurally valid UTF-8. -PROTOBUF_EXPORT int UTF8SpnStructurallyValid(StringPiece str); - -// Coerce UTF-8 byte string in src_str to be -// a structurally-valid equal-length string by selectively -// overwriting illegal bytes with replace_char (typically ' ' or '?'). -// replace_char must be legal printable 7-bit Ascii 0x20..0x7e. -// src_str is read-only. -// -// Returns pointer to output buffer, src_str.data() if no changes were made, -// or idst if some bytes were changed. idst is allocated by the caller -// and must be at least as big as src_str -// -// Optimized for: all structurally valid and no byte copying is done. -// -PROTOBUF_EXPORT char* UTF8CoerceToStructurallyValid(StringPiece str, char* dst, - char replace_char); - -} // namespace internal - -// This lives in message_lite.h now, but we leave this here for any users that -// #include common.h and not message_lite.h. -PROTOBUF_EXPORT void ShutdownProtobufLibrary(); - -namespace internal { - -// Strongly references the given variable such that the linker will be forced -// to pull in this variable's translation unit. -template -void StrongReference(const T& var) { - auto volatile unused = &var; - (void)&unused; // Use address to avoid an extra load of "unused". -} - -} // namespace internal - -#if PROTOBUF_USE_EXCEPTIONS -class FatalException : public std::exception { - public: - FatalException(const char* filename, int line, const std::string& message) - : filename_(filename), line_(line), message_(message) {} - virtual ~FatalException() throw(); - - virtual const char* what() const throw(); - - const char* filename() const { return filename_; } - int line() const { return line_; } - const std::string& message() const { return message_; } - - private: - const char* filename_; - const int line_; - const std::string message_; -}; -#endif - -// This is at the end of the file instead of the beginning to work around a bug -// in some versions of MSVC. -using std::string; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_COMMON_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/hash.h b/third_party/protobuf-lite/google/protobuf/stubs/hash.h deleted file mode 100644 index a7ec06807..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/hash.h +++ /dev/null @@ -1,114 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) - -#ifndef GOOGLE_PROTOBUF_STUBS_HASH_H__ -#define GOOGLE_PROTOBUF_STUBS_HASH_H__ - -#include -#include -#include -#include - -# define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START \ - namespace google { \ - namespace protobuf { -# define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END }} - -namespace google { -namespace protobuf { - -template -struct hash : public std::hash {}; - -template -struct hash { - inline size_t operator()(const Key* key) const { - return reinterpret_cast(key); - } -}; - -// Unlike the old SGI version, the TR1 "hash" does not special-case char*. So, -// we go ahead and provide our own implementation. -template <> -struct hash { - inline size_t operator()(const char* str) const { - size_t result = 0; - for (; *str != '\0'; str++) { - result = 5 * result + static_cast(*str); - } - return result; - } -}; - -template<> -struct hash { - size_t operator()(bool x) const { - return static_cast(x); - } -}; - -template <> -struct hash { - inline size_t operator()(const std::string& key) const { - return hash()(key.c_str()); - } - - static const size_t bucket_size = 4; - static const size_t min_buckets = 8; - inline bool operator()(const std::string& a, const std::string& b) const { - return a < b; - } -}; - -template -struct hash > { - inline size_t operator()(const std::pair& key) const { - size_t first_hash = hash()(key.first); - size_t second_hash = hash()(key.second); - - // FIXME(kenton): What is the best way to compute this hash? I have - // no idea! This seems a bit better than an XOR. - return first_hash * ((1 << 16) - 1) + second_hash; - } - - static const size_t bucket_size = 4; - static const size_t min_buckets = 8; - inline bool operator()(const std::pair& a, - const std::pair& b) const { - return a < b; - } -}; - -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_STUBS_HASH_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/int128.h b/third_party/protobuf-lite/google/protobuf/stubs/int128.h deleted file mode 100644 index dc70d96eb..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/int128.h +++ /dev/null @@ -1,387 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_STUBS_INT128_H_ -#define GOOGLE_PROTOBUF_STUBS_INT128_H_ - -#include - -#include - -#include - -namespace google { -namespace protobuf { - -struct uint128_pod; - -// TODO(xiaofeng): Define GOOGLE_PROTOBUF_HAS_CONSTEXPR when constexpr is -// available. -#ifdef GOOGLE_PROTOBUF_HAS_CONSTEXPR -# define UINT128_CONSTEXPR constexpr -#else -# define UINT128_CONSTEXPR -#endif - -// An unsigned 128-bit integer type. Thread-compatible. -class PROTOBUF_EXPORT uint128 { - public: - UINT128_CONSTEXPR uint128(); // Sets to 0, but don't trust on this behavior. - UINT128_CONSTEXPR uint128(uint64 top, uint64 bottom); -#ifndef SWIG - UINT128_CONSTEXPR uint128(int bottom); - UINT128_CONSTEXPR uint128(uint32 bottom); // Top 96 bits = 0 -#endif - UINT128_CONSTEXPR uint128(uint64 bottom); // hi_ = 0 - UINT128_CONSTEXPR uint128(const uint128_pod &val); - - // Trivial copy constructor, assignment operator and destructor. - - void Initialize(uint64 top, uint64 bottom); - - // Arithmetic operators. - uint128& operator+=(const uint128& b); - uint128& operator-=(const uint128& b); - uint128& operator*=(const uint128& b); - // Long division/modulo for uint128. - uint128& operator/=(const uint128& b); - uint128& operator%=(const uint128& b); - uint128 operator++(int); - uint128 operator--(int); - uint128& operator<<=(int); - uint128& operator>>=(int); - uint128& operator&=(const uint128& b); - uint128& operator|=(const uint128& b); - uint128& operator^=(const uint128& b); - uint128& operator++(); - uint128& operator--(); - - friend uint64 Uint128Low64(const uint128& v); - friend uint64 Uint128High64(const uint128& v); - - // We add "std::" to avoid including all of port.h. - PROTOBUF_EXPORT friend std::ostream& operator<<(std::ostream& o, - const uint128& b); - - private: - static void DivModImpl(uint128 dividend, uint128 divisor, - uint128* quotient_ret, uint128* remainder_ret); - - // Little-endian memory order optimizations can benefit from - // having lo_ first, hi_ last. - // See util/endian/endian.h and Load128/Store128 for storing a uint128. - uint64 lo_; - uint64 hi_; - - // Not implemented, just declared for catching automatic type conversions. - uint128(uint8); - uint128(uint16); - uint128(float v); - uint128(double v); -}; - -// This is a POD form of uint128 which can be used for static variables which -// need to be operated on as uint128. -struct uint128_pod { - // Note: The ordering of fields is different than 'class uint128' but the - // same as its 2-arg constructor. This enables more obvious initialization - // of static instances, which is the primary reason for this struct in the - // first place. This does not seem to defeat any optimizations wrt - // operations involving this struct. - uint64 hi; - uint64 lo; -}; - -PROTOBUF_EXPORT extern const uint128_pod kuint128max; - -// allow uint128 to be logged -PROTOBUF_EXPORT extern std::ostream& operator<<(std::ostream& o, - const uint128& b); - -// Methods to access low and high pieces of 128-bit value. -// Defined externally from uint128 to facilitate conversion -// to native 128-bit types when compilers support them. -inline uint64 Uint128Low64(const uint128& v) { return v.lo_; } -inline uint64 Uint128High64(const uint128& v) { return v.hi_; } - -// TODO: perhaps it would be nice to have int128, a signed 128-bit type? - -// -------------------------------------------------------------------------- -// Implementation details follow -// -------------------------------------------------------------------------- -inline bool operator==(const uint128& lhs, const uint128& rhs) { - return (Uint128Low64(lhs) == Uint128Low64(rhs) && - Uint128High64(lhs) == Uint128High64(rhs)); -} -inline bool operator!=(const uint128& lhs, const uint128& rhs) { - return !(lhs == rhs); -} - -inline UINT128_CONSTEXPR uint128::uint128() : lo_(0), hi_(0) {} -inline UINT128_CONSTEXPR uint128::uint128(uint64 top, uint64 bottom) - : lo_(bottom), hi_(top) {} -inline UINT128_CONSTEXPR uint128::uint128(const uint128_pod& v) - : lo_(v.lo), hi_(v.hi) {} -inline UINT128_CONSTEXPR uint128::uint128(uint64 bottom) - : lo_(bottom), hi_(0) {} -#ifndef SWIG -inline UINT128_CONSTEXPR uint128::uint128(uint32 bottom) - : lo_(bottom), hi_(0) {} -inline UINT128_CONSTEXPR uint128::uint128(int bottom) - : lo_(bottom), hi_(static_cast((bottom < 0) ? -1 : 0)) {} -#endif - -#undef UINT128_CONSTEXPR - -inline void uint128::Initialize(uint64 top, uint64 bottom) { - hi_ = top; - lo_ = bottom; -} - -// Comparison operators. - -#define CMP128(op) \ -inline bool operator op(const uint128& lhs, const uint128& rhs) { \ - return (Uint128High64(lhs) == Uint128High64(rhs)) ? \ - (Uint128Low64(lhs) op Uint128Low64(rhs)) : \ - (Uint128High64(lhs) op Uint128High64(rhs)); \ -} - -CMP128(<) -CMP128(>) -CMP128(>=) -CMP128(<=) - -#undef CMP128 - -// Unary operators - -inline uint128 operator-(const uint128& val) { - const uint64 hi_flip = ~Uint128High64(val); - const uint64 lo_flip = ~Uint128Low64(val); - const uint64 lo_add = lo_flip + 1; - if (lo_add < lo_flip) { - return uint128(hi_flip + 1, lo_add); - } - return uint128(hi_flip, lo_add); -} - -inline bool operator!(const uint128& val) { - return !Uint128High64(val) && !Uint128Low64(val); -} - -// Logical operators. - -inline uint128 operator~(const uint128& val) { - return uint128(~Uint128High64(val), ~Uint128Low64(val)); -} - -#define LOGIC128(op) \ -inline uint128 operator op(const uint128& lhs, const uint128& rhs) { \ - return uint128(Uint128High64(lhs) op Uint128High64(rhs), \ - Uint128Low64(lhs) op Uint128Low64(rhs)); \ -} - -LOGIC128(|) -LOGIC128(&) -LOGIC128(^) - -#undef LOGIC128 - -#define LOGICASSIGN128(op) \ -inline uint128& uint128::operator op(const uint128& other) { \ - hi_ op other.hi_; \ - lo_ op other.lo_; \ - return *this; \ -} - -LOGICASSIGN128(|=) -LOGICASSIGN128(&=) -LOGICASSIGN128(^=) - -#undef LOGICASSIGN128 - -// Shift operators. - -inline uint128 operator<<(const uint128& val, int amount) { - // uint64 shifts of >= 64 are undefined, so we will need some special-casing. - if (amount < 64) { - if (amount == 0) { - return val; - } - uint64 new_hi = (Uint128High64(val) << amount) | - (Uint128Low64(val) >> (64 - amount)); - uint64 new_lo = Uint128Low64(val) << amount; - return uint128(new_hi, new_lo); - } else if (amount < 128) { - return uint128(Uint128Low64(val) << (amount - 64), 0); - } else { - return uint128(0, 0); - } -} - -inline uint128 operator>>(const uint128& val, int amount) { - // uint64 shifts of >= 64 are undefined, so we will need some special-casing. - if (amount < 64) { - if (amount == 0) { - return val; - } - uint64 new_hi = Uint128High64(val) >> amount; - uint64 new_lo = (Uint128Low64(val) >> amount) | - (Uint128High64(val) << (64 - amount)); - return uint128(new_hi, new_lo); - } else if (amount < 128) { - return uint128(0, Uint128High64(val) >> (amount - 64)); - } else { - return uint128(0, 0); - } -} - -inline uint128& uint128::operator<<=(int amount) { - // uint64 shifts of >= 64 are undefined, so we will need some special-casing. - if (amount < 64) { - if (amount != 0) { - hi_ = (hi_ << amount) | (lo_ >> (64 - amount)); - lo_ = lo_ << amount; - } - } else if (amount < 128) { - hi_ = lo_ << (amount - 64); - lo_ = 0; - } else { - hi_ = 0; - lo_ = 0; - } - return *this; -} - -inline uint128& uint128::operator>>=(int amount) { - // uint64 shifts of >= 64 are undefined, so we will need some special-casing. - if (amount < 64) { - if (amount != 0) { - lo_ = (lo_ >> amount) | (hi_ << (64 - amount)); - hi_ = hi_ >> amount; - } - } else if (amount < 128) { - lo_ = hi_ >> (amount - 64); - hi_ = 0; - } else { - lo_ = 0; - hi_ = 0; - } - return *this; -} - -inline uint128 operator+(const uint128& lhs, const uint128& rhs) { - return uint128(lhs) += rhs; -} - -inline uint128 operator-(const uint128& lhs, const uint128& rhs) { - return uint128(lhs) -= rhs; -} - -inline uint128 operator*(const uint128& lhs, const uint128& rhs) { - return uint128(lhs) *= rhs; -} - -inline uint128 operator/(const uint128& lhs, const uint128& rhs) { - return uint128(lhs) /= rhs; -} - -inline uint128 operator%(const uint128& lhs, const uint128& rhs) { - return uint128(lhs) %= rhs; -} - -inline uint128& uint128::operator+=(const uint128& b) { - hi_ += b.hi_; - uint64 lolo = lo_ + b.lo_; - if (lolo < lo_) - ++hi_; - lo_ = lolo; - return *this; -} - -inline uint128& uint128::operator-=(const uint128& b) { - hi_ -= b.hi_; - if (b.lo_ > lo_) - --hi_; - lo_ -= b.lo_; - return *this; -} - -inline uint128& uint128::operator*=(const uint128& b) { - uint64 a96 = hi_ >> 32; - uint64 a64 = hi_ & 0xffffffffu; - uint64 a32 = lo_ >> 32; - uint64 a00 = lo_ & 0xffffffffu; - uint64 b96 = b.hi_ >> 32; - uint64 b64 = b.hi_ & 0xffffffffu; - uint64 b32 = b.lo_ >> 32; - uint64 b00 = b.lo_ & 0xffffffffu; - // multiply [a96 .. a00] x [b96 .. b00] - // terms higher than c96 disappear off the high side - // terms c96 and c64 are safe to ignore carry bit - uint64 c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96; - uint64 c64 = a64 * b00 + a32 * b32 + a00 * b64; - this->hi_ = (c96 << 32) + c64; - this->lo_ = 0; - // add terms after this one at a time to capture carry - *this += uint128(a32 * b00) << 32; - *this += uint128(a00 * b32) << 32; - *this += a00 * b00; - return *this; -} - -inline uint128 uint128::operator++(int) { - uint128 tmp(*this); - *this += 1; - return tmp; -} - -inline uint128 uint128::operator--(int) { - uint128 tmp(*this); - *this -= 1; - return tmp; -} - -inline uint128& uint128::operator++() { - *this += 1; - return *this; -} - -inline uint128& uint128::operator--() { - *this -= 1; - return *this; -} - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_INT128_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/logging.h b/third_party/protobuf-lite/google/protobuf/stubs/logging.h deleted file mode 100644 index f37048d67..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/logging.h +++ /dev/null @@ -1,241 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_STUBS_LOGGING_H_ -#define GOOGLE_PROTOBUF_STUBS_LOGGING_H_ - -#include -#include - -#include - -// =================================================================== -// emulates google3/base/logging.h - -namespace google { -namespace protobuf { - -enum LogLevel { - LOGLEVEL_INFO, // Informational. This is never actually used by - // libprotobuf. - LOGLEVEL_WARNING, // Warns about issues that, although not technically a - // problem now, could cause problems in the future. For - // example, a // warning will be printed when parsing a - // message that is near the message size limit. - LOGLEVEL_ERROR, // An error occurred which should never happen during - // normal use. - LOGLEVEL_FATAL, // An error occurred from which the library cannot - // recover. This usually indicates a programming error - // in the code which calls the library, especially when - // compiled in debug mode. - -#ifdef NDEBUG - LOGLEVEL_DFATAL = LOGLEVEL_ERROR -#else - LOGLEVEL_DFATAL = LOGLEVEL_FATAL -#endif -}; - -class StringPiece; -namespace util { -class Status; -} -class uint128; -namespace internal { - -class LogFinisher; - -class PROTOBUF_EXPORT LogMessage { - public: - LogMessage(LogLevel level, const char* filename, int line); - ~LogMessage(); - - LogMessage& operator<<(const std::string& value); - LogMessage& operator<<(const char* value); - LogMessage& operator<<(char value); - LogMessage& operator<<(int value); - LogMessage& operator<<(uint value); - LogMessage& operator<<(long value); - LogMessage& operator<<(unsigned long value); - LogMessage& operator<<(long long value); - LogMessage& operator<<(unsigned long long value); - LogMessage& operator<<(double value); - LogMessage& operator<<(void* value); - LogMessage& operator<<(const StringPiece& value); - LogMessage& operator<<(const util::Status& status); - LogMessage& operator<<(const uint128& value); - - private: - friend class LogFinisher; - void Finish(); - - LogLevel level_; - const char* filename_; - int line_; - std::string message_; -}; - -// Used to make the entire "LOG(BLAH) << etc." expression have a void return -// type and print a newline after each message. -class PROTOBUF_EXPORT LogFinisher { - public: - void operator=(LogMessage& other); -}; - -template -bool IsOk(T status) { return status.ok(); } -template<> -inline bool IsOk(bool status) { return status; } - -} // namespace internal - -// Undef everything in case we're being mixed with some other Google library -// which already defined them itself. Presumably all Google libraries will -// support the same syntax for these so it should not be a big deal if they -// end up using our definitions instead. -#undef GOOGLE_LOG -#undef GOOGLE_LOG_IF - -#undef GOOGLE_CHECK -#undef GOOGLE_CHECK_OK -#undef GOOGLE_CHECK_EQ -#undef GOOGLE_CHECK_NE -#undef GOOGLE_CHECK_LT -#undef GOOGLE_CHECK_LE -#undef GOOGLE_CHECK_GT -#undef GOOGLE_CHECK_GE -#undef GOOGLE_CHECK_NOTNULL - -#undef GOOGLE_DLOG -#undef GOOGLE_DCHECK -#undef GOOGLE_DCHECK_OK -#undef GOOGLE_DCHECK_EQ -#undef GOOGLE_DCHECK_NE -#undef GOOGLE_DCHECK_LT -#undef GOOGLE_DCHECK_LE -#undef GOOGLE_DCHECK_GT -#undef GOOGLE_DCHECK_GE - -#define GOOGLE_LOG(LEVEL) \ - ::google::protobuf::internal::LogFinisher() = \ - ::google::protobuf::internal::LogMessage( \ - ::google::protobuf::LOGLEVEL_##LEVEL, __FILE__, __LINE__) -#define GOOGLE_LOG_IF(LEVEL, CONDITION) \ - !(CONDITION) ? (void)0 : GOOGLE_LOG(LEVEL) - -#define GOOGLE_CHECK(EXPRESSION) \ - GOOGLE_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": " -#define GOOGLE_CHECK_OK(A) GOOGLE_CHECK(::google::protobuf::internal::IsOk(A)) -#define GOOGLE_CHECK_EQ(A, B) GOOGLE_CHECK((A) == (B)) -#define GOOGLE_CHECK_NE(A, B) GOOGLE_CHECK((A) != (B)) -#define GOOGLE_CHECK_LT(A, B) GOOGLE_CHECK((A) < (B)) -#define GOOGLE_CHECK_LE(A, B) GOOGLE_CHECK((A) <= (B)) -#define GOOGLE_CHECK_GT(A, B) GOOGLE_CHECK((A) > (B)) -#define GOOGLE_CHECK_GE(A, B) GOOGLE_CHECK((A) >= (B)) - -namespace internal { -template -T* CheckNotNull(const char* /* file */, int /* line */, - const char* name, T* val) { - if (val == nullptr) { - GOOGLE_LOG(FATAL) << name; - } - return val; -} -} // namespace internal -#define GOOGLE_CHECK_NOTNULL(A) \ - ::google::protobuf::internal::CheckNotNull( \ - __FILE__, __LINE__, "'" #A "' must not be nullptr", (A)) - -#ifdef NDEBUG - -#define GOOGLE_DLOG(LEVEL) GOOGLE_LOG_IF(LEVEL, false) - -#define GOOGLE_DCHECK(EXPRESSION) while(false) GOOGLE_CHECK(EXPRESSION) -#define GOOGLE_DCHECK_OK(E) GOOGLE_DCHECK(::google::protobuf::internal::IsOk(E)) -#define GOOGLE_DCHECK_EQ(A, B) GOOGLE_DCHECK((A) == (B)) -#define GOOGLE_DCHECK_NE(A, B) GOOGLE_DCHECK((A) != (B)) -#define GOOGLE_DCHECK_LT(A, B) GOOGLE_DCHECK((A) < (B)) -#define GOOGLE_DCHECK_LE(A, B) GOOGLE_DCHECK((A) <= (B)) -#define GOOGLE_DCHECK_GT(A, B) GOOGLE_DCHECK((A) > (B)) -#define GOOGLE_DCHECK_GE(A, B) GOOGLE_DCHECK((A) >= (B)) - -#else // NDEBUG - -#define GOOGLE_DLOG GOOGLE_LOG - -#define GOOGLE_DCHECK GOOGLE_CHECK -#define GOOGLE_DCHECK_OK GOOGLE_CHECK_OK -#define GOOGLE_DCHECK_EQ GOOGLE_CHECK_EQ -#define GOOGLE_DCHECK_NE GOOGLE_CHECK_NE -#define GOOGLE_DCHECK_LT GOOGLE_CHECK_LT -#define GOOGLE_DCHECK_LE GOOGLE_CHECK_LE -#define GOOGLE_DCHECK_GT GOOGLE_CHECK_GT -#define GOOGLE_DCHECK_GE GOOGLE_CHECK_GE - -#endif // !NDEBUG - -typedef void LogHandler(LogLevel level, const char* filename, int line, - const std::string& message); - -// The protobuf library sometimes writes warning and error messages to -// stderr. These messages are primarily useful for developers, but may -// also help end users figure out a problem. If you would prefer that -// these messages be sent somewhere other than stderr, call SetLogHandler() -// to set your own handler. This returns the old handler. Set the handler -// to nullptr to ignore log messages (but see also LogSilencer, below). -// -// Obviously, SetLogHandler is not thread-safe. You should only call it -// at initialization time, and probably not from library code. If you -// simply want to suppress log messages temporarily (e.g. because you -// have some code that tends to trigger them frequently and you know -// the warnings are not important to you), use the LogSilencer class -// below. -PROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func); - -// Create a LogSilencer if you want to temporarily suppress all log -// messages. As long as any LogSilencer objects exist, non-fatal -// log messages will be discarded (the current LogHandler will *not* -// be called). Constructing a LogSilencer is thread-safe. You may -// accidentally suppress log messages occurring in another thread, but -// since messages are generally for debugging purposes only, this isn't -// a big deal. If you want to intercept log messages, use SetLogHandler(). -class PROTOBUF_EXPORT LogSilencer { - public: - LogSilencer(); - ~LogSilencer(); -}; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_LOGGING_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/macros.h b/third_party/protobuf-lite/google/protobuf/stubs/macros.h deleted file mode 100644 index c556d0223..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/macros.h +++ /dev/null @@ -1,120 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_MACROS_H__ -#define GOOGLE_PROTOBUF_MACROS_H__ - -#include - -namespace google { -namespace protobuf { - -#undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS -#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ - TypeName(const TypeName&); \ - void operator=(const TypeName&) - -#undef GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS -#define GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ - TypeName(); \ - TypeName(const TypeName&); \ - void operator=(const TypeName&) - -// =================================================================== -// from google3/base/basictypes.h - -// The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr. -// The expression is a compile-time constant, and therefore can be -// used in defining new arrays, for example. -// -// GOOGLE_ARRAYSIZE catches a few type errors. If you see a compiler error -// -// "warning: division by zero in ..." -// -// when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer. -// You should only use GOOGLE_ARRAYSIZE on statically allocated arrays. -// -// The following comments are on the implementation details, and can -// be ignored by the users. -// -// ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in -// the array) and sizeof(*(arr)) (the # of bytes in one array -// element). If the former is divisible by the latter, perhaps arr is -// indeed an array, in which case the division result is the # of -// elements in the array. Otherwise, arr cannot possibly be an array, -// and we generate a compiler error to prevent the code from -// compiling. -// -// Since the size of bool is implementation-defined, we need to cast -// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final -// result has type size_t. -// -// This macro is not perfect as it wrongfully accepts certain -// pointers, namely where the pointer size is divisible by the pointee -// size. Since all our code has to go through a 32-bit compiler, -// where a pointer is 4 bytes, this means all pointers to a type whose -// size is 3 or greater than 4 will be (righteously) rejected. -// -// Kudos to Jorg Brown for this simple and elegant implementation. - -#undef GOOGLE_ARRAYSIZE -#define GOOGLE_ARRAYSIZE(a) \ - ((sizeof(a) / sizeof(*(a))) / \ - static_cast(!(sizeof(a) % sizeof(*(a))))) - -// The COMPILE_ASSERT macro can be used to verify that a compile time -// expression is true. For example, you could use it to verify the -// size of a static array: -// -// COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES, -// content_type_names_incorrect_size); -// -// or to make sure a struct is smaller than a certain size: -// -// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); -// -// The second argument to the macro is the name of the variable. If -// the expression is false, most compilers will issue a warning/error -// containing the name of the variable. - -namespace internal { - -template -struct CompileAssert { -}; - -} // namespace internal - -#define GOOGLE_COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) - -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_MACROS_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/map_util.h b/third_party/protobuf-lite/google/protobuf/stubs/map_util.h deleted file mode 100644 index 24e098ad1..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/map_util.h +++ /dev/null @@ -1,769 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2014 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// from google3/util/gtl/map_util.h -// Author: Anton Carver - -#ifndef GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__ -#define GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__ - -#include -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { -// Local implementation of RemoveConst to avoid including base/type_traits.h. -template struct RemoveConst { typedef T type; }; -template struct RemoveConst : RemoveConst {}; -} // namespace internal - -// -// Find*() -// - -// Returns a const reference to the value associated with the given key if it -// exists. Crashes otherwise. -// -// This is intended as a replacement for operator[] as an rvalue (for reading) -// when the key is guaranteed to exist. -// -// operator[] for lookup is discouraged for several reasons: -// * It has a side-effect of inserting missing keys -// * It is not thread-safe (even when it is not inserting, it can still -// choose to resize the underlying storage) -// * It invalidates iterators (when it chooses to resize) -// * It default constructs a value object even if it doesn't need to -// -// This version assumes the key is printable, and includes it in the fatal log -// message. -template -const typename Collection::value_type::second_type& -FindOrDie(const Collection& collection, - const typename Collection::value_type::first_type& key) { - typename Collection::const_iterator it = collection.find(key); - GOOGLE_CHECK(it != collection.end()) << "Map key not found: " << key; - return it->second; -} - -// Same as above, but returns a non-const reference. -template -typename Collection::value_type::second_type& -FindOrDie(Collection& collection, // NOLINT - const typename Collection::value_type::first_type& key) { - typename Collection::iterator it = collection.find(key); - GOOGLE_CHECK(it != collection.end()) << "Map key not found: " << key; - return it->second; -} - -// Same as FindOrDie above, but doesn't log the key on failure. -template -const typename Collection::value_type::second_type& -FindOrDieNoPrint(const Collection& collection, - const typename Collection::value_type::first_type& key) { - typename Collection::const_iterator it = collection.find(key); - GOOGLE_CHECK(it != collection.end()) << "Map key not found"; - return it->second; -} - -// Same as above, but returns a non-const reference. -template -typename Collection::value_type::second_type& -FindOrDieNoPrint(Collection& collection, // NOLINT - const typename Collection::value_type::first_type& key) { - typename Collection::iterator it = collection.find(key); - GOOGLE_CHECK(it != collection.end()) << "Map key not found"; - return it->second; -} - -// Returns a const reference to the value associated with the given key if it -// exists, otherwise returns a const reference to the provided default value. -// -// WARNING: If a temporary object is passed as the default "value," -// this function will return a reference to that temporary object, -// which will be destroyed at the end of the statement. A common -// example: if you have a map with string values, and you pass a char* -// as the default "value," either use the returned value immediately -// or store it in a string (not string&). -// Details: http://go/findwithdefault -template -const typename Collection::value_type::second_type& -FindWithDefault(const Collection& collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& value) { - typename Collection::const_iterator it = collection.find(key); - if (it == collection.end()) { - return value; - } - return it->second; -} - -// Returns a pointer to the const value associated with the given key if it -// exists, or nullptr otherwise. -template -const typename Collection::value_type::second_type* -FindOrNull(const Collection& collection, - const typename Collection::value_type::first_type& key) { - typename Collection::const_iterator it = collection.find(key); - if (it == collection.end()) { - return 0; - } - return &it->second; -} - -// Same as above but returns a pointer to the non-const value. -template -typename Collection::value_type::second_type* -FindOrNull(Collection& collection, // NOLINT - const typename Collection::value_type::first_type& key) { - typename Collection::iterator it = collection.find(key); - if (it == collection.end()) { - return 0; - } - return &it->second; -} - -// Returns the pointer value associated with the given key. If none is found, -// nullptr is returned. The function is designed to be used with a map of keys to -// pointers. -// -// This function does not distinguish between a missing key and a key mapped -// to nullptr. -template -typename Collection::value_type::second_type -FindPtrOrNull(const Collection& collection, - const typename Collection::value_type::first_type& key) { - typename Collection::const_iterator it = collection.find(key); - if (it == collection.end()) { - return typename Collection::value_type::second_type(); - } - return it->second; -} - -// Same as above, except takes non-const reference to collection. -// -// This function is needed for containers that propagate constness to the -// pointee, such as boost::ptr_map. -template -typename Collection::value_type::second_type -FindPtrOrNull(Collection& collection, // NOLINT - const typename Collection::value_type::first_type& key) { - typename Collection::iterator it = collection.find(key); - if (it == collection.end()) { - return typename Collection::value_type::second_type(); - } - return it->second; -} - -// Finds the pointer value associated with the given key in a map whose values -// are linked_ptrs. Returns nullptr if key is not found. -template -typename Collection::value_type::second_type::element_type* -FindLinkedPtrOrNull(const Collection& collection, - const typename Collection::value_type::first_type& key) { - typename Collection::const_iterator it = collection.find(key); - if (it == collection.end()) { - return 0; - } - // Since linked_ptr::get() is a const member returning a non const, - // we do not need a version of this function taking a non const collection. - return it->second.get(); -} - -// Same as above, but dies if the key is not found. -template -typename Collection::value_type::second_type::element_type& -FindLinkedPtrOrDie(const Collection& collection, - const typename Collection::value_type::first_type& key) { - typename Collection::const_iterator it = collection.find(key); - GOOGLE_CHECK(it != collection.end()) << "key not found: " << key; - // Since linked_ptr::operator*() is a const member returning a non const, - // we do not need a version of this function taking a non const collection. - return *it->second; -} - -// Finds the value associated with the given key and copies it to *value (if not -// nullptr). Returns false if the key was not found, true otherwise. -template -bool FindCopy(const Collection& collection, - const Key& key, - Value* const value) { - typename Collection::const_iterator it = collection.find(key); - if (it == collection.end()) { - return false; - } - if (value) { - *value = it->second; - } - return true; -} - -// -// Contains*() -// - -// Returns true if and only if the given collection contains the given key. -template -bool ContainsKey(const Collection& collection, const Key& key) { - return collection.find(key) != collection.end(); -} - -// Returns true if and only if the given collection contains the given key-value -// pair. -template -bool ContainsKeyValuePair(const Collection& collection, - const Key& key, - const Value& value) { - typedef typename Collection::const_iterator const_iterator; - std::pair range = collection.equal_range(key); - for (const_iterator it = range.first; it != range.second; ++it) { - if (it->second == value) { - return true; - } - } - return false; -} - -// -// Insert*() -// - -// Inserts the given key-value pair into the collection. Returns true if and -// only if the key from the given pair didn't previously exist. Otherwise, the -// value in the map is replaced with the value from the given pair. -template -bool InsertOrUpdate(Collection* const collection, - const typename Collection::value_type& vt) { - std::pair ret = collection->insert(vt); - if (!ret.second) { - // update - ret.first->second = vt.second; - return false; - } - return true; -} - -// Same as above, except that the key and value are passed separately. -template -bool InsertOrUpdate(Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& value) { - return InsertOrUpdate( - collection, typename Collection::value_type(key, value)); -} - -// Inserts/updates all the key-value pairs from the range defined by the -// iterators "first" and "last" into the given collection. -template -void InsertOrUpdateMany(Collection* const collection, - InputIterator first, InputIterator last) { - for (; first != last; ++first) { - InsertOrUpdate(collection, *first); - } -} - -// Change the value associated with a particular key in a map or hash_map -// of the form map which owns the objects pointed to by the -// value pointers. If there was an existing value for the key, it is deleted. -// True indicates an insert took place, false indicates an update + delete. -template -bool InsertAndDeleteExisting( - Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& value) { - std::pair ret = - collection->insert(typename Collection::value_type(key, value)); - if (!ret.second) { - delete ret.first->second; - ret.first->second = value; - return false; - } - return true; -} - -// Inserts the given key and value into the given collection if and only if the -// given key did NOT already exist in the collection. If the key previously -// existed in the collection, the value is not changed. Returns true if the -// key-value pair was inserted; returns false if the key was already present. -template -bool InsertIfNotPresent(Collection* const collection, - const typename Collection::value_type& vt) { - return collection->insert(vt).second; -} - -// Same as above except the key and value are passed separately. -template -bool InsertIfNotPresent( - Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& value) { - return InsertIfNotPresent( - collection, typename Collection::value_type(key, value)); -} - -// Same as above except dies if the key already exists in the collection. -template -void InsertOrDie(Collection* const collection, - const typename Collection::value_type& value) { - GOOGLE_CHECK(InsertIfNotPresent(collection, value)) - << "duplicate value: " << value; -} - -// Same as above except doesn't log the value on error. -template -void InsertOrDieNoPrint(Collection* const collection, - const typename Collection::value_type& value) { - GOOGLE_CHECK(InsertIfNotPresent(collection, value)) << "duplicate value."; -} - -// Inserts the key-value pair into the collection. Dies if key was already -// present. -template -void InsertOrDie(Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& data) { - GOOGLE_CHECK(InsertIfNotPresent(collection, key, data)) - << "duplicate key: " << key; -} - -// Same as above except doesn't log the key on error. -template -void InsertOrDieNoPrint( - Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& data) { - GOOGLE_CHECK(InsertIfNotPresent(collection, key, data)) << "duplicate key."; -} - -// Inserts a new key and default-initialized value. Dies if the key was already -// present. Returns a reference to the value. Example usage: -// -// map m; -// SomeProto& proto = InsertKeyOrDie(&m, 3); -// proto.set_field("foo"); -template -typename Collection::value_type::second_type& InsertKeyOrDie( - Collection* const collection, - const typename Collection::value_type::first_type& key) { - typedef typename Collection::value_type value_type; - std::pair res = - collection->insert(value_type(key, typename value_type::second_type())); - GOOGLE_CHECK(res.second) << "duplicate key: " << key; - return res.first->second; -} - -// -// Lookup*() -// - -// Looks up a given key and value pair in a collection and inserts the key-value -// pair if it's not already present. Returns a reference to the value associated -// with the key. -template -typename Collection::value_type::second_type& -LookupOrInsert(Collection* const collection, - const typename Collection::value_type& vt) { - return collection->insert(vt).first->second; -} - -// Same as above except the key-value are passed separately. -template -typename Collection::value_type::second_type& -LookupOrInsert(Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& value) { - return LookupOrInsert( - collection, typename Collection::value_type(key, value)); -} - -// Counts the number of equivalent elements in the given "sequence", and stores -// the results in "count_map" with element as the key and count as the value. -// -// Example: -// vector v = {"a", "b", "c", "a", "b"}; -// map m; -// AddTokenCounts(v, 1, &m); -// assert(m["a"] == 2); -// assert(m["b"] == 2); -// assert(m["c"] == 1); -template -void AddTokenCounts( - const Sequence& sequence, - const typename Collection::value_type::second_type& increment, - Collection* const count_map) { - for (typename Sequence::const_iterator it = sequence.begin(); - it != sequence.end(); ++it) { - typename Collection::value_type::second_type& value = - LookupOrInsert(count_map, *it, - typename Collection::value_type::second_type()); - value += increment; - } -} - -// Returns a reference to the value associated with key. If not found, a value -// is default constructed on the heap and added to the map. -// -// This function is useful for containers of the form map, where -// inserting a new key, value pair involves constructing a new heap-allocated -// Value, and storing a pointer to that in the collection. -template -typename Collection::value_type::second_type& -LookupOrInsertNew(Collection* const collection, - const typename Collection::value_type::first_type& key) { - typedef typename std::iterator_traits< - typename Collection::value_type::second_type>::value_type Element; - std::pair ret = - collection->insert(typename Collection::value_type( - key, - static_cast(nullptr))); - if (ret.second) { - ret.first->second = new Element(); - } - return ret.first->second; -} - -// Same as above but constructs the value using the single-argument constructor -// and the given "arg". -template -typename Collection::value_type::second_type& -LookupOrInsertNew(Collection* const collection, - const typename Collection::value_type::first_type& key, - const Arg& arg) { - typedef typename std::iterator_traits< - typename Collection::value_type::second_type>::value_type Element; - std::pair ret = - collection->insert(typename Collection::value_type( - key, - static_cast(nullptr))); - if (ret.second) { - ret.first->second = new Element(arg); - } - return ret.first->second; -} - -// Lookup of linked/shared pointers is used in two scenarios: -// -// Use LookupOrInsertNewLinkedPtr if the container owns the elements. -// In this case it is fine working with the raw pointer as long as it is -// guaranteed that no other thread can delete/update an accessed element. -// A mutex will need to lock the container operation as well as the use -// of the returned elements. Finding an element may be performed using -// FindLinkedPtr*(). -// -// Use LookupOrInsertNewSharedPtr if the container does not own the elements -// for their whole lifetime. This is typically the case when a reader allows -// parallel updates to the container. In this case a Mutex only needs to lock -// container operations, but all element operations must be performed on the -// shared pointer. Finding an element must be performed using FindPtr*() and -// cannot be done with FindLinkedPtr*() even though it compiles. - -// Lookup a key in a map or hash_map whose values are linked_ptrs. If it is -// missing, set collection[key].reset(new Value::element_type) and return that. -// Value::element_type must be default constructable. -template -typename Collection::value_type::second_type::element_type* -LookupOrInsertNewLinkedPtr( - Collection* const collection, - const typename Collection::value_type::first_type& key) { - typedef typename Collection::value_type::second_type Value; - std::pair ret = - collection->insert(typename Collection::value_type(key, Value())); - if (ret.second) { - ret.first->second.reset(new typename Value::element_type); - } - return ret.first->second.get(); -} - -// A variant of LookupOrInsertNewLinkedPtr where the value is constructed using -// a single-parameter constructor. Note: the constructor argument is computed -// even if it will not be used, so only values cheap to compute should be passed -// here. On the other hand it does not matter how expensive the construction of -// the actual stored value is, as that only occurs if necessary. -template -typename Collection::value_type::second_type::element_type* -LookupOrInsertNewLinkedPtr( - Collection* const collection, - const typename Collection::value_type::first_type& key, - const Arg& arg) { - typedef typename Collection::value_type::second_type Value; - std::pair ret = - collection->insert(typename Collection::value_type(key, Value())); - if (ret.second) { - ret.first->second.reset(new typename Value::element_type(arg)); - } - return ret.first->second.get(); -} - -// Lookup a key in a map or hash_map whose values are shared_ptrs. If it is -// missing, set collection[key].reset(new Value::element_type). Unlike -// LookupOrInsertNewLinkedPtr, this function returns the shared_ptr instead of -// the raw pointer. Value::element_type must be default constructable. -template -typename Collection::value_type::second_type& -LookupOrInsertNewSharedPtr( - Collection* const collection, - const typename Collection::value_type::first_type& key) { - typedef typename Collection::value_type::second_type SharedPtr; - typedef typename Collection::value_type::second_type::element_type Element; - std::pair ret = - collection->insert(typename Collection::value_type(key, SharedPtr())); - if (ret.second) { - ret.first->second.reset(new Element()); - } - return ret.first->second; -} - -// A variant of LookupOrInsertNewSharedPtr where the value is constructed using -// a single-parameter constructor. Note: the constructor argument is computed -// even if it will not be used, so only values cheap to compute should be passed -// here. On the other hand it does not matter how expensive the construction of -// the actual stored value is, as that only occurs if necessary. -template -typename Collection::value_type::second_type& -LookupOrInsertNewSharedPtr( - Collection* const collection, - const typename Collection::value_type::first_type& key, - const Arg& arg) { - typedef typename Collection::value_type::second_type SharedPtr; - typedef typename Collection::value_type::second_type::element_type Element; - std::pair ret = - collection->insert(typename Collection::value_type(key, SharedPtr())); - if (ret.second) { - ret.first->second.reset(new Element(arg)); - } - return ret.first->second; -} - -// -// Misc Utility Functions -// - -// Updates the value associated with the given key. If the key was not already -// present, then the key-value pair are inserted and "previous" is unchanged. If -// the key was already present, the value is updated and "*previous" will -// contain a copy of the old value. -// -// InsertOrReturnExisting has complementary behavior that returns the -// address of an already existing value, rather than updating it. -template -bool UpdateReturnCopy(Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& value, - typename Collection::value_type::second_type* previous) { - std::pair ret = - collection->insert(typename Collection::value_type(key, value)); - if (!ret.second) { - // update - if (previous) { - *previous = ret.first->second; - } - ret.first->second = value; - return true; - } - return false; -} - -// Same as above except that the key and value are passed as a pair. -template -bool UpdateReturnCopy(Collection* const collection, - const typename Collection::value_type& vt, - typename Collection::value_type::second_type* previous) { - std::pair ret = collection->insert(vt); - if (!ret.second) { - // update - if (previous) { - *previous = ret.first->second; - } - ret.first->second = vt.second; - return true; - } - return false; -} - -// Tries to insert the given key-value pair into the collection. Returns nullptr if -// the insert succeeds. Otherwise, returns a pointer to the existing value. -// -// This complements UpdateReturnCopy in that it allows to update only after -// verifying the old value and still insert quickly without having to look up -// twice. Unlike UpdateReturnCopy this also does not come with the issue of an -// undefined previous* in case new data was inserted. -template -typename Collection::value_type::second_type* InsertOrReturnExisting( - Collection* const collection, const typename Collection::value_type& vt) { - std::pair ret = collection->insert(vt); - if (ret.second) { - return nullptr; // Inserted, no existing previous value. - } else { - return &ret.first->second; // Return address of already existing value. - } -} - -// Same as above, except for explicit key and data. -template -typename Collection::value_type::second_type* InsertOrReturnExisting( - Collection* const collection, - const typename Collection::value_type::first_type& key, - const typename Collection::value_type::second_type& data) { - return InsertOrReturnExisting(collection, - typename Collection::value_type(key, data)); -} - -// Erases the collection item identified by the given key, and returns the value -// associated with that key. It is assumed that the value (i.e., the -// mapped_type) is a pointer. Returns nullptr if the key was not found in the -// collection. -// -// Examples: -// map my_map; -// -// One line cleanup: -// delete EraseKeyReturnValuePtr(&my_map, "abc"); -// -// Use returned value: -// std::unique_ptr value_ptr( -// EraseKeyReturnValuePtr(&my_map, "abc")); -// if (value_ptr.get()) -// value_ptr->DoSomething(); -// -template -typename Collection::value_type::second_type EraseKeyReturnValuePtr( - Collection* const collection, - const typename Collection::value_type::first_type& key) { - typename Collection::iterator it = collection->find(key); - if (it == collection->end()) { - return nullptr; - } - typename Collection::value_type::second_type v = it->second; - collection->erase(it); - return v; -} - -// Inserts all the keys from map_container into key_container, which must -// support insert(MapContainer::key_type). -// -// Note: any initial contents of the key_container are not cleared. -template -void InsertKeysFromMap(const MapContainer& map_container, - KeyContainer* key_container) { - GOOGLE_CHECK(key_container != nullptr); - for (typename MapContainer::const_iterator it = map_container.begin(); - it != map_container.end(); ++it) { - key_container->insert(it->first); - } -} - -// Appends all the keys from map_container into key_container, which must -// support push_back(MapContainer::key_type). -// -// Note: any initial contents of the key_container are not cleared. -template -void AppendKeysFromMap(const MapContainer& map_container, - KeyContainer* key_container) { - GOOGLE_CHECK(key_container != nullptr); - for (typename MapContainer::const_iterator it = map_container.begin(); - it != map_container.end(); ++it) { - key_container->push_back(it->first); - } -} - -// A more specialized overload of AppendKeysFromMap to optimize reallocations -// for the common case in which we're appending keys to a vector and hence can -// (and sometimes should) call reserve() first. -// -// (It would be possible to play SFINAE games to call reserve() for any -// container that supports it, but this seems to get us 99% of what we need -// without the complexity of a SFINAE-based solution.) -template -void AppendKeysFromMap(const MapContainer& map_container, - std::vector* key_container) { - GOOGLE_CHECK(key_container != nullptr); - // We now have the opportunity to call reserve(). Calling reserve() every - // time is a bad idea for some use cases: libstdc++'s implementation of - // vector<>::reserve() resizes the vector's backing store to exactly the - // given size (unless it's already at least that big). Because of this, - // the use case that involves appending a lot of small maps (total size - // N) one by one to a vector would be O(N^2). But never calling reserve() - // loses the opportunity to improve the use case of adding from a large - // map to an empty vector (this improves performance by up to 33%). A - // number of heuristics are possible; see the discussion in - // cl/34081696. Here we use the simplest one. - if (key_container->empty()) { - key_container->reserve(map_container.size()); - } - for (typename MapContainer::const_iterator it = map_container.begin(); - it != map_container.end(); ++it) { - key_container->push_back(it->first); - } -} - -// Inserts all the values from map_container into value_container, which must -// support push_back(MapContainer::mapped_type). -// -// Note: any initial contents of the value_container are not cleared. -template -void AppendValuesFromMap(const MapContainer& map_container, - ValueContainer* value_container) { - GOOGLE_CHECK(value_container != nullptr); - for (typename MapContainer::const_iterator it = map_container.begin(); - it != map_container.end(); ++it) { - value_container->push_back(it->second); - } -} - -// A more specialized overload of AppendValuesFromMap to optimize reallocations -// for the common case in which we're appending values to a vector and hence -// can (and sometimes should) call reserve() first. -// -// (It would be possible to play SFINAE games to call reserve() for any -// container that supports it, but this seems to get us 99% of what we need -// without the complexity of a SFINAE-based solution.) -template -void AppendValuesFromMap(const MapContainer& map_container, - std::vector* value_container) { - GOOGLE_CHECK(value_container != nullptr); - // See AppendKeysFromMap for why this is done. - if (value_container->empty()) { - value_container->reserve(map_container.size()); - } - for (typename MapContainer::const_iterator it = map_container.begin(); - it != map_container.end(); ++it) { - value_container->push_back(it->second); - } -} - -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_STUBS_MAP_UTIL_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/mutex.h b/third_party/protobuf-lite/google/protobuf/stubs/mutex.h deleted file mode 100644 index b222ff746..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/mutex.h +++ /dev/null @@ -1,186 +0,0 @@ -// Copyright (c) 2006, Google Inc. -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_STUBS_MUTEX_H_ -#define GOOGLE_PROTOBUF_STUBS_MUTEX_H_ - -#include - -#ifdef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - -#include - -// GetMessage conflicts with GeneratedMessageReflection::GetMessage(). -#ifdef GetMessage -#undef GetMessage -#endif - -#endif - -#include - -// Define thread-safety annotations for use below, if we are building with -// Clang. -#if defined(__clang__) && !defined(SWIG) -#define GOOGLE_PROTOBUF_ACQUIRE(...) \ - __attribute__((acquire_capability(__VA_ARGS__))) -#define GOOGLE_PROTOBUF_RELEASE(...) \ - __attribute__((release_capability(__VA_ARGS__))) -#define GOOGLE_PROTOBUF_CAPABILITY(x) __attribute__((capability(x))) -#else -#define GOOGLE_PROTOBUF_ACQUIRE(...) -#define GOOGLE_PROTOBUF_RELEASE(...) -#define GOOGLE_PROTOBUF_CAPABILITY(x) -#endif - -#include - -// =================================================================== -// emulates google3/base/mutex.h -namespace google { -namespace protobuf { -namespace internal { - -#define GOOGLE_PROTOBUF_LINKER_INITIALIZED - -#ifdef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - -// This class is a lightweight replacement for std::mutex on Windows platforms. -// std::mutex does not work on Windows XP SP2 with the latest VC++ libraries, -// because it utilizes the Concurrency Runtime that is only supported on Windows -// XP SP3 and above. -class PROTOBUF_EXPORT CriticalSectionLock { - public: - CriticalSectionLock() { InitializeCriticalSection(&critical_section_); } - ~CriticalSectionLock() { DeleteCriticalSection(&critical_section_); } - void lock() { EnterCriticalSection(&critical_section_); } - void unlock() { LeaveCriticalSection(&critical_section_); } - - private: - CRITICAL_SECTION critical_section_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CriticalSectionLock); -}; - -#endif - -// Mutex is a natural type to wrap. As both google and other organization have -// specialized mutexes. gRPC also provides an injection mechanism for custom -// mutexes. -class GOOGLE_PROTOBUF_CAPABILITY("mutex") PROTOBUF_EXPORT WrappedMutex { - public: - WrappedMutex() = default; - void Lock() GOOGLE_PROTOBUF_ACQUIRE() { mu_.lock(); } - void Unlock() GOOGLE_PROTOBUF_RELEASE() { mu_.unlock(); } - // Crash if this Mutex is not held exclusively by this thread. - // May fail to crash when it should; will never crash when it should not. - void AssertHeld() const {} - - private: -#ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - std::mutex mu_; -#else // ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP - CriticalSectionLock mu_; -#endif // #ifndef GOOGLE_PROTOBUF_SUPPORT_WINDOWS_XP -}; - -using Mutex = WrappedMutex; - -// MutexLock(mu) acquires mu when constructed and releases it when destroyed. -class PROTOBUF_EXPORT MutexLock { - public: - explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); } - ~MutexLock() { this->mu_->Unlock(); } - private: - Mutex *const mu_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock); -}; - -// TODO(kenton): Implement these? Hard to implement portably. -typedef MutexLock ReaderMutexLock; -typedef MutexLock WriterMutexLock; - -// MutexLockMaybe is like MutexLock, but is a no-op when mu is nullptr. -class PROTOBUF_EXPORT MutexLockMaybe { - public: - explicit MutexLockMaybe(Mutex *mu) : - mu_(mu) { if (this->mu_ != nullptr) { this->mu_->Lock(); } } - ~MutexLockMaybe() { if (this->mu_ != nullptr) { this->mu_->Unlock(); } } - private: - Mutex *const mu_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe); -}; - -#if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) -template -class ThreadLocalStorage { - public: - ThreadLocalStorage() { - pthread_key_create(&key_, &ThreadLocalStorage::Delete); - } - ~ThreadLocalStorage() { - pthread_key_delete(key_); - } - T* Get() { - T* result = static_cast(pthread_getspecific(key_)); - if (result == nullptr) { - result = new T(); - pthread_setspecific(key_, result); - } - return result; - } - private: - static void Delete(void* value) { - delete static_cast(value); - } - pthread_key_t key_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage); -}; -#endif - -} // namespace internal - -// We made these internal so that they would show up as such in the docs, -// but we don't want to stick "internal::" in front of them everywhere. -using internal::Mutex; -using internal::MutexLock; -using internal::ReaderMutexLock; -using internal::WriterMutexLock; -using internal::MutexLockMaybe; - -} // namespace protobuf -} // namespace google - -#undef GOOGLE_PROTOBUF_ACQUIRE -#undef GOOGLE_PROTOBUF_RELEASE - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_MUTEX_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/once.h b/third_party/protobuf-lite/google/protobuf/stubs/once.h deleted file mode 100644 index 070d36d19..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/once.h +++ /dev/null @@ -1,55 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_STUBS_ONCE_H__ -#define GOOGLE_PROTOBUF_STUBS_ONCE_H__ - -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { - -using once_flag = std::once_flag; -template -void call_once(Args&&... args ) { - std::call_once(std::forward(args)...); -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_ONCE_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/platform_macros.h b/third_party/protobuf-lite/google/protobuf/stubs/platform_macros.h deleted file mode 100644 index ce1b1e365..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/platform_macros.h +++ /dev/null @@ -1,134 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2012 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ -#define GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ - -#define GOOGLE_PROTOBUF_PLATFORM_ERROR \ -#error "Host platform was not detected as supported by protobuf" - -// Processor architecture detection. For more info on what's defined, see: -// http://msdn.microsoft.com/en-us/library/b0084kay.aspx -// http://www.agner.org/optimize/calling_conventions.pdf -// or with gcc, run: "echo | gcc -E -dM -" -#if defined(_M_X64) || defined(__x86_64__) -#define GOOGLE_PROTOBUF_ARCH_X64 1 -#define GOOGLE_PROTOBUF_ARCH_64_BIT 1 -#elif defined(_M_IX86) || defined(__i386__) -#define GOOGLE_PROTOBUF_ARCH_IA32 1 -#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -#elif defined(__QNX__) -#define GOOGLE_PROTOBUF_ARCH_ARM_QNX 1 -#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -#elif defined(_M_ARM) || defined(__ARMEL__) -#define GOOGLE_PROTOBUF_ARCH_ARM 1 -#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -#elif defined(_M_ARM64) -#define GOOGLE_PROTOBUF_ARCH_ARM 1 -#define GOOGLE_PROTOBUF_ARCH_64_BIT 1 -#elif defined(__aarch64__) -#define GOOGLE_PROTOBUF_ARCH_AARCH64 1 -#define GOOGLE_PROTOBUF_ARCH_64_BIT 1 -#elif defined(__mips__) -#if defined(__LP64__) -#define GOOGLE_PROTOBUF_ARCH_MIPS64 1 -#define GOOGLE_PROTOBUF_ARCH_64_BIT 1 -#else -#define GOOGLE_PROTOBUF_ARCH_MIPS 1 -#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -#endif -#elif defined(__pnacl__) -#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -#elif defined(sparc) -#define GOOGLE_PROTOBUF_ARCH_SPARC 1 -#if defined(__sparc_v9__) || defined(__sparcv9) || defined(__arch64__) -#define GOOGLE_PROTOBUF_ARCH_64_BIT 1 -#else -#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -#endif -#elif defined(_POWER) || defined(__powerpc64__) || defined(__PPC64__) -#define GOOGLE_PROTOBUF_ARCH_POWER 1 -#define GOOGLE_PROTOBUF_ARCH_64_BIT 1 -#elif defined(__PPC__) -#define GOOGLE_PROTOBUF_ARCH_PPC 1 -#define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -#elif defined(__GNUC__) -# if (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)) -// We fallback to the generic Clang/GCC >= 4.7 implementation in atomicops.h -# elif defined(__clang__) -# if !__has_extension(c_atomic) -GOOGLE_PROTOBUF_PLATFORM_ERROR -# endif -// We fallback to the generic Clang/GCC >= 4.7 implementation in atomicops.h -# endif -# if __LP64__ -# define GOOGLE_PROTOBUF_ARCH_64_BIT 1 -# else -# define GOOGLE_PROTOBUF_ARCH_32_BIT 1 -# endif -#else -GOOGLE_PROTOBUF_PLATFORM_ERROR -#endif - -#if defined(__APPLE__) -#define GOOGLE_PROTOBUF_OS_APPLE -#include -#include -#if TARGET_OS_IPHONE -#define GOOGLE_PROTOBUF_OS_IPHONE -#endif -#elif defined(__EMSCRIPTEN__) -#define GOOGLE_PROTOBUF_OS_EMSCRIPTEN -#elif defined(__native_client__) -#define GOOGLE_PROTOBUF_OS_NACL -#elif defined(sun) -#define GOOGLE_PROTOBUF_OS_SOLARIS -#elif defined(_AIX) -#define GOOGLE_PROTOBUF_OS_AIX -#elif defined(__ANDROID__) -#define GOOGLE_PROTOBUF_OS_ANDROID -#endif - -#undef GOOGLE_PROTOBUF_PLATFORM_ERROR - -#if defined(GOOGLE_PROTOBUF_OS_ANDROID) || defined(GOOGLE_PROTOBUF_OS_IPHONE) || defined(__OpenBSD__) -// Android ndk does not support the __thread keyword very well yet. Here -// we use pthread_key_create()/pthread_getspecific()/... methods for -// TLS support on android. -// iOS and OpenBSD also do not support the __thread keyword. -#define GOOGLE_PROTOBUF_NO_THREADLOCAL -#endif - -#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1070 -// __thread keyword requires at least 10.7 -#define GOOGLE_PROTOBUF_NO_THREADLOCAL -#endif - -#endif // GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/port.h b/third_party/protobuf-lite/google/protobuf/stubs/port.h deleted file mode 100644 index 0fcee6891..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/port.h +++ /dev/null @@ -1,405 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_STUBS_PORT_H_ -#define GOOGLE_PROTOBUF_STUBS_PORT_H_ - -#include -#include -#include -#include -#include -#include - -#include - -#include - -#undef PROTOBUF_LITTLE_ENDIAN -#ifdef _WIN32 - // Assuming windows is always little-endian. - // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for - // optimization but also for correctness. We should define an - // different macro to test the big-endian code path in coded_stream. - #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) - #define PROTOBUF_LITTLE_ENDIAN 1 - #endif - #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER) - // If MSVC has "/RTCc" set, it will complain about truncating casts at - // runtime. This file contains some intentional truncating casts. - #pragma runtime_checks("c", off) - #endif -#else - #include // __BYTE_ORDER - #if defined(__OpenBSD__) - #include - #endif - #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \ - (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \ - (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \ - !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST) - #define PROTOBUF_LITTLE_ENDIAN 1 - #endif -#endif - -// These #includes are for the byte swap functions declared later on. -#ifdef _MSC_VER -#include // NOLINT(build/include) -#include -#elif defined(__APPLE__) -#include -#elif defined(__GLIBC__) || defined(__BIONIC__) || defined(__CYGWIN__) -#include // IWYU pragma: export -#endif - -// Legacy: some users reference these (internal-only) macros even though we -// don't need them any more. -#if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS) - #ifdef LIBPROTOBUF_EXPORTS - #define LIBPROTOBUF_EXPORT __declspec(dllexport) - #else - #define LIBPROTOBUF_EXPORT __declspec(dllimport) - #endif - #ifdef LIBPROTOC_EXPORTS - #define LIBPROTOC_EXPORT __declspec(dllexport) - #else - #define LIBPROTOC_EXPORT __declspec(dllimport) - #endif -#else - #define LIBPROTOBUF_EXPORT - #define LIBPROTOC_EXPORT -#endif - -#define PROTOBUF_RUNTIME_DEPRECATED(message) PROTOBUF_DEPRECATED_MSG(message) -#define GOOGLE_PROTOBUF_RUNTIME_DEPRECATED(message) \ - PROTOBUF_DEPRECATED_MSG(message) - -// =================================================================== -// from google3/base/port.h - -#if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \ - (defined(_MSC_VER) && _MSC_VER >= 1900)) -// Define this to 1 if the code is compiled in C++11 mode; leave it -// undefined otherwise. Do NOT define it to 0 -- that causes -// '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'. -#define LANG_CXX11 1 -#else -#error "Protobuf requires at least C++11." -#endif - -namespace google { -namespace protobuf { - -using ConstStringParam = const std::string &; - -typedef unsigned int uint; - -typedef int8_t int8; -typedef int16_t int16; -typedef int32_t int32; -typedef int64_t int64; - -typedef uint8_t uint8; -typedef uint16_t uint16; -typedef uint32_t uint32; -typedef uint64_t uint64; - -static const int32 kint32max = 0x7FFFFFFF; -static const int32 kint32min = -kint32max - 1; -static const int64 kint64max = PROTOBUF_LONGLONG(0x7FFFFFFFFFFFFFFF); -static const int64 kint64min = -kint64max - 1; -static const uint32 kuint32max = 0xFFFFFFFFu; -static const uint64 kuint64max = PROTOBUF_ULONGLONG(0xFFFFFFFFFFFFFFFF); - -#if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\ - defined(MEMORY_SANITIZER) - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus -uint16_t __sanitizer_unaligned_load16(const void *p); -uint32_t __sanitizer_unaligned_load32(const void *p); -uint64_t __sanitizer_unaligned_load64(const void *p); -void __sanitizer_unaligned_store16(void *p, uint16_t v); -void __sanitizer_unaligned_store32(void *p, uint32_t v); -void __sanitizer_unaligned_store64(void *p, uint64_t v); -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus - -inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) { - return __sanitizer_unaligned_load16(p); -} - -inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) { - return __sanitizer_unaligned_load32(p); -} - -inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) { - return __sanitizer_unaligned_load64(p); -} - -inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) { - __sanitizer_unaligned_store16(p, v); -} - -inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) { - __sanitizer_unaligned_store32(p, v); -} - -inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) { - __sanitizer_unaligned_store64(p, v); -} - -#elif defined(GOOGLE_PROTOBUF_USE_UNALIGNED) && GOOGLE_PROTOBUF_USE_UNALIGNED - -#define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast(_p)) -#define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast(_p)) -#define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast(_p)) - -#define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast(_p) = (_val)) -#define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast(_p) = (_val)) -#define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast(_p) = (_val)) - -#else -inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) { - uint16 t; - memcpy(&t, p, sizeof t); - return t; -} - -inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) { - uint32 t; - memcpy(&t, p, sizeof t); - return t; -} - -inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) { - uint64 t; - memcpy(&t, p, sizeof t); - return t; -} - -inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) { - memcpy(p, &v, sizeof v); -} - -inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) { - memcpy(p, &v, sizeof v); -} - -inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) { - memcpy(p, &v, sizeof v); -} -#endif - -#if defined(GOOGLE_PROTOBUF_OS_NACL) \ - || (defined(__ANDROID__) && defined(__clang__) \ - && (__clang_major__ == 3 && __clang_minor__ == 8) \ - && (__clang_patchlevel__ < 275480)) -# define GOOGLE_PROTOBUF_USE_PORTABLE_LOG2 -#endif - -// The following guarantees declaration of the byte swap functions. -#ifdef _MSC_VER -#define bswap_16(x) _byteswap_ushort(x) -#define bswap_32(x) _byteswap_ulong(x) -#define bswap_64(x) _byteswap_uint64(x) - -#elif defined(__APPLE__) -// Mac OS X / Darwin features -#define bswap_16(x) OSSwapInt16(x) -#define bswap_32(x) OSSwapInt32(x) -#define bswap_64(x) OSSwapInt64(x) - -#elif !defined(__GLIBC__) && !defined(__BIONIC__) && !defined(__CYGWIN__) - -#ifndef bswap_16 -static inline uint16 bswap_16(uint16 x) { - return static_cast(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8)); -} -#define bswap_16(x) bswap_16(x) -#endif - -#ifndef bswap_32 -static inline uint32 bswap_32(uint32 x) { - return (((x & 0xFF) << 24) | - ((x & 0xFF00) << 8) | - ((x & 0xFF0000) >> 8) | - ((x & 0xFF000000) >> 24)); -} -#define bswap_32(x) bswap_32(x) -#endif - -#ifndef bswap_64 -static inline uint64 bswap_64(uint64 x) { - return (((x & PROTOBUF_ULONGLONG(0xFF)) << 56) | - ((x & PROTOBUF_ULONGLONG(0xFF00)) << 40) | - ((x & PROTOBUF_ULONGLONG(0xFF0000)) << 24) | - ((x & PROTOBUF_ULONGLONG(0xFF000000)) << 8) | - ((x & PROTOBUF_ULONGLONG(0xFF00000000)) >> 8) | - ((x & PROTOBUF_ULONGLONG(0xFF0000000000)) >> 24) | - ((x & PROTOBUF_ULONGLONG(0xFF000000000000)) >> 40) | - ((x & PROTOBUF_ULONGLONG(0xFF00000000000000)) >> 56)); -} -#define bswap_64(x) bswap_64(x) -#endif - -#endif - -// =================================================================== -// from google3/util/bits/bits.h - -class Bits { - public: - static uint32 Log2FloorNonZero(uint32 n) { -#if defined(__GNUC__) - return 31 ^ static_cast(__builtin_clz(n)); -#elif defined(_MSC_VER) - unsigned long where; - _BitScanReverse(&where, n); - return where; -#else - return Log2FloorNonZero_Portable(n); -#endif - } - - static uint32 Log2FloorNonZero64(uint64 n) { - // Older versions of clang run into an instruction-selection failure when - // it encounters __builtin_clzll: - // https://bugs.chromium.org/p/nativeclient/issues/detail?id=4395 - // This includes arm-nacl-clang and clang in older Android NDK versions. - // To work around this, when we build with those we use the portable - // implementation instead. -#if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2) - return 63 ^ static_cast(__builtin_clzll(n)); -#elif defined(_MSC_VER) && defined(_M_X64) - unsigned long where; - _BitScanReverse64(&where, n); - return where; -#else - return Log2FloorNonZero64_Portable(n); -#endif - } - private: - static int Log2FloorNonZero_Portable(uint32 n) { - if (n == 0) - return -1; - int log = 0; - uint32 value = n; - for (int i = 4; i >= 0; --i) { - int shift = (1 << i); - uint32 x = value >> shift; - if (x != 0) { - value = x; - log += shift; - } - } - assert(value == 1); - return log; - } - - static int Log2FloorNonZero64_Portable(uint64 n) { - const uint32 topbits = static_cast(n >> 32); - if (topbits == 0) { - // Top bits are zero, so scan in bottom bits - return static_cast(Log2FloorNonZero(static_cast(n))); - } else { - return 32 + static_cast(Log2FloorNonZero(topbits)); - } - } -}; - -// =================================================================== -// from google3/util/endian/endian.h -PROTOBUF_EXPORT uint32 ghtonl(uint32 x); - -class BigEndian { - public: -#ifdef PROTOBUF_LITTLE_ENDIAN - - static uint16 FromHost16(uint16 x) { return bswap_16(x); } - static uint16 ToHost16(uint16 x) { return bswap_16(x); } - - static uint32 FromHost32(uint32 x) { return bswap_32(x); } - static uint32 ToHost32(uint32 x) { return bswap_32(x); } - - static uint64 FromHost64(uint64 x) { return bswap_64(x); } - static uint64 ToHost64(uint64 x) { return bswap_64(x); } - - static bool IsLittleEndian() { return true; } - -#else - - static uint16 FromHost16(uint16 x) { return x; } - static uint16 ToHost16(uint16 x) { return x; } - - static uint32 FromHost32(uint32 x) { return x; } - static uint32 ToHost32(uint32 x) { return x; } - - static uint64 FromHost64(uint64 x) { return x; } - static uint64 ToHost64(uint64 x) { return x; } - - static bool IsLittleEndian() { return false; } - -#endif /* ENDIAN */ - - // Functions to do unaligned loads and stores in big-endian order. - static uint16 Load16(const void *p) { - return ToHost16(GOOGLE_UNALIGNED_LOAD16(p)); - } - - static void Store16(void *p, uint16 v) { - GOOGLE_UNALIGNED_STORE16(p, FromHost16(v)); - } - - static uint32 Load32(const void *p) { - return ToHost32(GOOGLE_UNALIGNED_LOAD32(p)); - } - - static void Store32(void *p, uint32 v) { - GOOGLE_UNALIGNED_STORE32(p, FromHost32(v)); - } - - static uint64 Load64(const void *p) { - return ToHost64(GOOGLE_UNALIGNED_LOAD64(p)); - } - - static void Store64(void *p, uint64 v) { - GOOGLE_UNALIGNED_STORE64(p, FromHost64(v)); - } -}; - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_PORT_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/status.h b/third_party/protobuf-lite/google/protobuf/stubs/status.h deleted file mode 100644 index bededad54..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/status.h +++ /dev/null @@ -1,125 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 GOOGLE_PROTOBUF_STUBS_STATUS_H_ -#define GOOGLE_PROTOBUF_STUBS_STATUS_H_ - -#include -#include - -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace util { -namespace error { -// These values must match error codes defined in google/rpc/code.proto. -enum Code { - OK = 0, - CANCELLED = 1, - UNKNOWN = 2, - INVALID_ARGUMENT = 3, - DEADLINE_EXCEEDED = 4, - NOT_FOUND = 5, - ALREADY_EXISTS = 6, - PERMISSION_DENIED = 7, - UNAUTHENTICATED = 16, - RESOURCE_EXHAUSTED = 8, - FAILED_PRECONDITION = 9, - ABORTED = 10, - OUT_OF_RANGE = 11, - UNIMPLEMENTED = 12, - INTERNAL = 13, - UNAVAILABLE = 14, - DATA_LOSS = 15, -}; -} // namespace error - -class PROTOBUF_EXPORT Status { - public: - // Creates a "successful" status. - Status(); - - // Create a status in the canonical error space with the specified - // code, and error message. If "code == 0", error_message is - // ignored and a Status object identical to Status::OK is - // constructed. - Status(error::Code error_code, StringPiece error_message); - Status(const Status&); - Status& operator=(const Status& x); - ~Status() {} - - // Some pre-defined Status objects - static const Status OK; // Identical to 0-arg constructor - static const Status CANCELLED; - static const Status UNKNOWN; - - // Accessor - bool ok() const { - return error_code_ == error::OK; - } - int error_code() const { - return error_code_; - } - error::Code code() const { - return error_code_; - } - StringPiece error_message() const { - return error_message_; - } - StringPiece message() const { - return error_message_; - } - - bool operator==(const Status& x) const; - bool operator!=(const Status& x) const { - return !operator==(x); - } - - // Return a combination of the error code name and message. - std::string ToString() const; - - private: - error::Code error_code_; - std::string error_message_; -}; - -// Prints a human-readable representation of 'x' to 'os'. -PROTOBUF_EXPORT std::ostream& operator<<(std::ostream& os, const Status& x); - -} // namespace util -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_STATUS_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/statusor.h b/third_party/protobuf-lite/google/protobuf/stubs/statusor.h deleted file mode 100644 index c02e89a9f..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/statusor.h +++ /dev/null @@ -1,272 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// StatusOr is the union of a Status object and a T -// object. StatusOr models the concept of an object that is either a -// usable value, or an error Status explaining why such a value is -// not present. To this end, StatusOr does not allow its Status -// value to be Status::OK. Further, StatusOr does not allow the -// contained pointer to be nullptr. -// -// The primary use-case for StatusOr is as the return value of a -// function which may fail. -// -// Example client usage for a StatusOr, where T is not a pointer: -// -// StatusOr result = DoBigCalculationThatCouldFail(); -// if (result.ok()) { -// float answer = result.ValueOrDie(); -// printf("Big calculation yielded: %f", answer); -// } else { -// LOG(ERROR) << result.status(); -// } -// -// Example client usage for a StatusOr: -// -// StatusOr result = FooFactory::MakeNewFoo(arg); -// if (result.ok()) { -// std::unique_ptr foo(result.ValueOrDie()); -// foo->DoSomethingCool(); -// } else { -// LOG(ERROR) << result.status(); -// } -// -// Example client usage for a StatusOr>: -// -// StatusOr> result = FooFactory::MakeNewFoo(arg); -// if (result.ok()) { -// std::unique_ptr foo = result.ConsumeValueOrDie(); -// foo->DoSomethingCool(); -// } else { -// LOG(ERROR) << result.status(); -// } -// -// Example factory implementation returning StatusOr: -// -// StatusOr FooFactory::MakeNewFoo(int arg) { -// if (arg <= 0) { -// return ::util::Status(::util::error::INVALID_ARGUMENT, -// "Arg must be positive"); -// } else { -// return new Foo(arg); -// } -// } -// - -#ifndef GOOGLE_PROTOBUF_STUBS_STATUSOR_H_ -#define GOOGLE_PROTOBUF_STUBS_STATUSOR_H_ - -#include -#include -#include - -#include - -#include - -namespace google { -namespace protobuf { -namespace util { - -template -class StatusOr { - template friend class StatusOr; - - public: - // Construct a new StatusOr with Status::UNKNOWN status - StatusOr(); - - // Construct a new StatusOr with the given non-ok status. After calling - // this constructor, calls to ValueOrDie() will CHECK-fail. - // - // NOTE: Not explicit - we want to use StatusOr as a return - // value, so it is convenient and sensible to be able to do 'return - // Status()' when the return type is StatusOr. - // - // REQUIRES: status != Status::OK. This requirement is DCHECKed. - // In optimized builds, passing Status::OK here will have the effect - // of passing PosixErrorSpace::EINVAL as a fallback. - StatusOr(const Status& status); // NOLINT - - // Construct a new StatusOr with the given value. If T is a plain pointer, - // value must not be nullptr. After calling this constructor, calls to - // ValueOrDie() will succeed, and calls to status() will return OK. - // - // NOTE: Not explicit - we want to use StatusOr as a return type - // so it is convenient and sensible to be able to do 'return T()' - // when when the return type is StatusOr. - // - // REQUIRES: if T is a plain pointer, value != nullptr. This requirement is - // DCHECKed. In optimized builds, passing a null pointer here will have - // the effect of passing PosixErrorSpace::EINVAL as a fallback. - StatusOr(const T& value); // NOLINT - - // Copy constructor. - StatusOr(const StatusOr& other); - - // Conversion copy constructor, T must be copy constructible from U - template - StatusOr(const StatusOr& other); - - // Assignment operator. - StatusOr& operator=(const StatusOr& other); - - // Conversion assignment operator, T must be assignable from U - template - StatusOr& operator=(const StatusOr& other); - - // Returns a reference to our status. If this contains a T, then - // returns Status::OK. - const Status& status() const; - - // Returns this->status().ok() - bool ok() const; - - // Returns a reference to our current value, or CHECK-fails if !this->ok(). - // If you need to initialize a T object from the stored value, - // ConsumeValueOrDie() may be more efficient. - const T& ValueOrDie() const; - const T& value () const; - - private: - Status status_; - T value_; -}; - -//////////////////////////////////////////////////////////////////////////////// -// Implementation details for StatusOr - -namespace internal { - -class PROTOBUF_EXPORT StatusOrHelper { - public: - // Move type-agnostic error handling to the .cc. - static void Crash(const util::Status& status); - - // Customized behavior for StatusOr vs. StatusOr - template - struct Specialize; -}; - -template -struct StatusOrHelper::Specialize { - // For non-pointer T, a reference can never be nullptr. - static inline bool IsValueNull(const T& t) { return false; } -}; - -template -struct StatusOrHelper::Specialize { - static inline bool IsValueNull(const T* t) { return t == nullptr; } -}; - -} // namespace internal - -template -inline StatusOr::StatusOr() - : status_(util::Status::UNKNOWN) { -} - -template -inline StatusOr::StatusOr(const Status& status) { - if (status.ok()) { - status_ = Status(error::INTERNAL, "Status::OK is not a valid argument."); - } else { - status_ = status; - } -} - -template -inline StatusOr::StatusOr(const T& value) { - if (internal::StatusOrHelper::Specialize::IsValueNull(value)) { - status_ = Status(error::INTERNAL, "nullptr is not a valid argument."); - } else { - status_ = Status::OK; - value_ = value; - } -} - -template -inline StatusOr::StatusOr(const StatusOr& other) - : status_(other.status_), value_(other.value_) { -} - -template -inline StatusOr& StatusOr::operator=(const StatusOr& other) { - status_ = other.status_; - value_ = other.value_; - return *this; -} - -template -template -inline StatusOr::StatusOr(const StatusOr& other) - : status_(other.status_), value_(other.status_.ok() ? other.value_ : T()) { -} - -template -template -inline StatusOr& StatusOr::operator=(const StatusOr& other) { - status_ = other.status_; - if (status_.ok()) value_ = other.value_; - return *this; -} - -template -inline const Status& StatusOr::status() const { - return status_; -} - -template -inline bool StatusOr::ok() const { - return status().ok(); -} - -template -inline const T& StatusOr::ValueOrDie() const { - if (!status_.ok()) { - internal::StatusOrHelper::Crash(status_); - } - return value_; -} - -template -inline const T& StatusOr::value() const { - if (!status_.ok()) { - internal::StatusOrHelper::Crash(status_); - } - return value_; -} -} // namespace util -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_STATUSOR_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/stl_util.h b/third_party/protobuf-lite/google/protobuf/stubs/stl_util.h deleted file mode 100644 index d01f9ec94..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/stl_util.h +++ /dev/null @@ -1,71 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// from google3/util/gtl/stl_util.h - -#ifndef GOOGLE_PROTOBUF_STUBS_STL_UTIL_H__ -#define GOOGLE_PROTOBUF_STUBS_STL_UTIL_H__ - -#include - -namespace google { -namespace protobuf { - -// Inside Google, this function implements a horrible, disgusting hack in which -// we reach into the string's private implementation and resize it without -// initializing the new bytes. In some cases doing this can significantly -// improve performance. However, since it's totally non-portable it has no -// place in open source code. Feel free to fill this function in with your -// own disgusting hack if you want the perf boost. -inline void STLStringResizeUninitialized(std::string* s, size_t new_size) { - s->resize(new_size); -} - -// Return a mutable char* pointing to a string's internal buffer, -// which may not be null-terminated. Writing through this pointer will -// modify the string. -// -// string_as_array(&str)[i] is valid for 0 <= i < str.size() until the -// next call to a string method that invalidates iterators. -// -// As of 2006-04, there is no standard-blessed way of getting a -// mutable reference to a string's internal buffer. However, issue 530 -// (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) -// proposes this as the method. According to Matt Austern, this should -// already work on all current implementations. -inline char* string_as_array(std::string* str) { - // DO NOT USE const_cast(str->data())! See the unittest for why. - return str->empty() ? nullptr : &*str->begin(); -} - -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_STUBS_STL_UTIL_H__ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/stringpiece.h b/third_party/protobuf-lite/google/protobuf/stubs/stringpiece.h deleted file mode 100644 index fbcb20afc..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/stringpiece.h +++ /dev/null @@ -1,489 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// A StringPiece points to part or all of a string, Cord, double-quoted string -// literal, or other string-like object. A StringPiece does *not* own the -// string to which it points. A StringPiece is not null-terminated. -// -// You can use StringPiece as a function or method parameter. A StringPiece -// parameter can receive a double-quoted string literal argument, a "const -// char*" argument, a string argument, or a StringPiece argument with no data -// copying. Systematic use of StringPiece for arguments reduces data -// copies and strlen() calls. -// -// Prefer passing StringPieces by value: -// void MyFunction(StringPiece arg); -// If circumstances require, you may also pass by const reference: -// void MyFunction(const StringPiece& arg); // not preferred -// Both of these have the same lifetime semantics. Passing by value -// generates slightly smaller code. For more discussion, see the thread -// go/stringpiecebyvalue on c-users. -// -// StringPiece is also suitable for local variables if you know that -// the lifetime of the underlying object is longer than the lifetime -// of your StringPiece variable. -// -// Beware of binding a StringPiece to a temporary: -// StringPiece sp = obj.MethodReturningString(); // BAD: lifetime problem -// -// This code is okay: -// string str = obj.MethodReturningString(); // str owns its contents -// StringPiece sp(str); // GOOD, because str outlives sp -// -// StringPiece is sometimes a poor choice for a return value and usually a poor -// choice for a data member. If you do use a StringPiece this way, it is your -// responsibility to ensure that the object pointed to by the StringPiece -// outlives the StringPiece. -// -// A StringPiece may represent just part of a string; thus the name "Piece". -// For example, when splitting a string, vector is a natural data -// type for the output. For another example, a Cord is a non-contiguous, -// potentially very long string-like object. The Cord class has an interface -// that iteratively provides StringPiece objects that point to the -// successive pieces of a Cord object. -// -// A StringPiece is not null-terminated. If you write code that scans a -// StringPiece, you must check its length before reading any characters. -// Common idioms that work on null-terminated strings do not work on -// StringPiece objects. -// -// There are several ways to create a null StringPiece: -// StringPiece() -// StringPiece(nullptr) -// StringPiece(nullptr, 0) -// For all of the above, sp.data() == nullptr, sp.length() == 0, -// and sp.empty() == true. Also, if you create a StringPiece with -// a non-null pointer then sp.data() != nullptr. Once created, -// sp.data() will stay either nullptr or not-nullptr, except if you call -// sp.clear() or sp.set(). -// -// Thus, you can use StringPiece(nullptr) to signal an out-of-band value -// that is different from other StringPiece values. This is similar -// to the way that const char* p1 = nullptr; is different from -// const char* p2 = "";. -// -// There are many ways to create an empty StringPiece: -// StringPiece() -// StringPiece(nullptr) -// StringPiece(nullptr, 0) -// StringPiece("") -// StringPiece("", 0) -// StringPiece("abcdef", 0) -// StringPiece("abcdef"+6, 0) -// For all of the above, sp.length() will be 0 and sp.empty() will be true. -// For some empty StringPiece values, sp.data() will be nullptr. -// For some empty StringPiece values, sp.data() will not be nullptr. -// -// Be careful not to confuse: null StringPiece and empty StringPiece. -// The set of empty StringPieces properly includes the set of null StringPieces. -// That is, every null StringPiece is an empty StringPiece, -// but some non-null StringPieces are empty Stringpieces too. -// -// All empty StringPiece values compare equal to each other. -// Even a null StringPieces compares equal to a non-null empty StringPiece: -// StringPiece() == StringPiece("", 0) -// StringPiece(nullptr) == StringPiece("abc", 0) -// StringPiece(nullptr, 0) == StringPiece("abcdef"+6, 0) -// -// Look carefully at this example: -// StringPiece("") == nullptr -// True or false? TRUE, because StringPiece::operator== converts -// the right-hand side from nullptr to StringPiece(nullptr), -// and then compares two zero-length spans of characters. -// However, we are working to make this example produce a compile error. -// -// Suppose you want to write: -// bool TestWhat?(StringPiece sp) { return sp == nullptr; } // BAD -// Do not do that. Write one of these instead: -// bool TestNull(StringPiece sp) { return sp.data() == nullptr; } -// bool TestEmpty(StringPiece sp) { return sp.empty(); } -// The intent of TestWhat? is unclear. Did you mean TestNull or TestEmpty? -// Right now, TestWhat? behaves likes TestEmpty. -// We are working to make TestWhat? produce a compile error. -// TestNull is good to test for an out-of-band signal. -// TestEmpty is good to test for an empty StringPiece. -// -// Caveats (again): -// (1) The lifetime of the pointed-to string (or piece of a string) -// must be longer than the lifetime of the StringPiece. -// (2) There may or may not be a '\0' character after the end of -// StringPiece data. -// (3) A null StringPiece is empty. -// An empty StringPiece may or may not be a null StringPiece. - -#ifndef GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_ -#define GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_ - -#include -#include -#include -#include -#include -#include - -#include - -#include - -namespace google { -namespace protobuf { -// StringPiece has *two* size types. -// StringPiece::size_type -// is unsigned -// is 32 bits in LP32, 64 bits in LP64, 64 bits in LLP64 -// no future changes intended -// stringpiece_ssize_type -// is signed -// is 32 bits in LP32, 64 bits in LP64, 64 bits in LLP64 -// future changes intended: http://go/64BitStringPiece -// -typedef std::string::difference_type stringpiece_ssize_type; - -// STRINGPIECE_CHECK_SIZE protects us from 32-bit overflows. -// TODO(mec): delete this after stringpiece_ssize_type goes 64 bit. -#if !defined(NDEBUG) -#define STRINGPIECE_CHECK_SIZE 1 -#elif defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 -#define STRINGPIECE_CHECK_SIZE 1 -#else -#define STRINGPIECE_CHECK_SIZE 0 -#endif - -class PROTOBUF_EXPORT StringPiece { - private: - const char* ptr_; - stringpiece_ssize_type length_; - - // Prevent overflow in debug mode or fortified mode. - // sizeof(stringpiece_ssize_type) may be smaller than sizeof(size_t). - static stringpiece_ssize_type CheckedSsizeTFromSizeT(size_t size) { -#if STRINGPIECE_CHECK_SIZE > 0 -#ifdef max -#undef max -#endif - if (size > static_cast( - std::numeric_limits::max())) { - // Some people grep for this message in logs - // so take care if you ever change it. - LogFatalSizeTooBig(size, "size_t to int conversion"); - } -#endif - return static_cast(size); - } - - // Out-of-line error path. - static void LogFatalSizeTooBig(size_t size, const char* details); - - public: - // We provide non-explicit singleton constructors so users can pass - // in a "const char*" or a "string" wherever a "StringPiece" is - // expected. - // - // Style guide exception granted: - // http://goto/style-guide-exception-20978288 - StringPiece() : ptr_(nullptr), length_(0) {} - - StringPiece(const char* str) // NOLINT(runtime/explicit) - : ptr_(str), length_(0) { - if (str != nullptr) { - length_ = CheckedSsizeTFromSizeT(strlen(str)); - } - } - - template - StringPiece( // NOLINT(runtime/explicit) - const std::basic_string, Allocator>& str) - : ptr_(str.data()), length_(0) { - length_ = CheckedSsizeTFromSizeT(str.size()); - } - - StringPiece(const char* offset, stringpiece_ssize_type len) - : ptr_(offset), length_(len) { - assert(len >= 0); - } - - // Substring of another StringPiece. - // pos must be non-negative and <= x.length(). - StringPiece(StringPiece x, stringpiece_ssize_type pos); - // Substring of another StringPiece. - // pos must be non-negative and <= x.length(). - // len must be non-negative and will be pinned to at most x.length() - pos. - StringPiece(StringPiece x, - stringpiece_ssize_type pos, - stringpiece_ssize_type len); - - // data() may return a pointer to a buffer with embedded NULs, and the - // returned buffer may or may not be null terminated. Therefore it is - // typically a mistake to pass data() to a routine that expects a NUL - // terminated string. - const char* data() const { return ptr_; } - stringpiece_ssize_type size() const { return length_; } - stringpiece_ssize_type length() const { return length_; } - bool empty() const { return length_ == 0; } - - void clear() { - ptr_ = nullptr; - length_ = 0; - } - - void set(const char* data, stringpiece_ssize_type len) { - assert(len >= 0); - ptr_ = data; - length_ = len; - } - - void set(const char* str) { - ptr_ = str; - if (str != nullptr) - length_ = CheckedSsizeTFromSizeT(strlen(str)); - else - length_ = 0; - } - - void set(const void* data, stringpiece_ssize_type len) { - ptr_ = reinterpret_cast(data); - length_ = len; - } - - char operator[](stringpiece_ssize_type i) const { - assert(0 <= i); - assert(i < length_); - return ptr_[i]; - } - - void remove_prefix(stringpiece_ssize_type n) { - assert(length_ >= n); - ptr_ += n; - length_ -= n; - } - - void remove_suffix(stringpiece_ssize_type n) { - assert(length_ >= n); - length_ -= n; - } - - // returns {-1, 0, 1} - int compare(StringPiece x) const { - const stringpiece_ssize_type min_size = - length_ < x.length_ ? length_ : x.length_; - int r = memcmp(ptr_, x.ptr_, static_cast(min_size)); - if (r < 0) return -1; - if (r > 0) return 1; - if (length_ < x.length_) return -1; - if (length_ > x.length_) return 1; - return 0; - } - - std::string as_string() const { return ToString(); } - // We also define ToString() here, since many other string-like - // interfaces name the routine that converts to a C++ string - // "ToString", and it's confusing to have the method that does that - // for a StringPiece be called "as_string()". We also leave the - // "as_string()" method defined here for existing code. - std::string ToString() const { - if (ptr_ == nullptr) return ""; - return std::string(data(), static_cast(size())); - } - - explicit operator std::string() const { return ToString(); } - - void CopyToString(std::string* target) const; - void AppendToString(std::string* target) const; - - bool starts_with(StringPiece x) const { - return (length_ >= x.length_) && - (memcmp(ptr_, x.ptr_, static_cast(x.length_)) == 0); - } - - bool ends_with(StringPiece x) const { - return ((length_ >= x.length_) && - (memcmp(ptr_ + (length_-x.length_), x.ptr_, - static_cast(x.length_)) == 0)); - } - - // Checks whether StringPiece starts with x and if so advances the beginning - // of it to past the match. It's basically a shortcut for starts_with - // followed by remove_prefix. - bool Consume(StringPiece x); - // Like above but for the end of the string. - bool ConsumeFromEnd(StringPiece x); - - // standard STL container boilerplate - typedef char value_type; - typedef const char* pointer; - typedef const char& reference; - typedef const char& const_reference; - typedef size_t size_type; - typedef ptrdiff_t difference_type; - static const size_type npos; - typedef const char* const_iterator; - typedef const char* iterator; - typedef std::reverse_iterator const_reverse_iterator; - typedef std::reverse_iterator reverse_iterator; - iterator begin() const { return ptr_; } - iterator end() const { return ptr_ + length_; } - const_reverse_iterator rbegin() const { - return const_reverse_iterator(ptr_ + length_); - } - const_reverse_iterator rend() const { - return const_reverse_iterator(ptr_); - } - stringpiece_ssize_type max_size() const { return length_; } - stringpiece_ssize_type capacity() const { return length_; } - - // cpplint.py emits a false positive [build/include_what_you_use] - stringpiece_ssize_type copy(char* buf, size_type n, size_type pos = 0) const; // NOLINT - - bool contains(StringPiece s) const; - - stringpiece_ssize_type find(StringPiece s, size_type pos = 0) const; - stringpiece_ssize_type find(char c, size_type pos = 0) const; - stringpiece_ssize_type rfind(StringPiece s, size_type pos = npos) const; - stringpiece_ssize_type rfind(char c, size_type pos = npos) const; - - stringpiece_ssize_type find_first_of(StringPiece s, size_type pos = 0) const; - stringpiece_ssize_type find_first_of(char c, size_type pos = 0) const { - return find(c, pos); - } - stringpiece_ssize_type find_first_not_of(StringPiece s, - size_type pos = 0) const; - stringpiece_ssize_type find_first_not_of(char c, size_type pos = 0) const; - stringpiece_ssize_type find_last_of(StringPiece s, - size_type pos = npos) const; - stringpiece_ssize_type find_last_of(char c, size_type pos = npos) const { - return rfind(c, pos); - } - stringpiece_ssize_type find_last_not_of(StringPiece s, - size_type pos = npos) const; - stringpiece_ssize_type find_last_not_of(char c, size_type pos = npos) const; - - StringPiece substr(size_type pos, size_type n = npos) const; -}; - -// This large function is defined inline so that in a fairly common case where -// one of the arguments is a literal, the compiler can elide a lot of the -// following comparisons. -inline bool operator==(StringPiece x, StringPiece y) { - stringpiece_ssize_type len = x.size(); - if (len != y.size()) { - return false; - } - - return x.data() == y.data() || len <= 0 || - memcmp(x.data(), y.data(), static_cast(len)) == 0; -} - -inline bool operator!=(StringPiece x, StringPiece y) { - return !(x == y); -} - -inline bool operator<(StringPiece x, StringPiece y) { - const stringpiece_ssize_type min_size = - x.size() < y.size() ? x.size() : y.size(); - const int r = memcmp(x.data(), y.data(), static_cast(min_size)); - return (r < 0) || (r == 0 && x.size() < y.size()); -} - -inline bool operator>(StringPiece x, StringPiece y) { - return y < x; -} - -inline bool operator<=(StringPiece x, StringPiece y) { - return !(x > y); -} - -inline bool operator>=(StringPiece x, StringPiece y) { - return !(x < y); -} - -// allow StringPiece to be logged -extern std::ostream& operator<<(std::ostream& o, StringPiece piece); - -namespace internal { -// StringPiece is not a POD and can not be used in an union (pre C++11). We -// need a POD version of it. -struct StringPiecePod { - // Create from a StringPiece. - static StringPiecePod CreateFromStringPiece(StringPiece str) { - StringPiecePod pod; - pod.data_ = str.data(); - pod.size_ = str.size(); - return pod; - } - - // Cast to StringPiece. - operator StringPiece() const { return StringPiece(data_, size_); } - - bool operator==(const char* value) const { - return StringPiece(data_, size_) == StringPiece(value); - } - - char operator[](stringpiece_ssize_type i) const { - assert(0 <= i); - assert(i < size_); - return data_[i]; - } - - const char* data() const { return data_; } - - stringpiece_ssize_type size() const { - return size_; - } - - std::string ToString() const { - return std::string(data_, static_cast(size_)); - } - - explicit operator std::string() const { return ToString(); } - - private: - const char* data_; - stringpiece_ssize_type size_; -}; - -} // namespace internal -} // namespace protobuf -} // namespace google - -GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START -template<> struct hash { - size_t operator()(const StringPiece& s) const { - size_t result = 0; - for (const char *str = s.data(), *end = str + s.size(); str < end; str++) { - result = 5 * result + static_cast(*str); - } - return result; - } -}; -GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END - -#include - -#endif // STRINGS_STRINGPIECE_H_ diff --git a/third_party/protobuf-lite/google/protobuf/stubs/stringprintf.h b/third_party/protobuf-lite/google/protobuf/stubs/stringprintf.h deleted file mode 100644 index e3858be13..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/stringprintf.h +++ /dev/null @@ -1,85 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2012 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// from google3/base/stringprintf.h -// -// Printf variants that place their output in a C++ string. -// -// Usage: -// string result = StringPrintf("%d %s\n", 10, "hello"); -// SStringPrintf(&result, "%d %s\n", 10, "hello"); -// StringAppendF(&result, "%d %s\n", 20, "there"); - -#ifndef GOOGLE_PROTOBUF_STUBS_STRINGPRINTF_H -#define GOOGLE_PROTOBUF_STUBS_STRINGPRINTF_H - -#include -#include -#include - -#include - -#include - -namespace google { -namespace protobuf { - -// Return a C++ string -PROTOBUF_EXPORT extern std::string StringPrintf(const char* format, ...); - -// Store result into a supplied string and return it -PROTOBUF_EXPORT extern const std::string& SStringPrintf(std::string* dst, - const char* format, - ...); - -// Append result to a supplied string -PROTOBUF_EXPORT extern void StringAppendF(std::string* dst, const char* format, - ...); - -// Lower-level routine that takes a va_list and appends to a specified -// string. All other routines are just convenience wrappers around it. -PROTOBUF_EXPORT extern void StringAppendV(std::string* dst, const char* format, - va_list ap); - -// The max arguments supported by StringPrintfVector -PROTOBUF_EXPORT extern const int kStringPrintfVectorMaxArgs; - -// You can use this version when all your arguments are strings, but -// you don't know how many arguments you'll have at compile time. -// StringPrintfVector will LOG(FATAL) if v.size() > kStringPrintfVectorMaxArgs -PROTOBUF_EXPORT extern std::string StringPrintfVector( - const char* format, const std::vector& v); - -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_STRINGPRINTF_H diff --git a/third_party/protobuf-lite/google/protobuf/stubs/strutil.h b/third_party/protobuf-lite/google/protobuf/stubs/strutil.h deleted file mode 100644 index 8ce81f28c..000000000 --- a/third_party/protobuf-lite/google/protobuf/stubs/strutil.h +++ /dev/null @@ -1,953 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// from google3/strings/strutil.h - -#ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ -#define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__ - -#include -#include -#include - -#include -#include -#include - -namespace google { -namespace protobuf { - -#if defined(_MSC_VER) && _MSC_VER < 1800 -#define strtoll _strtoi64 -#define strtoull _strtoui64 -#elif defined(__DECCXX) && defined(__osf__) -// HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit. -#define strtoll strtol -#define strtoull strtoul -#endif - -// ---------------------------------------------------------------------- -// ascii_isalnum() -// Check if an ASCII character is alphanumeric. We can't use ctype's -// isalnum() because it is affected by locale. This function is applied -// to identifiers in the protocol buffer language, not to natural-language -// strings, so locale should not be taken into account. -// ascii_isdigit() -// Like above, but only accepts digits. -// ascii_isspace() -// Check if the character is a space character. -// ---------------------------------------------------------------------- - -inline bool ascii_isalnum(char c) { - return ('a' <= c && c <= 'z') || - ('A' <= c && c <= 'Z') || - ('0' <= c && c <= '9'); -} - -inline bool ascii_isdigit(char c) { - return ('0' <= c && c <= '9'); -} - -inline bool ascii_isspace(char c) { - return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || - c == '\r'; -} - -inline bool ascii_isupper(char c) { - return c >= 'A' && c <= 'Z'; -} - -inline bool ascii_islower(char c) { - return c >= 'a' && c <= 'z'; -} - -inline char ascii_toupper(char c) { - return ascii_islower(c) ? c - ('a' - 'A') : c; -} - -inline char ascii_tolower(char c) { - return ascii_isupper(c) ? c + ('a' - 'A') : c; -} - -inline int hex_digit_to_int(char c) { - /* Assume ASCII. */ - int x = static_cast(c); - if (x > '9') { - x += 9; - } - return x & 0xf; -} - -// ---------------------------------------------------------------------- -// HasPrefixString() -// Check if a string begins with a given prefix. -// StripPrefixString() -// Given a string and a putative prefix, returns the string minus the -// prefix string if the prefix matches, otherwise the original -// string. -// ---------------------------------------------------------------------- -inline bool HasPrefixString(StringPiece str, StringPiece prefix) { - return str.size() >= prefix.size() && - memcmp(str.data(), prefix.data(), prefix.size()) == 0; -} - -inline std::string StripPrefixString(const std::string& str, - const std::string& prefix) { - if (HasPrefixString(str, prefix)) { - return str.substr(prefix.size()); - } else { - return str; - } -} - -// ---------------------------------------------------------------------- -// HasSuffixString() -// Return true if str ends in suffix. -// StripSuffixString() -// Given a string and a putative suffix, returns the string minus the -// suffix string if the suffix matches, otherwise the original -// string. -// ---------------------------------------------------------------------- -inline bool HasSuffixString(StringPiece str, StringPiece suffix) { - return str.size() >= suffix.size() && - memcmp(str.data() + str.size() - suffix.size(), suffix.data(), - suffix.size()) == 0; -} - -inline std::string StripSuffixString(const std::string& str, - const std::string& suffix) { - if (HasSuffixString(str, suffix)) { - return str.substr(0, str.size() - suffix.size()); - } else { - return str; - } -} - -// ---------------------------------------------------------------------- -// ReplaceCharacters -// Replaces any occurrence of the character 'remove' (or the characters -// in 'remove') with the character 'replacewith'. -// Good for keeping html characters or protocol characters (\t) out -// of places where they might cause a problem. -// StripWhitespace -// Removes whitespaces from both ends of the given string. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT void ReplaceCharacters(std::string* s, const char* remove, - char replacewith); - -PROTOBUF_EXPORT void StripWhitespace(std::string* s); - -// ---------------------------------------------------------------------- -// LowerString() -// UpperString() -// ToUpper() -// Convert the characters in "s" to lowercase or uppercase. ASCII-only: -// these functions intentionally ignore locale because they are applied to -// identifiers used in the Protocol Buffer language, not to natural-language -// strings. -// ---------------------------------------------------------------------- - -inline void LowerString(std::string* s) { - std::string::iterator end = s->end(); - for (std::string::iterator i = s->begin(); i != end; ++i) { - // tolower() changes based on locale. We don't want this! - if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A'; - } -} - -inline void UpperString(std::string* s) { - std::string::iterator end = s->end(); - for (std::string::iterator i = s->begin(); i != end; ++i) { - // toupper() changes based on locale. We don't want this! - if ('a' <= *i && *i <= 'z') *i += 'A' - 'a'; - } -} - -inline void ToUpper(std::string* s) { UpperString(s); } - -inline std::string ToUpper(const std::string& s) { - std::string out = s; - UpperString(&out); - return out; -} - -// ---------------------------------------------------------------------- -// StringReplace() -// Give me a string and two patterns "old" and "new", and I replace -// the first instance of "old" in the string with "new", if it -// exists. RETURN a new string, regardless of whether the replacement -// happened or not. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT std::string StringReplace(const std::string& s, - const std::string& oldsub, - const std::string& newsub, - bool replace_all); - -// ---------------------------------------------------------------------- -// SplitStringUsing() -// Split a string using a character delimiter. Append the components -// to 'result'. If there are consecutive delimiters, this function skips -// over all of them. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT void SplitStringUsing(StringPiece full, const char* delim, - std::vector* res); - -// Split a string using one or more byte delimiters, presented -// as a nul-terminated c string. Append the components to 'result'. -// If there are consecutive delimiters, this function will return -// corresponding empty strings. If you want to drop the empty -// strings, try SplitStringUsing(). -// -// If "full" is the empty string, yields an empty string as the only value. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT void SplitStringAllowEmpty(StringPiece full, const char* delim, - std::vector* result); - -// ---------------------------------------------------------------------- -// Split() -// Split a string using a character delimiter. -// ---------------------------------------------------------------------- -inline std::vector Split(StringPiece full, const char* delim, - bool skip_empty = true) { - std::vector result; - if (skip_empty) { - SplitStringUsing(full, delim, &result); - } else { - SplitStringAllowEmpty(full, delim, &result); - } - return result; -} - -// ---------------------------------------------------------------------- -// JoinStrings() -// These methods concatenate a vector of strings into a C++ string, using -// the C-string "delim" as a separator between components. There are two -// flavors of the function, one flavor returns the concatenated string, -// another takes a pointer to the target string. In the latter case the -// target string is cleared and overwritten. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT void JoinStrings(const std::vector& components, - const char* delim, std::string* result); - -inline std::string JoinStrings(const std::vector& components, - const char* delim) { - std::string result; - JoinStrings(components, delim, &result); - return result; -} - -// ---------------------------------------------------------------------- -// UnescapeCEscapeSequences() -// Copies "source" to "dest", rewriting C-style escape sequences -// -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII -// equivalents. "dest" must be sufficiently large to hold all -// the characters in the rewritten string (i.e. at least as large -// as strlen(source) + 1 should be safe, since the replacements -// are always shorter than the original escaped sequences). It's -// safe for source and dest to be the same. RETURNS the length -// of dest. -// -// It allows hex sequences \xhh, or generally \xhhhhh with an -// arbitrary number of hex digits, but all of them together must -// specify a value of a single byte (e.g. \x0045 is equivalent -// to \x45, and \x1234 is erroneous). -// -// It also allows escape sequences of the form \uhhhh (exactly four -// hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight -// hex digits, upper or lower case) to specify a Unicode code -// point. The dest array will contain the UTF8-encoded version of -// that code-point (e.g., if source contains \u2019, then dest will -// contain the three bytes 0xE2, 0x80, and 0x99). -// -// Errors: In the first form of the call, errors are reported with -// LOG(ERROR). The same is true for the second form of the call if -// the pointer to the string std::vector is nullptr; otherwise, error -// messages are stored in the std::vector. In either case, the effect on -// the dest array is not defined, but rest of the source will be -// processed. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest); -PROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest, - std::vector* errors); - -// ---------------------------------------------------------------------- -// UnescapeCEscapeString() -// This does the same thing as UnescapeCEscapeSequences, but creates -// a new string. The caller does not need to worry about allocating -// a dest buffer. This should be used for non performance critical -// tasks such as printing debug messages. It is safe for src and dest -// to be the same. -// -// The second call stores its errors in a supplied string vector. -// If the string vector pointer is nullptr, it reports the errors with LOG(). -// -// In the first and second calls, the length of dest is returned. In the -// the third call, the new string is returned. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT int UnescapeCEscapeString(const std::string& src, - std::string* dest); -PROTOBUF_EXPORT int UnescapeCEscapeString(const std::string& src, - std::string* dest, - std::vector* errors); -PROTOBUF_EXPORT std::string UnescapeCEscapeString(const std::string& src); - -// ---------------------------------------------------------------------- -// CEscape() -// Escapes 'src' using C-style escape sequences and returns the resulting -// string. -// -// Escaped chars: \n, \r, \t, ", ', \, and !isprint(). -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT std::string CEscape(const std::string& src); - -// ---------------------------------------------------------------------- -// CEscapeAndAppend() -// Escapes 'src' using C-style escape sequences, and appends the escaped -// string to 'dest'. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT void CEscapeAndAppend(StringPiece src, std::string* dest); - -namespace strings { -// Like CEscape() but does not escape bytes with the upper bit set. -PROTOBUF_EXPORT std::string Utf8SafeCEscape(const std::string& src); - -// Like CEscape() but uses hex (\x) escapes instead of octals. -PROTOBUF_EXPORT std::string CHexEscape(const std::string& src); -} // namespace strings - -// ---------------------------------------------------------------------- -// strto32() -// strtou32() -// strto64() -// strtou64() -// Architecture-neutral plug compatible replacements for strtol() and -// strtoul(). Long's have different lengths on ILP-32 and LP-64 -// platforms, so using these is safer, from the point of view of -// overflow behavior, than using the standard libc functions. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT int32 strto32_adaptor(const char* nptr, char** endptr, - int base); -PROTOBUF_EXPORT uint32 strtou32_adaptor(const char* nptr, char** endptr, - int base); - -inline int32 strto32(const char *nptr, char **endptr, int base) { - if (sizeof(int32) == sizeof(long)) - return strtol(nptr, endptr, base); - else - return strto32_adaptor(nptr, endptr, base); -} - -inline uint32 strtou32(const char *nptr, char **endptr, int base) { - if (sizeof(uint32) == sizeof(unsigned long)) - return strtoul(nptr, endptr, base); - else - return strtou32_adaptor(nptr, endptr, base); -} - -// For now, long long is 64-bit on all the platforms we care about, so these -// functions can simply pass the call to strto[u]ll. -inline int64 strto64(const char *nptr, char **endptr, int base) { - GOOGLE_COMPILE_ASSERT(sizeof(int64) == sizeof(long long), - sizeof_int64_is_not_sizeof_long_long); - return strtoll(nptr, endptr, base); -} - -inline uint64 strtou64(const char *nptr, char **endptr, int base) { - GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long), - sizeof_uint64_is_not_sizeof_long_long); - return strtoull(nptr, endptr, base); -} - -// ---------------------------------------------------------------------- -// safe_strtob() -// safe_strto32() -// safe_strtou32() -// safe_strto64() -// safe_strtou64() -// safe_strtof() -// safe_strtod() -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value); - -PROTOBUF_EXPORT bool safe_strto32(const std::string& str, int32* value); -PROTOBUF_EXPORT bool safe_strtou32(const std::string& str, uint32* value); -inline bool safe_strto32(const char* str, int32* value) { - return safe_strto32(std::string(str), value); -} -inline bool safe_strto32(StringPiece str, int32* value) { - return safe_strto32(str.ToString(), value); -} -inline bool safe_strtou32(const char* str, uint32* value) { - return safe_strtou32(std::string(str), value); -} -inline bool safe_strtou32(StringPiece str, uint32* value) { - return safe_strtou32(str.ToString(), value); -} - -PROTOBUF_EXPORT bool safe_strto64(const std::string& str, int64* value); -PROTOBUF_EXPORT bool safe_strtou64(const std::string& str, uint64* value); -inline bool safe_strto64(const char* str, int64* value) { - return safe_strto64(std::string(str), value); -} -inline bool safe_strto64(StringPiece str, int64* value) { - return safe_strto64(str.ToString(), value); -} -inline bool safe_strtou64(const char* str, uint64* value) { - return safe_strtou64(std::string(str), value); -} -inline bool safe_strtou64(StringPiece str, uint64* value) { - return safe_strtou64(str.ToString(), value); -} - -PROTOBUF_EXPORT bool safe_strtof(const char* str, float* value); -PROTOBUF_EXPORT bool safe_strtod(const char* str, double* value); -inline bool safe_strtof(const std::string& str, float* value) { - return safe_strtof(str.c_str(), value); -} -inline bool safe_strtod(const std::string& str, double* value) { - return safe_strtod(str.c_str(), value); -} -inline bool safe_strtof(StringPiece str, float* value) { - return safe_strtof(str.ToString(), value); -} -inline bool safe_strtod(StringPiece str, double* value) { - return safe_strtod(str.ToString(), value); -} - -// ---------------------------------------------------------------------- -// FastIntToBuffer() -// FastHexToBuffer() -// FastHex64ToBuffer() -// FastHex32ToBuffer() -// FastTimeToBuffer() -// These are intended for speed. FastIntToBuffer() assumes the -// integer is non-negative. FastHexToBuffer() puts output in -// hex rather than decimal. FastTimeToBuffer() puts the output -// into RFC822 format. -// -// FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format, -// padded to exactly 16 bytes (plus one byte for '\0') -// -// FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format, -// padded to exactly 8 bytes (plus one byte for '\0') -// -// All functions take the output buffer as an arg. -// They all return a pointer to the beginning of the output, -// which may not be the beginning of the input buffer. -// ---------------------------------------------------------------------- - -// Suggested buffer size for FastToBuffer functions. Also works with -// DoubleToBuffer() and FloatToBuffer(). -static const int kFastToBufferSize = 32; - -PROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer); -PROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer); -char* FastUInt32ToBuffer(uint32 i, char* buffer); // inline below -char* FastUInt64ToBuffer(uint64 i, char* buffer); // inline below -PROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer); -PROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer); -PROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer); - -// at least 22 bytes long -inline char* FastIntToBuffer(int i, char* buffer) { - return (sizeof(i) == 4 ? - FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer)); -} -inline char* FastUIntToBuffer(unsigned int i, char* buffer) { - return (sizeof(i) == 4 ? - FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer)); -} -inline char* FastLongToBuffer(long i, char* buffer) { - return (sizeof(i) == 4 ? - FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer)); -} -inline char* FastULongToBuffer(unsigned long i, char* buffer) { - return (sizeof(i) == 4 ? - FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer)); -} - -// ---------------------------------------------------------------------- -// FastInt32ToBufferLeft() -// FastUInt32ToBufferLeft() -// FastInt64ToBufferLeft() -// FastUInt64ToBufferLeft() -// -// Like the Fast*ToBuffer() functions above, these are intended for speed. -// Unlike the Fast*ToBuffer() functions, however, these functions write -// their output to the beginning of the buffer (hence the name, as the -// output is left-aligned). The caller is responsible for ensuring that -// the buffer has enough space to hold the output. -// -// Returns a pointer to the end of the string (i.e. the null character -// terminating the string). -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer); -PROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer); -PROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer); -PROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer); - -// Just define these in terms of the above. -inline char* FastUInt32ToBuffer(uint32 i, char* buffer) { - FastUInt32ToBufferLeft(i, buffer); - return buffer; -} -inline char* FastUInt64ToBuffer(uint64 i, char* buffer) { - FastUInt64ToBufferLeft(i, buffer); - return buffer; -} - -inline std::string SimpleBtoa(bool value) { return value ? "true" : "false"; } - -// ---------------------------------------------------------------------- -// SimpleItoa() -// Description: converts an integer to a string. -// -// Return value: string -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT std::string SimpleItoa(int i); -PROTOBUF_EXPORT std::string SimpleItoa(unsigned int i); -PROTOBUF_EXPORT std::string SimpleItoa(long i); -PROTOBUF_EXPORT std::string SimpleItoa(unsigned long i); -PROTOBUF_EXPORT std::string SimpleItoa(long long i); -PROTOBUF_EXPORT std::string SimpleItoa(unsigned long long i); - -// ---------------------------------------------------------------------- -// SimpleDtoa() -// SimpleFtoa() -// DoubleToBuffer() -// FloatToBuffer() -// Description: converts a double or float to a string which, if -// passed to NoLocaleStrtod(), will produce the exact same original double -// (except in case of NaN; all NaNs are considered the same value). -// We try to keep the string short but it's not guaranteed to be as -// short as possible. -// -// DoubleToBuffer() and FloatToBuffer() write the text to the given -// buffer and return it. The buffer must be at least -// kDoubleToBufferSize bytes for doubles and kFloatToBufferSize -// bytes for floats. kFastToBufferSize is also guaranteed to be large -// enough to hold either. -// -// Return value: string -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT std::string SimpleDtoa(double value); -PROTOBUF_EXPORT std::string SimpleFtoa(float value); - -PROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer); -PROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer); - -// In practice, doubles should never need more than 24 bytes and floats -// should never need more than 14 (including null terminators), but we -// overestimate to be safe. -static const int kDoubleToBufferSize = 32; -static const int kFloatToBufferSize = 24; - -namespace strings { - -enum PadSpec { - NO_PAD = 1, - ZERO_PAD_2, - ZERO_PAD_3, - ZERO_PAD_4, - ZERO_PAD_5, - ZERO_PAD_6, - ZERO_PAD_7, - ZERO_PAD_8, - ZERO_PAD_9, - ZERO_PAD_10, - ZERO_PAD_11, - ZERO_PAD_12, - ZERO_PAD_13, - ZERO_PAD_14, - ZERO_PAD_15, - ZERO_PAD_16, -}; - -struct Hex { - uint64 value; - enum PadSpec spec; - template - explicit Hex(Int v, PadSpec s = NO_PAD) - : spec(s) { - // Prevent sign-extension by casting integers to - // their unsigned counterparts. -#ifdef LANG_CXX11 - static_assert( - sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8, - "Unknown integer type"); -#endif - value = sizeof(v) == 1 ? static_cast(v) - : sizeof(v) == 2 ? static_cast(v) - : sizeof(v) == 4 ? static_cast(v) - : static_cast(v); - } -}; - -struct PROTOBUF_EXPORT AlphaNum { - const char *piece_data_; // move these to string_ref eventually - size_t piece_size_; // move these to string_ref eventually - - char digits[kFastToBufferSize]; - - // No bool ctor -- bools convert to an integral type. - // A bool ctor would also convert incoming pointers (bletch). - - AlphaNum(int i32) - : piece_data_(digits), - piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {} - AlphaNum(unsigned int u32) - : piece_data_(digits), - piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {} - AlphaNum(long long i64) - : piece_data_(digits), - piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {} - AlphaNum(unsigned long long u64) - : piece_data_(digits), - piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {} - - // Note: on some architectures, "long" is only 32 bits, not 64, but the - // performance hit of using FastInt64ToBufferLeft to handle 32-bit values - // is quite minor. - AlphaNum(long i64) - : piece_data_(digits), - piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {} - AlphaNum(unsigned long u64) - : piece_data_(digits), - piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {} - - AlphaNum(float f) - : piece_data_(digits), piece_size_(strlen(FloatToBuffer(f, digits))) {} - AlphaNum(double f) - : piece_data_(digits), piece_size_(strlen(DoubleToBuffer(f, digits))) {} - - AlphaNum(Hex hex); - - AlphaNum(const char* c_str) - : piece_data_(c_str), piece_size_(strlen(c_str)) {} - // TODO: Add a string_ref constructor, eventually - // AlphaNum(const StringPiece &pc) : piece(pc) {} - - AlphaNum(const std::string& str) - : piece_data_(str.data()), piece_size_(str.size()) {} - - AlphaNum(StringPiece str) - : piece_data_(str.data()), piece_size_(str.size()) {} - - AlphaNum(internal::StringPiecePod str) - : piece_data_(str.data()), piece_size_(str.size()) {} - - size_t size() const { return piece_size_; } - const char *data() const { return piece_data_; } - - private: - // Use ":" not ':' - AlphaNum(char c); // NOLINT(runtime/explicit) - - // Disallow copy and assign. - AlphaNum(const AlphaNum&); - void operator=(const AlphaNum&); -}; - -} // namespace strings - -using strings::AlphaNum; - -// ---------------------------------------------------------------------- -// StrCat() -// This merges the given strings or numbers, with no delimiter. This -// is designed to be the fastest possible way to construct a string out -// of a mix of raw C strings, strings, bool values, -// and numeric values. -// -// Don't use this for user-visible strings. The localization process -// works poorly on strings built up out of fragments. -// -// For clarity and performance, don't use StrCat when appending to a -// string. In particular, avoid using any of these (anti-)patterns: -// str.append(StrCat(...) -// str += StrCat(...) -// str = StrCat(str, ...) -// where the last is the worse, with the potential to change a loop -// from a linear time operation with O(1) dynamic allocations into a -// quadratic time operation with O(n) dynamic allocations. StrAppend -// is a better choice than any of the above, subject to the restriction -// of StrAppend(&str, a, b, c, ...) that none of the a, b, c, ... may -// be a reference into str. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f, - const AlphaNum& g); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f, - const AlphaNum& g, const AlphaNum& h); -PROTOBUF_EXPORT std::string StrCat(const AlphaNum& a, const AlphaNum& b, - const AlphaNum& c, const AlphaNum& d, - const AlphaNum& e, const AlphaNum& f, - const AlphaNum& g, const AlphaNum& h, - const AlphaNum& i); - -inline std::string StrCat(const AlphaNum& a) { - return std::string(a.data(), a.size()); -} - -// ---------------------------------------------------------------------- -// StrAppend() -// Same as above, but adds the output to the given string. -// WARNING: For speed, StrAppend does not try to check each of its input -// arguments to be sure that they are not a subset of the string being -// appended to. That is, while this will work: -// -// string s = "foo"; -// s += s; -// -// This will not (necessarily) work: -// -// string s = "foo"; -// StrAppend(&s, s); -// -// Note: while StrCat supports appending up to 9 arguments, StrAppend -// is currently limited to 4. That's rarely an issue except when -// automatically transforming StrCat to StrAppend, and can easily be -// worked around as consecutive calls to StrAppend are quite efficient. -// ---------------------------------------------------------------------- - -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a); -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a, - const AlphaNum& b); -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a, - const AlphaNum& b, const AlphaNum& c); -PROTOBUF_EXPORT void StrAppend(std::string* dest, const AlphaNum& a, - const AlphaNum& b, const AlphaNum& c, - const AlphaNum& d); - -// ---------------------------------------------------------------------- -// Join() -// These methods concatenate a range of components into a C++ string, using -// the C-string "delim" as a separator between components. -// ---------------------------------------------------------------------- -template -void Join(Iterator start, Iterator end, const char* delim, - std::string* result) { - for (Iterator it = start; it != end; ++it) { - if (it != start) { - result->append(delim); - } - StrAppend(result, *it); - } -} - -template -std::string Join(const Range& components, const char* delim) { - std::string result; - Join(components.begin(), components.end(), delim, &result); - return result; -} - -// ---------------------------------------------------------------------- -// ToHex() -// Return a lower-case hex string representation of the given integer. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT std::string ToHex(uint64 num); - -// ---------------------------------------------------------------------- -// GlobalReplaceSubstring() -// Replaces all instances of a substring in a string. Does nothing -// if 'substring' is empty. Returns the number of replacements. -// -// NOTE: The string pieces must not overlap s. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT int GlobalReplaceSubstring(const std::string& substring, - const std::string& replacement, - std::string* s); - -// ---------------------------------------------------------------------- -// Base64Unescape() -// Converts "src" which is encoded in Base64 to its binary equivalent and -// writes it to "dest". If src contains invalid characters, dest is cleared -// and the function returns false. Returns true on success. -// ---------------------------------------------------------------------- -PROTOBUF_EXPORT bool Base64Unescape(StringPiece src, std::string* dest); - -// ---------------------------------------------------------------------- -// WebSafeBase64Unescape() -// This is a variation of Base64Unescape which uses '-' instead of '+', and -// '_' instead of '/'. src is not null terminated, instead specify len. I -// recommend that slen - -#include - -namespace google { -namespace protobuf { -namespace internal { - -struct DateTime { - int year; - int month; - int day; - int hour; - int minute; - int second; -}; - -// Converts a timestamp (seconds elapsed since 1970-01-01T00:00:00, could be -// negative to represent time before 1970-01-01) to DateTime. Returns false -// if the timestamp is not in the range between 0001-01-01T00:00:00 and -// 9999-12-31T23:59:59. -bool PROTOBUF_EXPORT SecondsToDateTime(int64 seconds, DateTime* time); -// Converts DateTime to a timestamp (seconds since 1970-01-01T00:00:00). -// Returns false if the DateTime is not valid or is not in the valid range. -bool PROTOBUF_EXPORT DateTimeToSeconds(const DateTime& time, int64* seconds); - -void PROTOBUF_EXPORT GetCurrentTime(int64* seconds, int32* nanos); - -// Formats a time string in RFC3339 format. -// -// For example, "2015-05-20T13:29:35.120Z". For nanos, 0, 3, 6 or 9 fractional -// digits will be used depending on how many are required to represent the exact -// value. -// -// Note that "nanos" must in the range of [0, 999999999]. -std::string PROTOBUF_EXPORT FormatTime(int64 seconds, int32 nanos); -// Parses a time string. This method accepts RFC3339 date/time string with UTC -// offset. For example, "2015-05-20T13:29:35.120-08:00". -bool PROTOBUF_EXPORT ParseTime(const std::string& value, int64* seconds, - int32* nanos); - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_STUBS_TIME_H_ diff --git a/third_party/protobuf-lite/google/protobuf/unknown_field_set.h b/third_party/protobuf-lite/google/protobuf/unknown_field_set.h deleted file mode 100644 index ab3633dad..000000000 --- a/third_party/protobuf-lite/google/protobuf/unknown_field_set.h +++ /dev/null @@ -1,411 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// Contains classes used to keep track of unrecognized fields seen while -// parsing a protocol message. - -#ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ -#define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#ifdef SWIG -#error "You cannot SWIG proto headers" -#endif - -namespace google { -namespace protobuf { -namespace internal { -class InternalMetadata; // metadata_lite.h -class WireFormat; // wire_format.h -class MessageSetFieldSkipperUsingCord; -// extension_set_heavy.cc -} // namespace internal - -class Message; // message.h -class UnknownField; // below - -// An UnknownFieldSet contains fields that were encountered while parsing a -// message but were not defined by its type. Keeping track of these can be -// useful, especially in that they may be written if the message is serialized -// again without being cleared in between. This means that software which -// simply receives messages and forwards them to other servers does not need -// to be updated every time a new field is added to the message definition. -// -// To get the UnknownFieldSet attached to any message, call -// Reflection::GetUnknownFields(). -// -// This class is necessarily tied to the protocol buffer wire format, unlike -// the Reflection interface which is independent of any serialization scheme. -class PROTOBUF_EXPORT UnknownFieldSet { - public: - UnknownFieldSet(); - ~UnknownFieldSet(); - - // Remove all fields. - inline void Clear(); - - // Remove all fields and deallocate internal data objects - void ClearAndFreeMemory(); - - // Is this set empty? - inline bool empty() const; - - // Merge the contents of some other UnknownFieldSet with this one. - void MergeFrom(const UnknownFieldSet& other); - - // Similar to above, but this function will destroy the contents of other. - void MergeFromAndDestroy(UnknownFieldSet* other); - - // Merge the contents an UnknownFieldSet with the UnknownFieldSet in - // *metadata, if there is one. If *metadata doesn't have an UnknownFieldSet - // then add one to it and make it be a copy of the first arg. - static void MergeToInternalMetadata(const UnknownFieldSet& other, - internal::InternalMetadata* metadata); - - // Swaps the contents of some other UnknownFieldSet with this one. - inline void Swap(UnknownFieldSet* x); - - // Computes (an estimate of) the total number of bytes currently used for - // storing the unknown fields in memory. Does NOT include - // sizeof(*this) in the calculation. - size_t SpaceUsedExcludingSelfLong() const; - - int SpaceUsedExcludingSelf() const { - return internal::ToIntSize(SpaceUsedExcludingSelfLong()); - } - - // Version of SpaceUsed() including sizeof(*this). - size_t SpaceUsedLong() const; - - int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); } - - // Returns the number of fields present in the UnknownFieldSet. - inline int field_count() const; - // Get a field in the set, where 0 <= index < field_count(). The fields - // appear in the order in which they were added. - inline const UnknownField& field(int index) const; - // Get a mutable pointer to a field in the set, where - // 0 <= index < field_count(). The fields appear in the order in which - // they were added. - inline UnknownField* mutable_field(int index); - - // Adding fields --------------------------------------------------- - - void AddVarint(int number, uint64 value); - void AddFixed32(int number, uint32 value); - void AddFixed64(int number, uint64 value); - void AddLengthDelimited(int number, const std::string& value); - std::string* AddLengthDelimited(int number); - UnknownFieldSet* AddGroup(int number); - - // Adds an unknown field from another set. - void AddField(const UnknownField& field); - - // Delete fields with indices in the range [start .. start+num-1]. - // Caution: implementation moves all fields with indices [start+num .. ]. - void DeleteSubrange(int start, int num); - - // Delete all fields with a specific field number. The order of left fields - // is preserved. - // Caution: implementation moves all fields after the first deleted field. - void DeleteByNumber(int number); - - // Parsing helpers ------------------------------------------------- - // These work exactly like the similarly-named methods of Message. - - bool MergeFromCodedStream(io::CodedInputStream* input); - bool ParseFromCodedStream(io::CodedInputStream* input); - bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input); - bool ParseFromArray(const void* data, int size); - inline bool ParseFromString(const std::string& data) { - return ParseFromArray(data.data(), static_cast(data.size())); - } - - // Merges this message's unknown field data (if any). This works whether - // the message is a lite or full proto (for legacy reasons, lite and full - // return different types for MessageType::unknown_fields()). - template - bool MergeFromMessage(const MessageType& message); - - static const UnknownFieldSet& default_instance(); - - private: - // For InternalMergeFrom - friend class UnknownField; - // Merges from other UnknownFieldSet. This method assumes, that this object - // is newly created and has no fields. - void InternalMergeFrom(const UnknownFieldSet& other); - void ClearFallback(); - - template ::value, int>::type = 0> - bool InternalMergeFromMessage(const MessageType& message) { - MergeFrom(message.GetReflection()->GetUnknownFields(message)); - return true; - } - - template ::value && - !std::is_base_of::value, - int>::type = 0> - bool InternalMergeFromMessage(const MessageType& message) { - const auto& unknown_fields = message.unknown_fields(); - io::ArrayInputStream array_stream(unknown_fields.data(), - unknown_fields.size()); - io::CodedInputStream coded_stream(&array_stream); - return MergeFromCodedStream(&coded_stream); - } - - std::vector fields_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet); -}; - -namespace internal { - -inline void WriteVarint(uint32 num, uint64 val, UnknownFieldSet* unknown) { - unknown->AddVarint(num, val); -} -inline void WriteLengthDelimited(uint32 num, StringPiece val, - UnknownFieldSet* unknown) { - unknown->AddLengthDelimited(num)->assign(val.data(), val.size()); -} - -PROTOBUF_EXPORT -const char* UnknownGroupParse(UnknownFieldSet* unknown, const char* ptr, - ParseContext* ctx); -PROTOBUF_EXPORT -const char* UnknownFieldParse(uint64 tag, UnknownFieldSet* unknown, - const char* ptr, ParseContext* ctx); - -} // namespace internal - -// Represents one field in an UnknownFieldSet. -class PROTOBUF_EXPORT UnknownField { - public: - enum Type { - TYPE_VARINT, - TYPE_FIXED32, - TYPE_FIXED64, - TYPE_LENGTH_DELIMITED, - TYPE_GROUP - }; - - // The field's field number, as seen on the wire. - inline int number() const; - - // The field type. - inline Type type() const; - - // Accessors ------------------------------------------------------- - // Each method works only for UnknownFields of the corresponding type. - - inline uint64 varint() const; - inline uint32 fixed32() const; - inline uint64 fixed64() const; - inline const std::string& length_delimited() const; - inline const UnknownFieldSet& group() const; - - inline void set_varint(uint64 value); - inline void set_fixed32(uint32 value); - inline void set_fixed64(uint64 value); - inline void set_length_delimited(const std::string& value); - inline std::string* mutable_length_delimited(); - inline UnknownFieldSet* mutable_group(); - - // Serialization API. - // These methods can take advantage of the underlying implementation and may - // archieve a better performance than using getters to retrieve the data and - // do the serialization yourself. - void SerializeLengthDelimitedNoTag(io::CodedOutputStream* output) const { - output->SetCur(InternalSerializeLengthDelimitedNoTag(output->Cur(), - output->EpsCopy())); - } - - inline size_t GetLengthDelimitedSize() const; - uint8* InternalSerializeLengthDelimitedNoTag( - uint8* target, io::EpsCopyOutputStream* stream) const; - - - // If this UnknownField contains a pointer, delete it. - void Delete(); - - // Make a deep copy of any pointers in this UnknownField. - void DeepCopy(const UnknownField& other); - - // Set the wire type of this UnknownField. Should only be used when this - // UnknownField is being created. - inline void SetType(Type type); - - union LengthDelimited { - std::string* string_value; - }; - - uint32 number_; - uint32 type_; - union { - uint64 varint_; - uint32 fixed32_; - uint64 fixed64_; - mutable union LengthDelimited length_delimited_; - UnknownFieldSet* group_; - } data_; -}; - -// =================================================================== -// inline implementations - -inline UnknownFieldSet::UnknownFieldSet() {} - -inline UnknownFieldSet::~UnknownFieldSet() { Clear(); } - -inline void UnknownFieldSet::ClearAndFreeMemory() { Clear(); } - -inline void UnknownFieldSet::Clear() { - if (!fields_.empty()) { - ClearFallback(); - } -} - -inline bool UnknownFieldSet::empty() const { return fields_.empty(); } - -inline void UnknownFieldSet::Swap(UnknownFieldSet* x) { - fields_.swap(x->fields_); -} - -inline int UnknownFieldSet::field_count() const { - return static_cast(fields_.size()); -} -inline const UnknownField& UnknownFieldSet::field(int index) const { - return (fields_)[static_cast(index)]; -} -inline UnknownField* UnknownFieldSet::mutable_field(int index) { - return &(fields_)[static_cast(index)]; -} - -inline void UnknownFieldSet::AddLengthDelimited(int number, - const std::string& value) { - AddLengthDelimited(number)->assign(value); -} - - - - -inline int UnknownField::number() const { return static_cast(number_); } -inline UnknownField::Type UnknownField::type() const { - return static_cast(type_); -} - -inline uint64 UnknownField::varint() const { - assert(type() == TYPE_VARINT); - return data_.varint_; -} -inline uint32 UnknownField::fixed32() const { - assert(type() == TYPE_FIXED32); - return data_.fixed32_; -} -inline uint64 UnknownField::fixed64() const { - assert(type() == TYPE_FIXED64); - return data_.fixed64_; -} -inline const std::string& UnknownField::length_delimited() const { - assert(type() == TYPE_LENGTH_DELIMITED); - return *data_.length_delimited_.string_value; -} -inline const UnknownFieldSet& UnknownField::group() const { - assert(type() == TYPE_GROUP); - return *data_.group_; -} - -inline void UnknownField::set_varint(uint64 value) { - assert(type() == TYPE_VARINT); - data_.varint_ = value; -} -inline void UnknownField::set_fixed32(uint32 value) { - assert(type() == TYPE_FIXED32); - data_.fixed32_ = value; -} -inline void UnknownField::set_fixed64(uint64 value) { - assert(type() == TYPE_FIXED64); - data_.fixed64_ = value; -} -inline void UnknownField::set_length_delimited(const std::string& value) { - assert(type() == TYPE_LENGTH_DELIMITED); - data_.length_delimited_.string_value->assign(value); -} -inline std::string* UnknownField::mutable_length_delimited() { - assert(type() == TYPE_LENGTH_DELIMITED); - return data_.length_delimited_.string_value; -} -inline UnknownFieldSet* UnknownField::mutable_group() { - assert(type() == TYPE_GROUP); - return data_.group_; -} -template -bool UnknownFieldSet::MergeFromMessage(const MessageType& message) { - // SFINAE will route to the right version. - return InternalMergeFromMessage(message); -} - - -inline size_t UnknownField::GetLengthDelimitedSize() const { - GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type()); - return data_.length_delimited_.string_value->size(); -} - -inline void UnknownField::SetType(Type type) { - type_ = type; -} - - -} // namespace protobuf -} // namespace google - -#include -#endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ diff --git a/third_party/protobuf-lite/google/protobuf/wire_format_lite.h b/third_party/protobuf-lite/google/protobuf/wire_format_lite.h deleted file mode 100644 index c742fe869..000000000 --- a/third_party/protobuf-lite/google/protobuf/wire_format_lite.h +++ /dev/null @@ -1,1866 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// atenasio@google.com (Chris Atenasio) (ZigZag transform) -// wink@google.com (Wink Saville) (refactored from wire_format.h) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// This header is logically internal, but is made public because it is used -// from protocol-compiler-generated code, which may reside in other components. - -#ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ -#define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -// Do UTF-8 validation on string type in Debug build only -#ifndef NDEBUG -#define GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED -#endif - -// Avoid conflict with iOS where #defines TYPE_BOOL. -// -// If some one needs the macro TYPE_BOOL in a file that includes this header, -// it's possible to bring it back using push/pop_macro as follows. -// -// #pragma push_macro("TYPE_BOOL") -// #include this header and/or all headers that need the macro to be undefined. -// #pragma pop_macro("TYPE_BOOL") -#undef TYPE_BOOL - - -namespace google { -namespace protobuf { -namespace internal { - -#include - -// This class is for internal use by the protocol buffer library and by -// protocol-compiler-generated message classes. It must not be called -// directly by clients. -// -// This class contains helpers for implementing the binary protocol buffer -// wire format without the need for reflection. Use WireFormat when using -// reflection. -// -// This class is really a namespace that contains only static methods. -class PROTOBUF_EXPORT WireFormatLite { - public: - // ----------------------------------------------------------------- - // Helper constants and functions related to the format. These are - // mostly meant for internal and generated code to use. - - // The wire format is composed of a sequence of tag/value pairs, each - // of which contains the value of one field (or one element of a repeated - // field). Each tag is encoded as a varint. The lower bits of the tag - // identify its wire type, which specifies the format of the data to follow. - // The rest of the bits contain the field number. Each type of field (as - // declared by FieldDescriptor::Type, in descriptor.h) maps to one of - // these wire types. Immediately following each tag is the field's value, - // encoded in the format specified by the wire type. Because the tag - // identifies the encoding of this data, it is possible to skip - // unrecognized fields for forwards compatibility. - - enum WireType { - WIRETYPE_VARINT = 0, - WIRETYPE_FIXED64 = 1, - WIRETYPE_LENGTH_DELIMITED = 2, - WIRETYPE_START_GROUP = 3, - WIRETYPE_END_GROUP = 4, - WIRETYPE_FIXED32 = 5, - }; - - // Lite alternative to FieldDescriptor::Type. Must be kept in sync. - enum FieldType { - TYPE_DOUBLE = 1, - TYPE_FLOAT = 2, - TYPE_INT64 = 3, - TYPE_UINT64 = 4, - TYPE_INT32 = 5, - TYPE_FIXED64 = 6, - TYPE_FIXED32 = 7, - TYPE_BOOL = 8, - TYPE_STRING = 9, - TYPE_GROUP = 10, - TYPE_MESSAGE = 11, - TYPE_BYTES = 12, - TYPE_UINT32 = 13, - TYPE_ENUM = 14, - TYPE_SFIXED32 = 15, - TYPE_SFIXED64 = 16, - TYPE_SINT32 = 17, - TYPE_SINT64 = 18, - MAX_FIELD_TYPE = 18, - }; - - // Lite alternative to FieldDescriptor::CppType. Must be kept in sync. - enum CppType { - CPPTYPE_INT32 = 1, - CPPTYPE_INT64 = 2, - CPPTYPE_UINT32 = 3, - CPPTYPE_UINT64 = 4, - CPPTYPE_DOUBLE = 5, - CPPTYPE_FLOAT = 6, - CPPTYPE_BOOL = 7, - CPPTYPE_ENUM = 8, - CPPTYPE_STRING = 9, - CPPTYPE_MESSAGE = 10, - MAX_CPPTYPE = 10, - }; - - // Helper method to get the CppType for a particular Type. - static CppType FieldTypeToCppType(FieldType type); - - // Given a FieldDescriptor::Type return its WireType - static inline WireFormatLite::WireType WireTypeForFieldType( - WireFormatLite::FieldType type) { - return kWireTypeForFieldType[type]; - } - - // Number of bits in a tag which identify the wire type. - static constexpr int kTagTypeBits = 3; - // Mask for those bits. - static constexpr uint32 kTagTypeMask = (1 << kTagTypeBits) - 1; - - // Helper functions for encoding and decoding tags. (Inlined below and in - // _inl.h) - // - // This is different from MakeTag(field->number(), field->type()) in the - // case of packed repeated fields. - constexpr static uint32 MakeTag(int field_number, WireType type); - static WireType GetTagWireType(uint32 tag); - static int GetTagFieldNumber(uint32 tag); - - // Compute the byte size of a tag. For groups, this includes both the start - // and end tags. - static inline size_t TagSize(int field_number, - WireFormatLite::FieldType type); - - // Skips a field value with the given tag. The input should start - // positioned immediately after the tag. Skipped values are simply - // discarded, not recorded anywhere. See WireFormat::SkipField() for a - // version that records to an UnknownFieldSet. - static bool SkipField(io::CodedInputStream* input, uint32 tag); - - // Skips a field value with the given tag. The input should start - // positioned immediately after the tag. Skipped values are recorded to a - // CodedOutputStream. - static bool SkipField(io::CodedInputStream* input, uint32 tag, - io::CodedOutputStream* output); - - // Reads and ignores a message from the input. Skipped values are simply - // discarded, not recorded anywhere. See WireFormat::SkipMessage() for a - // version that records to an UnknownFieldSet. - static bool SkipMessage(io::CodedInputStream* input); - - // Reads and ignores a message from the input. Skipped values are recorded - // to a CodedOutputStream. - static bool SkipMessage(io::CodedInputStream* input, - io::CodedOutputStream* output); - - // This macro does the same thing as WireFormatLite::MakeTag(), but the - // result is usable as a compile-time constant, which makes it usable - // as a switch case or a template input. WireFormatLite::MakeTag() is more - // type-safe, though, so prefer it if possible. -#define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \ - static_cast((static_cast(FIELD_NUMBER) << 3) | (TYPE)) - - // These are the tags for the old MessageSet format, which was defined as: - // message MessageSet { - // repeated group Item = 1 { - // required int32 type_id = 2; - // required string message = 3; - // } - // } - static constexpr int kMessageSetItemNumber = 1; - static constexpr int kMessageSetTypeIdNumber = 2; - static constexpr int kMessageSetMessageNumber = 3; - static const int kMessageSetItemStartTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( - kMessageSetItemNumber, WireFormatLite::WIRETYPE_START_GROUP); - static const int kMessageSetItemEndTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( - kMessageSetItemNumber, WireFormatLite::WIRETYPE_END_GROUP); - static const int kMessageSetTypeIdTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( - kMessageSetTypeIdNumber, WireFormatLite::WIRETYPE_VARINT); - static const int kMessageSetMessageTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( - kMessageSetMessageNumber, WireFormatLite::WIRETYPE_LENGTH_DELIMITED); - - // Byte size of all tags of a MessageSet::Item combined. - static const size_t kMessageSetItemTagsSize; - - // Helper functions for converting between floats/doubles and IEEE-754 - // uint32s/uint64s so that they can be written. (Assumes your platform - // uses IEEE-754 floats.) - static uint32 EncodeFloat(float value); - static float DecodeFloat(uint32 value); - static uint64 EncodeDouble(double value); - static double DecodeDouble(uint64 value); - - // Helper functions for mapping signed integers to unsigned integers in - // such a way that numbers with small magnitudes will encode to smaller - // varints. If you simply static_cast a negative number to an unsigned - // number and varint-encode it, it will always take 10 bytes, defeating - // the purpose of varint. So, for the "sint32" and "sint64" field types, - // we ZigZag-encode the values. - static uint32 ZigZagEncode32(int32 n); - static int32 ZigZagDecode32(uint32 n); - static uint64 ZigZagEncode64(int64 n); - static int64 ZigZagDecode64(uint64 n); - - // ================================================================= - // Methods for reading/writing individual field. - - // Read fields, not including tags. The assumption is that you already - // read the tag to determine what field to read. - - // For primitive fields, we just use a templatized routine parameterized by - // the represented type and the FieldType. These are specialized with the - // appropriate definition for each declared type. - template - PROTOBUF_ALWAYS_INLINE static bool ReadPrimitive(io::CodedInputStream* input, - CType* value); - - // Reads repeated primitive values, with optimizations for repeats. - // tag_size and tag should both be compile-time constants provided by the - // protocol compiler. - template - PROTOBUF_ALWAYS_INLINE static bool ReadRepeatedPrimitive( - int tag_size, uint32 tag, io::CodedInputStream* input, - RepeatedField* value); - - // Identical to ReadRepeatedPrimitive, except will not inline the - // implementation. - template - static bool ReadRepeatedPrimitiveNoInline(int tag_size, uint32 tag, - io::CodedInputStream* input, - RepeatedField* value); - - // Reads a primitive value directly from the provided buffer. It returns a - // pointer past the segment of data that was read. - // - // This is only implemented for the types with fixed wire size, e.g. - // float, double, and the (s)fixed* types. - template - PROTOBUF_ALWAYS_INLINE static const uint8* ReadPrimitiveFromArray( - const uint8* buffer, CType* value); - - // Reads a primitive packed field. - // - // This is only implemented for packable types. - template - PROTOBUF_ALWAYS_INLINE static bool ReadPackedPrimitive( - io::CodedInputStream* input, RepeatedField* value); - - // Identical to ReadPackedPrimitive, except will not inline the - // implementation. - template - static bool ReadPackedPrimitiveNoInline(io::CodedInputStream* input, - RepeatedField* value); - - // Read a packed enum field. If the is_valid function is not NULL, values for - // which is_valid(value) returns false are silently dropped. - static bool ReadPackedEnumNoInline(io::CodedInputStream* input, - bool (*is_valid)(int), - RepeatedField* values); - - // Read a packed enum field. If the is_valid function is not NULL, values for - // which is_valid(value) returns false are appended to unknown_fields_stream. - static bool ReadPackedEnumPreserveUnknowns( - io::CodedInputStream* input, int field_number, bool (*is_valid)(int), - io::CodedOutputStream* unknown_fields_stream, RepeatedField* values); - - // Read a string. ReadString(..., std::string* value) requires an - // existing std::string. - static inline bool ReadString(io::CodedInputStream* input, - std::string* value); - // ReadString(..., std::string** p) is internal-only, and should only be - // called from generated code. It starts by setting *p to "new std::string" if - // *p == &GetEmptyStringAlreadyInited(). It then invokes - // ReadString(io::CodedInputStream* input, *p). This is useful for reducing - // code size. - static inline bool ReadString(io::CodedInputStream* input, std::string** p); - // Analogous to ReadString(). - static bool ReadBytes(io::CodedInputStream* input, std::string* value); - static bool ReadBytes(io::CodedInputStream* input, std::string** p); - - enum Operation { - PARSE = 0, - SERIALIZE = 1, - }; - - // Returns true if the data is valid UTF-8. - static bool VerifyUtf8String(const char* data, int size, Operation op, - const char* field_name); - - template - static inline bool ReadGroup(int field_number, io::CodedInputStream* input, - MessageType* value); - - template - static inline bool ReadMessage(io::CodedInputStream* input, - MessageType* value); - - template - static inline bool ReadMessageNoVirtual(io::CodedInputStream* input, - MessageType* value) { - return ReadMessage(input, value); - } - - // Write a tag. The Write*() functions typically include the tag, so - // normally there's no need to call this unless using the Write*NoTag() - // variants. - PROTOBUF_ALWAYS_INLINE static void WriteTag(int field_number, WireType type, - io::CodedOutputStream* output); - - // Write fields, without tags. - PROTOBUF_ALWAYS_INLINE static void WriteInt32NoTag( - int32 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteInt64NoTag( - int64 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteUInt32NoTag( - uint32 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteUInt64NoTag( - uint64 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteSInt32NoTag( - int32 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteSInt64NoTag( - int64 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteFixed32NoTag( - uint32 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteFixed64NoTag( - uint64 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteSFixed32NoTag( - int32 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteSFixed64NoTag( - int64 value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteFloatNoTag( - float value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteDoubleNoTag( - double value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteBoolNoTag( - bool value, io::CodedOutputStream* output); - PROTOBUF_ALWAYS_INLINE static void WriteEnumNoTag( - int value, io::CodedOutputStream* output); - - // Write array of primitive fields, without tags - static void WriteFloatArray(const float* a, int n, - io::CodedOutputStream* output); - static void WriteDoubleArray(const double* a, int n, - io::CodedOutputStream* output); - static void WriteFixed32Array(const uint32* a, int n, - io::CodedOutputStream* output); - static void WriteFixed64Array(const uint64* a, int n, - io::CodedOutputStream* output); - static void WriteSFixed32Array(const int32* a, int n, - io::CodedOutputStream* output); - static void WriteSFixed64Array(const int64* a, int n, - io::CodedOutputStream* output); - static void WriteBoolArray(const bool* a, int n, - io::CodedOutputStream* output); - - // Write fields, including tags. - static void WriteInt32(int field_number, int32 value, - io::CodedOutputStream* output); - static void WriteInt64(int field_number, int64 value, - io::CodedOutputStream* output); - static void WriteUInt32(int field_number, uint32 value, - io::CodedOutputStream* output); - static void WriteUInt64(int field_number, uint64 value, - io::CodedOutputStream* output); - static void WriteSInt32(int field_number, int32 value, - io::CodedOutputStream* output); - static void WriteSInt64(int field_number, int64 value, - io::CodedOutputStream* output); - static void WriteFixed32(int field_number, uint32 value, - io::CodedOutputStream* output); - static void WriteFixed64(int field_number, uint64 value, - io::CodedOutputStream* output); - static void WriteSFixed32(int field_number, int32 value, - io::CodedOutputStream* output); - static void WriteSFixed64(int field_number, int64 value, - io::CodedOutputStream* output); - static void WriteFloat(int field_number, float value, - io::CodedOutputStream* output); - static void WriteDouble(int field_number, double value, - io::CodedOutputStream* output); - static void WriteBool(int field_number, bool value, - io::CodedOutputStream* output); - static void WriteEnum(int field_number, int value, - io::CodedOutputStream* output); - - static void WriteString(int field_number, const std::string& value, - io::CodedOutputStream* output); - static void WriteBytes(int field_number, const std::string& value, - io::CodedOutputStream* output); - static void WriteStringMaybeAliased(int field_number, - const std::string& value, - io::CodedOutputStream* output); - static void WriteBytesMaybeAliased(int field_number, const std::string& value, - io::CodedOutputStream* output); - - static void WriteGroup(int field_number, const MessageLite& value, - io::CodedOutputStream* output); - static void WriteMessage(int field_number, const MessageLite& value, - io::CodedOutputStream* output); - // Like above, but these will check if the output stream has enough - // space to write directly to a flat array. - static void WriteGroupMaybeToArray(int field_number, const MessageLite& value, - io::CodedOutputStream* output); - static void WriteMessageMaybeToArray(int field_number, - const MessageLite& value, - io::CodedOutputStream* output); - - // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The - // pointer must point at an instance of MessageType, *not* a subclass (or - // the subclass must not override SerializeWithCachedSizes()). - template - static inline void WriteGroupNoVirtual(int field_number, - const MessageType& value, - io::CodedOutputStream* output); - template - static inline void WriteMessageNoVirtual(int field_number, - const MessageType& value, - io::CodedOutputStream* output); - - // Like above, but use only *ToArray methods of CodedOutputStream. - PROTOBUF_ALWAYS_INLINE static uint8* WriteTagToArray(int field_number, - WireType type, - uint8* target); - - // Write fields, without tags. - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32NoTagToArray(int32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64NoTagToArray(int64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32NoTagToArray(uint32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64NoTagToArray(uint64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32NoTagToArray(int32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64NoTagToArray(int64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32NoTagToArray(uint32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64NoTagToArray(uint64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32NoTagToArray(int32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64NoTagToArray(int64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatNoTagToArray(float value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleNoTagToArray(double value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolNoTagToArray(bool value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumNoTagToArray(int value, - uint8* target); - - // Write fields, without tags. These require that value.size() > 0. - template - PROTOBUF_ALWAYS_INLINE static uint8* WritePrimitiveNoTagToArray( - const RepeatedField& value, uint8* (*Writer)(T, uint8*), - uint8* target); - template - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixedNoTagToArray( - const RepeatedField& value, uint8* (*Writer)(T, uint8*), - uint8* target); - - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64NoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatNoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleNoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolNoTagToArray( - const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumNoTagToArray( - const RepeatedField& value, uint8* output); - - // Write fields, including tags. - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32ToArray(int field_number, - int32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64ToArray(int field_number, - int64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32ToArray(int field_number, - uint32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64ToArray(int field_number, - uint64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32ToArray(int field_number, - int32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64ToArray(int field_number, - int64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32ToArray(int field_number, - uint32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64ToArray(int field_number, - uint64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32ToArray(int field_number, - int32 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64ToArray(int field_number, - int64 value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatToArray(int field_number, - float value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleToArray(int field_number, - double value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolToArray(int field_number, - bool value, - uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumToArray(int field_number, - int value, - uint8* target); - - template - PROTOBUF_ALWAYS_INLINE static uint8* WritePrimitiveToArray( - int field_number, const RepeatedField& value, - uint8* (*Writer)(int, T, uint8*), uint8* target); - - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt32ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteInt64ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt32ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteUInt64ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt32ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSInt64ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed32ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFixed64ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed32ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteSFixed64ToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteFloatToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteDoubleToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteBoolToArray( - int field_number, const RepeatedField& value, uint8* output); - PROTOBUF_ALWAYS_INLINE static uint8* WriteEnumToArray( - int field_number, const RepeatedField& value, uint8* output); - - PROTOBUF_ALWAYS_INLINE static uint8* WriteStringToArray( - int field_number, const std::string& value, uint8* target); - PROTOBUF_ALWAYS_INLINE static uint8* WriteBytesToArray( - int field_number, const std::string& value, uint8* target); - - // Whether to serialize deterministically (e.g., map keys are - // sorted) is a property of a CodedOutputStream, and in the process - // of serialization, the "ToArray" variants may be invoked. But they don't - // have a CodedOutputStream available, so they get an additional parameter - // telling them whether to serialize deterministically. - template - PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteGroup( - int field_number, const MessageType& value, uint8* target, - io::EpsCopyOutputStream* stream); - template - PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteMessage( - int field_number, const MessageType& value, uint8* target, - io::EpsCopyOutputStream* stream); - - // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The - // pointer must point at an instance of MessageType, *not* a subclass (or - // the subclass must not override SerializeWithCachedSizes()). - template - PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteGroupNoVirtualToArray( - int field_number, const MessageType& value, uint8* target); - template - PROTOBUF_ALWAYS_INLINE static uint8* InternalWriteMessageNoVirtualToArray( - int field_number, const MessageType& value, uint8* target); - - // For backward-compatibility, the last four methods also have versions - // that are non-deterministic always. - PROTOBUF_ALWAYS_INLINE static uint8* WriteGroupToArray( - int field_number, const MessageLite& value, uint8* target) { - io::EpsCopyOutputStream stream( - target, - value.GetCachedSize() + - static_cast(2 * io::CodedOutputStream::VarintSize32( - static_cast(field_number) << 3)), - io::CodedOutputStream::IsDefaultSerializationDeterministic()); - return InternalWriteGroup(field_number, value, target, &stream); - } - PROTOBUF_ALWAYS_INLINE static uint8* WriteMessageToArray( - int field_number, const MessageLite& value, uint8* target) { - int size = value.GetCachedSize(); - io::EpsCopyOutputStream stream( - target, - size + static_cast(io::CodedOutputStream::VarintSize32( - static_cast(field_number) << 3) + - io::CodedOutputStream::VarintSize32(size)), - io::CodedOutputStream::IsDefaultSerializationDeterministic()); - return InternalWriteMessage(field_number, value, target, &stream); - } - - // Compute the byte size of a field. The XxSize() functions do NOT include - // the tag, so you must also call TagSize(). (This is because, for repeated - // fields, you should only call TagSize() once and multiply it by the element - // count, but you may have to call XxSize() for each individual element.) - static inline size_t Int32Size(int32 value); - static inline size_t Int64Size(int64 value); - static inline size_t UInt32Size(uint32 value); - static inline size_t UInt64Size(uint64 value); - static inline size_t SInt32Size(int32 value); - static inline size_t SInt64Size(int64 value); - static inline size_t EnumSize(int value); - - static size_t Int32Size(const RepeatedField& value); - static size_t Int64Size(const RepeatedField& value); - static size_t UInt32Size(const RepeatedField& value); - static size_t UInt64Size(const RepeatedField& value); - static size_t SInt32Size(const RepeatedField& value); - static size_t SInt64Size(const RepeatedField& value); - static size_t EnumSize(const RepeatedField& value); - - // These types always have the same size. - static constexpr size_t kFixed32Size = 4; - static constexpr size_t kFixed64Size = 8; - static constexpr size_t kSFixed32Size = 4; - static constexpr size_t kSFixed64Size = 8; - static constexpr size_t kFloatSize = 4; - static constexpr size_t kDoubleSize = 8; - static constexpr size_t kBoolSize = 1; - - static inline size_t StringSize(const std::string& value); - static inline size_t BytesSize(const std::string& value); - - template - static inline size_t GroupSize(const MessageType& value); - template - static inline size_t MessageSize(const MessageType& value); - - // Like above, but de-virtualize the call to ByteSize(). The - // pointer must point at an instance of MessageType, *not* a subclass (or - // the subclass must not override ByteSize()). - template - static inline size_t GroupSizeNoVirtual(const MessageType& value); - template - static inline size_t MessageSizeNoVirtual(const MessageType& value); - - // Given the length of data, calculate the byte size of the data on the - // wire if we encode the data as a length delimited field. - static inline size_t LengthDelimitedSize(size_t length); - - private: - // A helper method for the repeated primitive reader. This method has - // optimizations for primitive types that have fixed size on the wire, and - // can be read using potentially faster paths. - template - PROTOBUF_ALWAYS_INLINE static bool ReadRepeatedFixedSizePrimitive( - int tag_size, uint32 tag, io::CodedInputStream* input, - RepeatedField* value); - - // Like ReadRepeatedFixedSizePrimitive but for packed primitive fields. - template - PROTOBUF_ALWAYS_INLINE static bool ReadPackedFixedSizePrimitive( - io::CodedInputStream* input, RepeatedField* value); - - static const CppType kFieldTypeToCppTypeMap[]; - static const WireFormatLite::WireType kWireTypeForFieldType[]; - static void WriteSubMessageMaybeToArray(int size, const MessageLite& value, - io::CodedOutputStream* output); - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite); -}; - -// A class which deals with unknown values. The default implementation just -// discards them. WireFormat defines a subclass which writes to an -// UnknownFieldSet. This class is used by ExtensionSet::ParseField(), since -// ExtensionSet is part of the lite library but UnknownFieldSet is not. -class PROTOBUF_EXPORT FieldSkipper { - public: - FieldSkipper() {} - virtual ~FieldSkipper() {} - - // Skip a field whose tag has already been consumed. - virtual bool SkipField(io::CodedInputStream* input, uint32 tag); - - // Skip an entire message or group, up to an end-group tag (which is consumed) - // or end-of-stream. - virtual bool SkipMessage(io::CodedInputStream* input); - - // Deal with an already-parsed unrecognized enum value. The default - // implementation does nothing, but the UnknownFieldSet-based implementation - // saves it as an unknown varint. - virtual void SkipUnknownEnum(int field_number, int value); -}; - -// Subclass of FieldSkipper which saves skipped fields to a CodedOutputStream. - -class PROTOBUF_EXPORT CodedOutputStreamFieldSkipper : public FieldSkipper { - public: - explicit CodedOutputStreamFieldSkipper(io::CodedOutputStream* unknown_fields) - : unknown_fields_(unknown_fields) {} - ~CodedOutputStreamFieldSkipper() override {} - - // implements FieldSkipper ----------------------------------------- - bool SkipField(io::CodedInputStream* input, uint32 tag) override; - bool SkipMessage(io::CodedInputStream* input) override; - void SkipUnknownEnum(int field_number, int value) override; - - protected: - io::CodedOutputStream* unknown_fields_; -}; - -// inline methods ==================================================== - -inline WireFormatLite::CppType WireFormatLite::FieldTypeToCppType( - FieldType type) { - return kFieldTypeToCppTypeMap[type]; -} - -constexpr inline uint32 WireFormatLite::MakeTag(int field_number, - WireType type) { - return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type); -} - -inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32 tag) { - return static_cast(tag & kTagTypeMask); -} - -inline int WireFormatLite::GetTagFieldNumber(uint32 tag) { - return static_cast(tag >> kTagTypeBits); -} - -inline size_t WireFormatLite::TagSize(int field_number, - WireFormatLite::FieldType type) { - size_t result = io::CodedOutputStream::VarintSize32( - static_cast(field_number << kTagTypeBits)); - if (type == TYPE_GROUP) { - // Groups have both a start and an end tag. - return result * 2; - } else { - return result; - } -} - -inline uint32 WireFormatLite::EncodeFloat(float value) { - return bit_cast(value); -} - -inline float WireFormatLite::DecodeFloat(uint32 value) { - return bit_cast(value); -} - -inline uint64 WireFormatLite::EncodeDouble(double value) { - return bit_cast(value); -} - -inline double WireFormatLite::DecodeDouble(uint64 value) { - return bit_cast(value); -} - -// ZigZag Transform: Encodes signed integers so that they can be -// effectively used with varint encoding. -// -// varint operates on unsigned integers, encoding smaller numbers into -// fewer bytes. If you try to use it on a signed integer, it will treat -// this number as a very large unsigned integer, which means that even -// small signed numbers like -1 will take the maximum number of bytes -// (10) to encode. ZigZagEncode() maps signed integers to unsigned -// in such a way that those with a small absolute value will have smaller -// encoded values, making them appropriate for encoding using varint. -// -// int32 -> uint32 -// ------------------------- -// 0 -> 0 -// -1 -> 1 -// 1 -> 2 -// -2 -> 3 -// ... -> ... -// 2147483647 -> 4294967294 -// -2147483648 -> 4294967295 -// -// >> encode >> -// << decode << - -inline uint32 WireFormatLite::ZigZagEncode32(int32 n) { - // Note: the right-shift must be arithmetic - // Note: left shift must be unsigned because of overflow - return (static_cast(n) << 1) ^ static_cast(n >> 31); -} - -inline int32 WireFormatLite::ZigZagDecode32(uint32 n) { - // Note: Using unsigned types prevent undefined behavior - return static_cast((n >> 1) ^ (~(n & 1) + 1)); -} - -inline uint64 WireFormatLite::ZigZagEncode64(int64 n) { - // Note: the right-shift must be arithmetic - // Note: left shift must be unsigned because of overflow - return (static_cast(n) << 1) ^ static_cast(n >> 63); -} - -inline int64 WireFormatLite::ZigZagDecode64(uint64 n) { - // Note: Using unsigned types prevent undefined behavior - return static_cast((n >> 1) ^ (~(n & 1) + 1)); -} - -// String is for UTF-8 text only, but, even so, ReadString() can simply -// call ReadBytes(). - -inline bool WireFormatLite::ReadString(io::CodedInputStream* input, - std::string* value) { - return ReadBytes(input, value); -} - -inline bool WireFormatLite::ReadString(io::CodedInputStream* input, - std::string** p) { - return ReadBytes(input, p); -} - -inline uint8* InternalSerializeUnknownMessageSetItemsToArray( - const std::string& unknown_fields, uint8* target, - io::EpsCopyOutputStream* stream) { - return stream->WriteRaw(unknown_fields.data(), - static_cast(unknown_fields.size()), target); -} - -inline size_t ComputeUnknownMessageSetItemsSize( - const std::string& unknown_fields) { - return unknown_fields.size(); -} - -// Implementation details of ReadPrimitive. - -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, int32* value) { - uint32 temp; - if (!input->ReadVarint32(&temp)) return false; - *value = static_cast(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, int64* value) { - uint64 temp; - if (!input->ReadVarint64(&temp)) return false; - *value = static_cast(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, uint32* value) { - return input->ReadVarint32(value); -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, uint64* value) { - return input->ReadVarint64(value); -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, int32* value) { - uint32 temp; - if (!input->ReadVarint32(&temp)) return false; - *value = ZigZagDecode32(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, int64* value) { - uint64 temp; - if (!input->ReadVarint64(&temp)) return false; - *value = ZigZagDecode64(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, uint32* value) { - return input->ReadLittleEndian32(value); -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, uint64* value) { - return input->ReadLittleEndian64(value); -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, int32* value) { - uint32 temp; - if (!input->ReadLittleEndian32(&temp)) return false; - *value = static_cast(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, int64* value) { - uint64 temp; - if (!input->ReadLittleEndian64(&temp)) return false; - *value = static_cast(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, float* value) { - uint32 temp; - if (!input->ReadLittleEndian32(&temp)) return false; - *value = DecodeFloat(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, double* value) { - uint64 temp; - if (!input->ReadLittleEndian64(&temp)) return false; - *value = DecodeDouble(temp); - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, bool* value) { - uint64 temp; - if (!input->ReadVarint64(&temp)) return false; - *value = temp != 0; - return true; -} -template <> -inline bool WireFormatLite::ReadPrimitive( - io::CodedInputStream* input, int* value) { - uint32 temp; - if (!input->ReadVarint32(&temp)) return false; - *value = static_cast(temp); - return true; -} - -template <> -inline const uint8* -WireFormatLite::ReadPrimitiveFromArray( - const uint8* buffer, uint32* value) { - return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value); -} -template <> -inline const uint8* -WireFormatLite::ReadPrimitiveFromArray( - const uint8* buffer, uint64* value) { - return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value); -} -template <> -inline const uint8* -WireFormatLite::ReadPrimitiveFromArray( - const uint8* buffer, int32* value) { - uint32 temp; - buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp); - *value = static_cast(temp); - return buffer; -} -template <> -inline const uint8* -WireFormatLite::ReadPrimitiveFromArray( - const uint8* buffer, int64* value) { - uint64 temp; - buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp); - *value = static_cast(temp); - return buffer; -} -template <> -inline const uint8* -WireFormatLite::ReadPrimitiveFromArray( - const uint8* buffer, float* value) { - uint32 temp; - buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp); - *value = DecodeFloat(temp); - return buffer; -} -template <> -inline const uint8* -WireFormatLite::ReadPrimitiveFromArray( - const uint8* buffer, double* value) { - uint64 temp; - buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp); - *value = DecodeDouble(temp); - return buffer; -} - -template -inline bool WireFormatLite::ReadRepeatedPrimitive( - int, // tag_size, unused. - uint32 tag, io::CodedInputStream* input, RepeatedField* values) { - CType value; - if (!ReadPrimitive(input, &value)) return false; - values->Add(value); - int elements_already_reserved = values->Capacity() - values->size(); - while (elements_already_reserved > 0 && input->ExpectTag(tag)) { - if (!ReadPrimitive(input, &value)) return false; - values->AddAlreadyReserved(value); - elements_already_reserved--; - } - return true; -} - -template -inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive( - int tag_size, uint32 tag, io::CodedInputStream* input, - RepeatedField* values) { - GOOGLE_DCHECK_EQ(UInt32Size(tag), static_cast(tag_size)); - CType value; - if (!ReadPrimitive(input, &value)) return false; - values->Add(value); - - // For fixed size values, repeated values can be read more quickly by - // reading directly from a raw array. - // - // We can get a tight loop by only reading as many elements as can be - // added to the RepeatedField without having to do any resizing. Additionally, - // we only try to read as many elements as are available from the current - // buffer space. Doing so avoids having to perform boundary checks when - // reading the value: the maximum number of elements that can be read is - // known outside of the loop. - const void* void_pointer; - int size; - input->GetDirectBufferPointerInline(&void_pointer, &size); - if (size > 0) { - const uint8* buffer = reinterpret_cast(void_pointer); - // The number of bytes each type occupies on the wire. - const int per_value_size = tag_size + static_cast(sizeof(value)); - - // parentheses around (std::min) prevents macro expansion of min(...) - int elements_available = - (std::min)(values->Capacity() - values->size(), size / per_value_size); - int num_read = 0; - while (num_read < elements_available && - (buffer = io::CodedInputStream::ExpectTagFromArray(buffer, tag)) != - NULL) { - buffer = ReadPrimitiveFromArray(buffer, &value); - values->AddAlreadyReserved(value); - ++num_read; - } - const int read_bytes = num_read * per_value_size; - if (read_bytes > 0) { - input->Skip(read_bytes); - } - } - return true; -} - -// Specializations of ReadRepeatedPrimitive for the fixed size types, which use -// the optimized code path. -#define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ - template <> \ - inline bool WireFormatLite::ReadRepeatedPrimitive< \ - CPPTYPE, WireFormatLite::DECLARED_TYPE>( \ - int tag_size, uint32 tag, io::CodedInputStream* input, \ - RepeatedField* values) { \ - return ReadRepeatedFixedSizePrimitive( \ - tag_size, tag, input, values); \ - } - -READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32) -READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64) -READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32) -READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64) -READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT) -READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE) - -#undef READ_REPEATED_FIXED_SIZE_PRIMITIVE - -template -bool WireFormatLite::ReadRepeatedPrimitiveNoInline( - int tag_size, uint32 tag, io::CodedInputStream* input, - RepeatedField* value) { - return ReadRepeatedPrimitive(tag_size, tag, input, - value); -} - -template -inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input, - RepeatedField* values) { - int length; - if (!input->ReadVarintSizeAsInt(&length)) return false; - io::CodedInputStream::Limit limit = input->PushLimit(length); - while (input->BytesUntilLimit() > 0) { - CType value; - if (!ReadPrimitive(input, &value)) return false; - values->Add(value); - } - input->PopLimit(limit); - return true; -} - -template -inline bool WireFormatLite::ReadPackedFixedSizePrimitive( - io::CodedInputStream* input, RepeatedField* values) { - int length; - if (!input->ReadVarintSizeAsInt(&length)) return false; - const int old_entries = values->size(); - const int new_entries = length / static_cast(sizeof(CType)); - const int new_bytes = new_entries * static_cast(sizeof(CType)); - if (new_bytes != length) return false; - // We would *like* to pre-allocate the buffer to write into (for - // speed), but *must* avoid performing a very large allocation due - // to a malicious user-supplied "length" above. So we have a fast - // path that pre-allocates when the "length" is less than a bound. - // We determine the bound by calling BytesUntilTotalBytesLimit() and - // BytesUntilLimit(). These return -1 to mean "no limit set". - // There are four cases: - // TotalBytesLimit Limit - // -1 -1 Use slow path. - // -1 >= 0 Use fast path if length <= Limit. - // >= 0 -1 Use slow path. - // >= 0 >= 0 Use fast path if length <= min(both limits). - int64 bytes_limit = input->BytesUntilTotalBytesLimit(); - if (bytes_limit == -1) { - bytes_limit = input->BytesUntilLimit(); - } else { - // parentheses around (std::min) prevents macro expansion of min(...) - bytes_limit = - (std::min)(bytes_limit, static_cast(input->BytesUntilLimit())); - } - if (bytes_limit >= new_bytes) { - // Fast-path that pre-allocates *values to the final size. -#if defined(PROTOBUF_LITTLE_ENDIAN) - values->Resize(old_entries + new_entries, 0); - // values->mutable_data() may change after Resize(), so do this after: - void* dest = reinterpret_cast(values->mutable_data() + old_entries); - if (!input->ReadRaw(dest, new_bytes)) { - values->Truncate(old_entries); - return false; - } -#else - values->Reserve(old_entries + new_entries); - CType value; - for (int i = 0; i < new_entries; ++i) { - if (!ReadPrimitive(input, &value)) return false; - values->AddAlreadyReserved(value); - } -#endif - } else { - // This is the slow-path case where "length" may be too large to - // safely allocate. We read as much as we can into *values - // without pre-allocating "length" bytes. - CType value; - for (int i = 0; i < new_entries; ++i) { - if (!ReadPrimitive(input, &value)) return false; - values->Add(value); - } - } - return true; -} - -// Specializations of ReadPackedPrimitive for the fixed size types, which use -// an optimized code path. -#define READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \ - template <> \ - inline bool \ - WireFormatLite::ReadPackedPrimitive( \ - io::CodedInputStream * input, RepeatedField * values) { \ - return ReadPackedFixedSizePrimitive( \ - input, values); \ - } - -READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32) -READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64) -READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32) -READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64) -READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT) -READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE) - -#undef READ_REPEATED_PACKED_FIXED_SIZE_PRIMITIVE - -template -bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input, - RepeatedField* values) { - return ReadPackedPrimitive(input, values); -} - - -template -inline bool WireFormatLite::ReadGroup(int field_number, - io::CodedInputStream* input, - MessageType* value) { - if (!input->IncrementRecursionDepth()) return false; - if (!value->MergePartialFromCodedStream(input)) return false; - input->UnsafeDecrementRecursionDepth(); - // Make sure the last thing read was an end tag for this group. - if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) { - return false; - } - return true; -} -template -inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input, - MessageType* value) { - int length; - if (!input->ReadVarintSizeAsInt(&length)) return false; - std::pair p = - input->IncrementRecursionDepthAndPushLimit(length); - if (p.second < 0 || !value->MergePartialFromCodedStream(input)) return false; - // Make sure that parsing stopped when the limit was hit, not at an endgroup - // tag. - return input->DecrementRecursionDepthAndPopLimit(p.first); -} - -// =================================================================== - -inline void WireFormatLite::WriteTag(int field_number, WireType type, - io::CodedOutputStream* output) { - output->WriteTag(MakeTag(field_number, type)); -} - -inline void WireFormatLite::WriteInt32NoTag(int32 value, - io::CodedOutputStream* output) { - output->WriteVarint32SignExtended(value); -} -inline void WireFormatLite::WriteInt64NoTag(int64 value, - io::CodedOutputStream* output) { - output->WriteVarint64(static_cast(value)); -} -inline void WireFormatLite::WriteUInt32NoTag(uint32 value, - io::CodedOutputStream* output) { - output->WriteVarint32(value); -} -inline void WireFormatLite::WriteUInt64NoTag(uint64 value, - io::CodedOutputStream* output) { - output->WriteVarint64(value); -} -inline void WireFormatLite::WriteSInt32NoTag(int32 value, - io::CodedOutputStream* output) { - output->WriteVarint32(ZigZagEncode32(value)); -} -inline void WireFormatLite::WriteSInt64NoTag(int64 value, - io::CodedOutputStream* output) { - output->WriteVarint64(ZigZagEncode64(value)); -} -inline void WireFormatLite::WriteFixed32NoTag(uint32 value, - io::CodedOutputStream* output) { - output->WriteLittleEndian32(value); -} -inline void WireFormatLite::WriteFixed64NoTag(uint64 value, - io::CodedOutputStream* output) { - output->WriteLittleEndian64(value); -} -inline void WireFormatLite::WriteSFixed32NoTag(int32 value, - io::CodedOutputStream* output) { - output->WriteLittleEndian32(static_cast(value)); -} -inline void WireFormatLite::WriteSFixed64NoTag(int64 value, - io::CodedOutputStream* output) { - output->WriteLittleEndian64(static_cast(value)); -} -inline void WireFormatLite::WriteFloatNoTag(float value, - io::CodedOutputStream* output) { - output->WriteLittleEndian32(EncodeFloat(value)); -} -inline void WireFormatLite::WriteDoubleNoTag(double value, - io::CodedOutputStream* output) { - output->WriteLittleEndian64(EncodeDouble(value)); -} -inline void WireFormatLite::WriteBoolNoTag(bool value, - io::CodedOutputStream* output) { - output->WriteVarint32(value ? 1 : 0); -} -inline void WireFormatLite::WriteEnumNoTag(int value, - io::CodedOutputStream* output) { - output->WriteVarint32SignExtended(value); -} - -// See comment on ReadGroupNoVirtual to understand the need for this template -// parameter name. -template -inline void WireFormatLite::WriteGroupNoVirtual( - int field_number, const MessageType_WorkAroundCppLookupDefect& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_START_GROUP, output); - value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output); - WriteTag(field_number, WIRETYPE_END_GROUP, output); -} -template -inline void WireFormatLite::WriteMessageNoVirtual( - int field_number, const MessageType_WorkAroundCppLookupDefect& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); - output->WriteVarint32( - value.MessageType_WorkAroundCppLookupDefect::GetCachedSize()); - value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output); -} - -// =================================================================== - -inline uint8* WireFormatLite::WriteTagToArray(int field_number, WireType type, - uint8* target) { - return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type), - target); -} - -inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value, - uint8* target) { - return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target); -} -inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value, - uint8* target) { - return io::CodedOutputStream::WriteVarint64ToArray(static_cast(value), - target); -} -inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value, - uint8* target) { - return io::CodedOutputStream::WriteVarint32ToArray(value, target); -} -inline uint8* WireFormatLite::WriteUInt64NoTagToArray(uint64 value, - uint8* target) { - return io::CodedOutputStream::WriteVarint64ToArray(value, target); -} -inline uint8* WireFormatLite::WriteSInt32NoTagToArray(int32 value, - uint8* target) { - return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value), - target); -} -inline uint8* WireFormatLite::WriteSInt64NoTagToArray(int64 value, - uint8* target) { - return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value), - target); -} -inline uint8* WireFormatLite::WriteFixed32NoTagToArray(uint32 value, - uint8* target) { - return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target); -} -inline uint8* WireFormatLite::WriteFixed64NoTagToArray(uint64 value, - uint8* target) { - return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target); -} -inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(int32 value, - uint8* target) { - return io::CodedOutputStream::WriteLittleEndian32ToArray( - static_cast(value), target); -} -inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(int64 value, - uint8* target) { - return io::CodedOutputStream::WriteLittleEndian64ToArray( - static_cast(value), target); -} -inline uint8* WireFormatLite::WriteFloatNoTagToArray(float value, - uint8* target) { - return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value), - target); -} -inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value, - uint8* target) { - return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value), - target); -} -inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value, uint8* target) { - return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target); -} -inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value, uint8* target) { - return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target); -} - -template -inline uint8* WireFormatLite::WritePrimitiveNoTagToArray( - const RepeatedField& value, uint8* (*Writer)(T, uint8*), uint8* target) { - const int n = value.size(); - GOOGLE_DCHECK_GT(n, 0); - - const T* ii = value.data(); - int i = 0; - do { - target = Writer(ii[i], target); - } while (++i < n); - - return target; -} - -template -inline uint8* WireFormatLite::WriteFixedNoTagToArray( - const RepeatedField& value, uint8* (*Writer)(T, uint8*), uint8* target) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - (void)Writer; - - const int n = value.size(); - GOOGLE_DCHECK_GT(n, 0); - - const T* ii = value.data(); - const int bytes = n * static_cast(sizeof(ii[0])); - memcpy(target, ii, static_cast(bytes)); - return target + bytes; -#else - return WritePrimitiveNoTagToArray(value, Writer, target); -#endif -} - -inline uint8* WireFormatLite::WriteInt32NoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteInt32NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteInt64NoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteInt64NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteUInt32NoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteUInt32NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteUInt64NoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteUInt64NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteSInt32NoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteSInt32NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteSInt64NoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteSInt64NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteFixed32NoTagToArray( - const RepeatedField& value, uint8* target) { - return WriteFixedNoTagToArray(value, WriteFixed32NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteFixed64NoTagToArray( - const RepeatedField& value, uint8* target) { - return WriteFixedNoTagToArray(value, WriteFixed64NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteSFixed32NoTagToArray( - const RepeatedField& value, uint8* target) { - return WriteFixedNoTagToArray(value, WriteSFixed32NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteSFixed64NoTagToArray( - const RepeatedField& value, uint8* target) { - return WriteFixedNoTagToArray(value, WriteSFixed64NoTagToArray, target); -} -inline uint8* WireFormatLite::WriteFloatNoTagToArray( - const RepeatedField& value, uint8* target) { - return WriteFixedNoTagToArray(value, WriteFloatNoTagToArray, target); -} -inline uint8* WireFormatLite::WriteDoubleNoTagToArray( - const RepeatedField& value, uint8* target) { - return WriteFixedNoTagToArray(value, WriteDoubleNoTagToArray, target); -} -inline uint8* WireFormatLite::WriteBoolNoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteBoolNoTagToArray, target); -} -inline uint8* WireFormatLite::WriteEnumNoTagToArray( - const RepeatedField& value, uint8* target) { - return WritePrimitiveNoTagToArray(value, WriteEnumNoTagToArray, target); -} - -inline uint8* WireFormatLite::WriteInt32ToArray(int field_number, int32 value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteInt32NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteInt64ToArray(int field_number, int64 value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteInt64NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number, uint32 value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteUInt32NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number, uint64 value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteUInt64NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number, int32 value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteSInt32NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number, int64 value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteSInt64NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number, - uint32 value, uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target); - return WriteFixed32NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number, - uint64 value, uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target); - return WriteFixed64NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number, - int32 value, uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target); - return WriteSFixed32NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number, - int64 value, uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target); - return WriteSFixed64NoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteFloatToArray(int field_number, float value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target); - return WriteFloatNoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteDoubleToArray(int field_number, double value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target); - return WriteDoubleNoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteBoolToArray(int field_number, bool value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteBoolNoTagToArray(value, target); -} -inline uint8* WireFormatLite::WriteEnumToArray(int field_number, int value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_VARINT, target); - return WriteEnumNoTagToArray(value, target); -} - -template -inline uint8* WireFormatLite::WritePrimitiveToArray( - int field_number, const RepeatedField& value, - uint8* (*Writer)(int, T, uint8*), uint8* target) { - const int n = value.size(); - if (n == 0) { - return target; - } - - const T* ii = value.data(); - int i = 0; - do { - target = Writer(field_number, ii[i], target); - } while (++i < n); - - return target; -} - -inline uint8* WireFormatLite::WriteInt32ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteInt32ToArray, target); -} -inline uint8* WireFormatLite::WriteInt64ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteInt64ToArray, target); -} -inline uint8* WireFormatLite::WriteUInt32ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteUInt32ToArray, target); -} -inline uint8* WireFormatLite::WriteUInt64ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteUInt64ToArray, target); -} -inline uint8* WireFormatLite::WriteSInt32ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteSInt32ToArray, target); -} -inline uint8* WireFormatLite::WriteSInt64ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteSInt64ToArray, target); -} -inline uint8* WireFormatLite::WriteFixed32ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteFixed32ToArray, - target); -} -inline uint8* WireFormatLite::WriteFixed64ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteFixed64ToArray, - target); -} -inline uint8* WireFormatLite::WriteSFixed32ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteSFixed32ToArray, - target); -} -inline uint8* WireFormatLite::WriteSFixed64ToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteSFixed64ToArray, - target); -} -inline uint8* WireFormatLite::WriteFloatToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteFloatToArray, target); -} -inline uint8* WireFormatLite::WriteDoubleToArray( - int field_number, const RepeatedField& value, uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteDoubleToArray, target); -} -inline uint8* WireFormatLite::WriteBoolToArray(int field_number, - const RepeatedField& value, - uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteBoolToArray, target); -} -inline uint8* WireFormatLite::WriteEnumToArray(int field_number, - const RepeatedField& value, - uint8* target) { - return WritePrimitiveToArray(field_number, value, WriteEnumToArray, target); -} -inline uint8* WireFormatLite::WriteStringToArray(int field_number, - const std::string& value, - uint8* target) { - // String is for UTF-8 text only - // WARNING: In wire_format.cc, both strings and bytes are handled by - // WriteString() to avoid code duplication. If the implementations become - // different, you will need to update that usage. - target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); - return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); -} -inline uint8* WireFormatLite::WriteBytesToArray(int field_number, - const std::string& value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); - return io::CodedOutputStream::WriteStringWithSizeToArray(value, target); -} - - -template -inline uint8* WireFormatLite::InternalWriteGroup( - int field_number, const MessageType& value, uint8* target, - io::EpsCopyOutputStream* stream) { - target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target); - target = value._InternalSerialize(target, stream); - target = stream->EnsureSpace(target); - return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target); -} -template -inline uint8* WireFormatLite::InternalWriteMessage( - int field_number, const MessageType& value, uint8* target, - io::EpsCopyOutputStream* stream) { - target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); - target = io::CodedOutputStream::WriteVarint32ToArray( - static_cast(value.GetCachedSize()), target); - return value._InternalSerialize(target, stream); -} - -// See comment on ReadGroupNoVirtual to understand the need for this template -// parameter name. -template -inline uint8* WireFormatLite::InternalWriteGroupNoVirtualToArray( - int field_number, const MessageType_WorkAroundCppLookupDefect& value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target); - target = value.MessageType_WorkAroundCppLookupDefect:: - SerializeWithCachedSizesToArray(target); - return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target); -} -template -inline uint8* WireFormatLite::InternalWriteMessageNoVirtualToArray( - int field_number, const MessageType_WorkAroundCppLookupDefect& value, - uint8* target) { - target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target); - target = io::CodedOutputStream::WriteVarint32ToArray( - static_cast( - value.MessageType_WorkAroundCppLookupDefect::GetCachedSize()), - target); - return value - .MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizesToArray( - target); -} - -// =================================================================== - -inline size_t WireFormatLite::Int32Size(int32 value) { - return io::CodedOutputStream::VarintSize32SignExtended(value); -} -inline size_t WireFormatLite::Int64Size(int64 value) { - return io::CodedOutputStream::VarintSize64(static_cast(value)); -} -inline size_t WireFormatLite::UInt32Size(uint32 value) { - return io::CodedOutputStream::VarintSize32(value); -} -inline size_t WireFormatLite::UInt64Size(uint64 value) { - return io::CodedOutputStream::VarintSize64(value); -} -inline size_t WireFormatLite::SInt32Size(int32 value) { - return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value)); -} -inline size_t WireFormatLite::SInt64Size(int64 value) { - return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value)); -} -inline size_t WireFormatLite::EnumSize(int value) { - return io::CodedOutputStream::VarintSize32SignExtended(value); -} - -inline size_t WireFormatLite::StringSize(const std::string& value) { - return LengthDelimitedSize(value.size()); -} -inline size_t WireFormatLite::BytesSize(const std::string& value) { - return LengthDelimitedSize(value.size()); -} - - -template -inline size_t WireFormatLite::GroupSize(const MessageType& value) { - return value.ByteSizeLong(); -} -template -inline size_t WireFormatLite::MessageSize(const MessageType& value) { - return LengthDelimitedSize(value.ByteSizeLong()); -} - -// See comment on ReadGroupNoVirtual to understand the need for this template -// parameter name. -template -inline size_t WireFormatLite::GroupSizeNoVirtual( - const MessageType_WorkAroundCppLookupDefect& value) { - return value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong(); -} -template -inline size_t WireFormatLite::MessageSizeNoVirtual( - const MessageType_WorkAroundCppLookupDefect& value) { - return LengthDelimitedSize( - value.MessageType_WorkAroundCppLookupDefect::ByteSizeLong()); -} - -inline size_t WireFormatLite::LengthDelimitedSize(size_t length) { - // The static_cast here prevents an error in certain compiler configurations - // but is not technically correct--if length is too large to fit in a uint32 - // then it will be silently truncated. We will need to fix this if we ever - // decide to start supporting serialized messages greater than 2 GiB in size. - return length + - io::CodedOutputStream::VarintSize32(static_cast(length)); -} - -template -bool ParseMessageSetItemImpl(io::CodedInputStream* input, MS ms) { - // This method parses a group which should contain two fields: - // required int32 type_id = 2; - // required data message = 3; - - uint32 last_type_id = 0; - - // If we see message data before the type_id, we'll append it to this so - // we can parse it later. - std::string message_data; - - while (true) { - const uint32 tag = input->ReadTagNoLastTag(); - if (tag == 0) return false; - - switch (tag) { - case WireFormatLite::kMessageSetTypeIdTag: { - uint32 type_id; - if (!input->ReadVarint32(&type_id)) return false; - last_type_id = type_id; - - if (!message_data.empty()) { - // We saw some message data before the type_id. Have to parse it - // now. - io::CodedInputStream sub_input( - reinterpret_cast(message_data.data()), - static_cast(message_data.size())); - sub_input.SetRecursionLimit(input->RecursionBudget()); - if (!ms.ParseField(last_type_id, &sub_input)) { - return false; - } - message_data.clear(); - } - - break; - } - - case WireFormatLite::kMessageSetMessageTag: { - if (last_type_id == 0) { - // We haven't seen a type_id yet. Append this data to message_data. - uint32 length; - if (!input->ReadVarint32(&length)) return false; - if (static_cast(length) < 0) return false; - uint32 size = static_cast( - length + io::CodedOutputStream::VarintSize32(length)); - message_data.resize(size); - auto ptr = reinterpret_cast(&message_data[0]); - ptr = io::CodedOutputStream::WriteVarint32ToArray(length, ptr); - if (!input->ReadRaw(ptr, length)) return false; - } else { - // Already saw type_id, so we can parse this directly. - if (!ms.ParseField(last_type_id, input)) { - return false; - } - } - - break; - } - - case WireFormatLite::kMessageSetItemEndTag: { - return true; - } - - default: { - if (!ms.SkipField(tag, input)) return false; - } - } - } -} - -} // namespace internal -} // namespace protobuf -} // namespace google - -#include - -#endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ diff --git a/third_party/protobuf-lite/implicit_weak_message.cc b/third_party/protobuf-lite/implicit_weak_message.cc deleted file mode 100644 index 539061693..000000000 --- a/third_party/protobuf-lite/implicit_weak_message.cc +++ /dev/null @@ -1,65 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { - -const char* ImplicitWeakMessage::_InternalParse(const char* ptr, - ParseContext* ctx) { - return ctx->AppendString(ptr, &data_); -} - -ExplicitlyConstructed - implicit_weak_message_default_instance; -internal::once_flag implicit_weak_message_once_init_; - -void InitImplicitWeakMessageDefaultInstance() { - implicit_weak_message_default_instance.DefaultConstruct(); -} - -const ImplicitWeakMessage* ImplicitWeakMessage::default_instance() { - internal::call_once(implicit_weak_message_once_init_, - InitImplicitWeakMessageDefaultInstance); - return &implicit_weak_message_default_instance.get(); -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/int128.cc b/third_party/protobuf-lite/int128.cc deleted file mode 100644 index 2119e6550..000000000 --- a/third_party/protobuf-lite/int128.cc +++ /dev/null @@ -1,192 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include // NOLINT(readability/streams) -#include - -#include - -#include - -namespace google { -namespace protobuf { - -const uint128_pod kuint128max = { - static_cast(PROTOBUF_LONGLONG(0xFFFFFFFFFFFFFFFF)), - static_cast(PROTOBUF_LONGLONG(0xFFFFFFFFFFFFFFFF)) -}; - -// Returns the 0-based position of the last set bit (i.e., most significant bit) -// in the given uint64. The argument may not be 0. -// -// For example: -// Given: 5 (decimal) == 101 (binary) -// Returns: 2 -#define STEP(T, n, pos, sh) \ - do { \ - if ((n) >= (static_cast(1) << (sh))) { \ - (n) = (n) >> (sh); \ - (pos) |= (sh); \ - } \ - } while (0) -static inline int Fls64(uint64 n) { - GOOGLE_DCHECK_NE(0, n); - int pos = 0; - STEP(uint64, n, pos, 0x20); - uint32 n32 = n; - STEP(uint32, n32, pos, 0x10); - STEP(uint32, n32, pos, 0x08); - STEP(uint32, n32, pos, 0x04); - return pos + ((PROTOBUF_ULONGLONG(0x3333333322221100) >> (n32 << 2)) & 0x3); -} -#undef STEP - -// Like Fls64() above, but returns the 0-based position of the last set bit -// (i.e., most significant bit) in the given uint128. The argument may not be 0. -static inline int Fls128(uint128 n) { - if (uint64 hi = Uint128High64(n)) { - return Fls64(hi) + 64; - } - return Fls64(Uint128Low64(n)); -} - -void uint128::DivModImpl(uint128 dividend, uint128 divisor, - uint128* quotient_ret, uint128* remainder_ret) { - if (divisor == 0) { - GOOGLE_LOG(FATAL) << "Division or mod by zero: dividend.hi=" << dividend.hi_ - << ", lo=" << dividend.lo_; - } else if (dividend < divisor) { - *quotient_ret = 0; - *remainder_ret = dividend; - return; - } else { - int dividend_bit_length = Fls128(dividend); - int divisor_bit_length = Fls128(divisor); - int difference = dividend_bit_length - divisor_bit_length; - uint128 quotient = 0; - while (difference >= 0) { - quotient <<= 1; - uint128 shifted_divisor = divisor << difference; - if (shifted_divisor <= dividend) { - dividend -= shifted_divisor; - quotient += 1; - } - difference -= 1; - } - //record the final quotient and remainder - *quotient_ret = quotient; - *remainder_ret = dividend; - } -} - - -uint128& uint128::operator/=(const uint128& divisor) { - uint128 quotient = 0; - uint128 remainder = 0; - DivModImpl(*this, divisor, "ient, &remainder); - *this = quotient; - return *this; -} -uint128& uint128::operator%=(const uint128& divisor) { - uint128 quotient = 0; - uint128 remainder = 0; - DivModImpl(*this, divisor, "ient, &remainder); - *this = remainder; - return *this; -} - -std::ostream& operator<<(std::ostream& o, const uint128& b) { - std::ios_base::fmtflags flags = o.flags(); - - // Select a divisor which is the largest power of the base < 2^64. - uint128 div; - std::streamsize div_base_log; - switch (flags & std::ios::basefield) { - case std::ios::hex: - div = - static_cast(PROTOBUF_ULONGLONG(0x1000000000000000)); // 16^15 - div_base_log = 15; - break; - case std::ios::oct: - div = static_cast( - PROTOBUF_ULONGLONG(01000000000000000000000)); // 8^21 - div_base_log = 21; - break; - default: // std::ios::dec - div = static_cast( - PROTOBUF_ULONGLONG(10000000000000000000)); // 10^19 - div_base_log = 19; - break; - } - - // Now piece together the uint128 representation from three chunks of - // the original value, each less than "div" and therefore representable - // as a uint64. - std::ostringstream os; - std::ios_base::fmtflags copy_mask = - std::ios::basefield | std::ios::showbase | std::ios::uppercase; - os.setf(flags & copy_mask, copy_mask); - uint128 high = b; - uint128 low; - uint128::DivModImpl(high, div, &high, &low); - uint128 mid; - uint128::DivModImpl(high, div, &high, &mid); - if (high.lo_ != 0) { - os << high.lo_; - os << std::noshowbase << std::setfill('0') << std::setw(div_base_log); - os << mid.lo_; - os << std::setw(div_base_log); - } else if (mid.lo_ != 0) { - os << mid.lo_; - os << std::noshowbase << std::setfill('0') << std::setw(div_base_log); - } - os << low.lo_; - std::string rep = os.str(); - - // Add the requisite padding. - std::streamsize width = o.width(0); - if (width > rep.size()) { - if ((flags & std::ios::adjustfield) == std::ios::left) { - rep.append(width - rep.size(), o.fill()); - } else { - rep.insert(static_cast(0), - width - rep.size(), o.fill()); - } - } - - // Stream the final representation in a single "<<" call. - return o << rep; -} - -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/io_win32.cc b/third_party/protobuf-lite/io_win32.cc deleted file mode 100644 index d22ceac77..000000000 --- a/third_party/protobuf-lite/io_win32.cc +++ /dev/null @@ -1,470 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: laszlocsomor@google.com (Laszlo Csomor) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -// Implementation for long-path-aware open/mkdir/access/etc. on Windows, as well -// as for the supporting utility functions. -// -// These functions convert the input path to an absolute Windows path -// with "\\?\" prefix, then pass that to _wopen/_wmkdir/_waccess/etc. -// (declared in ) respectively. This allows working with files/directories -// whose paths are longer than MAX_PATH (260 chars). -// -// This file is only used on Windows, it's empty on other platforms. - -#if defined(_WIN32) && !defined(_XBOX_ONE) - -// Comment this out to fall back to using the ANSI versions (open, mkdir, ...) -// instead of the Unicode ones (_wopen, _wmkdir, ...). Doing so can be useful to -// debug failing tests if that's caused by the long path support. -#define SUPPORT_LONGPATHS - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN 1 -#endif - -#include - -#include -#include -#include -#include - -namespace google { -namespace protobuf { -namespace io { -namespace win32 { -namespace { - -using std::string; -using std::wstring; - -template -struct CharTraits { - static bool is_alpha(char_type ch); -}; - -template <> -struct CharTraits { - static bool is_alpha(char ch) { return isalpha(ch); } -}; - -template <> -struct CharTraits { - static bool is_alpha(wchar_t ch) { return iswalpha(ch); } -}; - -template -bool null_or_empty(const char_type* s) { - return s == nullptr || *s == 0; -} - -// Returns true if the path starts with a drive letter, e.g. "c:". -// Note that this won't check for the "\" after the drive letter, so this also -// returns true for "c:foo" (which is "c:\${PWD}\foo"). -// This check requires that a path not have a longpath prefix ("\\?\"). -template -bool has_drive_letter(const char_type* ch) { - return CharTraits::is_alpha(ch[0]) && ch[1] == ':'; -} - -// Returns true if the path starts with a longpath prefix ("\\?\"). -template -bool has_longpath_prefix(const char_type* path) { - return path[0] == '\\' && path[1] == '\\' && path[2] == '?' && - path[3] == '\\'; -} - -template -bool is_separator(char_type c) { - return c == '/' || c == '\\'; -} - -// Returns true if the path starts with a drive specifier (e.g. "c:\"). -template -bool is_path_absolute(const char_type* path) { - return has_drive_letter(path) && is_separator(path[2]); -} - -template -bool is_drive_relative(const char_type* path) { - return has_drive_letter(path) && (path[2] == 0 || !is_separator(path[2])); -} - -wstring join_paths(const wstring& path1, const wstring& path2) { - if (path1.empty() || is_path_absolute(path2.c_str()) || - has_longpath_prefix(path2.c_str())) { - return path2; - } - if (path2.empty()) { - return path1; - } - - if (is_separator(path1[path1.size() - 1])) { - return is_separator(path2[0]) ? (path1 + path2.substr(1)) - : (path1 + path2); - } else { - return is_separator(path2[0]) ? (path1 + path2) - : (path1 + L'\\' + path2); - } -} - -wstring normalize(wstring path) { - if (has_longpath_prefix(path.c_str())) { - path = path.substr(4); - } - - static const wstring dot(L"."); - static const wstring dotdot(L".."); - const WCHAR* p = path.c_str(); - - std::vector segments; - int segment_start = -1; - // Find the path segments in `path` (separated by "/"). - for (int i = 0;; ++i) { - if (!is_separator(p[i]) && p[i] != L'\0') { - // The current character does not end a segment, so start one unless it's - // already started. - if (segment_start < 0) { - segment_start = i; - } - } else if (segment_start >= 0 && i > segment_start) { - // The current character is "/" or "\0", so this ends a segment. - // Add that to `segments` if there's anything to add; handle "." and "..". - wstring segment(p, segment_start, i - segment_start); - segment_start = -1; - if (segment == dotdot) { - if (!segments.empty() && - (!has_drive_letter(segments[0].c_str()) || segments.size() > 1)) { - segments.pop_back(); - } - } else if (segment != dot && !segment.empty()) { - segments.push_back(segment); - } - } - if (p[i] == L'\0') { - break; - } - } - - // Handle the case when `path` is just a drive specifier (or some degenerate - // form of it, e.g. "c:\.."). - if (segments.size() == 1 && segments[0].size() == 2 && - has_drive_letter(segments[0].c_str())) { - return segments[0] + L'\\'; - } - - // Join all segments. - bool first = true; - std::wstringstream result; - for (int i = 0; i < segments.size(); ++i) { - if (!first) { - result << L'\\'; - } - first = false; - result << segments[i]; - } - // Preserve trailing separator if the input contained it. - if (!path.empty() && is_separator(p[path.size() - 1])) { - result << L'\\'; - } - return result.str(); -} - -bool as_windows_path(const char* path, wstring* result) { - if (null_or_empty(path)) { - result->clear(); - return true; - } - wstring wpath; - if (!strings::utf8_to_wcs(path, &wpath)) { - return false; - } - if (has_longpath_prefix(wpath.c_str())) { - *result = wpath; - return true; - } - if (is_separator(path[0]) || is_drive_relative(path)) { - return false; - } - - - if (!is_path_absolute(wpath.c_str())) { - int size = ::GetCurrentDirectoryW(0, nullptr); - if (size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) { - return false; - } - std::unique_ptr wcwd(new WCHAR[size]); - ::GetCurrentDirectoryW(size, wcwd.get()); - wpath = join_paths(wcwd.get(), wpath); - } - wpath = normalize(wpath); - if (!has_longpath_prefix(wpath.c_str())) { - // Add the "\\?\" prefix unconditionally. This way we prevent the Win32 API - // from processing the path and "helpfully" removing trailing dots from the - // path, for example. - // See https://github.com/bazelbuild/bazel/issues/2935 - wpath = wstring(L"\\\\?\\") + wpath; - } - *result = wpath; - return true; -} - -} // namespace - -int open(const char* path, int flags, int mode) { -#ifdef SUPPORT_LONGPATHS - wstring wpath; - if (!as_windows_path(path, &wpath)) { - errno = ENOENT; - return -1; - } - return ::_wopen(wpath.c_str(), flags, mode); -#else - return ::_open(path, flags, mode); -#endif -} - -int mkdir(const char* path, int _mode) { -#ifdef SUPPORT_LONGPATHS - wstring wpath; - if (!as_windows_path(path, &wpath)) { - errno = ENOENT; - return -1; - } - return ::_wmkdir(wpath.c_str()); -#else // not SUPPORT_LONGPATHS - return ::_mkdir(path); -#endif // not SUPPORT_LONGPATHS -} - -int access(const char* path, int mode) { -#ifdef SUPPORT_LONGPATHS - wstring wpath; - if (!as_windows_path(path, &wpath)) { - errno = ENOENT; - return -1; - } - return ::_waccess(wpath.c_str(), mode); -#else - return ::_access(path, mode); -#endif -} - -int chdir(const char* path) { -#ifdef SUPPORT_LONGPATHS - wstring wpath; - if (!as_windows_path(path, &wpath)) { - errno = ENOENT; - return -1; - } - return ::_wchdir(wpath.c_str()); -#else - return ::_chdir(path); -#endif -} - -int stat(const char* path, struct _stat* buffer) { -#ifdef SUPPORT_LONGPATHS - wstring wpath; - if (!as_windows_path(path, &wpath)) { - errno = ENOENT; - return -1; - } - return ::_wstat(wpath.c_str(), buffer); -#else // not SUPPORT_LONGPATHS - return ::_stat(path, buffer); -#endif // not SUPPORT_LONGPATHS -} - -FILE* fopen(const char* path, const char* mode) { -#ifdef SUPPORT_LONGPATHS - if (null_or_empty(path)) { - errno = EINVAL; - return nullptr; - } - wstring wpath; - if (!as_windows_path(path, &wpath)) { - errno = ENOENT; - return nullptr; - } - wstring wmode; - if (!strings::utf8_to_wcs(mode, &wmode)) { - errno = EINVAL; - return nullptr; - } - return ::_wfopen(wpath.c_str(), wmode.c_str()); -#else - return ::fopen(path, mode); -#endif -} - -int close(int fd) { return ::_close(fd); } - -int dup(int fd) { return ::_dup(fd); } - -int dup2(int fd1, int fd2) { return ::_dup2(fd1, fd2); } - -int read(int fd, void* buffer, size_t size) { - return ::_read(fd, buffer, size); -} - -int setmode(int fd, int mode) { return ::_setmode(fd, mode); } - -int write(int fd, const void* buffer, size_t size) { - return ::_write(fd, buffer, size); -} - -wstring testonly_utf8_to_winpath(const char* path) { - wstring wpath; - return as_windows_path(path, &wpath) ? wpath : wstring(); -} - -ExpandWildcardsResult ExpandWildcards( - const string& path, std::function consume) { - if (path.find_first_of("*?") == string::npos) { - // There are no wildcards in the path, we don't need to expand it. - consume(path); - return ExpandWildcardsResult::kSuccess; - } - - wstring wpath; - if (!as_windows_path(path.c_str(), &wpath)) { - return ExpandWildcardsResult::kErrorInputPathConversion; - } - - static const wstring kDot = L"."; - static const wstring kDotDot = L".."; - WIN32_FIND_DATAW metadata; - HANDLE handle = ::FindFirstFileW(wpath.c_str(), &metadata); - if (handle == INVALID_HANDLE_VALUE) { - // The pattern does not match any files (or directories). - return ExpandWildcardsResult::kErrorNoMatchingFile; - } - - string::size_type pos = path.find_last_of("\\/"); - string dirname; - if (pos != string::npos) { - dirname = path.substr(0, pos + 1); - } - - ExpandWildcardsResult matched = ExpandWildcardsResult::kErrorNoMatchingFile; - do { - // Ignore ".", "..", and directories. - if ((metadata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 && - kDot != metadata.cFileName && kDotDot != metadata.cFileName) { - matched = ExpandWildcardsResult::kSuccess; - string filename; - if (!strings::wcs_to_utf8(metadata.cFileName, &filename)) { - return ExpandWildcardsResult::kErrorOutputPathConversion; - } - - if (dirname.empty()) { - consume(filename); - } else { - consume(dirname + filename); - } - } - } while (::FindNextFileW(handle, &metadata)); - FindClose(handle); - return matched; -} - -namespace strings { - -bool wcs_to_mbs(const WCHAR* s, string* out, bool outUtf8) { - if (null_or_empty(s)) { - out->clear(); - return true; - } - BOOL usedDefaultChar = FALSE; - SetLastError(0); - int size = WideCharToMultiByte( - outUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, nullptr, 0, nullptr, - outUtf8 ? nullptr : &usedDefaultChar); - if ((size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) - || usedDefaultChar) { - return false; - } - std::unique_ptr astr(new CHAR[size]); - WideCharToMultiByte( - outUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, astr.get(), size, nullptr, nullptr); - out->assign(astr.get()); - return true; -} - -bool mbs_to_wcs(const char* s, wstring* out, bool inUtf8) { - if (null_or_empty(s)) { - out->clear(); - return true; - } - - SetLastError(0); - int size = - MultiByteToWideChar(inUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, nullptr, 0); - if (size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) { - return false; - } - std::unique_ptr wstr(new WCHAR[size]); - MultiByteToWideChar( - inUtf8 ? CP_UTF8 : CP_ACP, 0, s, -1, wstr.get(), size + 1); - out->assign(wstr.get()); - return true; -} - -bool utf8_to_wcs(const char* input, wstring* out) { - return mbs_to_wcs(input, out, true); -} - -bool wcs_to_utf8(const wchar_t* input, string* out) { - return wcs_to_mbs(input, out, true); -} - -} // namespace strings -} // namespace win32 -} // namespace io -} // namespace protobuf -} // namespace google - -#endif // defined(_WIN32) diff --git a/third_party/protobuf-lite/message_lite.cc b/third_party/protobuf-lite/message_lite.cc deleted file mode 100644 index 0e8599147..000000000 --- a/third_party/protobuf-lite/message_lite.cc +++ /dev/null @@ -1,583 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Authors: wink@google.com (Wink Saville), -// kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { - -std::string MessageLite::InitializationErrorString() const { - return "(cannot determine missing fields for lite message)"; -} - -std::string MessageLite::DebugString() const { - std::uintptr_t address = reinterpret_cast(this); - return StrCat("MessageLite at 0x", strings::Hex(address)); -} - -namespace { - -// When serializing, we first compute the byte size, then serialize the message. -// If serialization produces a different number of bytes than expected, we -// call this function, which crashes. The problem could be due to a bug in the -// protobuf implementation but is more likely caused by concurrent modification -// of the message. This function attempts to distinguish between the two and -// provide a useful error message. -void ByteSizeConsistencyError(size_t byte_size_before_serialization, - size_t byte_size_after_serialization, - size_t bytes_produced_by_serialization, - const MessageLite& message) { - GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization) - << message.GetTypeName() - << " was modified concurrently during serialization."; - GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serialization) - << "Byte size calculation and serialization were inconsistent. This " - "may indicate a bug in protocol buffers or it may be caused by " - "concurrent modification of " - << message.GetTypeName() << "."; - GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal."; -} - -std::string InitializationErrorMessage(const char* action, - const MessageLite& message) { - // Note: We want to avoid depending on strutil in the lite library, otherwise - // we'd use: - // - // return strings::Substitute( - // "Can't $0 message of type \"$1\" because it is missing required " - // "fields: $2", - // action, message.GetTypeName(), - // message.InitializationErrorString()); - - std::string result; - result += "Can't "; - result += action; - result += " message of type \""; - result += message.GetTypeName(); - result += "\" because it is missing required fields: "; - result += message.InitializationErrorString(); - return result; -} - -inline StringPiece as_string_view(const void* data, int size) { - return StringPiece(static_cast(data), size); -} - -// Returns true of all required fields are present / have values. -inline bool CheckFieldPresence(const internal::ParseContext& ctx, - const MessageLite& msg, - MessageLite::ParseFlags parse_flags) { - if (PROTOBUF_PREDICT_FALSE((parse_flags & MessageLite::kMergePartial) != 0)) { - return true; - } - return msg.IsInitializedWithErrors(); -} - -} // namespace - -void MessageLite::LogInitializationErrorMessage() const { - GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this); -} - -namespace internal { - -template -bool MergeFromImpl(StringPiece input, MessageLite* msg, - MessageLite::ParseFlags parse_flags) { - const char* ptr; - internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), - aliasing, &ptr, input); - ptr = msg->_InternalParse(ptr, &ctx); - // ctx has an explicit limit set (length of string_view). - if (PROTOBUF_PREDICT_TRUE(ptr && ctx.EndedAtLimit())) { - return CheckFieldPresence(ctx, *msg, parse_flags); - } - return false; -} - -template -bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg, - MessageLite::ParseFlags parse_flags) { - const char* ptr; - internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), - aliasing, &ptr, input); - ptr = msg->_InternalParse(ptr, &ctx); - // ctx has no explicit limit (hence we end on end of stream) - if (PROTOBUF_PREDICT_TRUE(ptr && ctx.EndedAtEndOfStream())) { - return CheckFieldPresence(ctx, *msg, parse_flags); - } - return false; -} - -template -bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, - MessageLite::ParseFlags parse_flags) { - const char* ptr; - internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), - aliasing, &ptr, input.zcis, input.limit); - ptr = msg->_InternalParse(ptr, &ctx); - if (PROTOBUF_PREDICT_FALSE(!ptr)) return false; - ctx.BackUp(ptr); - if (PROTOBUF_PREDICT_TRUE(ctx.EndedAtLimit())) { - return CheckFieldPresence(ctx, *msg, parse_flags); - } - return false; -} - -template bool MergeFromImpl(StringPiece input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); -template bool MergeFromImpl(StringPiece input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); -template bool MergeFromImpl(io::ZeroCopyInputStream* input, - MessageLite* msg, - MessageLite::ParseFlags parse_flags); -template bool MergeFromImpl(io::ZeroCopyInputStream* input, - MessageLite* msg, - MessageLite::ParseFlags parse_flags); -template bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); -template bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, - MessageLite::ParseFlags parse_flags); - -} // namespace internal - -MessageLite* MessageLite::New(Arena* arena) const { - MessageLite* message = New(); - if (arena != NULL) { - arena->Own(message); - } - return message; -} - -class ZeroCopyCodedInputStream : public io::ZeroCopyInputStream { - public: - ZeroCopyCodedInputStream(io::CodedInputStream* cis) : cis_(cis) {} - bool Next(const void** data, int* size) final { - if (!cis_->GetDirectBufferPointer(data, size)) return false; - cis_->Skip(*size); - return true; - } - void BackUp(int count) final { cis_->Advance(-count); } - bool Skip(int count) final { return cis_->Skip(count); } - int64_t ByteCount() const final { return 0; } - - bool aliasing_enabled() { return cis_->aliasing_enabled_; } - - private: - io::CodedInputStream* cis_; -}; - -bool MessageLite::MergeFromImpl(io::CodedInputStream* input, - MessageLite::ParseFlags parse_flags) { - ZeroCopyCodedInputStream zcis(input); - const char* ptr; - internal::ParseContext ctx(input->RecursionBudget(), zcis.aliasing_enabled(), - &ptr, &zcis); - // MergePartialFromCodedStream allows terminating the wireformat by 0 or - // end-group tag. Leaving it up to the caller to verify correct ending by - // calling LastTagWas on input. We need to maintain this behavior. - ctx.TrackCorrectEnding(); - ctx.data().pool = input->GetExtensionPool(); - ctx.data().factory = input->GetExtensionFactory(); - ptr = _InternalParse(ptr, &ctx); - if (PROTOBUF_PREDICT_FALSE(!ptr)) return false; - ctx.BackUp(ptr); - if (!ctx.EndedAtEndOfStream()) { - GOOGLE_DCHECK(ctx.LastTag() != 1); // We can't end on a pushed limit. - if (ctx.IsExceedingLimit(ptr)) return false; - input->SetLastTag(ctx.LastTag()); - } else { - input->SetConsumed(); - } - return CheckFieldPresence(ctx, *this, parse_flags); -} - -bool MessageLite::MergePartialFromCodedStream(io::CodedInputStream* input) { - return MergeFromImpl(input, kMergePartial); -} - -bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) { - return MergeFromImpl(input, kMerge); -} - -bool MessageLite::ParseFromCodedStream(io::CodedInputStream* input) { - Clear(); - return MergeFromImpl(input, kParse); -} - -bool MessageLite::ParsePartialFromCodedStream(io::CodedInputStream* input) { - Clear(); - return MergeFromImpl(input, kParsePartial); -} - -bool MessageLite::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) { - return ParseFrom(input); -} - -bool MessageLite::ParsePartialFromZeroCopyStream( - io::ZeroCopyInputStream* input) { - return ParseFrom(input); -} - -bool MessageLite::ParseFromFileDescriptor(int file_descriptor) { - io::FileInputStream input(file_descriptor); - return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0; -} - -bool MessageLite::ParsePartialFromFileDescriptor(int file_descriptor) { - io::FileInputStream input(file_descriptor); - return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0; -} - -bool MessageLite::ParseFromIstream(std::istream* input) { - io::IstreamInputStream zero_copy_input(input); - return ParseFromZeroCopyStream(&zero_copy_input) && input->eof(); -} - -bool MessageLite::ParsePartialFromIstream(std::istream* input) { - io::IstreamInputStream zero_copy_input(input); - return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof(); -} - -bool MessageLite::MergePartialFromBoundedZeroCopyStream( - io::ZeroCopyInputStream* input, int size) { - return ParseFrom(internal::BoundedZCIS{input, size}); -} - -bool MessageLite::MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, - int size) { - return ParseFrom(internal::BoundedZCIS{input, size}); -} - -bool MessageLite::ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, - int size) { - return ParseFrom(internal::BoundedZCIS{input, size}); -} - -bool MessageLite::ParsePartialFromBoundedZeroCopyStream( - io::ZeroCopyInputStream* input, int size) { - return ParseFrom(internal::BoundedZCIS{input, size}); -} - -bool MessageLite::ParseFromString(ConstStringParam data) { - return ParseFrom(data); -} - -bool MessageLite::ParsePartialFromString(ConstStringParam data) { - return ParseFrom(data); -} - -bool MessageLite::ParseFromArray(const void* data, int size) { - return ParseFrom(as_string_view(data, size)); -} - -bool MessageLite::ParsePartialFromArray(const void* data, int size) { - return ParseFrom(as_string_view(data, size)); -} - -bool MessageLite::MergeFromString(ConstStringParam data) { - return ParseFrom(data); -} - - -// =================================================================== - -inline uint8* SerializeToArrayImpl(const MessageLite& msg, uint8* target, - int size) { - constexpr bool debug = false; - if (debug) { - // Force serialization to a stream with a block size of 1, which forces - // all writes to the stream to cross buffers triggering all fallback paths - // in the unittests when serializing to string / array. - io::ArrayOutputStream stream(target, size, 1); - uint8* ptr; - io::EpsCopyOutputStream out( - &stream, io::CodedOutputStream::IsDefaultSerializationDeterministic(), - &ptr); - ptr = msg._InternalSerialize(ptr, &out); - out.Trim(ptr); - GOOGLE_DCHECK(!out.HadError() && stream.ByteCount() == size); - return target + size; - } else { - io::EpsCopyOutputStream out( - target, size, - io::CodedOutputStream::IsDefaultSerializationDeterministic()); - auto res = msg._InternalSerialize(target, &out); - GOOGLE_DCHECK(target + size == res); - return res; - } -} - -uint8* MessageLite::SerializeWithCachedSizesToArray(uint8* target) const { - // We only optimize this when using optimize_for = SPEED. In other cases - // we just use the CodedOutputStream path. - return SerializeToArrayImpl(*this, target, GetCachedSize()); -} - -bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const { - GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); - return SerializePartialToCodedStream(output); -} - -bool MessageLite::SerializePartialToCodedStream( - io::CodedOutputStream* output) const { - const size_t size = ByteSizeLong(); // Force size to be cached. - if (size > INT_MAX) { - GOOGLE_LOG(ERROR) << GetTypeName() - << " exceeded maximum protobuf size of 2GB: " << size; - return false; - } - - int original_byte_count = output->ByteCount(); - SerializeWithCachedSizes(output); - if (output->HadError()) { - return false; - } - int final_byte_count = output->ByteCount(); - - if (final_byte_count - original_byte_count != size) { - ByteSizeConsistencyError(size, ByteSizeLong(), - final_byte_count - original_byte_count, *this); - } - - return true; -} - -bool MessageLite::SerializeToZeroCopyStream( - io::ZeroCopyOutputStream* output) const { - GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); - return SerializePartialToZeroCopyStream(output); -} - -bool MessageLite::SerializePartialToZeroCopyStream( - io::ZeroCopyOutputStream* output) const { - const size_t size = ByteSizeLong(); // Force size to be cached. - if (size > INT_MAX) { - GOOGLE_LOG(ERROR) << GetTypeName() - << " exceeded maximum protobuf size of 2GB: " << size; - return false; - } - - uint8* target; - io::EpsCopyOutputStream stream( - output, io::CodedOutputStream::IsDefaultSerializationDeterministic(), - &target); - target = _InternalSerialize(target, &stream); - stream.Trim(target); - if (stream.HadError()) return false; - return true; -} - -bool MessageLite::SerializeToFileDescriptor(int file_descriptor) const { - io::FileOutputStream output(file_descriptor); - return SerializeToZeroCopyStream(&output) && output.Flush(); -} - -bool MessageLite::SerializePartialToFileDescriptor(int file_descriptor) const { - io::FileOutputStream output(file_descriptor); - return SerializePartialToZeroCopyStream(&output) && output.Flush(); -} - -bool MessageLite::SerializeToOstream(std::ostream* output) const { - { - io::OstreamOutputStream zero_copy_output(output); - if (!SerializeToZeroCopyStream(&zero_copy_output)) return false; - } - return output->good(); -} - -bool MessageLite::SerializePartialToOstream(std::ostream* output) const { - io::OstreamOutputStream zero_copy_output(output); - return SerializePartialToZeroCopyStream(&zero_copy_output); -} - -bool MessageLite::AppendToString(std::string* output) const { - GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); - return AppendPartialToString(output); -} - -bool MessageLite::AppendPartialToString(std::string* output) const { - size_t old_size = output->size(); - size_t byte_size = ByteSizeLong(); - if (byte_size > INT_MAX) { - GOOGLE_LOG(ERROR) << GetTypeName() - << " exceeded maximum protobuf size of 2GB: " << byte_size; - return false; - } - - STLStringResizeUninitialized(output, old_size + byte_size); - uint8* start = - reinterpret_cast(io::mutable_string_data(output) + old_size); - SerializeToArrayImpl(*this, start, byte_size); - return true; -} - -bool MessageLite::SerializeToString(std::string* output) const { - output->clear(); - return AppendToString(output); -} - -bool MessageLite::SerializePartialToString(std::string* output) const { - output->clear(); - return AppendPartialToString(output); -} - -bool MessageLite::SerializeToArray(void* data, int size) const { - GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); - return SerializePartialToArray(data, size); -} - -bool MessageLite::SerializePartialToArray(void* data, int size) const { - const size_t byte_size = ByteSizeLong(); - if (byte_size > INT_MAX) { - GOOGLE_LOG(ERROR) << GetTypeName() - << " exceeded maximum protobuf size of 2GB: " << byte_size; - return false; - } - if (size < byte_size) return false; - uint8* start = reinterpret_cast(data); - SerializeToArrayImpl(*this, start, byte_size); - return true; -} - -std::string MessageLite::SerializeAsString() const { - // If the compiler implements the (Named) Return Value Optimization, - // the local variable 'output' will not actually reside on the stack - // of this function, but will be overlaid with the object that the - // caller supplied for the return value to be constructed in. - std::string output; - if (!AppendToString(&output)) output.clear(); - return output; -} - -std::string MessageLite::SerializePartialAsString() const { - std::string output; - if (!AppendPartialToString(&output)) output.clear(); - return output; -} - - -namespace internal { - -template <> -MessageLite* GenericTypeHandler::NewFromPrototype( - const MessageLite* prototype, Arena* arena) { - return prototype->New(arena); -} -template <> -void GenericTypeHandler::Merge(const MessageLite& from, - MessageLite* to) { - to->CheckTypeAndMergeFrom(from); -} -template <> -void GenericTypeHandler::Merge(const std::string& from, - std::string* to) { - *to = from; -} - -} // namespace internal - - -// =================================================================== -// Shutdown support. - -namespace internal { - -struct ShutdownData { - ~ShutdownData() { - std::reverse(functions.begin(), functions.end()); - for (auto pair : functions) pair.first(pair.second); - } - - static ShutdownData* get() { - static auto* data = new ShutdownData; - return data; - } - - std::vector> functions; - Mutex mutex; -}; - -static void RunZeroArgFunc(const void* arg) { - void (*func)() = reinterpret_cast(const_cast(arg)); - func(); -} - -void OnShutdown(void (*func)()) { - OnShutdownRun(RunZeroArgFunc, reinterpret_cast(func)); -} - -void OnShutdownRun(void (*f)(const void*), const void* arg) { - auto shutdown_data = ShutdownData::get(); - MutexLock lock(&shutdown_data->mutex); - shutdown_data->functions.push_back(std::make_pair(f, arg)); -} - -} // namespace internal - -void ShutdownProtobufLibrary() { - // This function should be called only once, but accepts multiple calls. - static bool is_shutdown = false; - if (!is_shutdown) { - delete internal::ShutdownData::get(); - is_shutdown = true; - } -} - - -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/parse_context.cc b/third_party/protobuf-lite/parse_context.cc deleted file mode 100644 index 22cdcbba5..000000000 --- a/third_party/protobuf-lite/parse_context.cc +++ /dev/null @@ -1,593 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { - -namespace { - -// Only call if at start of tag. -bool ParseEndsInSlopRegion(const char* begin, int overrun, int depth) { - constexpr int kSlopBytes = EpsCopyInputStream::kSlopBytes; - GOOGLE_DCHECK(overrun >= 0); - GOOGLE_DCHECK(overrun <= kSlopBytes); - auto ptr = begin + overrun; - auto end = begin + kSlopBytes; - while (ptr < end) { - uint32 tag; - ptr = ReadTag(ptr, &tag); - if (ptr == nullptr || ptr > end) return false; - // ending on 0 tag is allowed and is the major reason for the necessity of - // this function. - if (tag == 0) return true; - switch (tag & 7) { - case 0: { // Varint - uint64 val; - ptr = VarintParse(ptr, &val); - if (ptr == nullptr) return false; - break; - } - case 1: { // fixed64 - ptr += 8; - break; - } - case 2: { // len delim - int32 size = ReadSize(&ptr); - if (ptr == nullptr || size > end - ptr) return false; - ptr += size; - break; - } - case 3: { // start group - depth++; - break; - } - case 4: { // end group - if (--depth < 0) return true; // We exit early - break; - } - case 5: { // fixed32 - ptr += 4; - break; - } - default: - return false; // Unknown wireformat - } - } - return false; -} - -} // namespace - -const char* EpsCopyInputStream::NextBuffer(int overrun, int depth) { - if (next_chunk_ == nullptr) return nullptr; // We've reached end of stream. - if (next_chunk_ != buffer_) { - GOOGLE_DCHECK(size_ > kSlopBytes); - // The chunk is large enough to be used directly - buffer_end_ = next_chunk_ + size_ - kSlopBytes; - auto res = next_chunk_; - next_chunk_ = buffer_; - if (aliasing_ == kOnPatch) aliasing_ = kNoDelta; - return res; - } - // Move the slop bytes of previous buffer to start of the patch buffer. - // Note we must use memmove because the previous buffer could be part of - // buffer_. - std::memmove(buffer_, buffer_end_, kSlopBytes); - if (overall_limit_ > 0 && - (depth < 0 || !ParseEndsInSlopRegion(buffer_, overrun, depth))) { - const void* data; - // ZeroCopyInputStream indicates Next may return 0 size buffers. Hence - // we loop. - while (StreamNext(&data)) { - if (size_ > kSlopBytes) { - // We got a large chunk - std::memcpy(buffer_ + kSlopBytes, data, kSlopBytes); - next_chunk_ = static_cast(data); - buffer_end_ = buffer_ + kSlopBytes; - if (aliasing_ >= kNoDelta) aliasing_ = kOnPatch; - return buffer_; - } else if (size_ > 0) { - std::memcpy(buffer_ + kSlopBytes, data, size_); - next_chunk_ = buffer_; - buffer_end_ = buffer_ + size_; - if (aliasing_ >= kNoDelta) aliasing_ = kOnPatch; - return buffer_; - } - GOOGLE_DCHECK(size_ == 0) << size_; - } - overall_limit_ = 0; // Next failed, no more needs for next - } - // End of stream or array - if (aliasing_ == kNoDelta) { - // If there is no more block and aliasing is true, the previous block - // is still valid and we can alias. We have users relying on string_view's - // obtained from protos to outlive the proto, when the parse was from an - // array. This guarantees string_view's are always aliased if parsed from - // an array. - aliasing_ = reinterpret_cast(buffer_end_) - - reinterpret_cast(buffer_); - } - next_chunk_ = nullptr; - buffer_end_ = buffer_ + kSlopBytes; - size_ = 0; - return buffer_; -} - -const char* EpsCopyInputStream::Next() { - GOOGLE_DCHECK(limit_ > kSlopBytes); - auto p = NextBuffer(0 /* immaterial */, -1); - if (p == nullptr) { - limit_end_ = buffer_end_; - // Distinguish ending on a pushed limit or ending on end-of-stream. - SetEndOfStream(); - return nullptr; - } - limit_ -= buffer_end_ - p; // Adjust limit_ relative to new anchor - limit_end_ = buffer_end_ + std::min(0, limit_); - return p; -} - -std::pair EpsCopyInputStream::DoneFallback(int overrun, - int depth) { - // Did we exceeded the limit (parse error). - if (PROTOBUF_PREDICT_FALSE(overrun > limit_)) return {nullptr, true}; - GOOGLE_DCHECK(overrun != limit_); // Guaranteed by caller. - GOOGLE_DCHECK(overrun < limit_); // Follows from above - // TODO(gerbens) Instead of this dcheck we could just assign, and remove - // updating the limit_end from PopLimit, ie. - // limit_end_ = buffer_end_ + (std::min)(0, limit_); - // if (ptr < limit_end_) return {ptr, false}; - GOOGLE_DCHECK(limit_end_ == buffer_end_ + (std::min)(0, limit_)); - // At this point we know the following assertion holds. - GOOGLE_DCHECK(limit_ > 0); - GOOGLE_DCHECK(limit_end_ == buffer_end_); // because limit_ > 0 - const char* p; - do { - // We are past the end of buffer_end_, in the slop region. - GOOGLE_DCHECK(overrun >= 0); - p = NextBuffer(overrun, depth); - if (p == nullptr) { - // We are at the end of the stream - if (PROTOBUF_PREDICT_FALSE(overrun != 0)) return {nullptr, true}; - GOOGLE_DCHECK(limit_ > 0); - limit_end_ = buffer_end_; - // Distinguish ending on a pushed limit or ending on end-of-stream. - SetEndOfStream(); - return {buffer_end_, true}; - } - limit_ -= buffer_end_ - p; // Adjust limit_ relative to new anchor - p += overrun; - overrun = p - buffer_end_; - } while (overrun >= 0); - limit_end_ = buffer_end_ + std::min(0, limit_); - return {p, false}; -} - -const char* EpsCopyInputStream::SkipFallback(const char* ptr, int size) { - return AppendSize(ptr, size, [](const char* p, int s) {}); -} - -const char* EpsCopyInputStream::ReadStringFallback(const char* ptr, int size, - std::string* str) { - str->clear(); - if (PROTOBUF_PREDICT_TRUE(size <= buffer_end_ - ptr + limit_)) { - // Reserve the string up to a static safe size. If strings are bigger than - // this we proceed by growing the string as needed. This protects against - // malicious payloads making protobuf hold on to a lot of memory. - str->reserve(str->size() + std::min(size, kSafeStringSize)); - } - return AppendSize(ptr, size, - [str](const char* p, int s) { str->append(p, s); }); -} - -const char* EpsCopyInputStream::AppendStringFallback(const char* ptr, int size, - std::string* str) { - if (PROTOBUF_PREDICT_TRUE(size <= buffer_end_ - ptr + limit_)) { - // Reserve the string up to a static safe size. If strings are bigger than - // this we proceed by growing the string as needed. This protects against - // malicious payloads making protobuf hold on to a lot of memory. - str->reserve(str->size() + std::min(size, kSafeStringSize)); - } - return AppendSize(ptr, size, - [str](const char* p, int s) { str->append(p, s); }); -} - - -template -const char* EpsCopyInputStream::ReadRepeatedFixed(const char* ptr, - Tag expected_tag, - RepeatedField* out) { - do { - out->Add(UnalignedLoad(ptr)); - ptr += sizeof(T); - if (PROTOBUF_PREDICT_FALSE(ptr >= limit_end_)) return ptr; - } while (UnalignedLoad(ptr) == expected_tag&& ptr += sizeof(Tag)); - return ptr; -} - -template -void byteswap(void* p); -template <> -void byteswap<1>(void* p) {} -template <> -void byteswap<4>(void* p) { - *static_cast(p) = bswap_32(*static_cast(p)); -} -template <> -void byteswap<8>(void* p) { - *static_cast(p) = bswap_64(*static_cast(p)); -} - -template -const char* EpsCopyInputStream::ReadPackedFixed(const char* ptr, int size, - RepeatedField* out) { - int nbytes = buffer_end_ + kSlopBytes - ptr; - while (size > nbytes) { - int num = nbytes / sizeof(T); - int old_entries = out->size(); - out->Reserve(old_entries + num); - int block_size = num * sizeof(T); - auto dst = out->AddNAlreadyReserved(num); -#ifdef PROTOBUF_LITTLE_ENDIAN - std::memcpy(dst, ptr, block_size); -#else - for (int i = 0; i < num; i++) - dst[i] = UnalignedLoad(ptr + i * sizeof(T)); -#endif - size -= block_size; - if (limit_ <= kSlopBytes) return nullptr; - ptr = Next(); - if (ptr == nullptr) return nullptr; - ptr += kSlopBytes - (nbytes - block_size); - nbytes = buffer_end_ + kSlopBytes - ptr; - } - int num = size / sizeof(T); - int old_entries = out->size(); - out->Reserve(old_entries + num); - int block_size = num * sizeof(T); - auto dst = out->AddNAlreadyReserved(num); -#ifdef PROTOBUF_LITTLE_ENDIAN - std::memcpy(dst, ptr, block_size); -#else - for (int i = 0; i < num; i++) dst[i] = UnalignedLoad(ptr + i * sizeof(T)); -#endif - ptr += block_size; - if (size != block_size) return nullptr; - return ptr; -} - -const char* EpsCopyInputStream::InitFrom(io::ZeroCopyInputStream* zcis) { - zcis_ = zcis; - const void* data; - int size; - limit_ = INT_MAX; - if (zcis->Next(&data, &size)) { - overall_limit_ -= size; - if (size > kSlopBytes) { - auto ptr = static_cast(data); - limit_ -= size - kSlopBytes; - limit_end_ = buffer_end_ = ptr + size - kSlopBytes; - next_chunk_ = buffer_; - if (aliasing_ == kOnPatch) aliasing_ = kNoDelta; - return ptr; - } else { - limit_end_ = buffer_end_ = buffer_ + kSlopBytes; - next_chunk_ = buffer_; - auto ptr = buffer_ + 2 * kSlopBytes - size; - std::memcpy(ptr, data, size); - return ptr; - } - } - overall_limit_ = 0; - next_chunk_ = nullptr; - size_ = 0; - limit_end_ = buffer_end_ = buffer_; - return buffer_; -} - -const char* ParseContext::ParseMessage(MessageLite* msg, const char* ptr) { - return ParseMessage(msg, ptr); -} -const char* ParseContext::ParseMessage(Message* msg, const char* ptr) { - // Use reinterptret case to prevent inclusion of non lite header - return ParseMessage(reinterpret_cast(msg), ptr); -} - -inline void WriteVarint(uint64 val, std::string* s) { - while (val >= 128) { - uint8 c = val | 0x80; - s->push_back(c); - val >>= 7; - } - s->push_back(val); -} - -void WriteVarint(uint32 num, uint64 val, std::string* s) { - WriteVarint(num << 3, s); - WriteVarint(val, s); -} - -void WriteLengthDelimited(uint32 num, StringPiece val, std::string* s) { - WriteVarint((num << 3) + 2, s); - WriteVarint(val.size(), s); - s->append(val.data(), val.size()); -} - -std::pair VarintParseSlow32(const char* p, uint32 res) { - for (std::uint32_t i = 2; i < 5; i++) { - uint32 byte = static_cast(p[i]); - res += (byte - 1) << (7 * i); - if (PROTOBUF_PREDICT_TRUE(byte < 128)) { - return {p + i + 1, res}; - } - } - // Accept >5 bytes - for (std::uint32_t i = 5; i < 10; i++) { - uint32 byte = static_cast(p[i]); - if (PROTOBUF_PREDICT_TRUE(byte < 128)) { - return {p + i + 1, res}; - } - } - return {nullptr, 0}; -} - -std::pair VarintParseSlow64(const char* p, uint32 res32) { - uint64 res = res32; - for (std::uint32_t i = 2; i < 10; i++) { - uint64 byte = static_cast(p[i]); - res += (byte - 1) << (7 * i); - if (PROTOBUF_PREDICT_TRUE(byte < 128)) { - return {p + i + 1, res}; - } - } - return {nullptr, 0}; -} - -std::pair ReadTagFallback(const char* p, uint32 res) { - for (std::uint32_t i = 2; i < 5; i++) { - uint32 byte = static_cast(p[i]); - res += (byte - 1) << (7 * i); - if (PROTOBUF_PREDICT_TRUE(byte < 128)) { - return {p + i + 1, res}; - } - } - return {nullptr, 0}; -} - -std::pair ReadSizeFallback(const char* p, uint32 res) { - for (std::uint32_t i = 1; i < 4; i++) { - uint32 byte = static_cast(p[i]); - res += (byte - 1) << (7 * i); - if (PROTOBUF_PREDICT_TRUE(byte < 128)) { - return {p + i + 1, res}; - } - } - std::uint32_t byte = static_cast(p[4]); - if (PROTOBUF_PREDICT_FALSE(byte >= 8)) return {nullptr, 0}; // size >= 2gb - res += (byte - 1) << 28; - // Protect against sign integer overflow in PushLimit. Limits are relative - // to buffer ends and ptr could potential be kSlopBytes beyond a buffer end. - // To protect against overflow we reject limits absurdly close to INT_MAX. - if (PROTOBUF_PREDICT_FALSE(res > INT_MAX - ParseContext::kSlopBytes)) { - return {nullptr, 0}; - } - return {p + 5, res}; -} - -const char* StringParser(const char* begin, const char* end, void* object, - ParseContext*) { - auto str = static_cast(object); - str->append(begin, end - begin); - return end; -} - -// Defined in wire_format_lite.cc -void PrintUTF8ErrorLog(const char* field_name, const char* operation_str, - bool emit_stacktrace); - -bool VerifyUTF8(StringPiece str, const char* field_name) { - if (!IsStructurallyValidUTF8(str)) { - PrintUTF8ErrorLog(field_name, "parsing", false); - return false; - } - return true; -} - -const char* InlineGreedyStringParser(std::string* s, const char* ptr, - ParseContext* ctx) { - int size = ReadSize(&ptr); - if (!ptr) return nullptr; - return ctx->ReadString(ptr, size, s); -} - - -template -const char* VarintParser(void* object, const char* ptr, ParseContext* ctx) { - return ctx->ReadPackedVarint(ptr, [object](uint64 varint) { - T val; - if (sign) { - if (sizeof(T) == 8) { - val = WireFormatLite::ZigZagDecode64(varint); - } else { - val = WireFormatLite::ZigZagDecode32(varint); - } - } else { - val = varint; - } - static_cast*>(object)->Add(val); - }); -} - -const char* PackedInt32Parser(void* object, const char* ptr, - ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} -const char* PackedUInt32Parser(void* object, const char* ptr, - ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} -const char* PackedInt64Parser(void* object, const char* ptr, - ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} -const char* PackedUInt64Parser(void* object, const char* ptr, - ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} -const char* PackedSInt32Parser(void* object, const char* ptr, - ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} -const char* PackedSInt64Parser(void* object, const char* ptr, - ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} - -const char* PackedEnumParser(void* object, const char* ptr, ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} - -const char* PackedBoolParser(void* object, const char* ptr, ParseContext* ctx) { - return VarintParser(object, ptr, ctx); -} - -template -const char* FixedParser(void* object, const char* ptr, ParseContext* ctx) { - int size = ReadSize(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - return ctx->ReadPackedFixed(ptr, size, - static_cast*>(object)); -} - -const char* PackedFixed32Parser(void* object, const char* ptr, - ParseContext* ctx) { - return FixedParser(object, ptr, ctx); -} -const char* PackedSFixed32Parser(void* object, const char* ptr, - ParseContext* ctx) { - return FixedParser(object, ptr, ctx); -} -const char* PackedFixed64Parser(void* object, const char* ptr, - ParseContext* ctx) { - return FixedParser(object, ptr, ctx); -} -const char* PackedSFixed64Parser(void* object, const char* ptr, - ParseContext* ctx) { - return FixedParser(object, ptr, ctx); -} -const char* PackedFloatParser(void* object, const char* ptr, - ParseContext* ctx) { - return FixedParser(object, ptr, ctx); -} -const char* PackedDoubleParser(void* object, const char* ptr, - ParseContext* ctx) { - return FixedParser(object, ptr, ctx); -} - -class UnknownFieldLiteParserHelper { - public: - explicit UnknownFieldLiteParserHelper(std::string* unknown) - : unknown_(unknown) {} - - void AddVarint(uint32 num, uint64 value) { - if (unknown_ == nullptr) return; - WriteVarint(num * 8, unknown_); - WriteVarint(value, unknown_); - } - void AddFixed64(uint32 num, uint64 value) { - if (unknown_ == nullptr) return; - WriteVarint(num * 8 + 1, unknown_); - char buffer[8]; - io::CodedOutputStream::WriteLittleEndian64ToArray( - value, reinterpret_cast(buffer)); - unknown_->append(buffer, 8); - } - const char* ParseLengthDelimited(uint32 num, const char* ptr, - ParseContext* ctx) { - int size = ReadSize(&ptr); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (unknown_ == nullptr) return ctx->Skip(ptr, size); - WriteVarint(num * 8 + 2, unknown_); - WriteVarint(size, unknown_); - return ctx->AppendString(ptr, size, unknown_); - } - const char* ParseGroup(uint32 num, const char* ptr, ParseContext* ctx) { - if (unknown_) WriteVarint(num * 8 + 3, unknown_); - ptr = ctx->ParseGroup(this, ptr, num * 8 + 3); - GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); - if (unknown_) WriteVarint(num * 8 + 4, unknown_); - return ptr; - } - void AddFixed32(uint32 num, uint32 value) { - if (unknown_ == nullptr) return; - WriteVarint(num * 8 + 5, unknown_); - char buffer[4]; - io::CodedOutputStream::WriteLittleEndian32ToArray( - value, reinterpret_cast(buffer)); - unknown_->append(buffer, 4); - } - - const char* _InternalParse(const char* ptr, ParseContext* ctx) { - return WireFormatParser(*this, ptr, ctx); - } - - private: - std::string* unknown_; -}; - -const char* UnknownGroupLiteParse(std::string* unknown, const char* ptr, - ParseContext* ctx) { - UnknownFieldLiteParserHelper field_parser(unknown); - return WireFormatParser(field_parser, ptr, ctx); -} - -const char* UnknownFieldParse(uint32 tag, std::string* unknown, const char* ptr, - ParseContext* ctx) { - UnknownFieldLiteParserHelper field_parser(unknown); - return FieldParser(tag, field_parser, ptr, ctx); -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/repeated_field.cc b/third_party/protobuf-lite/repeated_field.cc deleted file mode 100644 index 645067912..000000000 --- a/third_party/protobuf-lite/repeated_field.cc +++ /dev/null @@ -1,136 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include - -#include - -#include -#include -#include - -#include - -namespace google { -namespace protobuf { - -namespace internal { - -void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) { - int new_size = current_size_ + extend_amount; - if (total_size_ >= new_size) { - // N.B.: rep_ is non-NULL because extend_amount is always > 0, hence - // total_size must be non-zero since it is lower-bounded by new_size. - return &rep_->elements[current_size_]; - } - Rep* old_rep = rep_; - Arena* arena = GetArena(); - new_size = std::max(internal::kRepeatedFieldLowerClampLimit, - std::max(total_size_ * 2, new_size)); - GOOGLE_CHECK_LE(new_size, (std::numeric_limits::max() - kRepHeaderSize) / - sizeof(old_rep->elements[0])) - << "Requested size is too large to fit into size_t."; - size_t bytes = kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size; - if (arena == NULL) { - rep_ = reinterpret_cast(::operator new(bytes)); - } else { - rep_ = reinterpret_cast(Arena::CreateArray(arena, bytes)); - } -#if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) - const int old_total_size = total_size_; -#endif - total_size_ = new_size; - if (old_rep && old_rep->allocated_size > 0) { - memcpy(rep_->elements, old_rep->elements, - old_rep->allocated_size * sizeof(rep_->elements[0])); - rep_->allocated_size = old_rep->allocated_size; - } else { - rep_->allocated_size = 0; - } - if (arena == NULL) { -#if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation) - const size_t old_size = - old_total_size * sizeof(rep_->elements[0]) + kRepHeaderSize; - ::operator delete(static_cast(old_rep), old_size); -#else - ::operator delete(static_cast(old_rep)); -#endif - } - return &rep_->elements[current_size_]; -} - -void RepeatedPtrFieldBase::Reserve(int new_size) { - if (new_size > current_size_) { - InternalExtend(new_size - current_size_); - } -} - -void RepeatedPtrFieldBase::CloseGap(int start, int num) { - if (rep_ == NULL) return; - // Close up a gap of "num" elements starting at offset "start". - for (int i = start + num; i < rep_->allocated_size; ++i) - rep_->elements[i - num] = rep_->elements[i]; - current_size_ -= num; - rep_->allocated_size -= num; -} - -MessageLite* RepeatedPtrFieldBase::AddWeak(const MessageLite* prototype) { - if (rep_ != NULL && current_size_ < rep_->allocated_size) { - return reinterpret_cast(rep_->elements[current_size_++]); - } - if (!rep_ || rep_->allocated_size == total_size_) { - Reserve(total_size_ + 1); - } - ++rep_->allocated_size; - MessageLite* result = prototype - ? prototype->New(arena_) - : Arena::CreateMessage(arena_); - rep_->elements[current_size_++] = result; - return result; -} - -} // namespace internal - - -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField; -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField; -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField; -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField; -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField; -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField; -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedField; -template class PROTOBUF_EXPORT_TEMPLATE_DEFINE RepeatedPtrField; - -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/status.cc b/third_party/protobuf-lite/status.cc deleted file mode 100644 index 03b37c365..000000000 --- a/third_party/protobuf-lite/status.cc +++ /dev/null @@ -1,134 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include -#include -#include - -namespace google { -namespace protobuf { -namespace util { -namespace error { -inline std::string CodeEnumToString(error::Code code) { - switch (code) { - case OK: - return "OK"; - case CANCELLED: - return "CANCELLED"; - case UNKNOWN: - return "UNKNOWN"; - case INVALID_ARGUMENT: - return "INVALID_ARGUMENT"; - case DEADLINE_EXCEEDED: - return "DEADLINE_EXCEEDED"; - case NOT_FOUND: - return "NOT_FOUND"; - case ALREADY_EXISTS: - return "ALREADY_EXISTS"; - case PERMISSION_DENIED: - return "PERMISSION_DENIED"; - case UNAUTHENTICATED: - return "UNAUTHENTICATED"; - case RESOURCE_EXHAUSTED: - return "RESOURCE_EXHAUSTED"; - case FAILED_PRECONDITION: - return "FAILED_PRECONDITION"; - case ABORTED: - return "ABORTED"; - case OUT_OF_RANGE: - return "OUT_OF_RANGE"; - case UNIMPLEMENTED: - return "UNIMPLEMENTED"; - case INTERNAL: - return "INTERNAL"; - case UNAVAILABLE: - return "UNAVAILABLE"; - case DATA_LOSS: - return "DATA_LOSS"; - } - - // No default clause, clang will abort if a code is missing from - // above switch. - return "UNKNOWN"; -} -} // namespace error. - -const Status Status::OK = Status(); -const Status Status::CANCELLED = Status(error::CANCELLED, ""); -const Status Status::UNKNOWN = Status(error::UNKNOWN, ""); - -Status::Status() : error_code_(error::OK) { -} - -Status::Status(error::Code error_code, StringPiece error_message) - : error_code_(error_code) { - if (error_code != error::OK) { - error_message_ = error_message.ToString(); - } -} - -Status::Status(const Status& other) - : error_code_(other.error_code_), error_message_(other.error_message_) { -} - -Status& Status::operator=(const Status& other) { - error_code_ = other.error_code_; - error_message_ = other.error_message_; - return *this; -} - -bool Status::operator==(const Status& x) const { - return error_code_ == x.error_code_ && - error_message_ == x.error_message_; -} - -std::string Status::ToString() const { - if (error_code_ == error::OK) { - return "OK"; - } else { - if (error_message_.empty()) { - return error::CodeEnumToString(error_code_); - } else { - return error::CodeEnumToString(error_code_) + ":" + - error_message_; - } - } -} - -std::ostream& operator<<(std::ostream& os, const Status& x) { - os << x.ToString(); - return os; -} - -} // namespace util -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/statusor.cc b/third_party/protobuf-lite/statusor.cc deleted file mode 100644 index c744b8d23..000000000 --- a/third_party/protobuf-lite/statusor.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include - -namespace google { -namespace protobuf { -namespace util { -namespace internal { - -void StatusOrHelper::Crash(const Status& status) { - GOOGLE_LOG(FATAL) << "Attempting to fetch value instead of handling error " - << status.ToString(); -} - -} // namespace internal -} // namespace util -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/stringpiece.cc b/third_party/protobuf-lite/stringpiece.cc deleted file mode 100644 index 353c78cad..000000000 --- a/third_party/protobuf-lite/stringpiece.cc +++ /dev/null @@ -1,270 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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 - -#include -#include -#include -#include -#include - -#include - -namespace google { -namespace protobuf { -std::ostream& operator<<(std::ostream& o, StringPiece piece) { - o.write(piece.data(), piece.size()); - return o; -} - -// Out-of-line error path. -void StringPiece::LogFatalSizeTooBig(size_t size, const char* details) { - GOOGLE_LOG(FATAL) << "size too big: " << size << " details: " << details; -} - -StringPiece::StringPiece(StringPiece x, stringpiece_ssize_type pos) - : ptr_(x.ptr_ + pos), length_(x.length_ - pos) { - GOOGLE_DCHECK_LE(0, pos); - GOOGLE_DCHECK_LE(pos, x.length_); -} - -StringPiece::StringPiece(StringPiece x, - stringpiece_ssize_type pos, - stringpiece_ssize_type len) - : ptr_(x.ptr_ + pos), length_(std::min(len, x.length_ - pos)) { - GOOGLE_DCHECK_LE(0, pos); - GOOGLE_DCHECK_LE(pos, x.length_); - GOOGLE_DCHECK_GE(len, 0); -} - -void StringPiece::CopyToString(std::string* target) const { - target->assign(ptr_, length_); -} - -void StringPiece::AppendToString(std::string* target) const { - target->append(ptr_, length_); -} - -bool StringPiece::Consume(StringPiece x) { - if (starts_with(x)) { - ptr_ += x.length_; - length_ -= x.length_; - return true; - } - return false; -} - -bool StringPiece::ConsumeFromEnd(StringPiece x) { - if (ends_with(x)) { - length_ -= x.length_; - return true; - } - return false; -} - -stringpiece_ssize_type StringPiece::copy(char* buf, - size_type n, - size_type pos) const { - stringpiece_ssize_type ret = std::min(length_ - pos, n); - memcpy(buf, ptr_ + pos, ret); - return ret; -} - -bool StringPiece::contains(StringPiece s) const { - return find(s, 0) != npos; -} - -stringpiece_ssize_type StringPiece::find(StringPiece s, size_type pos) const { - if (length_ <= 0 || pos > static_cast(length_)) { - if (length_ == 0 && pos == 0 && s.length_ == 0) return 0; - return npos; - } - const char *result = std::search(ptr_ + pos, ptr_ + length_, - s.ptr_, s.ptr_ + s.length_); - return result == ptr_ + length_ ? npos : result - ptr_; -} - -stringpiece_ssize_type StringPiece::find(char c, size_type pos) const { - if (length_ <= 0 || pos >= static_cast(length_)) { - return npos; - } - const char* result = static_cast( - memchr(ptr_ + pos, c, length_ - pos)); - return result != nullptr ? result - ptr_ : npos; -} - -stringpiece_ssize_type StringPiece::rfind(StringPiece s, size_type pos) const { - if (length_ < s.length_) return npos; - const size_t ulen = length_; - if (s.length_ == 0) return std::min(ulen, pos); - - const char* last = ptr_ + std::min(ulen - s.length_, pos) + s.length_; - const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); - return result != last ? result - ptr_ : npos; -} - -// Search range is [0..pos] inclusive. If pos == npos, search everything. -stringpiece_ssize_type StringPiece::rfind(char c, size_type pos) const { - // Note: memrchr() is not available on Windows. - if (length_ <= 0) return npos; - for (stringpiece_ssize_type i = - std::min(pos, static_cast(length_ - 1)); - i >= 0; --i) { - if (ptr_[i] == c) { - return i; - } - } - return npos; -} - -// For each character in characters_wanted, sets the index corresponding -// to the ASCII code of that character to 1 in table. This is used by -// the find_.*_of methods below to tell whether or not a character is in -// the lookup table in constant time. -// The argument `table' must be an array that is large enough to hold all -// the possible values of an unsigned char. Thus it should be be declared -// as follows: -// bool table[UCHAR_MAX + 1] -static inline void BuildLookupTable(StringPiece characters_wanted, - bool* table) { - const stringpiece_ssize_type length = characters_wanted.length(); - const char* const data = characters_wanted.data(); - for (stringpiece_ssize_type i = 0; i < length; ++i) { - table[static_cast(data[i])] = true; - } -} - -stringpiece_ssize_type StringPiece::find_first_of(StringPiece s, - size_type pos) const { - if (length_ <= 0 || s.length_ <= 0) { - return npos; - } - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_first_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (stringpiece_ssize_type i = pos; i < length_; ++i) { - if (lookup[static_cast(ptr_[i])]) { - return i; - } - } - return npos; -} - -stringpiece_ssize_type StringPiece::find_first_not_of(StringPiece s, - size_type pos) const { - if (length_ <= 0) return npos; - if (s.length_ <= 0) return 0; - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (stringpiece_ssize_type i = pos; i < length_; ++i) { - if (!lookup[static_cast(ptr_[i])]) { - return i; - } - } - return npos; -} - -stringpiece_ssize_type StringPiece::find_first_not_of(char c, - size_type pos) const { - if (length_ <= 0) return npos; - - for (; pos < static_cast(length_); ++pos) { - if (ptr_[pos] != c) { - return pos; - } - } - return npos; -} - -stringpiece_ssize_type StringPiece::find_last_of(StringPiece s, - size_type pos) const { - if (length_ <= 0 || s.length_ <= 0) return npos; - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_last_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (stringpiece_ssize_type i = - std::min(pos, static_cast(length_ - 1)); i >= 0; --i) { - if (lookup[static_cast(ptr_[i])]) { - return i; - } - } - return npos; -} - -stringpiece_ssize_type StringPiece::find_last_not_of(StringPiece s, - size_type pos) const { - if (length_ <= 0) return npos; - - stringpiece_ssize_type i = std::min(pos, static_cast(length_ - 1)); - if (s.length_ <= 0) return i; - - // Avoid the cost of BuildLookupTable() for a single-character search. - if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos); - - bool lookup[UCHAR_MAX + 1] = { false }; - BuildLookupTable(s, lookup); - for (; i >= 0; --i) { - if (!lookup[static_cast(ptr_[i])]) { - return i; - } - } - return npos; -} - -stringpiece_ssize_type StringPiece::find_last_not_of(char c, - size_type pos) const { - if (length_ <= 0) return npos; - - for (stringpiece_ssize_type i = - std::min(pos, static_cast(length_ - 1)); i >= 0; --i) { - if (ptr_[i] != c) { - return i; - } - } - return npos; -} - -StringPiece StringPiece::substr(size_type pos, size_type n) const { - if (pos > length_) pos = length_; - if (n > length_ - pos) n = length_ - pos; - return StringPiece(ptr_ + pos, n); -} - -const StringPiece::size_type StringPiece::npos = size_type(-1); - -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/stringprintf.cc b/third_party/protobuf-lite/stringprintf.cc deleted file mode 100644 index 260316493..000000000 --- a/third_party/protobuf-lite/stringprintf.cc +++ /dev/null @@ -1,176 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2012 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// from google3/base/stringprintf.cc - -#include - -#include -#include // For va_list and related operations -#include // MSVC requires this for _vsnprintf -#include - -#include -#include - -namespace google { -namespace protobuf { - -#ifdef _MSC_VER -enum { IS_COMPILER_MSVC = 1 }; -#ifndef va_copy -// Define va_copy for MSVC. This is a hack, assuming va_list is simply a -// pointer into the stack and is safe to copy. -#define va_copy(dest, src) ((dest) = (src)) -#endif -#else -enum { IS_COMPILER_MSVC = 0 }; -#endif - -void StringAppendV(std::string* dst, const char* format, va_list ap) { - // First try with a small fixed size buffer - static const int kSpaceLength = 1024; - char space[kSpaceLength]; - - // It's possible for methods that use a va_list to invalidate - // the data in it upon use. The fix is to make a copy - // of the structure before using it and use that copy instead. - va_list backup_ap; - va_copy(backup_ap, ap); - int result = vsnprintf(space, kSpaceLength, format, backup_ap); - va_end(backup_ap); - - if (result < kSpaceLength) { - if (result >= 0) { - // Normal case -- everything fit. - dst->append(space, result); - return; - } - - if (IS_COMPILER_MSVC) { - // Error or MSVC running out of space. MSVC 8.0 and higher - // can be asked about space needed with the special idiom below: - va_copy(backup_ap, ap); - result = vsnprintf(nullptr, 0, format, backup_ap); - va_end(backup_ap); - } - - if (result < 0) { - // Just an error. - return; - } - } - - // Increase the buffer size to the size requested by vsnprintf, - // plus one for the closing \0. - int length = result+1; - char* buf = new char[length]; - - // Restore the va_list before we use it again - va_copy(backup_ap, ap); - result = vsnprintf(buf, length, format, backup_ap); - va_end(backup_ap); - - if (result >= 0 && result < length) { - // It fit - dst->append(buf, result); - } - delete[] buf; -} - -std::string StringPrintf(const char* format, ...) { - va_list ap; - va_start(ap, format); - std::string result; - StringAppendV(&result, format, ap); - va_end(ap); - return result; -} - -const std::string& SStringPrintf(std::string* dst, const char* format, ...) { - va_list ap; - va_start(ap, format); - dst->clear(); - StringAppendV(dst, format, ap); - va_end(ap); - return *dst; -} - -void StringAppendF(std::string* dst, const char* format, ...) { - va_list ap; - va_start(ap, format); - StringAppendV(dst, format, ap); - va_end(ap); -} - -// Max arguments supported by StringPrintVector -const int kStringPrintfVectorMaxArgs = 32; - -// An empty block of zero for filler arguments. This is const so that if -// printf tries to write to it (via %n) then the program gets a SIGSEGV -// and we can fix the problem or protect against an attack. -static const char string_printf_empty_block[256] = { '\0' }; - -std::string StringPrintfVector(const char* format, - const std::vector& v) { - GOOGLE_CHECK_LE(v.size(), kStringPrintfVectorMaxArgs) - << "StringPrintfVector currently only supports up to " - << kStringPrintfVectorMaxArgs << " arguments. " - << "Feel free to add support for more if you need it."; - - // Add filler arguments so that bogus format+args have a harder time - // crashing the program, corrupting the program (%n), - // or displaying random chunks of memory to users. - - const char* cstr[kStringPrintfVectorMaxArgs]; - for (int i = 0; i < v.size(); ++i) { - cstr[i] = v[i].c_str(); - } - for (int i = v.size(); i < GOOGLE_ARRAYSIZE(cstr); ++i) { - cstr[i] = &string_printf_empty_block[0]; - } - - // I do not know any way to pass kStringPrintfVectorMaxArgs arguments, - // or any way to build a va_list by hand, or any API for printf - // that accepts an array of arguments. The best I can do is stick - // this COMPILE_ASSERT right next to the actual statement. - - GOOGLE_COMPILE_ASSERT(kStringPrintfVectorMaxArgs == 32, arg_count_mismatch); - return StringPrintf(format, - cstr[0], cstr[1], cstr[2], cstr[3], cstr[4], - cstr[5], cstr[6], cstr[7], cstr[8], cstr[9], - cstr[10], cstr[11], cstr[12], cstr[13], cstr[14], - cstr[15], cstr[16], cstr[17], cstr[18], cstr[19], - cstr[20], cstr[21], cstr[22], cstr[23], cstr[24], - cstr[25], cstr[26], cstr[27], cstr[28], cstr[29], - cstr[30], cstr[31]); -} -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/structurally_valid.cc b/third_party/protobuf-lite/structurally_valid.cc deleted file mode 100644 index 9a476c3b4..000000000 --- a/third_party/protobuf-lite/structurally_valid.cc +++ /dev/null @@ -1,615 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: jrm@google.com (Jim Meehan) - -#include - -#include - -namespace google { -namespace protobuf { -namespace internal { - -// These four-byte entries compactly encode how many bytes 0..255 to delete -// in making a string replacement, how many bytes to add 0..255, and the offset -// 0..64k-1 of the replacement string in remap_string. -struct RemapEntry { - uint8 delete_bytes; - uint8 add_bytes; - uint16 bytes_offset; -}; - -// Exit type codes for state tables. All but the first get stuffed into -// signed one-byte entries. The first is only generated by executable code. -// To distinguish from next-state entries, these must be contiguous and -// all <= kExitNone -typedef enum { - kExitDstSpaceFull = 239, - kExitIllegalStructure, // 240 - kExitOK, // 241 - kExitReject, // ... - kExitReplace1, - kExitReplace2, - kExitReplace3, - kExitReplace21, - kExitReplace31, - kExitReplace32, - kExitReplaceOffset1, - kExitReplaceOffset2, - kExitReplace1S0, - kExitSpecial, - kExitDoAgain, - kExitRejectAlt, - kExitNone // 255 -} ExitReason; - - -// This struct represents one entire state table. The three initialized byte -// areas are state_table, remap_base, and remap_string. state0 and state0_size -// give the byte offset and length within state_table of the initial state -- -// table lookups are expected to start and end in this state, but for -// truncated UTF-8 strings, may end in a different state. These allow a quick -// test for that condition. entry_shift is 8 for tables subscripted by a full -// byte value and 6 for space-optimized tables subscripted by only six -// significant bits in UTF-8 continuation bytes. -typedef struct { - const uint32 state0; - const uint32 state0_size; - const uint32 total_size; - const int max_expand; - const int entry_shift; - const int bytes_per_entry; - const uint32 losub; - const uint32 hiadd; - const uint8* state_table; - const RemapEntry* remap_base; - const uint8* remap_string; - const uint8* fast_state; -} UTF8StateMachineObj; - -typedef UTF8StateMachineObj UTF8ScanObj; - -#define X__ (kExitIllegalStructure) -#define RJ_ (kExitReject) -#define S1_ (kExitReplace1) -#define S2_ (kExitReplace2) -#define S3_ (kExitReplace3) -#define S21 (kExitReplace21) -#define S31 (kExitReplace31) -#define S32 (kExitReplace32) -#define T1_ (kExitReplaceOffset1) -#define T2_ (kExitReplaceOffset2) -#define S11 (kExitReplace1S0) -#define SP_ (kExitSpecial) -#define D__ (kExitDoAgain) -#define RJA (kExitRejectAlt) - -// Entire table has 9 state blocks of 256 entries each -static const unsigned int utf8acceptnonsurrogates_STATE0 = 0; // state[0] -static const unsigned int utf8acceptnonsurrogates_STATE0_SIZE = 256; // =[1] -static const unsigned int utf8acceptnonsurrogates_TOTAL_SIZE = 2304; -static const unsigned int utf8acceptnonsurrogates_MAX_EXPAND_X4 = 0; -static const unsigned int utf8acceptnonsurrogates_SHIFT = 8; -static const unsigned int utf8acceptnonsurrogates_BYTES = 1; -static const unsigned int utf8acceptnonsurrogates_LOSUB = 0x20202020; -static const unsigned int utf8acceptnonsurrogates_HIADD = 0x00000000; - -static const uint8 utf8acceptnonsurrogates[] = { -// state[0] 0x000000 Byte 1 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 3, - 4, 5, 5, 5, 6, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[1] 0x000080 Byte 2 of 2 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[2] 0x000000 Byte 2 of 3 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[3] 0x001000 Byte 2 of 3 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[4] 0x000000 Byte 2 of 4 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[5] 0x040000 Byte 2 of 4 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[6] 0x100000 Byte 2 of 4 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[7] 0x00d000 Byte 2 of 3 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -// state[8] 0x00d800 Byte 3 of 3 -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, - -RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, -RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, -RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, -RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, RJ_, - -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, X__, -}; - -// Remap base[0] = (del, add, string_offset) -static const RemapEntry utf8acceptnonsurrogates_remap_base[] = { -{0, 0, 0} }; - -// Remap string[0] -static const unsigned char utf8acceptnonsurrogates_remap_string[] = { -0 }; - -static const unsigned char utf8acceptnonsurrogates_fast[256] = { -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -}; - -static const UTF8ScanObj utf8acceptnonsurrogates_obj = { - utf8acceptnonsurrogates_STATE0, - utf8acceptnonsurrogates_STATE0_SIZE, - utf8acceptnonsurrogates_TOTAL_SIZE, - utf8acceptnonsurrogates_MAX_EXPAND_X4, - utf8acceptnonsurrogates_SHIFT, - utf8acceptnonsurrogates_BYTES, - utf8acceptnonsurrogates_LOSUB, - utf8acceptnonsurrogates_HIADD, - utf8acceptnonsurrogates, - utf8acceptnonsurrogates_remap_base, - utf8acceptnonsurrogates_remap_string, - utf8acceptnonsurrogates_fast -}; - - -#undef X__ -#undef RJ_ -#undef S1_ -#undef S2_ -#undef S3_ -#undef S21 -#undef S31 -#undef S32 -#undef T1_ -#undef T2_ -#undef S11 -#undef SP_ -#undef D__ -#undef RJA - -// Return true if current Tbl pointer is within state0 range -// Note that unsigned compare checks both ends of range simultaneously -static inline bool InStateZero(const UTF8ScanObj* st, const uint8* Tbl) { - const uint8* Tbl0 = &st->state_table[st->state0]; - return (static_cast(Tbl - Tbl0) < st->state0_size); -} - -// Scan a UTF-8 string based on state table. -// Always scan complete UTF-8 characters -// Set number of bytes scanned. Return reason for exiting -int UTF8GenericScan(const UTF8ScanObj* st, - const char * str, - int str_length, - int* bytes_consumed) { - *bytes_consumed = 0; - if (str_length == 0) return kExitOK; - - int eshift = st->entry_shift; - const uint8* isrc = reinterpret_cast(str); - const uint8* src = isrc; - const uint8* srclimit = isrc + str_length; - const uint8* srclimit8 = str_length < 7 ? isrc : srclimit - 7; - const uint8* Tbl_0 = &st->state_table[st->state0]; - - DoAgain: - // Do state-table scan - int e = 0; - uint8 c; - const uint8* Tbl2 = &st->fast_state[0]; - const uint32 losub = st->losub; - const uint32 hiadd = st->hiadd; - // Check initial few bytes one at a time until 8-byte aligned - //---------------------------- - while ((((uintptr_t)src & 0x07) != 0) && - (src < srclimit) && - Tbl2[src[0]] == 0) { - src++; - } - if (((uintptr_t)src & 0x07) == 0) { - // Do fast for groups of 8 identity bytes. - // This covers a lot of 7-bit ASCII ~8x faster then the 1-byte loop, - // including slowing slightly on cr/lf/ht - //---------------------------- - while (src < srclimit8) { - uint32 s0123 = (reinterpret_cast(src))[0]; - uint32 s4567 = (reinterpret_cast(src))[1]; - src += 8; - // This is a fast range check for all bytes in [lowsub..0x80-hiadd) - uint32 temp = (s0123 - losub) | (s0123 + hiadd) | - (s4567 - losub) | (s4567 + hiadd); - if ((temp & 0x80808080) != 0) { - // We typically end up here on cr/lf/ht; src was incremented - int e0123 = (Tbl2[src[-8]] | Tbl2[src[-7]]) | - (Tbl2[src[-6]] | Tbl2[src[-5]]); - if (e0123 != 0) { - src -= 8; - break; - } // Exit on Non-interchange - e0123 = (Tbl2[src[-4]] | Tbl2[src[-3]]) | - (Tbl2[src[-2]] | Tbl2[src[-1]]); - if (e0123 != 0) { - src -= 4; - break; - } // Exit on Non-interchange - // Else OK, go around again - } - } - } - //---------------------------- - - // Byte-at-a-time scan - //---------------------------- - const uint8* Tbl = Tbl_0; - while (src < srclimit) { - c = *src; - e = Tbl[c]; - src++; - if (e >= kExitIllegalStructure) {break;} - Tbl = &Tbl_0[e << eshift]; - } - //---------------------------- - - // Exit possibilities: - // Some exit code, !state0, back up over last char - // Some exit code, state0, back up one byte exactly - // source consumed, !state0, back up over partial char - // source consumed, state0, exit OK - // For illegal byte in state0, avoid backup up over PREVIOUS char - // For truncated last char, back up to beginning of it - - if (e >= kExitIllegalStructure) { - // Back up over exactly one byte of rejected/illegal UTF-8 character - src--; - // Back up more if needed - if (!InStateZero(st, Tbl)) { - do { - src--; - } while ((src > isrc) && ((src[0] & 0xc0) == 0x80)); - } - } else if (!InStateZero(st, Tbl)) { - // Back up over truncated UTF-8 character - e = kExitIllegalStructure; - do { - src--; - } while ((src > isrc) && ((src[0] & 0xc0) == 0x80)); - } else { - // Normal termination, source fully consumed - e = kExitOK; - } - - if (e == kExitDoAgain) { - // Loop back up to the fast scan - goto DoAgain; - } - - *bytes_consumed = src - isrc; - return e; -} - -int UTF8GenericScanFastAscii(const UTF8ScanObj* st, - const char * str, - int str_length, - int* bytes_consumed) { - *bytes_consumed = 0; - if (str_length == 0) return kExitOK; - - const uint8* isrc = reinterpret_cast(str); - const uint8* src = isrc; - const uint8* srclimit = isrc + str_length; - const uint8* srclimit8 = str_length < 7 ? isrc : srclimit - 7; - int n; - int rest_consumed; - int exit_reason; - do { - // Check initial few bytes one at a time until 8-byte aligned - while ((((uintptr_t)src & 0x07) != 0) && - (src < srclimit) && (src[0] < 0x80)) { - src++; - } - if (((uintptr_t)src & 0x07) == 0) { - while ((src < srclimit8) && - (((reinterpret_cast(src)[0] | - reinterpret_cast(src)[1]) & 0x80808080) == 0)) { - src += 8; - } - } - while ((src < srclimit) && (src[0] < 0x80)) { - src++; - } - // Run state table on the rest - n = src - isrc; - exit_reason = UTF8GenericScan(st, str + n, str_length - n, &rest_consumed); - src += rest_consumed; - } while ( exit_reason == kExitDoAgain ); - - *bytes_consumed = src - isrc; - return exit_reason; -} - -// Hack: On some compilers the static tables are initialized at startup. -// We can't use them until they are initialized. However, some Protocol -// Buffer parsing happens at static init time and may try to validate -// UTF-8 strings. Since UTF-8 validation is only used for debugging -// anyway, we simply always return success if initialization hasn't -// occurred yet. -namespace { - -bool module_initialized_ = false; - -struct InitDetector { - InitDetector() { - module_initialized_ = true; - } -}; -InitDetector init_detector; - -} // namespace - -bool IsStructurallyValidUTF8(const char* buf, int len) { - if (!module_initialized_) return true; - - int bytes_consumed = 0; - UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj, - buf, len, &bytes_consumed); - return (bytes_consumed == len); -} - -int UTF8SpnStructurallyValid(StringPiece str) { - if (!module_initialized_) return str.size(); - - int bytes_consumed = 0; - UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj, - str.data(), str.size(), &bytes_consumed); - return bytes_consumed; -} - -// Coerce UTF-8 byte string in src_str to be -// a structurally-valid equal-length string by selectively -// overwriting illegal bytes with replace_char (typically blank). -// replace_char must be legal printable 7-bit Ascii 0x20..0x7e. -// src_str is read-only. If any overwriting is needed, a modified byte string -// is created in idst, length isrclen. -// -// Returns pointer to output buffer, isrc if no changes were made, -// or idst if some bytes were changed. -// -// Fast case: all is structurally valid and no byte copying is done. -// -char* UTF8CoerceToStructurallyValid(StringPiece src_str, char* idst, - const char replace_char) { - const char* isrc = src_str.data(); - const int len = src_str.length(); - int n = UTF8SpnStructurallyValid(src_str); - if (n == len) { // Normal case -- all is cool, return - return const_cast(isrc); - } else { // Unusual case -- copy w/o bad bytes - const char* src = isrc; - const char* srclimit = isrc + len; - char* dst = idst; - memmove(dst, src, n); // Copy initial good chunk - src += n; - dst += n; - while (src < srclimit) { // src points to bogus byte or is off the end - dst[0] = replace_char; // replace one bad byte - src++; - dst++; - StringPiece str2(src, srclimit - src); - n = UTF8SpnStructurallyValid(str2); // scan the remainder - memmove(dst, src, n); // copy next good chunk - src += n; - dst += n; - } - } - return idst; -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/strutil.cc b/third_party/protobuf-lite/strutil.cc deleted file mode 100644 index 2ecdb2bf7..000000000 --- a/third_party/protobuf-lite/strutil.cc +++ /dev/null @@ -1,2477 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// from google3/strings/strutil.cc - -#include - -#include -#include // FLT_DIG and DBL_DIG -#include -#include -#include -#include -#include - -#include -#include - -#ifdef _WIN32 -// MSVC has only _snprintf, not snprintf. -// -// MinGW has both snprintf and _snprintf, but they appear to be different -// functions. The former is buggy. When invoked like so: -// char buffer[32]; -// snprintf(buffer, 32, "%.*g\n", FLT_DIG, 1.23e10f); -// it prints "1.23000e+10". This is plainly wrong: %g should never print -// trailing zeros after the decimal point. For some reason this bug only -// occurs with some input values, not all. In any case, _snprintf does the -// right thing, so we use it. -#define snprintf _snprintf -#endif - -namespace google { -namespace protobuf { - -// These are defined as macros on some platforms. #undef them so that we can -// redefine them. -#undef isxdigit -#undef isprint - -// The definitions of these in ctype.h change based on locale. Since our -// string manipulation is all in relation to the protocol buffer and C++ -// languages, we always want to use the C locale. So, we re-define these -// exactly as we want them. -inline bool isxdigit(char c) { - return ('0' <= c && c <= '9') || - ('a' <= c && c <= 'f') || - ('A' <= c && c <= 'F'); -} - -inline bool isprint(char c) { - return c >= 0x20 && c <= 0x7E; -} - -// ---------------------------------------------------------------------- -// ReplaceCharacters -// Replaces any occurrence of the character 'remove' (or the characters -// in 'remove') with the character 'replacewith'. -// ---------------------------------------------------------------------- -void ReplaceCharacters(std::string *s, const char *remove, char replacewith) { - const char *str_start = s->c_str(); - const char *str = str_start; - for (str = strpbrk(str, remove); - str != nullptr; - str = strpbrk(str + 1, remove)) { - (*s)[str - str_start] = replacewith; - } -} - -void StripWhitespace(std::string *str) { - int str_length = str->length(); - - // Strip off leading whitespace. - int first = 0; - while (first < str_length && ascii_isspace(str->at(first))) { - ++first; - } - // If entire string is white space. - if (first == str_length) { - str->clear(); - return; - } - if (first > 0) { - str->erase(0, first); - str_length -= first; - } - - // Strip off trailing whitespace. - int last = str_length - 1; - while (last >= 0 && ascii_isspace(str->at(last))) { - --last; - } - if (last != (str_length - 1) && last >= 0) { - str->erase(last + 1, std::string::npos); - } -} - -// ---------------------------------------------------------------------- -// StringReplace() -// Replace the "old" pattern with the "new" pattern in a string, -// and append the result to "res". If replace_all is false, -// it only replaces the first instance of "old." -// ---------------------------------------------------------------------- - -void StringReplace(const std::string &s, const std::string &oldsub, - const std::string &newsub, bool replace_all, - std::string *res) { - if (oldsub.empty()) { - res->append(s); // if empty, append the given string. - return; - } - - std::string::size_type start_pos = 0; - std::string::size_type pos; - do { - pos = s.find(oldsub, start_pos); - if (pos == std::string::npos) { - break; - } - res->append(s, start_pos, pos - start_pos); - res->append(newsub); - start_pos = pos + oldsub.size(); // start searching again after the "old" - } while (replace_all); - res->append(s, start_pos, s.length() - start_pos); -} - -// ---------------------------------------------------------------------- -// StringReplace() -// Give me a string and two patterns "old" and "new", and I replace -// the first instance of "old" in the string with "new", if it -// exists. If "global" is true; call this repeatedly until it -// fails. RETURN a new string, regardless of whether the replacement -// happened or not. -// ---------------------------------------------------------------------- - -std::string StringReplace(const std::string &s, const std::string &oldsub, - const std::string &newsub, bool replace_all) { - std::string ret; - StringReplace(s, oldsub, newsub, replace_all, &ret); - return ret; -} - -// ---------------------------------------------------------------------- -// SplitStringUsing() -// Split a string using a character delimiter. Append the components -// to 'result'. -// -// Note: For multi-character delimiters, this routine will split on *ANY* of -// the characters in the string, not the entire string as a single delimiter. -// ---------------------------------------------------------------------- -template -static inline void SplitStringToIteratorUsing(StringPiece full, - const char *delim, ITR &result) { - // Optimize the common case where delim is a single character. - if (delim[0] != '\0' && delim[1] == '\0') { - char c = delim[0]; - const char* p = full.data(); - const char* end = p + full.size(); - while (p != end) { - if (*p == c) { - ++p; - } else { - const char* start = p; - while (++p != end && *p != c); - *result++ = std::string(start, p - start); - } - } - return; - } - - std::string::size_type begin_index, end_index; - begin_index = full.find_first_not_of(delim); - while (begin_index != std::string::npos) { - end_index = full.find_first_of(delim, begin_index); - if (end_index == std::string::npos) { - *result++ = std::string(full.substr(begin_index)); - return; - } - *result++ = - std::string(full.substr(begin_index, (end_index - begin_index))); - begin_index = full.find_first_not_of(delim, end_index); - } -} - -void SplitStringUsing(StringPiece full, const char *delim, - std::vector *result) { - std::back_insert_iterator > it(*result); - SplitStringToIteratorUsing(full, delim, it); -} - -// Split a string using a character delimiter. Append the components -// to 'result'. If there are consecutive delimiters, this function -// will return corresponding empty strings. The string is split into -// at most the specified number of pieces greedily. This means that the -// last piece may possibly be split further. To split into as many pieces -// as possible, specify 0 as the number of pieces. -// -// If "full" is the empty string, yields an empty string as the only value. -// -// If "pieces" is negative for some reason, it returns the whole string -// ---------------------------------------------------------------------- -template -static inline void SplitStringToIteratorAllowEmpty(StringPiece full, - const char *delim, - int pieces, ITR &result) { - std::string::size_type begin_index, end_index; - begin_index = 0; - - for (int i = 0; (i < pieces-1) || (pieces == 0); i++) { - end_index = full.find_first_of(delim, begin_index); - if (end_index == std::string::npos) { - *result++ = std::string(full.substr(begin_index)); - return; - } - *result++ = - std::string(full.substr(begin_index, (end_index - begin_index))); - begin_index = end_index + 1; - } - *result++ = std::string(full.substr(begin_index)); -} - -void SplitStringAllowEmpty(StringPiece full, const char *delim, - std::vector *result) { - std::back_insert_iterator > it(*result); - SplitStringToIteratorAllowEmpty(full, delim, 0, it); -} - -// ---------------------------------------------------------------------- -// JoinStrings() -// This merges a vector of string components with delim inserted -// as separaters between components. -// -// ---------------------------------------------------------------------- -template -static void JoinStringsIterator(const ITERATOR &start, const ITERATOR &end, - const char *delim, std::string *result) { - GOOGLE_CHECK(result != nullptr); - result->clear(); - int delim_length = strlen(delim); - - // Precompute resulting length so we can reserve() memory in one shot. - int length = 0; - for (ITERATOR iter = start; iter != end; ++iter) { - if (iter != start) { - length += delim_length; - } - length += iter->size(); - } - result->reserve(length); - - // Now combine everything. - for (ITERATOR iter = start; iter != end; ++iter) { - if (iter != start) { - result->append(delim, delim_length); - } - result->append(iter->data(), iter->size()); - } -} - -void JoinStrings(const std::vector &components, const char *delim, - std::string *result) { - JoinStringsIterator(components.begin(), components.end(), delim, result); -} - -// ---------------------------------------------------------------------- -// UnescapeCEscapeSequences() -// This does all the unescaping that C does: \ooo, \r, \n, etc -// Returns length of resulting string. -// The implementation of \x parses any positive number of hex digits, -// but it is an error if the value requires more than 8 bits, and the -// result is truncated to 8 bits. -// -// The second call stores its errors in a supplied string vector. -// If the string vector pointer is nullptr, it reports the errors with LOG(). -// ---------------------------------------------------------------------- - -#define IS_OCTAL_DIGIT(c) (((c) >= '0') && ((c) <= '7')) - -// Protocol buffers doesn't ever care about errors, but I don't want to remove -// the code. -#define LOG_STRING(LEVEL, VECTOR) GOOGLE_LOG_IF(LEVEL, false) - -int UnescapeCEscapeSequences(const char* source, char* dest) { - return UnescapeCEscapeSequences(source, dest, nullptr); -} - -int UnescapeCEscapeSequences(const char *source, char *dest, - std::vector *errors) { - GOOGLE_DCHECK(errors == nullptr) << "Error reporting not implemented."; - - char* d = dest; - const char* p = source; - - // Small optimization for case where source = dest and there's no escaping - while ( p == d && *p != '\0' && *p != '\\' ) - p++, d++; - - while (*p != '\0') { - if (*p != '\\') { - *d++ = *p++; - } else { - switch ( *++p ) { // skip past the '\\' - case '\0': - LOG_STRING(ERROR, errors) << "String cannot end with \\"; - *d = '\0'; - return d - dest; // we're done with p - case 'a': *d++ = '\a'; break; - case 'b': *d++ = '\b'; break; - case 'f': *d++ = '\f'; break; - case 'n': *d++ = '\n'; break; - case 'r': *d++ = '\r'; break; - case 't': *d++ = '\t'; break; - case 'v': *d++ = '\v'; break; - case '\\': *d++ = '\\'; break; - case '?': *d++ = '\?'; break; // \? Who knew? - case '\'': *d++ = '\''; break; - case '"': *d++ = '\"'; break; - case '0': case '1': case '2': case '3': // octal digit: 1 to 3 digits - case '4': case '5': case '6': case '7': { - char ch = *p - '0'; - if ( IS_OCTAL_DIGIT(p[1]) ) - ch = ch * 8 + *++p - '0'; - if ( IS_OCTAL_DIGIT(p[1]) ) // safe (and easy) to do this twice - ch = ch * 8 + *++p - '0'; // now points at last digit - *d++ = ch; - break; - } - case 'x': case 'X': { - if (!isxdigit(p[1])) { - if (p[1] == '\0') { - LOG_STRING(ERROR, errors) << "String cannot end with \\x"; - } else { - LOG_STRING(ERROR, errors) << - "\\x cannot be followed by non-hex digit: \\" << *p << p[1]; - } - break; - } - unsigned int ch = 0; - const char *hex_start = p; - while (isxdigit(p[1])) // arbitrarily many hex digits - ch = (ch << 4) + hex_digit_to_int(*++p); - if (ch > 0xFF) - LOG_STRING(ERROR, errors) - << "Value of " - << "\\" << std::string(hex_start, p + 1 - hex_start) - << " exceeds 8 bits"; - *d++ = ch; - break; - } -#if 0 // TODO(kenton): Support \u and \U? Requires runetochar(). - case 'u': { - // \uhhhh => convert 4 hex digits to UTF-8 - char32 rune = 0; - const char *hex_start = p; - for (int i = 0; i < 4; ++i) { - if (isxdigit(p[1])) { // Look one char ahead. - rune = (rune << 4) + hex_digit_to_int(*++p); // Advance p. - } else { - LOG_STRING(ERROR, errors) - << "\\u must be followed by 4 hex digits: \\" - << std::string(hex_start, p+1-hex_start); - break; - } - } - d += runetochar(d, &rune); - break; - } - case 'U': { - // \Uhhhhhhhh => convert 8 hex digits to UTF-8 - char32 rune = 0; - const char *hex_start = p; - for (int i = 0; i < 8; ++i) { - if (isxdigit(p[1])) { // Look one char ahead. - // Don't change rune until we're sure this - // is within the Unicode limit, but do advance p. - char32 newrune = (rune << 4) + hex_digit_to_int(*++p); - if (newrune > 0x10FFFF) { - LOG_STRING(ERROR, errors) - << "Value of \\" - << std::string(hex_start, p + 1 - hex_start) - << " exceeds Unicode limit (0x10FFFF)"; - break; - } else { - rune = newrune; - } - } else { - LOG_STRING(ERROR, errors) - << "\\U must be followed by 8 hex digits: \\" - << std::string(hex_start, p+1-hex_start); - break; - } - } - d += runetochar(d, &rune); - break; - } -#endif - default: - LOG_STRING(ERROR, errors) << "Unknown escape sequence: \\" << *p; - } - p++; // read past letter we escaped - } - } - *d = '\0'; - return d - dest; -} - -// ---------------------------------------------------------------------- -// UnescapeCEscapeString() -// This does the same thing as UnescapeCEscapeSequences, but creates -// a new string. The caller does not need to worry about allocating -// a dest buffer. This should be used for non performance critical -// tasks such as printing debug messages. It is safe for src and dest -// to be the same. -// -// The second call stores its errors in a supplied string vector. -// If the string vector pointer is nullptr, it reports the errors with LOG(). -// -// In the first and second calls, the length of dest is returned. In the -// the third call, the new string is returned. -// ---------------------------------------------------------------------- -int UnescapeCEscapeString(const std::string &src, std::string *dest) { - return UnescapeCEscapeString(src, dest, nullptr); -} - -int UnescapeCEscapeString(const std::string &src, std::string *dest, - std::vector *errors) { - std::unique_ptr unescaped(new char[src.size() + 1]); - int len = UnescapeCEscapeSequences(src.c_str(), unescaped.get(), errors); - GOOGLE_CHECK(dest); - dest->assign(unescaped.get(), len); - return len; -} - -std::string UnescapeCEscapeString(const std::string &src) { - std::unique_ptr unescaped(new char[src.size() + 1]); - int len = UnescapeCEscapeSequences(src.c_str(), unescaped.get(), nullptr); - return std::string(unescaped.get(), len); -} - -// ---------------------------------------------------------------------- -// CEscapeString() -// CHexEscapeString() -// Copies 'src' to 'dest', escaping dangerous characters using -// C-style escape sequences. This is very useful for preparing query -// flags. 'src' and 'dest' should not overlap. The 'Hex' version uses -// hexadecimal rather than octal sequences. -// Returns the number of bytes written to 'dest' (not including the \0) -// or -1 if there was insufficient space. -// -// Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped. -// ---------------------------------------------------------------------- -int CEscapeInternal(const char* src, int src_len, char* dest, - int dest_len, bool use_hex, bool utf8_safe) { - const char* src_end = src + src_len; - int used = 0; - bool last_hex_escape = false; // true if last output char was \xNN - - for (; src < src_end; src++) { - if (dest_len - used < 2) // Need space for two letter escape - return -1; - - bool is_hex_escape = false; - switch (*src) { - case '\n': dest[used++] = '\\'; dest[used++] = 'n'; break; - case '\r': dest[used++] = '\\'; dest[used++] = 'r'; break; - case '\t': dest[used++] = '\\'; dest[used++] = 't'; break; - case '\"': dest[used++] = '\\'; dest[used++] = '\"'; break; - case '\'': dest[used++] = '\\'; dest[used++] = '\''; break; - case '\\': dest[used++] = '\\'; dest[used++] = '\\'; break; - default: - // Note that if we emit \xNN and the src character after that is a hex - // digit then that digit must be escaped too to prevent it being - // interpreted as part of the character code by C. - if ((!utf8_safe || static_cast(*src) < 0x80) && - (!isprint(*src) || - (last_hex_escape && isxdigit(*src)))) { - if (dest_len - used < 4) // need space for 4 letter escape - return -1; - sprintf(dest + used, (use_hex ? "\\x%02x" : "\\%03o"), - static_cast(*src)); - is_hex_escape = use_hex; - used += 4; - } else { - dest[used++] = *src; break; - } - } - last_hex_escape = is_hex_escape; - } - - if (dest_len - used < 1) // make sure that there is room for \0 - return -1; - - dest[used] = '\0'; // doesn't count towards return value though - return used; -} - -// Calculates the length of the C-style escaped version of 'src'. -// Assumes that non-printable characters are escaped using octal sequences, and -// that UTF-8 bytes are not handled specially. -static inline size_t CEscapedLength(StringPiece src) { - static char c_escaped_len[256] = { - 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, // \t, \n, \r - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // ", ' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // '0'..'9' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'A'..'O' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, // 'P'..'Z', '\' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'a'..'o' - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, // 'p'..'z', DEL - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - }; - - size_t escaped_len = 0; - for (int i = 0; i < src.size(); ++i) { - unsigned char c = static_cast(src[i]); - escaped_len += c_escaped_len[c]; - } - return escaped_len; -} - -// ---------------------------------------------------------------------- -// Escapes 'src' using C-style escape sequences, and appends the escaped string -// to 'dest'. This version is faster than calling CEscapeInternal as it computes -// the required space using a lookup table, and also does not do any special -// handling for Hex or UTF-8 characters. -// ---------------------------------------------------------------------- -void CEscapeAndAppend(StringPiece src, std::string *dest) { - size_t escaped_len = CEscapedLength(src); - if (escaped_len == src.size()) { - dest->append(src.data(), src.size()); - return; - } - - size_t cur_dest_len = dest->size(); - dest->resize(cur_dest_len + escaped_len); - char* append_ptr = &(*dest)[cur_dest_len]; - - for (int i = 0; i < src.size(); ++i) { - unsigned char c = static_cast(src[i]); - switch (c) { - case '\n': *append_ptr++ = '\\'; *append_ptr++ = 'n'; break; - case '\r': *append_ptr++ = '\\'; *append_ptr++ = 'r'; break; - case '\t': *append_ptr++ = '\\'; *append_ptr++ = 't'; break; - case '\"': *append_ptr++ = '\\'; *append_ptr++ = '\"'; break; - case '\'': *append_ptr++ = '\\'; *append_ptr++ = '\''; break; - case '\\': *append_ptr++ = '\\'; *append_ptr++ = '\\'; break; - default: - if (!isprint(c)) { - *append_ptr++ = '\\'; - *append_ptr++ = '0' + c / 64; - *append_ptr++ = '0' + (c % 64) / 8; - *append_ptr++ = '0' + c % 8; - } else { - *append_ptr++ = c; - } - break; - } - } -} - -std::string CEscape(const std::string &src) { - std::string dest; - CEscapeAndAppend(src, &dest); - return dest; -} - -namespace strings { - -std::string Utf8SafeCEscape(const std::string &src) { - const int dest_length = src.size() * 4 + 1; // Maximum possible expansion - std::unique_ptr dest(new char[dest_length]); - const int len = CEscapeInternal(src.data(), src.size(), - dest.get(), dest_length, false, true); - GOOGLE_DCHECK_GE(len, 0); - return std::string(dest.get(), len); -} - -std::string CHexEscape(const std::string &src) { - const int dest_length = src.size() * 4 + 1; // Maximum possible expansion - std::unique_ptr dest(new char[dest_length]); - const int len = CEscapeInternal(src.data(), src.size(), - dest.get(), dest_length, true, false); - GOOGLE_DCHECK_GE(len, 0); - return std::string(dest.get(), len); -} - -} // namespace strings - -// ---------------------------------------------------------------------- -// strto32_adaptor() -// strtou32_adaptor() -// Implementation of strto[u]l replacements that have identical -// overflow and underflow characteristics for both ILP-32 and LP-64 -// platforms, including errno preservation in error-free calls. -// ---------------------------------------------------------------------- - -int32 strto32_adaptor(const char *nptr, char **endptr, int base) { - const int saved_errno = errno; - errno = 0; - const long result = strtol(nptr, endptr, base); - if (errno == ERANGE && result == LONG_MIN) { - return kint32min; - } else if (errno == ERANGE && result == LONG_MAX) { - return kint32max; - } else if (errno == 0 && result < kint32min) { - errno = ERANGE; - return kint32min; - } else if (errno == 0 && result > kint32max) { - errno = ERANGE; - return kint32max; - } - if (errno == 0) - errno = saved_errno; - return static_cast(result); -} - -uint32 strtou32_adaptor(const char *nptr, char **endptr, int base) { - const int saved_errno = errno; - errno = 0; - const unsigned long result = strtoul(nptr, endptr, base); - if (errno == ERANGE && result == ULONG_MAX) { - return kuint32max; - } else if (errno == 0 && result > kuint32max) { - errno = ERANGE; - return kuint32max; - } - if (errno == 0) - errno = saved_errno; - return static_cast(result); -} - -inline bool safe_parse_sign(std::string *text /*inout*/, - bool *negative_ptr /*output*/) { - const char* start = text->data(); - const char* end = start + text->size(); - - // Consume whitespace. - while (start < end && (start[0] == ' ')) { - ++start; - } - while (start < end && (end[-1] == ' ')) { - --end; - } - if (start >= end) { - return false; - } - - // Consume sign. - *negative_ptr = (start[0] == '-'); - if (*negative_ptr || start[0] == '+') { - ++start; - if (start >= end) { - return false; - } - } - *text = text->substr(start - text->data(), end - start); - return true; -} - -template -bool safe_parse_positive_int(std::string text, IntType *value_p) { - int base = 10; - IntType value = 0; - const IntType vmax = std::numeric_limits::max(); - assert(vmax > 0); - assert(vmax >= base); - const IntType vmax_over_base = vmax / base; - const char* start = text.data(); - const char* end = start + text.size(); - // loop over digits - for (; start < end; ++start) { - unsigned char c = static_cast(start[0]); - int digit = c - '0'; - if (digit >= base || digit < 0) { - *value_p = value; - return false; - } - if (value > vmax_over_base) { - *value_p = vmax; - return false; - } - value *= base; - if (value > vmax - digit) { - *value_p = vmax; - return false; - } - value += digit; - } - *value_p = value; - return true; -} - -template -bool safe_parse_negative_int(const std::string &text, IntType *value_p) { - int base = 10; - IntType value = 0; - const IntType vmin = std::numeric_limits::min(); - assert(vmin < 0); - assert(vmin <= 0 - base); - IntType vmin_over_base = vmin / base; - // 2003 c++ standard [expr.mul] - // "... the sign of the remainder is implementation-defined." - // Although (vmin/base)*base + vmin%base is always vmin. - // 2011 c++ standard tightens the spec but we cannot rely on it. - if (vmin % base > 0) { - vmin_over_base += 1; - } - const char* start = text.data(); - const char* end = start + text.size(); - // loop over digits - for (; start < end; ++start) { - unsigned char c = static_cast(start[0]); - int digit = c - '0'; - if (digit >= base || digit < 0) { - *value_p = value; - return false; - } - if (value < vmin_over_base) { - *value_p = vmin; - return false; - } - value *= base; - if (value < vmin + digit) { - *value_p = vmin; - return false; - } - value -= digit; - } - *value_p = value; - return true; -} - -template -bool safe_int_internal(std::string text, IntType *value_p) { - *value_p = 0; - bool negative; - if (!safe_parse_sign(&text, &negative)) { - return false; - } - if (!negative) { - return safe_parse_positive_int(text, value_p); - } else { - return safe_parse_negative_int(text, value_p); - } -} - -template -bool safe_uint_internal(std::string text, IntType *value_p) { - *value_p = 0; - bool negative; - if (!safe_parse_sign(&text, &negative) || negative) { - return false; - } - return safe_parse_positive_int(text, value_p); -} - -// ---------------------------------------------------------------------- -// FastIntToBuffer() -// FastInt64ToBuffer() -// FastHexToBuffer() -// FastHex64ToBuffer() -// FastHex32ToBuffer() -// ---------------------------------------------------------------------- - -// Offset into buffer where FastInt64ToBuffer places the end of string -// null character. Also used by FastInt64ToBufferLeft. -static const int kFastInt64ToBufferOffset = 21; - -char *FastInt64ToBuffer(int64 i, char* buffer) { - // We could collapse the positive and negative sections, but that - // would be slightly slower for positive numbers... - // 22 bytes is enough to store -2**64, -18446744073709551616. - char* p = buffer + kFastInt64ToBufferOffset; - *p-- = '\0'; - if (i >= 0) { - do { - *p-- = '0' + i % 10; - i /= 10; - } while (i > 0); - return p + 1; - } else { - // On different platforms, % and / have different behaviors for - // negative numbers, so we need to jump through hoops to make sure - // we don't divide negative numbers. - if (i > -10) { - i = -i; - *p-- = '0' + i; - *p = '-'; - return p; - } else { - // Make sure we aren't at MIN_INT, in which case we can't say i = -i - i = i + 10; - i = -i; - *p-- = '0' + i % 10; - // Undo what we did a moment ago - i = i / 10 + 1; - do { - *p-- = '0' + i % 10; - i /= 10; - } while (i > 0); - *p = '-'; - return p; - } - } -} - -// Offset into buffer where FastInt32ToBuffer places the end of string -// null character. Also used by FastInt32ToBufferLeft -static const int kFastInt32ToBufferOffset = 11; - -// Yes, this is a duplicate of FastInt64ToBuffer. But, we need this for the -// compiler to generate 32 bit arithmetic instructions. It's much faster, at -// least with 32 bit binaries. -char *FastInt32ToBuffer(int32 i, char* buffer) { - // We could collapse the positive and negative sections, but that - // would be slightly slower for positive numbers... - // 12 bytes is enough to store -2**32, -4294967296. - char* p = buffer + kFastInt32ToBufferOffset; - *p-- = '\0'; - if (i >= 0) { - do { - *p-- = '0' + i % 10; - i /= 10; - } while (i > 0); - return p + 1; - } else { - // On different platforms, % and / have different behaviors for - // negative numbers, so we need to jump through hoops to make sure - // we don't divide negative numbers. - if (i > -10) { - i = -i; - *p-- = '0' + i; - *p = '-'; - return p; - } else { - // Make sure we aren't at MIN_INT, in which case we can't say i = -i - i = i + 10; - i = -i; - *p-- = '0' + i % 10; - // Undo what we did a moment ago - i = i / 10 + 1; - do { - *p-- = '0' + i % 10; - i /= 10; - } while (i > 0); - *p = '-'; - return p; - } - } -} - -char *FastHexToBuffer(int i, char* buffer) { - GOOGLE_CHECK(i >= 0) << "FastHexToBuffer() wants non-negative integers, not " << i; - - static const char *hexdigits = "0123456789abcdef"; - char *p = buffer + 21; - *p-- = '\0'; - do { - *p-- = hexdigits[i & 15]; // mod by 16 - i >>= 4; // divide by 16 - } while (i > 0); - return p + 1; -} - -char *InternalFastHexToBuffer(uint64 value, char* buffer, int num_byte) { - static const char *hexdigits = "0123456789abcdef"; - buffer[num_byte] = '\0'; - for (int i = num_byte - 1; i >= 0; i--) { -#ifdef _M_X64 - // MSVC x64 platform has a bug optimizing the uint32(value) in the #else - // block. Given that the uint32 cast was to improve performance on 32-bit - // platforms, we use 64-bit '&' directly. - buffer[i] = hexdigits[value & 0xf]; -#else - buffer[i] = hexdigits[uint32(value) & 0xf]; -#endif - value >>= 4; - } - return buffer; -} - -char *FastHex64ToBuffer(uint64 value, char* buffer) { - return InternalFastHexToBuffer(value, buffer, 16); -} - -char *FastHex32ToBuffer(uint32 value, char* buffer) { - return InternalFastHexToBuffer(value, buffer, 8); -} - -// ---------------------------------------------------------------------- -// FastInt32ToBufferLeft() -// FastUInt32ToBufferLeft() -// FastInt64ToBufferLeft() -// FastUInt64ToBufferLeft() -// -// Like the Fast*ToBuffer() functions above, these are intended for speed. -// Unlike the Fast*ToBuffer() functions, however, these functions write -// their output to the beginning of the buffer (hence the name, as the -// output is left-aligned). The caller is responsible for ensuring that -// the buffer has enough space to hold the output. -// -// Returns a pointer to the end of the string (i.e. the null character -// terminating the string). -// ---------------------------------------------------------------------- - -static const char two_ASCII_digits[100][2] = { - {'0','0'}, {'0','1'}, {'0','2'}, {'0','3'}, {'0','4'}, - {'0','5'}, {'0','6'}, {'0','7'}, {'0','8'}, {'0','9'}, - {'1','0'}, {'1','1'}, {'1','2'}, {'1','3'}, {'1','4'}, - {'1','5'}, {'1','6'}, {'1','7'}, {'1','8'}, {'1','9'}, - {'2','0'}, {'2','1'}, {'2','2'}, {'2','3'}, {'2','4'}, - {'2','5'}, {'2','6'}, {'2','7'}, {'2','8'}, {'2','9'}, - {'3','0'}, {'3','1'}, {'3','2'}, {'3','3'}, {'3','4'}, - {'3','5'}, {'3','6'}, {'3','7'}, {'3','8'}, {'3','9'}, - {'4','0'}, {'4','1'}, {'4','2'}, {'4','3'}, {'4','4'}, - {'4','5'}, {'4','6'}, {'4','7'}, {'4','8'}, {'4','9'}, - {'5','0'}, {'5','1'}, {'5','2'}, {'5','3'}, {'5','4'}, - {'5','5'}, {'5','6'}, {'5','7'}, {'5','8'}, {'5','9'}, - {'6','0'}, {'6','1'}, {'6','2'}, {'6','3'}, {'6','4'}, - {'6','5'}, {'6','6'}, {'6','7'}, {'6','8'}, {'6','9'}, - {'7','0'}, {'7','1'}, {'7','2'}, {'7','3'}, {'7','4'}, - {'7','5'}, {'7','6'}, {'7','7'}, {'7','8'}, {'7','9'}, - {'8','0'}, {'8','1'}, {'8','2'}, {'8','3'}, {'8','4'}, - {'8','5'}, {'8','6'}, {'8','7'}, {'8','8'}, {'8','9'}, - {'9','0'}, {'9','1'}, {'9','2'}, {'9','3'}, {'9','4'}, - {'9','5'}, {'9','6'}, {'9','7'}, {'9','8'}, {'9','9'} -}; - -char* FastUInt32ToBufferLeft(uint32 u, char* buffer) { - uint32 digits; - const char *ASCII_digits = nullptr; - // The idea of this implementation is to trim the number of divides to as few - // as possible by using multiplication and subtraction rather than mod (%), - // and by outputting two digits at a time rather than one. - // The huge-number case is first, in the hopes that the compiler will output - // that case in one branch-free block of code, and only output conditional - // branches into it from below. - if (u >= 1000000000) { // >= 1,000,000,000 - digits = u / 100000000; // 100,000,000 - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; -sublt100_000_000: - u -= digits * 100000000; // 100,000,000 -lt100_000_000: - digits = u / 1000000; // 1,000,000 - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; -sublt1_000_000: - u -= digits * 1000000; // 1,000,000 -lt1_000_000: - digits = u / 10000; // 10,000 - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; -sublt10_000: - u -= digits * 10000; // 10,000 -lt10_000: - digits = u / 100; - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; -sublt100: - u -= digits * 100; -lt100: - digits = u; - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; -done: - *buffer = 0; - return buffer; - } - - if (u < 100) { - digits = u; - if (u >= 10) goto lt100; - *buffer++ = '0' + digits; - goto done; - } - if (u < 10000) { // 10,000 - if (u >= 1000) goto lt10_000; - digits = u / 100; - *buffer++ = '0' + digits; - goto sublt100; - } - if (u < 1000000) { // 1,000,000 - if (u >= 100000) goto lt1_000_000; - digits = u / 10000; // 10,000 - *buffer++ = '0' + digits; - goto sublt10_000; - } - if (u < 100000000) { // 100,000,000 - if (u >= 10000000) goto lt100_000_000; - digits = u / 1000000; // 1,000,000 - *buffer++ = '0' + digits; - goto sublt1_000_000; - } - // we already know that u < 1,000,000,000 - digits = u / 100000000; // 100,000,000 - *buffer++ = '0' + digits; - goto sublt100_000_000; -} - -char* FastInt32ToBufferLeft(int32 i, char* buffer) { - uint32 u = 0; - if (i < 0) { - *buffer++ = '-'; - u -= i; - } else { - u = i; - } - return FastUInt32ToBufferLeft(u, buffer); -} - -char* FastUInt64ToBufferLeft(uint64 u64, char* buffer) { - int digits; - const char *ASCII_digits = nullptr; - - uint32 u = static_cast(u64); - if (u == u64) return FastUInt32ToBufferLeft(u, buffer); - - uint64 top_11_digits = u64 / 1000000000; - buffer = FastUInt64ToBufferLeft(top_11_digits, buffer); - u = u64 - (top_11_digits * 1000000000); - - digits = u / 10000000; // 10,000,000 - GOOGLE_DCHECK_LT(digits, 100); - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; - u -= digits * 10000000; // 10,000,000 - digits = u / 100000; // 100,000 - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; - u -= digits * 100000; // 100,000 - digits = u / 1000; // 1,000 - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; - u -= digits * 1000; // 1,000 - digits = u / 10; - ASCII_digits = two_ASCII_digits[digits]; - buffer[0] = ASCII_digits[0]; - buffer[1] = ASCII_digits[1]; - buffer += 2; - u -= digits * 10; - digits = u; - *buffer++ = '0' + digits; - *buffer = 0; - return buffer; -} - -char* FastInt64ToBufferLeft(int64 i, char* buffer) { - uint64 u = 0; - if (i < 0) { - *buffer++ = '-'; - u -= i; - } else { - u = i; - } - return FastUInt64ToBufferLeft(u, buffer); -} - -// ---------------------------------------------------------------------- -// SimpleItoa() -// Description: converts an integer to a string. -// -// Return value: string -// ---------------------------------------------------------------------- - -std::string SimpleItoa(int i) { - char buffer[kFastToBufferSize]; - return (sizeof(i) == 4) ? - FastInt32ToBuffer(i, buffer) : - FastInt64ToBuffer(i, buffer); -} - -std::string SimpleItoa(unsigned int i) { - char buffer[kFastToBufferSize]; - return std::string(buffer, (sizeof(i) == 4) - ? FastUInt32ToBufferLeft(i, buffer) - : FastUInt64ToBufferLeft(i, buffer)); -} - -std::string SimpleItoa(long i) { - char buffer[kFastToBufferSize]; - return (sizeof(i) == 4) ? - FastInt32ToBuffer(i, buffer) : - FastInt64ToBuffer(i, buffer); -} - -std::string SimpleItoa(unsigned long i) { - char buffer[kFastToBufferSize]; - return std::string(buffer, (sizeof(i) == 4) - ? FastUInt32ToBufferLeft(i, buffer) - : FastUInt64ToBufferLeft(i, buffer)); -} - -std::string SimpleItoa(long long i) { - char buffer[kFastToBufferSize]; - return (sizeof(i) == 4) ? - FastInt32ToBuffer(i, buffer) : - FastInt64ToBuffer(i, buffer); -} - -std::string SimpleItoa(unsigned long long i) { - char buffer[kFastToBufferSize]; - return std::string(buffer, (sizeof(i) == 4) - ? FastUInt32ToBufferLeft(i, buffer) - : FastUInt64ToBufferLeft(i, buffer)); -} - -// ---------------------------------------------------------------------- -// SimpleDtoa() -// SimpleFtoa() -// DoubleToBuffer() -// FloatToBuffer() -// We want to print the value without losing precision, but we also do -// not want to print more digits than necessary. This turns out to be -// trickier than it sounds. Numbers like 0.2 cannot be represented -// exactly in binary. If we print 0.2 with a very large precision, -// e.g. "%.50g", we get "0.2000000000000000111022302462515654042363167". -// On the other hand, if we set the precision too low, we lose -// significant digits when printing numbers that actually need them. -// It turns out there is no precision value that does the right thing -// for all numbers. -// -// Our strategy is to first try printing with a precision that is never -// over-precise, then parse the result with strtod() to see if it -// matches. If not, we print again with a precision that will always -// give a precise result, but may use more digits than necessary. -// -// An arguably better strategy would be to use the algorithm described -// in "How to Print Floating-Point Numbers Accurately" by Steele & -// White, e.g. as implemented by David M. Gay's dtoa(). It turns out, -// however, that the following implementation is about as fast as -// DMG's code. Furthermore, DMG's code locks mutexes, which means it -// will not scale well on multi-core machines. DMG's code is slightly -// more accurate (in that it will never use more digits than -// necessary), but this is probably irrelevant for most users. -// -// Rob Pike and Ken Thompson also have an implementation of dtoa() in -// third_party/fmt/fltfmt.cc. Their implementation is similar to this -// one in that it makes guesses and then uses strtod() to check them. -// Their implementation is faster because they use their own code to -// generate the digits in the first place rather than use snprintf(), -// thus avoiding format string parsing overhead. However, this makes -// it considerably more complicated than the following implementation, -// and it is embedded in a larger library. If speed turns out to be -// an issue, we could re-implement this in terms of their -// implementation. -// ---------------------------------------------------------------------- - -std::string SimpleDtoa(double value) { - char buffer[kDoubleToBufferSize]; - return DoubleToBuffer(value, buffer); -} - -std::string SimpleFtoa(float value) { - char buffer[kFloatToBufferSize]; - return FloatToBuffer(value, buffer); -} - -static inline bool IsValidFloatChar(char c) { - return ('0' <= c && c <= '9') || - c == 'e' || c == 'E' || - c == '+' || c == '-'; -} - -void DelocalizeRadix(char* buffer) { - // Fast check: if the buffer has a normal decimal point, assume no - // translation is needed. - if (strchr(buffer, '.') != nullptr) return; - - // Find the first unknown character. - while (IsValidFloatChar(*buffer)) ++buffer; - - if (*buffer == '\0') { - // No radix character found. - return; - } - - // We are now pointing at the locale-specific radix character. Replace it - // with '.'. - *buffer = '.'; - ++buffer; - - if (!IsValidFloatChar(*buffer) && *buffer != '\0') { - // It appears the radix was a multi-byte character. We need to remove the - // extra bytes. - char* target = buffer; - do { ++buffer; } while (!IsValidFloatChar(*buffer) && *buffer != '\0'); - memmove(target, buffer, strlen(buffer) + 1); - } -} - -char* DoubleToBuffer(double value, char* buffer) { - // DBL_DIG is 15 for IEEE-754 doubles, which are used on almost all - // platforms these days. Just in case some system exists where DBL_DIG - // is significantly larger -- and risks overflowing our buffer -- we have - // this assert. - GOOGLE_COMPILE_ASSERT(DBL_DIG < 20, DBL_DIG_is_too_big); - - if (value == std::numeric_limits::infinity()) { - strcpy(buffer, "inf"); - return buffer; - } else if (value == -std::numeric_limits::infinity()) { - strcpy(buffer, "-inf"); - return buffer; - } else if (std::isnan(value)) { - strcpy(buffer, "nan"); - return buffer; - } - - int snprintf_result = - snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG, value); - - // The snprintf should never overflow because the buffer is significantly - // larger than the precision we asked for. - GOOGLE_DCHECK(snprintf_result > 0 && snprintf_result < kDoubleToBufferSize); - - // We need to make parsed_value volatile in order to force the compiler to - // write it out to the stack. Otherwise, it may keep the value in a - // register, and if it does that, it may keep it as a long double instead - // of a double. This long double may have extra bits that make it compare - // unequal to "value" even though it would be exactly equal if it were - // truncated to a double. - volatile double parsed_value = internal::NoLocaleStrtod(buffer, nullptr); - if (parsed_value != value) { - int snprintf_result = - snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG+2, value); - - // Should never overflow; see above. - GOOGLE_DCHECK(snprintf_result > 0 && snprintf_result < kDoubleToBufferSize); - } - - DelocalizeRadix(buffer); - return buffer; -} - -static int memcasecmp(const char *s1, const char *s2, size_t len) { - const unsigned char *us1 = reinterpret_cast(s1); - const unsigned char *us2 = reinterpret_cast(s2); - - for ( int i = 0; i < len; i++ ) { - const int diff = - static_cast(static_cast(ascii_tolower(us1[i]))) - - static_cast(static_cast(ascii_tolower(us2[i]))); - if (diff != 0) return diff; - } - return 0; -} - -inline bool CaseEqual(StringPiece s1, StringPiece s2) { - if (s1.size() != s2.size()) return false; - return memcasecmp(s1.data(), s2.data(), s1.size()) == 0; -} - -bool safe_strtob(StringPiece str, bool* value) { - GOOGLE_CHECK(value != nullptr) << "nullptr output boolean given."; - if (CaseEqual(str, "true") || CaseEqual(str, "t") || - CaseEqual(str, "yes") || CaseEqual(str, "y") || - CaseEqual(str, "1")) { - *value = true; - return true; - } - if (CaseEqual(str, "false") || CaseEqual(str, "f") || - CaseEqual(str, "no") || CaseEqual(str, "n") || - CaseEqual(str, "0")) { - *value = false; - return true; - } - return false; -} - -bool safe_strtof(const char* str, float* value) { - char* endptr; - errno = 0; // errno only gets set on errors -#if defined(_WIN32) || defined (__hpux) // has no strtof() - *value = internal::NoLocaleStrtod(str, &endptr); -#else - *value = strtof(str, &endptr); -#endif - return *str != 0 && *endptr == 0 && errno == 0; -} - -bool safe_strtod(const char* str, double* value) { - char* endptr; - *value = internal::NoLocaleStrtod(str, &endptr); - if (endptr != str) { - while (ascii_isspace(*endptr)) ++endptr; - } - // Ignore range errors from strtod. The values it - // returns on underflow and overflow are the right - // fallback in a robust setting. - return *str != '\0' && *endptr == '\0'; -} - -bool safe_strto32(const std::string &str, int32 *value) { - return safe_int_internal(str, value); -} - -bool safe_strtou32(const std::string &str, uint32 *value) { - return safe_uint_internal(str, value); -} - -bool safe_strto64(const std::string &str, int64 *value) { - return safe_int_internal(str, value); -} - -bool safe_strtou64(const std::string &str, uint64 *value) { - return safe_uint_internal(str, value); -} - -char* FloatToBuffer(float value, char* buffer) { - // FLT_DIG is 6 for IEEE-754 floats, which are used on almost all - // platforms these days. Just in case some system exists where FLT_DIG - // is significantly larger -- and risks overflowing our buffer -- we have - // this assert. - GOOGLE_COMPILE_ASSERT(FLT_DIG < 10, FLT_DIG_is_too_big); - - if (value == std::numeric_limits::infinity()) { - strcpy(buffer, "inf"); - return buffer; - } else if (value == -std::numeric_limits::infinity()) { - strcpy(buffer, "-inf"); - return buffer; - } else if (std::isnan(value)) { - strcpy(buffer, "nan"); - return buffer; - } - - int snprintf_result = - snprintf(buffer, kFloatToBufferSize, "%.*g", FLT_DIG, value); - - // The snprintf should never overflow because the buffer is significantly - // larger than the precision we asked for. - GOOGLE_DCHECK(snprintf_result > 0 && snprintf_result < kFloatToBufferSize); - - float parsed_value; - if (!safe_strtof(buffer, &parsed_value) || parsed_value != value) { - int snprintf_result = - snprintf(buffer, kFloatToBufferSize, "%.*g", FLT_DIG+3, value); - - // Should never overflow; see above. - GOOGLE_DCHECK(snprintf_result > 0 && snprintf_result < kFloatToBufferSize); - } - - DelocalizeRadix(buffer); - return buffer; -} - -namespace strings { - -AlphaNum::AlphaNum(strings::Hex hex) { - char *const end = &digits[kFastToBufferSize]; - char *writer = end; - uint64 value = hex.value; - uint64 width = hex.spec; - // We accomplish minimum width by OR'ing in 0x10000 to the user's value, - // where 0x10000 is the smallest hex number that is as wide as the user - // asked for. - uint64 mask = ((static_cast(1) << (width - 1) * 4)) | value; - static const char hexdigits[] = "0123456789abcdef"; - do { - *--writer = hexdigits[value & 0xF]; - value >>= 4; - mask >>= 4; - } while (mask != 0); - piece_data_ = writer; - piece_size_ = end - writer; -} - -} // namespace strings - -// ---------------------------------------------------------------------- -// StrCat() -// This merges the given strings or integers, with no delimiter. This -// is designed to be the fastest possible way to construct a string out -// of a mix of raw C strings, C++ strings, and integer values. -// ---------------------------------------------------------------------- - -// Append is merely a version of memcpy that returns the address of the byte -// after the area just overwritten. It comes in multiple flavors to minimize -// call overhead. -static char *Append1(char *out, const AlphaNum &x) { - if (x.size() > 0) { - memcpy(out, x.data(), x.size()); - out += x.size(); - } - return out; -} - -static char *Append2(char *out, const AlphaNum &x1, const AlphaNum &x2) { - if (x1.size() > 0) { - memcpy(out, x1.data(), x1.size()); - out += x1.size(); - } - if (x2.size() > 0) { - memcpy(out, x2.data(), x2.size()); - out += x2.size(); - } - return out; -} - -static char *Append4(char *out, const AlphaNum &x1, const AlphaNum &x2, - const AlphaNum &x3, const AlphaNum &x4) { - if (x1.size() > 0) { - memcpy(out, x1.data(), x1.size()); - out += x1.size(); - } - if (x2.size() > 0) { - memcpy(out, x2.data(), x2.size()); - out += x2.size(); - } - if (x3.size() > 0) { - memcpy(out, x3.data(), x3.size()); - out += x3.size(); - } - if (x4.size() > 0) { - memcpy(out, x4.data(), x4.size()); - out += x4.size(); - } - return out; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b) { - std::string result; - result.resize(a.size() + b.size()); - char *const begin = &*result.begin(); - char *out = Append2(begin, a, b); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c) { - std::string result; - result.resize(a.size() + b.size() + c.size()); - char *const begin = &*result.begin(); - char *out = Append2(begin, a, b); - out = Append1(out, c); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append1(out, e); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append2(out, e, f); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f, - const AlphaNum &g) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size() + g.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append2(out, e, f); - out = Append1(out, g); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f, - const AlphaNum &g, const AlphaNum &h) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size() + g.size() + h.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append4(out, e, f, g, h); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -std::string StrCat(const AlphaNum &a, const AlphaNum &b, const AlphaNum &c, - const AlphaNum &d, const AlphaNum &e, const AlphaNum &f, - const AlphaNum &g, const AlphaNum &h, const AlphaNum &i) { - std::string result; - result.resize(a.size() + b.size() + c.size() + d.size() + e.size() + - f.size() + g.size() + h.size() + i.size()); - char *const begin = &*result.begin(); - char *out = Append4(begin, a, b, c, d); - out = Append4(out, e, f, g, h); - out = Append1(out, i); - GOOGLE_DCHECK_EQ(out, begin + result.size()); - return result; -} - -// It's possible to call StrAppend with a char * pointer that is partway into -// the string we're appending to. However the results of this are random. -// Therefore, check for this in debug mode. Use unsigned math so we only have -// to do one comparison. -#define GOOGLE_DCHECK_NO_OVERLAP(dest, src) \ - GOOGLE_DCHECK_GT(uintptr_t((src).data() - (dest).data()), \ - uintptr_t((dest).size())) - -void StrAppend(std::string *result, const AlphaNum &a) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - result->append(a.data(), a.size()); -} - -void StrAppend(std::string *result, const AlphaNum &a, const AlphaNum &b) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - GOOGLE_DCHECK_NO_OVERLAP(*result, b); - std::string::size_type old_size = result->size(); - result->resize(old_size + a.size() + b.size()); - char *const begin = &*result->begin(); - char *out = Append2(begin + old_size, a, b); - GOOGLE_DCHECK_EQ(out, begin + result->size()); -} - -void StrAppend(std::string *result, const AlphaNum &a, const AlphaNum &b, - const AlphaNum &c) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - GOOGLE_DCHECK_NO_OVERLAP(*result, b); - GOOGLE_DCHECK_NO_OVERLAP(*result, c); - std::string::size_type old_size = result->size(); - result->resize(old_size + a.size() + b.size() + c.size()); - char *const begin = &*result->begin(); - char *out = Append2(begin + old_size, a, b); - out = Append1(out, c); - GOOGLE_DCHECK_EQ(out, begin + result->size()); -} - -void StrAppend(std::string *result, const AlphaNum &a, const AlphaNum &b, - const AlphaNum &c, const AlphaNum &d) { - GOOGLE_DCHECK_NO_OVERLAP(*result, a); - GOOGLE_DCHECK_NO_OVERLAP(*result, b); - GOOGLE_DCHECK_NO_OVERLAP(*result, c); - GOOGLE_DCHECK_NO_OVERLAP(*result, d); - std::string::size_type old_size = result->size(); - result->resize(old_size + a.size() + b.size() + c.size() + d.size()); - char *const begin = &*result->begin(); - char *out = Append4(begin + old_size, a, b, c, d); - GOOGLE_DCHECK_EQ(out, begin + result->size()); -} - -int GlobalReplaceSubstring(const std::string &substring, - const std::string &replacement, std::string *s) { - GOOGLE_CHECK(s != nullptr); - if (s->empty() || substring.empty()) - return 0; - std::string tmp; - int num_replacements = 0; - int pos = 0; - for (int match_pos = s->find(substring.data(), pos, substring.length()); - match_pos != std::string::npos; pos = match_pos + substring.length(), - match_pos = s->find(substring.data(), pos, substring.length())) { - ++num_replacements; - // Append the original content before the match. - tmp.append(*s, pos, match_pos - pos); - // Append the replacement for the match. - tmp.append(replacement.begin(), replacement.end()); - } - // Append the content after the last match. If no replacements were made, the - // original string is left untouched. - if (num_replacements > 0) { - tmp.append(*s, pos, s->length() - pos); - s->swap(tmp); - } - return num_replacements; -} - -int CalculateBase64EscapedLen(int input_len, bool do_padding) { - // Base64 encodes three bytes of input at a time. If the input is not - // divisible by three, we pad as appropriate. - // - // (from http://tools.ietf.org/html/rfc3548) - // Special processing is performed if fewer than 24 bits are available - // at the end of the data being encoded. A full encoding quantum is - // always completed at the end of a quantity. When fewer than 24 input - // bits are available in an input group, zero bits are added (on the - // right) to form an integral number of 6-bit groups. Padding at the - // end of the data is performed using the '=' character. Since all base - // 64 input is an integral number of octets, only the following cases - // can arise: - - - // Base64 encodes each three bytes of input into four bytes of output. - int len = (input_len / 3) * 4; - - if (input_len % 3 == 0) { - // (from http://tools.ietf.org/html/rfc3548) - // (1) the final quantum of encoding input is an integral multiple of 24 - // bits; here, the final unit of encoded output will be an integral - // multiple of 4 characters with no "=" padding, - } else if (input_len % 3 == 1) { - // (from http://tools.ietf.org/html/rfc3548) - // (2) the final quantum of encoding input is exactly 8 bits; here, the - // final unit of encoded output will be two characters followed by two - // "=" padding characters, or - len += 2; - if (do_padding) { - len += 2; - } - } else { // (input_len % 3 == 2) - // (from http://tools.ietf.org/html/rfc3548) - // (3) the final quantum of encoding input is exactly 16 bits; here, the - // final unit of encoded output will be three characters followed by one - // "=" padding character. - len += 3; - if (do_padding) { - len += 1; - } - } - - assert(len >= input_len); // make sure we didn't overflow - return len; -} - -// Base64Escape does padding, so this calculation includes padding. -int CalculateBase64EscapedLen(int input_len) { - return CalculateBase64EscapedLen(input_len, true); -} - -// ---------------------------------------------------------------------- -// int Base64Unescape() - base64 decoder -// int Base64Escape() - base64 encoder -// int WebSafeBase64Unescape() - Google's variation of base64 decoder -// int WebSafeBase64Escape() - Google's variation of base64 encoder -// -// Check out -// http://tools.ietf.org/html/rfc2045 for formal description, but what we -// care about is that... -// Take the encoded stuff in groups of 4 characters and turn each -// character into a code 0 to 63 thus: -// A-Z map to 0 to 25 -// a-z map to 26 to 51 -// 0-9 map to 52 to 61 -// +(- for WebSafe) maps to 62 -// /(_ for WebSafe) maps to 63 -// There will be four numbers, all less than 64 which can be represented -// by a 6 digit binary number (aaaaaa, bbbbbb, cccccc, dddddd respectively). -// Arrange the 6 digit binary numbers into three bytes as such: -// aaaaaabb bbbbcccc ccdddddd -// Equals signs (one or two) are used at the end of the encoded block to -// indicate that the text was not an integer multiple of three bytes long. -// ---------------------------------------------------------------------- - -int Base64UnescapeInternal(const char *src_param, int szsrc, - char *dest, int szdest, - const signed char* unbase64) { - static const char kPad64Equals = '='; - static const char kPad64Dot = '.'; - - int decode = 0; - int destidx = 0; - int state = 0; - unsigned int ch = 0; - unsigned int temp = 0; - - // If "char" is signed by default, using *src as an array index results in - // accessing negative array elements. Treat the input as a pointer to - // unsigned char to avoid this. - const unsigned char *src = reinterpret_cast(src_param); - - // The GET_INPUT macro gets the next input character, skipping - // over any whitespace, and stopping when we reach the end of the - // string or when we read any non-data character. The arguments are - // an arbitrary identifier (used as a label for goto) and the number - // of data bytes that must remain in the input to avoid aborting the - // loop. -#define GET_INPUT(label, remain) \ - label: \ - --szsrc; \ - ch = *src++; \ - decode = unbase64[ch]; \ - if (decode < 0) { \ - if (ascii_isspace(ch) && szsrc >= remain) \ - goto label; \ - state = 4 - remain; \ - break; \ - } - - // if dest is null, we're just checking to see if it's legal input - // rather than producing output. (I suspect this could just be done - // with a regexp...). We duplicate the loop so this test can be - // outside it instead of in every iteration. - - if (dest) { - // This loop consumes 4 input bytes and produces 3 output bytes - // per iteration. We can't know at the start that there is enough - // data left in the string for a full iteration, so the loop may - // break out in the middle; if so 'state' will be set to the - // number of input bytes read. - - while (szsrc >= 4) { - // We'll start by optimistically assuming that the next four - // bytes of the string (src[0..3]) are four good data bytes - // (that is, no nulls, whitespace, padding chars, or illegal - // chars). We need to test src[0..2] for nulls individually - // before constructing temp to preserve the property that we - // never read past a null in the string (no matter how long - // szsrc claims the string is). - - if (!src[0] || !src[1] || !src[2] || - (temp = ((unsigned(unbase64[src[0]]) << 18) | - (unsigned(unbase64[src[1]]) << 12) | - (unsigned(unbase64[src[2]]) << 6) | - (unsigned(unbase64[src[3]])))) & 0x80000000) { - // Iff any of those four characters was bad (null, illegal, - // whitespace, padding), then temp's high bit will be set - // (because unbase64[] is -1 for all bad characters). - // - // We'll back up and resort to the slower decoder, which knows - // how to handle those cases. - - GET_INPUT(first, 4); - temp = decode; - GET_INPUT(second, 3); - temp = (temp << 6) | decode; - GET_INPUT(third, 2); - temp = (temp << 6) | decode; - GET_INPUT(fourth, 1); - temp = (temp << 6) | decode; - } else { - // We really did have four good data bytes, so advance four - // characters in the string. - - szsrc -= 4; - src += 4; - decode = -1; - ch = '\0'; - } - - // temp has 24 bits of input, so write that out as three bytes. - - if (destidx+3 > szdest) return -1; - dest[destidx+2] = temp; - temp >>= 8; - dest[destidx+1] = temp; - temp >>= 8; - dest[destidx] = temp; - destidx += 3; - } - } else { - while (szsrc >= 4) { - if (!src[0] || !src[1] || !src[2] || - (temp = ((unsigned(unbase64[src[0]]) << 18) | - (unsigned(unbase64[src[1]]) << 12) | - (unsigned(unbase64[src[2]]) << 6) | - (unsigned(unbase64[src[3]])))) & 0x80000000) { - GET_INPUT(first_no_dest, 4); - GET_INPUT(second_no_dest, 3); - GET_INPUT(third_no_dest, 2); - GET_INPUT(fourth_no_dest, 1); - } else { - szsrc -= 4; - src += 4; - decode = -1; - ch = '\0'; - } - destidx += 3; - } - } - -#undef GET_INPUT - - // if the loop terminated because we read a bad character, return - // now. - if (decode < 0 && ch != '\0' && - ch != kPad64Equals && ch != kPad64Dot && !ascii_isspace(ch)) - return -1; - - if (ch == kPad64Equals || ch == kPad64Dot) { - // if we stopped by hitting an '=' or '.', un-read that character -- we'll - // look at it again when we count to check for the proper number of - // equals signs at the end. - ++szsrc; - --src; - } else { - // This loop consumes 1 input byte per iteration. It's used to - // clean up the 0-3 input bytes remaining when the first, faster - // loop finishes. 'temp' contains the data from 'state' input - // characters read by the first loop. - while (szsrc > 0) { - --szsrc; - ch = *src++; - decode = unbase64[ch]; - if (decode < 0) { - if (ascii_isspace(ch)) { - continue; - } else if (ch == '\0') { - break; - } else if (ch == kPad64Equals || ch == kPad64Dot) { - // back up one character; we'll read it again when we check - // for the correct number of pad characters at the end. - ++szsrc; - --src; - break; - } else { - return -1; - } - } - - // Each input character gives us six bits of output. - temp = (temp << 6) | decode; - ++state; - if (state == 4) { - // If we've accumulated 24 bits of output, write that out as - // three bytes. - if (dest) { - if (destidx+3 > szdest) return -1; - dest[destidx+2] = temp; - temp >>= 8; - dest[destidx+1] = temp; - temp >>= 8; - dest[destidx] = temp; - } - destidx += 3; - state = 0; - temp = 0; - } - } - } - - // Process the leftover data contained in 'temp' at the end of the input. - int expected_equals = 0; - switch (state) { - case 0: - // Nothing left over; output is a multiple of 3 bytes. - break; - - case 1: - // Bad input; we have 6 bits left over. - return -1; - - case 2: - // Produce one more output byte from the 12 input bits we have left. - if (dest) { - if (destidx+1 > szdest) return -1; - temp >>= 4; - dest[destidx] = temp; - } - ++destidx; - expected_equals = 2; - break; - - case 3: - // Produce two more output bytes from the 18 input bits we have left. - if (dest) { - if (destidx+2 > szdest) return -1; - temp >>= 2; - dest[destidx+1] = temp; - temp >>= 8; - dest[destidx] = temp; - } - destidx += 2; - expected_equals = 1; - break; - - default: - // state should have no other values at this point. - GOOGLE_LOG(FATAL) << "This can't happen; base64 decoder state = " << state; - } - - // The remainder of the string should be all whitespace, mixed with - // exactly 0 equals signs, or exactly 'expected_equals' equals - // signs. (Always accepting 0 equals signs is a google extension - // not covered in the RFC, as is accepting dot as the pad character.) - - int equals = 0; - while (szsrc > 0 && *src) { - if (*src == kPad64Equals || *src == kPad64Dot) - ++equals; - else if (!ascii_isspace(*src)) - return -1; - --szsrc; - ++src; - } - - return (equals == 0 || equals == expected_equals) ? destidx : -1; -} - -// The arrays below were generated by the following code -// #include -// #include -// #include -// #include -// main() -// { -// static const char Base64[] = -// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -// const char *pos; -// int idx, i, j; -// printf(" "); -// for (i = 0; i < 255; i += 8) { -// for (j = i; j < i + 8; j++) { -// pos = strchr(Base64, j); -// if ((pos == nullptr) || (j == 0)) -// idx = -1; -// else -// idx = pos - Base64; -// if (idx == -1) -// printf(" %2d, ", idx); -// else -// printf(" %2d/""*%c*""/,", idx, j); -// } -// printf("\n "); -// } -// } -// -// where the value of "Base64[]" was replaced by one of the base-64 conversion -// tables from the functions below. -static const signed char kUnBase64[] = { - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */, - 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/, - 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1, - -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/, - 7/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/, - 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/, - 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1, - -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/, - 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/, - 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/, - 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1 -}; -static const signed char kUnWebSafeBase64[] = { - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, 62/*-*/, -1, -1, - 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/, - 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1, - -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/, - 7/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/, - 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/, - 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, 63/*_*/, - -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/, - 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/, - 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/, - 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1 -}; - -int WebSafeBase64Unescape(const char *src, int szsrc, char *dest, int szdest) { - return Base64UnescapeInternal(src, szsrc, dest, szdest, kUnWebSafeBase64); -} - -static bool Base64UnescapeInternal(const char *src, int slen, std::string *dest, - const signed char *unbase64) { - // Determine the size of the output string. Base64 encodes every 3 bytes into - // 4 characters. any leftover chars are added directly for good measure. - // This is documented in the base64 RFC: http://tools.ietf.org/html/rfc3548 - const int dest_len = 3 * (slen / 4) + (slen % 4); - - dest->resize(dest_len); - - // We are getting the destination buffer by getting the beginning of the - // string and converting it into a char *. - const int len = Base64UnescapeInternal(src, slen, string_as_array(dest), - dest_len, unbase64); - if (len < 0) { - dest->clear(); - return false; - } - - // could be shorter if there was padding - GOOGLE_DCHECK_LE(len, dest_len); - dest->erase(len); - - return true; -} - -bool Base64Unescape(StringPiece src, std::string *dest) { - return Base64UnescapeInternal(src.data(), src.size(), dest, kUnBase64); -} - -bool WebSafeBase64Unescape(StringPiece src, std::string *dest) { - return Base64UnescapeInternal(src.data(), src.size(), dest, kUnWebSafeBase64); -} - -int Base64EscapeInternal(const unsigned char *src, int szsrc, - char *dest, int szdest, const char *base64, - bool do_padding) { - static const char kPad64 = '='; - - if (szsrc <= 0) return 0; - - if (szsrc * 4 > szdest * 3) return 0; - - char *cur_dest = dest; - const unsigned char *cur_src = src; - - char *limit_dest = dest + szdest; - const unsigned char *limit_src = src + szsrc; - - // Three bytes of data encodes to four characters of cyphertext. - // So we can pump through three-byte chunks atomically. - while (cur_src < limit_src - 3) { // keep going as long as we have >= 32 bits - uint32 in = BigEndian::Load32(cur_src) >> 8; - - cur_dest[0] = base64[in >> 18]; - in &= 0x3FFFF; - cur_dest[1] = base64[in >> 12]; - in &= 0xFFF; - cur_dest[2] = base64[in >> 6]; - in &= 0x3F; - cur_dest[3] = base64[in]; - - cur_dest += 4; - cur_src += 3; - } - // To save time, we didn't update szdest or szsrc in the loop. So do it now. - szdest = limit_dest - cur_dest; - szsrc = limit_src - cur_src; - - /* now deal with the tail (<=3 bytes) */ - switch (szsrc) { - case 0: - // Nothing left; nothing more to do. - break; - case 1: { - // One byte left: this encodes to two characters, and (optionally) - // two pad characters to round out the four-character cypherblock. - if ((szdest -= 2) < 0) return 0; - uint32 in = cur_src[0]; - cur_dest[0] = base64[in >> 2]; - in &= 0x3; - cur_dest[1] = base64[in << 4]; - cur_dest += 2; - if (do_padding) { - if ((szdest -= 2) < 0) return 0; - cur_dest[0] = kPad64; - cur_dest[1] = kPad64; - cur_dest += 2; - } - break; - } - case 2: { - // Two bytes left: this encodes to three characters, and (optionally) - // one pad character to round out the four-character cypherblock. - if ((szdest -= 3) < 0) return 0; - uint32 in = BigEndian::Load16(cur_src); - cur_dest[0] = base64[in >> 10]; - in &= 0x3FF; - cur_dest[1] = base64[in >> 4]; - in &= 0x00F; - cur_dest[2] = base64[in << 2]; - cur_dest += 3; - if (do_padding) { - if ((szdest -= 1) < 0) return 0; - cur_dest[0] = kPad64; - cur_dest += 1; - } - break; - } - case 3: { - // Three bytes left: same as in the big loop above. We can't do this in - // the loop because the loop above always reads 4 bytes, and the fourth - // byte is past the end of the input. - if ((szdest -= 4) < 0) return 0; - uint32 in = (cur_src[0] << 16) + BigEndian::Load16(cur_src + 1); - cur_dest[0] = base64[in >> 18]; - in &= 0x3FFFF; - cur_dest[1] = base64[in >> 12]; - in &= 0xFFF; - cur_dest[2] = base64[in >> 6]; - in &= 0x3F; - cur_dest[3] = base64[in]; - cur_dest += 4; - break; - } - default: - // Should not be reached: blocks of 4 bytes are handled - // in the while loop before this switch statement. - GOOGLE_LOG(FATAL) << "Logic problem? szsrc = " << szsrc; - break; - } - return (cur_dest - dest); -} - -static const char kBase64Chars[] = -"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -static const char kWebSafeBase64Chars[] = -"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; - -int Base64Escape(const unsigned char *src, int szsrc, char *dest, int szdest) { - return Base64EscapeInternal(src, szsrc, dest, szdest, kBase64Chars, true); -} -int WebSafeBase64Escape(const unsigned char *src, int szsrc, char *dest, - int szdest, bool do_padding) { - return Base64EscapeInternal(src, szsrc, dest, szdest, - kWebSafeBase64Chars, do_padding); -} - -void Base64EscapeInternal(const unsigned char *src, int szsrc, - std::string *dest, bool do_padding, - const char *base64_chars) { - const int calc_escaped_size = - CalculateBase64EscapedLen(szsrc, do_padding); - dest->resize(calc_escaped_size); - const int escaped_len = Base64EscapeInternal(src, szsrc, - string_as_array(dest), - dest->size(), - base64_chars, - do_padding); - GOOGLE_DCHECK_EQ(calc_escaped_size, escaped_len); - dest->erase(escaped_len); -} - -void Base64Escape(const unsigned char *src, int szsrc, std::string *dest, - bool do_padding) { - Base64EscapeInternal(src, szsrc, dest, do_padding, kBase64Chars); -} - -void WebSafeBase64Escape(const unsigned char *src, int szsrc, std::string *dest, - bool do_padding) { - Base64EscapeInternal(src, szsrc, dest, do_padding, kWebSafeBase64Chars); -} - -void Base64Escape(StringPiece src, std::string *dest) { - Base64Escape(reinterpret_cast(src.data()), - src.size(), dest, true); -} - -void WebSafeBase64Escape(StringPiece src, std::string *dest) { - WebSafeBase64Escape(reinterpret_cast(src.data()), - src.size(), dest, false); -} - -void WebSafeBase64EscapeWithPadding(StringPiece src, std::string *dest) { - WebSafeBase64Escape(reinterpret_cast(src.data()), - src.size(), dest, true); -} - -// Helper to append a Unicode code point to a string as UTF8, without bringing -// in any external dependencies. -int EncodeAsUTF8Char(uint32 code_point, char* output) { - uint32 tmp = 0; - int len = 0; - if (code_point <= 0x7f) { - tmp = code_point; - len = 1; - } else if (code_point <= 0x07ff) { - tmp = 0x0000c080 | - ((code_point & 0x07c0) << 2) | - (code_point & 0x003f); - len = 2; - } else if (code_point <= 0xffff) { - tmp = 0x00e08080 | - ((code_point & 0xf000) << 4) | - ((code_point & 0x0fc0) << 2) | - (code_point & 0x003f); - len = 3; - } else { - // UTF-16 is only defined for code points up to 0x10FFFF, and UTF-8 is - // normally only defined up to there as well. - tmp = 0xf0808080 | - ((code_point & 0x1c0000) << 6) | - ((code_point & 0x03f000) << 4) | - ((code_point & 0x000fc0) << 2) | - (code_point & 0x003f); - len = 4; - } - tmp = ghtonl(tmp); - memcpy(output, reinterpret_cast(&tmp) + sizeof(tmp) - len, len); - return len; -} - -// Table of UTF-8 character lengths, based on first byte -static const unsigned char kUTF8LenTbl[256] = { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - -// Return length of a single UTF-8 source character -int UTF8FirstLetterNumBytes(const char* src, int len) { - if (len == 0) { - return 0; - } - return kUTF8LenTbl[*reinterpret_cast(src)]; -} - -// ---------------------------------------------------------------------- -// CleanStringLineEndings() -// Clean up a multi-line string to conform to Unix line endings. -// Reads from src and appends to dst, so usually dst should be empty. -// -// If there is no line ending at the end of a non-empty string, it can -// be added automatically. -// -// Four different types of input are correctly handled: -// -// - Unix/Linux files: line ending is LF: pass through unchanged -// -// - DOS/Windows files: line ending is CRLF: convert to LF -// -// - Legacy Mac files: line ending is CR: convert to LF -// -// - Garbled files: random line endings: convert gracefully -// lonely CR, lonely LF, CRLF: convert to LF -// -// @param src The multi-line string to convert -// @param dst The converted string is appended to this string -// @param auto_end_last_line Automatically terminate the last line -// -// Limitations: -// -// This does not do the right thing for CRCRLF files created by -// broken programs that do another Unix->DOS conversion on files -// that are already in CRLF format. For this, a two-pass approach -// brute-force would be needed that -// -// (1) determines the presence of LF (first one is ok) -// (2) if yes, removes any CR, else convert every CR to LF - -void CleanStringLineEndings(const std::string &src, std::string *dst, - bool auto_end_last_line) { - if (dst->empty()) { - dst->append(src); - CleanStringLineEndings(dst, auto_end_last_line); - } else { - std::string tmp = src; - CleanStringLineEndings(&tmp, auto_end_last_line); - dst->append(tmp); - } -} - -void CleanStringLineEndings(std::string *str, bool auto_end_last_line) { - ptrdiff_t output_pos = 0; - bool r_seen = false; - ptrdiff_t len = str->size(); - - char *p = &(*str)[0]; - - for (ptrdiff_t input_pos = 0; input_pos < len;) { - if (!r_seen && input_pos + 8 < len) { - uint64_t v = GOOGLE_UNALIGNED_LOAD64(p + input_pos); - // Loop over groups of 8 bytes at a time until we come across - // a word that has a byte whose value is less than or equal to - // '\r' (i.e. could contain a \n (0x0a) or a \r (0x0d) ). - // - // We use a has_less macro that quickly tests a whole 64-bit - // word to see if any of the bytes has a value < N. - // - // For more details, see: - // http://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord -#define has_less(x, n) (((x) - ~0ULL / 255 * (n)) & ~(x) & ~0ULL / 255 * 128) - if (!has_less(v, '\r' + 1)) { -#undef has_less - // No byte in this word has a value that could be a \r or a \n - if (output_pos != input_pos) { - GOOGLE_UNALIGNED_STORE64(p + output_pos, v); - } - input_pos += 8; - output_pos += 8; - continue; - } - } - std::string::const_reference in = p[input_pos]; - if (in == '\r') { - if (r_seen) p[output_pos++] = '\n'; - r_seen = true; - } else if (in == '\n') { - if (input_pos != output_pos) - p[output_pos++] = '\n'; - else - output_pos++; - r_seen = false; - } else { - if (r_seen) p[output_pos++] = '\n'; - r_seen = false; - if (input_pos != output_pos) - p[output_pos++] = in; - else - output_pos++; - } - input_pos++; - } - if (r_seen || - (auto_end_last_line && output_pos > 0 && p[output_pos - 1] != '\n')) { - str->resize(output_pos + 1); - str->operator[](output_pos) = '\n'; - } else if (output_pos < len) { - str->resize(output_pos); - } -} - -namespace internal { - -// ---------------------------------------------------------------------- -// NoLocaleStrtod() -// This code will make you cry. -// ---------------------------------------------------------------------- - -namespace { - -// Returns a string identical to *input except that the character pointed to -// by radix_pos (which should be '.') is replaced with the locale-specific -// radix character. -std::string LocalizeRadix(const char *input, const char *radix_pos) { - // Determine the locale-specific radix character by calling sprintf() to - // print the number 1.5, then stripping off the digits. As far as I can - // tell, this is the only portable, thread-safe way to get the C library - // to divuldge the locale's radix character. No, localeconv() is NOT - // thread-safe. - char temp[16]; - int size = snprintf(temp, sizeof(temp), "%.1f", 1.5); - GOOGLE_CHECK_EQ(temp[0], '1'); - GOOGLE_CHECK_EQ(temp[size - 1], '5'); - GOOGLE_CHECK_LE(size, 6); - - // Now replace the '.' in the input with it. - std::string result; - result.reserve(strlen(input) + size - 3); - result.append(input, radix_pos); - result.append(temp + 1, size - 2); - result.append(radix_pos + 1); - return result; -} - -} // namespace - -double NoLocaleStrtod(const char *str, char **endptr) { - // We cannot simply set the locale to "C" temporarily with setlocale() - // as this is not thread-safe. Instead, we try to parse in the current - // locale first. If parsing stops at a '.' character, then this is a - // pretty good hint that we're actually in some other locale in which - // '.' is not the radix character. - - char *temp_endptr; - double result = strtod(str, &temp_endptr); - if (endptr != NULL) *endptr = temp_endptr; - if (*temp_endptr != '.') return result; - - // Parsing halted on a '.'. Perhaps we're in a different locale? Let's - // try to replace the '.' with a locale-specific radix character and - // try again. - std::string localized = LocalizeRadix(str, temp_endptr); - const char *localized_cstr = localized.c_str(); - char *localized_endptr; - result = strtod(localized_cstr, &localized_endptr); - if ((localized_endptr - localized_cstr) > (temp_endptr - str)) { - // This attempt got further, so replacing the decimal must have helped. - // Update endptr to point at the right location. - if (endptr != NULL) { - // size_diff is non-zero if the localized radix has multiple bytes. - int size_diff = localized.size() - strlen(str); - // const_cast is necessary to match the strtod() interface. - *endptr = const_cast( - str + (localized_endptr - localized_cstr - size_diff)); - } - } - - return result; -} - -} // namespace internal - -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/time.cc b/third_party/protobuf-lite/time.cc deleted file mode 100644 index 922be76d1..000000000 --- a/third_party/protobuf-lite/time.cc +++ /dev/null @@ -1,365 +0,0 @@ -#include - -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace internal { - -namespace { -static const int64 kSecondsPerMinute = 60; -static const int64 kSecondsPerHour = 3600; -static const int64 kSecondsPerDay = kSecondsPerHour * 24; -static const int64 kSecondsPer400Years = - kSecondsPerDay * (400 * 365 + 400 / 4 - 3); -// Seconds from 0001-01-01T00:00:00 to 1970-01-01T:00:00:00 -static const int64 kSecondsFromEraToEpoch = 62135596800LL; -// The range of timestamp values we support. -static const int64 kMinTime = -62135596800LL; // 0001-01-01T00:00:00 -static const int64 kMaxTime = 253402300799LL; // 9999-12-31T23:59:59 - -static const int kNanosPerMillisecond = 1000000; -static const int kNanosPerMicrosecond = 1000; - -// Count the seconds from the given year (start at Jan 1, 00:00) to 100 years -// after. -int64 SecondsPer100Years(int year) { - if (year % 400 == 0 || year % 400 > 300) { - return kSecondsPerDay * (100 * 365 + 100 / 4); - } else { - return kSecondsPerDay * (100 * 365 + 100 / 4 - 1); - } -} - -// Count the seconds from the given year (start at Jan 1, 00:00) to 4 years -// after. -int64 SecondsPer4Years(int year) { - if ((year % 100 == 0 || year % 100 > 96) && - !(year % 400 == 0 || year % 400 > 396)) { - // No leap years. - return kSecondsPerDay * (4 * 365); - } else { - // One leap years. - return kSecondsPerDay * (4 * 365 + 1); - } -} - -bool IsLeapYear(int year) { - return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); -} - -int64 SecondsPerYear(int year) { - return kSecondsPerDay * (IsLeapYear(year) ? 366 : 365); -} - -static const int kDaysInMonth[13] = { - 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 -}; - -int64 SecondsPerMonth(int month, bool leap) { - if (month == 2 && leap) { - return kSecondsPerDay * (kDaysInMonth[month] + 1); - } - return kSecondsPerDay * kDaysInMonth[month]; -} - -static const int kDaysSinceJan[13] = { - 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, -}; - -bool ValidateDateTime(const DateTime& time) { - if (time.year < 1 || time.year > 9999 || - time.month < 1 || time.month > 12 || - time.day < 1 || time.day > 31 || - time.hour < 0 || time.hour > 23 || - time.minute < 0 || time.minute > 59 || - time.second < 0 || time.second > 59) { - return false; - } - if (time.month == 2 && IsLeapYear(time.year)) { - return time.day <= kDaysInMonth[time.month] + 1; - } else { - return time.day <= kDaysInMonth[time.month]; - } -} - -// Count the number of seconds elapsed from 0001-01-01T00:00:00 to the given -// time. -int64 SecondsSinceCommonEra(const DateTime& time) { - int64 result = 0; - // Years should be between 1 and 9999. - assert(time.year >= 1 && time.year <= 9999); - int year = 1; - if ((time.year - year) >= 400) { - int count_400years = (time.year - year) / 400; - result += kSecondsPer400Years * count_400years; - year += count_400years * 400; - } - while ((time.year - year) >= 100) { - result += SecondsPer100Years(year); - year += 100; - } - while ((time.year - year) >= 4) { - result += SecondsPer4Years(year); - year += 4; - } - while (time.year > year) { - result += SecondsPerYear(year); - ++year; - } - // Months should be between 1 and 12. - assert(time.month >= 1 && time.month <= 12); - int month = time.month; - result += kSecondsPerDay * kDaysSinceJan[month]; - if (month > 2 && IsLeapYear(year)) { - result += kSecondsPerDay; - } - assert(time.day >= 1 && - time.day <= (month == 2 && IsLeapYear(year) - ? kDaysInMonth[month] + 1 - : kDaysInMonth[month])); - result += kSecondsPerDay * (time.day - 1); - result += kSecondsPerHour * time.hour + - kSecondsPerMinute * time.minute + - time.second; - return result; -} - -// Format nanoseconds with either 3, 6, or 9 digits depending on the required -// precision to represent the exact value. -std::string FormatNanos(int32 nanos) { - if (nanos % kNanosPerMillisecond == 0) { - return StringPrintf("%03d", nanos / kNanosPerMillisecond); - } else if (nanos % kNanosPerMicrosecond == 0) { - return StringPrintf("%06d", nanos / kNanosPerMicrosecond); - } else { - return StringPrintf("%09d", nanos); - } -} - -// Parses an integer from a null-terminated char sequence. The method -// consumes at most "width" chars. Returns a pointer after the consumed -// integer, or nullptr if the data does not start with an integer or the -// integer value does not fall in the range of [min_value, max_value]. -const char* ParseInt(const char* data, int width, int min_value, - int max_value, int* result) { - if (!ascii_isdigit(*data)) { - return nullptr; - } - int value = 0; - for (int i = 0; i < width; ++i, ++data) { - if (ascii_isdigit(*data)) { - value = value * 10 + (*data - '0'); - } else { - break; - } - } - if (value >= min_value && value <= max_value) { - *result = value; - return data; - } else { - return nullptr; - } -} - -// Consumes the fractional parts of a second into nanos. For example, -// "010" will be parsed to 10000000 nanos. -const char* ParseNanos(const char* data, int32* nanos) { - if (!ascii_isdigit(*data)) { - return nullptr; - } - int value = 0; - int len = 0; - // Consume as many digits as there are but only take the first 9 into - // account. - while (ascii_isdigit(*data)) { - if (len < 9) { - value = value * 10 + *data - '0'; - } - ++len; - ++data; - } - while (len < 9) { - value = value * 10; - ++len; - } - *nanos = value; - return data; -} - -const char* ParseTimezoneOffset(const char* data, int64* offset) { - // Accept format "HH:MM". E.g., "08:00" - int hour; - if ((data = ParseInt(data, 2, 0, 23, &hour)) == nullptr) { - return nullptr; - } - if (*data++ != ':') { - return nullptr; - } - int minute; - if ((data = ParseInt(data, 2, 0, 59, &minute)) == nullptr) { - return nullptr; - } - *offset = (hour * 60 + minute) * 60; - return data; -} -} // namespace - -bool SecondsToDateTime(int64 seconds, DateTime* time) { - if (seconds < kMinTime || seconds > kMaxTime) { - return false; - } - // It's easier to calculate the DateTime starting from 0001-01-01T00:00:00 - seconds = seconds + kSecondsFromEraToEpoch; - int year = 1; - if (seconds >= kSecondsPer400Years) { - int count_400years = seconds / kSecondsPer400Years; - year += 400 * count_400years; - seconds %= kSecondsPer400Years; - } - while (seconds >= SecondsPer100Years(year)) { - seconds -= SecondsPer100Years(year); - year += 100; - } - while (seconds >= SecondsPer4Years(year)) { - seconds -= SecondsPer4Years(year); - year += 4; - } - while (seconds >= SecondsPerYear(year)) { - seconds -= SecondsPerYear(year); - year += 1; - } - bool leap = IsLeapYear(year); - int month = 1; - while (seconds >= SecondsPerMonth(month, leap)) { - seconds -= SecondsPerMonth(month, leap); - ++month; - } - int day = 1 + seconds / kSecondsPerDay; - seconds %= kSecondsPerDay; - int hour = seconds / kSecondsPerHour; - seconds %= kSecondsPerHour; - int minute = seconds / kSecondsPerMinute; - seconds %= kSecondsPerMinute; - time->year = year; - time->month = month; - time->day = day; - time->hour = hour; - time->minute = minute; - time->second = static_cast(seconds); - return true; -} - -bool DateTimeToSeconds(const DateTime& time, int64* seconds) { - if (!ValidateDateTime(time)) { - return false; - } - *seconds = SecondsSinceCommonEra(time) - kSecondsFromEraToEpoch; - return true; -} - -void GetCurrentTime(int64* seconds, int32* nanos) { - // TODO(xiaofeng): Improve the accuracy of this implementation (or just - // remove this method from protobuf). - *seconds = time(nullptr); - *nanos = 0; -} - -std::string FormatTime(int64 seconds, int32 nanos) { - DateTime time; - if (nanos < 0 || nanos > 999999999 || !SecondsToDateTime(seconds, &time)) { - return "InvalidTime"; - } - std::string result = - StringPrintf("%04d-%02d-%02dT%02d:%02d:%02d", time.year, time.month, - time.day, time.hour, time.minute, time.second); - if (nanos != 0) { - result += "." + FormatNanos(nanos); - } - return result + "Z"; -} - -bool ParseTime(const std::string& value, int64* seconds, int32* nanos) { - DateTime time; - const char* data = value.c_str(); - // We only accept: - // Z-normalized: 2015-05-20T13:29:35.120Z - // With UTC offset: 2015-05-20T13:29:35.120-08:00 - - // Parse year - if ((data = ParseInt(data, 4, 1, 9999, &time.year)) == nullptr) { - return false; - } - // Expect '-' - if (*data++ != '-') return false; - // Parse month - if ((data = ParseInt(data, 2, 1, 12, &time.month)) == nullptr) { - return false; - } - // Expect '-' - if (*data++ != '-') return false; - // Parse day - if ((data = ParseInt(data, 2, 1, 31, &time.day)) == nullptr) { - return false; - } - // Expect 'T' - if (*data++ != 'T') return false; - // Parse hour - if ((data = ParseInt(data, 2, 0, 23, &time.hour)) == nullptr) { - return false; - } - // Expect ':' - if (*data++ != ':') return false; - // Parse minute - if ((data = ParseInt(data, 2, 0, 59, &time.minute)) == nullptr) { - return false; - } - // Expect ':' - if (*data++ != ':') return false; - // Parse second - if ((data = ParseInt(data, 2, 0, 59, &time.second)) == nullptr) { - return false; - } - if (!DateTimeToSeconds(time, seconds)) { - return false; - } - // Parse nanoseconds. - if (*data == '.') { - ++data; - // Parse nanoseconds. - if ((data = ParseNanos(data, nanos)) == nullptr) { - return false; - } - } else { - *nanos = 0; - } - // Parse UTC offsets. - if (*data == 'Z') { - ++data; - } else if (*data == '+') { - ++data; - int64 offset; - if ((data = ParseTimezoneOffset(data, &offset)) == nullptr) { - return false; - } - *seconds -= offset; - } else if (*data == '-') { - ++data; - int64 offset; - if ((data = ParseTimezoneOffset(data, &offset)) == nullptr) { - return false; - } - *seconds += offset; - } else { - return false; - } - // Done with parsing. - return *data == 0; -} - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/wire_format_lite.cc b/third_party/protobuf-lite/wire_format_lite.cc deleted file mode 100644 index dc2560828..000000000 --- a/third_party/protobuf-lite/wire_format_lite.cc +++ /dev/null @@ -1,778 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - - -#include - -namespace google { -namespace protobuf { -namespace internal { - -#if !defined(_MSC_VER) || _MSC_VER >= 1900 -// Old version of MSVC doesn't like definitions of inline constants, GCC -// requires them. -const int WireFormatLite::kMessageSetItemStartTag; -const int WireFormatLite::kMessageSetItemEndTag; -const int WireFormatLite::kMessageSetTypeIdTag; -const int WireFormatLite::kMessageSetMessageTag; - -#endif - -// IBM xlC requires prefixing constants with WireFormatLite:: -const size_t WireFormatLite::kMessageSetItemTagsSize = - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetItemStartTag>::value + - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetItemEndTag>::value + - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetTypeIdTag>::value + - io::CodedOutputStream::StaticVarintSize32< - WireFormatLite::kMessageSetMessageTag>::value; - -const WireFormatLite::CppType - WireFormatLite::kFieldTypeToCppTypeMap[MAX_FIELD_TYPE + 1] = { - static_cast(0), // 0 is reserved for errors - - CPPTYPE_DOUBLE, // TYPE_DOUBLE - CPPTYPE_FLOAT, // TYPE_FLOAT - CPPTYPE_INT64, // TYPE_INT64 - CPPTYPE_UINT64, // TYPE_UINT64 - CPPTYPE_INT32, // TYPE_INT32 - CPPTYPE_UINT64, // TYPE_FIXED64 - CPPTYPE_UINT32, // TYPE_FIXED32 - CPPTYPE_BOOL, // TYPE_BOOL - CPPTYPE_STRING, // TYPE_STRING - CPPTYPE_MESSAGE, // TYPE_GROUP - CPPTYPE_MESSAGE, // TYPE_MESSAGE - CPPTYPE_STRING, // TYPE_BYTES - CPPTYPE_UINT32, // TYPE_UINT32 - CPPTYPE_ENUM, // TYPE_ENUM - CPPTYPE_INT32, // TYPE_SFIXED32 - CPPTYPE_INT64, // TYPE_SFIXED64 - CPPTYPE_INT32, // TYPE_SINT32 - CPPTYPE_INT64, // TYPE_SINT64 -}; - -const WireFormatLite::WireType - WireFormatLite::kWireTypeForFieldType[MAX_FIELD_TYPE + 1] = { - static_cast(-1), // invalid - WireFormatLite::WIRETYPE_FIXED64, // TYPE_DOUBLE - WireFormatLite::WIRETYPE_FIXED32, // TYPE_FLOAT - WireFormatLite::WIRETYPE_VARINT, // TYPE_INT64 - WireFormatLite::WIRETYPE_VARINT, // TYPE_UINT64 - WireFormatLite::WIRETYPE_VARINT, // TYPE_INT32 - WireFormatLite::WIRETYPE_FIXED64, // TYPE_FIXED64 - WireFormatLite::WIRETYPE_FIXED32, // TYPE_FIXED32 - WireFormatLite::WIRETYPE_VARINT, // TYPE_BOOL - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_STRING - WireFormatLite::WIRETYPE_START_GROUP, // TYPE_GROUP - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_MESSAGE - WireFormatLite::WIRETYPE_LENGTH_DELIMITED, // TYPE_BYTES - WireFormatLite::WIRETYPE_VARINT, // TYPE_UINT32 - WireFormatLite::WIRETYPE_VARINT, // TYPE_ENUM - WireFormatLite::WIRETYPE_FIXED32, // TYPE_SFIXED32 - WireFormatLite::WIRETYPE_FIXED64, // TYPE_SFIXED64 - WireFormatLite::WIRETYPE_VARINT, // TYPE_SINT32 - WireFormatLite::WIRETYPE_VARINT, // TYPE_SINT64 -}; - -bool WireFormatLite::SkipField(io::CodedInputStream* input, uint32 tag) { - // Field number 0 is illegal. - if (WireFormatLite::GetTagFieldNumber(tag) == 0) return false; - switch (WireFormatLite::GetTagWireType(tag)) { - case WireFormatLite::WIRETYPE_VARINT: { - uint64 value; - if (!input->ReadVarint64(&value)) return false; - return true; - } - case WireFormatLite::WIRETYPE_FIXED64: { - uint64 value; - if (!input->ReadLittleEndian64(&value)) return false; - return true; - } - case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: { - uint32 length; - if (!input->ReadVarint32(&length)) return false; - if (!input->Skip(length)) return false; - return true; - } - case WireFormatLite::WIRETYPE_START_GROUP: { - if (!input->IncrementRecursionDepth()) return false; - if (!SkipMessage(input)) return false; - input->DecrementRecursionDepth(); - // Check that the ending tag matched the starting tag. - if (!input->LastTagWas( - WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag), - WireFormatLite::WIRETYPE_END_GROUP))) { - return false; - } - return true; - } - case WireFormatLite::WIRETYPE_END_GROUP: { - return false; - } - case WireFormatLite::WIRETYPE_FIXED32: { - uint32 value; - if (!input->ReadLittleEndian32(&value)) return false; - return true; - } - default: { - return false; - } - } -} - -bool WireFormatLite::SkipField(io::CodedInputStream* input, uint32 tag, - io::CodedOutputStream* output) { - // Field number 0 is illegal. - if (WireFormatLite::GetTagFieldNumber(tag) == 0) return false; - switch (WireFormatLite::GetTagWireType(tag)) { - case WireFormatLite::WIRETYPE_VARINT: { - uint64 value; - if (!input->ReadVarint64(&value)) return false; - output->WriteVarint32(tag); - output->WriteVarint64(value); - return true; - } - case WireFormatLite::WIRETYPE_FIXED64: { - uint64 value; - if (!input->ReadLittleEndian64(&value)) return false; - output->WriteVarint32(tag); - output->WriteLittleEndian64(value); - return true; - } - case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: { - uint32 length; - if (!input->ReadVarint32(&length)) return false; - output->WriteVarint32(tag); - output->WriteVarint32(length); - // TODO(mkilavuz): Provide API to prevent extra string copying. - std::string temp; - if (!input->ReadString(&temp, length)) return false; - output->WriteString(temp); - return true; - } - case WireFormatLite::WIRETYPE_START_GROUP: { - output->WriteVarint32(tag); - if (!input->IncrementRecursionDepth()) return false; - if (!SkipMessage(input, output)) return false; - input->DecrementRecursionDepth(); - // Check that the ending tag matched the starting tag. - if (!input->LastTagWas( - WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag), - WireFormatLite::WIRETYPE_END_GROUP))) { - return false; - } - return true; - } - case WireFormatLite::WIRETYPE_END_GROUP: { - return false; - } - case WireFormatLite::WIRETYPE_FIXED32: { - uint32 value; - if (!input->ReadLittleEndian32(&value)) return false; - output->WriteVarint32(tag); - output->WriteLittleEndian32(value); - return true; - } - default: { - return false; - } - } -} - -bool WireFormatLite::SkipMessage(io::CodedInputStream* input) { - while (true) { - uint32 tag = input->ReadTag(); - if (tag == 0) { - // End of input. This is a valid place to end, so return true. - return true; - } - - WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag); - - if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) { - // Must be the end of the message. - return true; - } - - if (!SkipField(input, tag)) return false; - } -} - -bool WireFormatLite::SkipMessage(io::CodedInputStream* input, - io::CodedOutputStream* output) { - while (true) { - uint32 tag = input->ReadTag(); - if (tag == 0) { - // End of input. This is a valid place to end, so return true. - return true; - } - - WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag); - - if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) { - output->WriteVarint32(tag); - // Must be the end of the message. - return true; - } - - if (!SkipField(input, tag, output)) return false; - } -} - -bool FieldSkipper::SkipField(io::CodedInputStream* input, uint32 tag) { - return WireFormatLite::SkipField(input, tag); -} - -bool FieldSkipper::SkipMessage(io::CodedInputStream* input) { - return WireFormatLite::SkipMessage(input); -} - -void FieldSkipper::SkipUnknownEnum(int /* field_number */, int /* value */) { - // Nothing. -} - -bool CodedOutputStreamFieldSkipper::SkipField(io::CodedInputStream* input, - uint32 tag) { - return WireFormatLite::SkipField(input, tag, unknown_fields_); -} - -bool CodedOutputStreamFieldSkipper::SkipMessage(io::CodedInputStream* input) { - return WireFormatLite::SkipMessage(input, unknown_fields_); -} - -void CodedOutputStreamFieldSkipper::SkipUnknownEnum(int field_number, - int value) { - unknown_fields_->WriteVarint32(field_number); - unknown_fields_->WriteVarint64(value); -} - -bool WireFormatLite::ReadPackedEnumPreserveUnknowns( - io::CodedInputStream* input, int field_number, bool (*is_valid)(int), - io::CodedOutputStream* unknown_fields_stream, RepeatedField* values) { - uint32 length; - if (!input->ReadVarint32(&length)) return false; - io::CodedInputStream::Limit limit = input->PushLimit(length); - while (input->BytesUntilLimit() > 0) { - int value; - if (!ReadPrimitive(input, &value)) { - return false; - } - if (is_valid == NULL || is_valid(value)) { - values->Add(value); - } else { - uint32 tag = WireFormatLite::MakeTag(field_number, - WireFormatLite::WIRETYPE_VARINT); - unknown_fields_stream->WriteVarint32(tag); - unknown_fields_stream->WriteVarint32(value); - } - } - input->PopLimit(limit); - return true; -} - -#if !defined(PROTOBUF_LITTLE_ENDIAN) - -namespace { -void EncodeFixedSizeValue(float v, uint8* dest) { - WireFormatLite::WriteFloatNoTagToArray(v, dest); -} - -void EncodeFixedSizeValue(double v, uint8* dest) { - WireFormatLite::WriteDoubleNoTagToArray(v, dest); -} - -void EncodeFixedSizeValue(uint32 v, uint8* dest) { - WireFormatLite::WriteFixed32NoTagToArray(v, dest); -} - -void EncodeFixedSizeValue(uint64 v, uint8* dest) { - WireFormatLite::WriteFixed64NoTagToArray(v, dest); -} - -void EncodeFixedSizeValue(int32 v, uint8* dest) { - WireFormatLite::WriteSFixed32NoTagToArray(v, dest); -} - -void EncodeFixedSizeValue(int64 v, uint8* dest) { - WireFormatLite::WriteSFixed64NoTagToArray(v, dest); -} - -void EncodeFixedSizeValue(bool v, uint8* dest) { - WireFormatLite::WriteBoolNoTagToArray(v, dest); -} -} // anonymous namespace - -#endif // !defined(PROTOBUF_LITTLE_ENDIAN) - -template -static void WriteArray(const CType* a, int n, io::CodedOutputStream* output) { -#if defined(PROTOBUF_LITTLE_ENDIAN) - output->WriteRaw(reinterpret_cast(a), n * sizeof(a[0])); -#else - const int kAtATime = 128; - uint8 buf[sizeof(CType) * kAtATime]; - for (int i = 0; i < n; i += kAtATime) { - int to_do = std::min(kAtATime, n - i); - uint8* ptr = buf; - for (int j = 0; j < to_do; j++) { - EncodeFixedSizeValue(a[i + j], ptr); - ptr += sizeof(a[0]); - } - output->WriteRaw(buf, to_do * sizeof(a[0])); - } -#endif -} - -void WireFormatLite::WriteFloatArray(const float* a, int n, - io::CodedOutputStream* output) { - WriteArray(a, n, output); -} - -void WireFormatLite::WriteDoubleArray(const double* a, int n, - io::CodedOutputStream* output) { - WriteArray(a, n, output); -} - -void WireFormatLite::WriteFixed32Array(const uint32* a, int n, - io::CodedOutputStream* output) { - WriteArray(a, n, output); -} - -void WireFormatLite::WriteFixed64Array(const uint64* a, int n, - io::CodedOutputStream* output) { - WriteArray(a, n, output); -} - -void WireFormatLite::WriteSFixed32Array(const int32* a, int n, - io::CodedOutputStream* output) { - WriteArray(a, n, output); -} - -void WireFormatLite::WriteSFixed64Array(const int64* a, int n, - io::CodedOutputStream* output) { - WriteArray(a, n, output); -} - -void WireFormatLite::WriteBoolArray(const bool* a, int n, - io::CodedOutputStream* output) { - WriteArray(a, n, output); -} - -void WireFormatLite::WriteInt32(int field_number, int32 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteInt32NoTag(value, output); -} -void WireFormatLite::WriteInt64(int field_number, int64 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteInt64NoTag(value, output); -} -void WireFormatLite::WriteUInt32(int field_number, uint32 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteUInt32NoTag(value, output); -} -void WireFormatLite::WriteUInt64(int field_number, uint64 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteUInt64NoTag(value, output); -} -void WireFormatLite::WriteSInt32(int field_number, int32 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteSInt32NoTag(value, output); -} -void WireFormatLite::WriteSInt64(int field_number, int64 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteSInt64NoTag(value, output); -} -void WireFormatLite::WriteFixed32(int field_number, uint32 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_FIXED32, output); - WriteFixed32NoTag(value, output); -} -void WireFormatLite::WriteFixed64(int field_number, uint64 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_FIXED64, output); - WriteFixed64NoTag(value, output); -} -void WireFormatLite::WriteSFixed32(int field_number, int32 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_FIXED32, output); - WriteSFixed32NoTag(value, output); -} -void WireFormatLite::WriteSFixed64(int field_number, int64 value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_FIXED64, output); - WriteSFixed64NoTag(value, output); -} -void WireFormatLite::WriteFloat(int field_number, float value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_FIXED32, output); - WriteFloatNoTag(value, output); -} -void WireFormatLite::WriteDouble(int field_number, double value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_FIXED64, output); - WriteDoubleNoTag(value, output); -} -void WireFormatLite::WriteBool(int field_number, bool value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteBoolNoTag(value, output); -} -void WireFormatLite::WriteEnum(int field_number, int value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_VARINT, output); - WriteEnumNoTag(value, output); -} - -void WireFormatLite::WriteString(int field_number, const std::string& value, - io::CodedOutputStream* output) { - // String is for UTF-8 text only - WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); - GOOGLE_CHECK_LE(value.size(), kint32max); - output->WriteVarint32(value.size()); - output->WriteString(value); -} -void WireFormatLite::WriteStringMaybeAliased(int field_number, - const std::string& value, - io::CodedOutputStream* output) { - // String is for UTF-8 text only - WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); - GOOGLE_CHECK_LE(value.size(), kint32max); - output->WriteVarint32(value.size()); - output->WriteRawMaybeAliased(value.data(), value.size()); -} -void WireFormatLite::WriteBytes(int field_number, const std::string& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); - GOOGLE_CHECK_LE(value.size(), kint32max); - output->WriteVarint32(value.size()); - output->WriteString(value); -} -void WireFormatLite::WriteBytesMaybeAliased(int field_number, - const std::string& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); - GOOGLE_CHECK_LE(value.size(), kint32max); - output->WriteVarint32(value.size()); - output->WriteRawMaybeAliased(value.data(), value.size()); -} - - -void WireFormatLite::WriteGroup(int field_number, const MessageLite& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_START_GROUP, output); - value.SerializeWithCachedSizes(output); - WriteTag(field_number, WIRETYPE_END_GROUP, output); -} - -void WireFormatLite::WriteMessage(int field_number, const MessageLite& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); - const int size = value.GetCachedSize(); - output->WriteVarint32(size); - value.SerializeWithCachedSizes(output); -} - -void WireFormatLite::WriteSubMessageMaybeToArray( - int size, const MessageLite& value, io::CodedOutputStream* output) { - output->SetCur(value._InternalSerialize(output->Cur(), output->EpsCopy())); -} - -void WireFormatLite::WriteGroupMaybeToArray(int field_number, - const MessageLite& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_START_GROUP, output); - const int size = value.GetCachedSize(); - WriteSubMessageMaybeToArray(size, value, output); - WriteTag(field_number, WIRETYPE_END_GROUP, output); -} - -void WireFormatLite::WriteMessageMaybeToArray(int field_number, - const MessageLite& value, - io::CodedOutputStream* output) { - WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output); - const int size = value.GetCachedSize(); - output->WriteVarint32(size); - WriteSubMessageMaybeToArray(size, value, output); -} - -PROTOBUF_ALWAYS_INLINE static bool ReadBytesToString( - io::CodedInputStream* input, std::string* value); -inline static bool ReadBytesToString(io::CodedInputStream* input, - std::string* value) { - uint32 length; - return input->ReadVarint32(&length) && input->ReadString(value, length); -} - -bool WireFormatLite::ReadBytes(io::CodedInputStream* input, - std::string* value) { - return ReadBytesToString(input, value); -} - -bool WireFormatLite::ReadBytes(io::CodedInputStream* input, std::string** p) { - if (*p == &GetEmptyStringAlreadyInited()) { - *p = new std::string(); - } - return ReadBytesToString(input, *p); -} - -void PrintUTF8ErrorLog(const char* field_name, const char* operation_str, - bool emit_stacktrace) { - std::string stacktrace; - std::string quoted_field_name = ""; - if (field_name != nullptr) { - quoted_field_name = StringPrintf(" '%s'", field_name); - } - GOOGLE_LOG(ERROR) << "String field" << quoted_field_name << " contains invalid " - << "UTF-8 data when " << operation_str << " a protocol " - << "buffer. Use the 'bytes' type if you intend to send raw " - << "bytes. " << stacktrace; -} - -bool WireFormatLite::VerifyUtf8String(const char* data, int size, Operation op, - const char* field_name) { - if (!IsStructurallyValidUTF8(data, size)) { - const char* operation_str = NULL; - switch (op) { - case PARSE: - operation_str = "parsing"; - break; - case SERIALIZE: - operation_str = "serializing"; - break; - // no default case: have the compiler warn if a case is not covered. - } - PrintUTF8ErrorLog(field_name, operation_str, false); - return false; - } - return true; -} - -// this code is deliberately written such that clang makes it into really -// efficient SSE code. -template -static size_t VarintSize(const T* data, const int n) { - static_assert(sizeof(T) == 4, "This routine only works for 32 bit integers"); - // is_unsigned => !ZigZag - static_assert( - (std::is_unsigned::value ^ ZigZag) || std::is_signed::value, - "Cannot ZigZag encode unsigned types"); - // is_unsigned => !SignExtended - static_assert( - (std::is_unsigned::value ^ SignExtended) || std::is_signed::value, - "Cannot SignExtended unsigned types"); - static_assert(!(SignExtended && ZigZag), - "Cannot SignExtended and ZigZag on the same type"); - uint32 sum = n; - uint32 msb_sum = 0; - for (int i = 0; i < n; i++) { - uint32 x = data[i]; - if (ZigZag) { - x = WireFormatLite::ZigZagEncode32(x); - } else if (SignExtended) { - msb_sum += x >> 31; - } - // clang is so smart that it produces optimal SSE sequence unrolling - // the loop 8 ints at a time. With a sequence of 4 - // cmpres = cmpgt x, sizeclass ( -1 or 0) - // sum = sum - cmpres - if (x > 0x7F) sum++; - if (x > 0x3FFF) sum++; - if (x > 0x1FFFFF) sum++; - if (x > 0xFFFFFFF) sum++; - } - if (SignExtended) sum += msb_sum * 5; - return sum; -} - -template -static size_t VarintSize64(const T* data, const int n) { - static_assert(sizeof(T) == 8, "This routine only works for 64 bit integers"); - // is_unsigned => !ZigZag - static_assert(!ZigZag || !std::is_unsigned::value, - "Cannot ZigZag encode unsigned types"); - uint64 sum = n; - for (int i = 0; i < n; i++) { - uint64 x = data[i]; - if (ZigZag) { - x = WireFormatLite::ZigZagEncode64(x); - } - // First step is a binary search, we can't branch in sse so we use the - // result of the compare to adjust sum and appropriately. This code is - // written to make clang recognize the vectorization. - uint64 tmp = x >= (static_cast(1) << 35) ? -1 : 0; - sum += 5 & tmp; - x >>= 35 & tmp; - if (x > 0x7F) sum++; - if (x > 0x3FFF) sum++; - if (x > 0x1FFFFF) sum++; - if (x > 0xFFFFFFF) sum++; - } - return sum; -} - -// GCC does not recognize the vectorization opportunity -// and other platforms are untested, in those cases using the optimized -// varint size routine for each element is faster. -// Hence we enable it only for clang -#if defined(__SSE__) && defined(__clang__) -size_t WireFormatLite::Int32Size(const RepeatedField& value) { - return VarintSize(value.data(), value.size()); -} - -size_t WireFormatLite::UInt32Size(const RepeatedField& value) { - return VarintSize(value.data(), value.size()); -} - -size_t WireFormatLite::SInt32Size(const RepeatedField& value) { - return VarintSize(value.data(), value.size()); -} - -size_t WireFormatLite::EnumSize(const RepeatedField& value) { - // On ILP64, sizeof(int) == 8, which would require a different template. - return VarintSize(value.data(), value.size()); -} - -#else // !(defined(__SSE4_1__) && defined(__clang__)) - -size_t WireFormatLite::Int32Size(const RepeatedField& value) { - size_t out = 0; - const int n = value.size(); - for (int i = 0; i < n; i++) { - out += Int32Size(value.Get(i)); - } - return out; -} - -size_t WireFormatLite::UInt32Size(const RepeatedField& value) { - size_t out = 0; - const int n = value.size(); - for (int i = 0; i < n; i++) { - out += UInt32Size(value.Get(i)); - } - return out; -} - -size_t WireFormatLite::SInt32Size(const RepeatedField& value) { - size_t out = 0; - const int n = value.size(); - for (int i = 0; i < n; i++) { - out += SInt32Size(value.Get(i)); - } - return out; -} - -size_t WireFormatLite::EnumSize(const RepeatedField& value) { - size_t out = 0; - const int n = value.size(); - for (int i = 0; i < n; i++) { - out += EnumSize(value.Get(i)); - } - return out; -} - -#endif - -// Micro benchmarks show that the SSE improved loop only starts beating -// the normal loop on Haswell platforms and then only for >32 ints. We -// disable this for now. Some specialized users might find it worthwhile to -// enable this. -#define USE_SSE_FOR_64_BIT_INTEGER_ARRAYS 0 -#if USE_SSE_FOR_64_BIT_INTEGER_ARRAYS -size_t WireFormatLite::Int64Size(const RepeatedField& value) { - return VarintSize64(value.data(), value.size()); -} - -size_t WireFormatLite::UInt64Size(const RepeatedField& value) { - return VarintSize64(value.data(), value.size()); -} - -size_t WireFormatLite::SInt64Size(const RepeatedField& value) { - return VarintSize64(value.data(), value.size()); -} - -#else - -size_t WireFormatLite::Int64Size(const RepeatedField& value) { - size_t out = 0; - const int n = value.size(); - for (int i = 0; i < n; i++) { - out += Int64Size(value.Get(i)); - } - return out; -} - -size_t WireFormatLite::UInt64Size(const RepeatedField& value) { - size_t out = 0; - const int n = value.size(); - for (int i = 0; i < n; i++) { - out += UInt64Size(value.Get(i)); - } - return out; -} - -size_t WireFormatLite::SInt64Size(const RepeatedField& value) { - size_t out = 0; - const int n = value.size(); - for (int i = 0; i < n; i++) { - out += SInt64Size(value.Get(i)); - } - return out; -} - -#endif - -} // namespace internal -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/zero_copy_stream.cc b/third_party/protobuf-lite/zero_copy_stream.cc deleted file mode 100644 index f81555e55..000000000 --- a/third_party/protobuf-lite/zero_copy_stream.cc +++ /dev/null @@ -1,55 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include - -#include -#include - -namespace google { -namespace protobuf { -namespace io { - - -bool ZeroCopyOutputStream::WriteAliasedRaw(const void* /* data */, - int /* size */) { - GOOGLE_LOG(FATAL) << "This ZeroCopyOutputStream doesn't support aliasing. " - "Reaching here usually means a ZeroCopyOutputStream " - "implementation bug."; - return false; -} - -} // namespace io -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/zero_copy_stream_impl.cc b/third_party/protobuf-lite/zero_copy_stream_impl.cc deleted file mode 100644 index 52617e9ef..000000000 --- a/third_party/protobuf-lite/zero_copy_stream_impl.cc +++ /dev/null @@ -1,366 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#ifndef _MSC_VER -#include -#include -#include -#include -#endif -#include - -#include -#include - -#include -#include -#include -#include -#include - - -namespace google { -namespace protobuf { -namespace io { - -#ifdef _WIN32 -// Win32 lseek is broken: If invoked on a non-seekable file descriptor, its -// return value is undefined. We re-define it to always produce an error. -#define lseek(fd, offset, origin) ((off_t)-1) -// DO NOT include , instead create functions in io_win32.{h,cc} and import -// them like we do below. -using google::protobuf::io::win32::access; -using google::protobuf::io::win32::close; -using google::protobuf::io::win32::open; -using google::protobuf::io::win32::read; -using google::protobuf::io::win32::write; -#endif - -namespace { - -// EINTR sucks. -int close_no_eintr(int fd) { - int result; - do { - result = close(fd); - } while (result < 0 && errno == EINTR); - return result; -} - -} // namespace - -// =================================================================== - -FileInputStream::FileInputStream(int file_descriptor, int block_size) - : copying_input_(file_descriptor), impl_(©ing_input_, block_size) {} - -bool FileInputStream::Close() { return copying_input_.Close(); } - -bool FileInputStream::Next(const void** data, int* size) { - return impl_.Next(data, size); -} - -void FileInputStream::BackUp(int count) { impl_.BackUp(count); } - -bool FileInputStream::Skip(int count) { return impl_.Skip(count); } - -int64_t FileInputStream::ByteCount() const { return impl_.ByteCount(); } - -FileInputStream::CopyingFileInputStream::CopyingFileInputStream( - int file_descriptor) - : file_(file_descriptor), - close_on_delete_(false), - is_closed_(false), - errno_(0), - previous_seek_failed_(false) {} - -FileInputStream::CopyingFileInputStream::~CopyingFileInputStream() { - if (close_on_delete_) { - if (!Close()) { - GOOGLE_LOG(ERROR) << "close() failed: " << strerror(errno_); - } - } -} - -bool FileInputStream::CopyingFileInputStream::Close() { - GOOGLE_CHECK(!is_closed_); - - is_closed_ = true; - if (close_no_eintr(file_) != 0) { - // The docs on close() do not specify whether a file descriptor is still - // open after close() fails with EIO. However, the glibc source code - // seems to indicate that it is not. - errno_ = errno; - return false; - } - - return true; -} - -int FileInputStream::CopyingFileInputStream::Read(void* buffer, int size) { - GOOGLE_CHECK(!is_closed_); - - int result; - do { - result = read(file_, buffer, size); - } while (result < 0 && errno == EINTR); - - if (result < 0) { - // Read error (not EOF). - errno_ = errno; - } - - return result; -} - -int FileInputStream::CopyingFileInputStream::Skip(int count) { - GOOGLE_CHECK(!is_closed_); - - if (!previous_seek_failed_ && lseek(file_, count, SEEK_CUR) != (off_t)-1) { - // Seek succeeded. - return count; - } else { - // Failed to seek. - - // Note to self: Don't seek again. This file descriptor doesn't - // support it. - previous_seek_failed_ = true; - - // Use the default implementation. - return CopyingInputStream::Skip(count); - } -} - -// =================================================================== - -FileOutputStream::FileOutputStream(int file_descriptor, int block_size) - : CopyingOutputStreamAdaptor(©ing_output_), - copying_output_(file_descriptor) {} - -bool FileOutputStream::Close() { - bool flush_succeeded = Flush(); - return copying_output_.Close() && flush_succeeded; -} - -FileOutputStream::CopyingFileOutputStream::CopyingFileOutputStream( - int file_descriptor) - : file_(file_descriptor), - close_on_delete_(false), - is_closed_(false), - errno_(0) {} - -FileOutputStream::~FileOutputStream() { Flush(); } - -FileOutputStream::CopyingFileOutputStream::~CopyingFileOutputStream() { - if (close_on_delete_) { - if (!Close()) { - GOOGLE_LOG(ERROR) << "close() failed: " << strerror(errno_); - } - } -} - -bool FileOutputStream::CopyingFileOutputStream::Close() { - GOOGLE_CHECK(!is_closed_); - - is_closed_ = true; - if (close_no_eintr(file_) != 0) { - // The docs on close() do not specify whether a file descriptor is still - // open after close() fails with EIO. However, the glibc source code - // seems to indicate that it is not. - errno_ = errno; - return false; - } - - return true; -} - -bool FileOutputStream::CopyingFileOutputStream::Write(const void* buffer, - int size) { - GOOGLE_CHECK(!is_closed_); - int total_written = 0; - - const uint8* buffer_base = reinterpret_cast(buffer); - - while (total_written < size) { - int bytes; - do { - bytes = write(file_, buffer_base + total_written, size - total_written); - } while (bytes < 0 && errno == EINTR); - - if (bytes <= 0) { - // Write error. - - // FIXME(kenton): According to the man page, if write() returns zero, - // there was no error; write() simply did not write anything. It's - // unclear under what circumstances this might happen, but presumably - // errno won't be set in this case. I am confused as to how such an - // event should be handled. For now I'm treating it as an error, since - // retrying seems like it could lead to an infinite loop. I suspect - // this never actually happens anyway. - - if (bytes < 0) { - errno_ = errno; - } - return false; - } - total_written += bytes; - } - - return true; -} - -// =================================================================== - -IstreamInputStream::IstreamInputStream(std::istream* input, int block_size) - : copying_input_(input), impl_(©ing_input_, block_size) {} - -bool IstreamInputStream::Next(const void** data, int* size) { - return impl_.Next(data, size); -} - -void IstreamInputStream::BackUp(int count) { impl_.BackUp(count); } - -bool IstreamInputStream::Skip(int count) { return impl_.Skip(count); } - -int64_t IstreamInputStream::ByteCount() const { return impl_.ByteCount(); } - -IstreamInputStream::CopyingIstreamInputStream::CopyingIstreamInputStream( - std::istream* input) - : input_(input) {} - -IstreamInputStream::CopyingIstreamInputStream::~CopyingIstreamInputStream() {} - -int IstreamInputStream::CopyingIstreamInputStream::Read(void* buffer, - int size) { - input_->read(reinterpret_cast(buffer), size); - int result = input_->gcount(); - if (result == 0 && input_->fail() && !input_->eof()) { - return -1; - } - return result; -} - -// =================================================================== - -OstreamOutputStream::OstreamOutputStream(std::ostream* output, int block_size) - : copying_output_(output), impl_(©ing_output_, block_size) {} - -OstreamOutputStream::~OstreamOutputStream() { impl_.Flush(); } - -bool OstreamOutputStream::Next(void** data, int* size) { - return impl_.Next(data, size); -} - -void OstreamOutputStream::BackUp(int count) { impl_.BackUp(count); } - -int64_t OstreamOutputStream::ByteCount() const { return impl_.ByteCount(); } - -OstreamOutputStream::CopyingOstreamOutputStream::CopyingOstreamOutputStream( - std::ostream* output) - : output_(output) {} - -OstreamOutputStream::CopyingOstreamOutputStream::~CopyingOstreamOutputStream() { -} - -bool OstreamOutputStream::CopyingOstreamOutputStream::Write(const void* buffer, - int size) { - output_->write(reinterpret_cast(buffer), size); - return output_->good(); -} - -// =================================================================== - -ConcatenatingInputStream::ConcatenatingInputStream( - ZeroCopyInputStream* const streams[], int count) - : streams_(streams), stream_count_(count), bytes_retired_(0) { -} - -bool ConcatenatingInputStream::Next(const void** data, int* size) { - while (stream_count_ > 0) { - if (streams_[0]->Next(data, size)) return true; - - // That stream is done. Advance to the next one. - bytes_retired_ += streams_[0]->ByteCount(); - ++streams_; - --stream_count_; - } - - // No more streams. - return false; -} - -void ConcatenatingInputStream::BackUp(int count) { - if (stream_count_ > 0) { - streams_[0]->BackUp(count); - } else { - GOOGLE_LOG(DFATAL) << "Can't BackUp() after failed Next()."; - } -} - -bool ConcatenatingInputStream::Skip(int count) { - while (stream_count_ > 0) { - // Assume that ByteCount() can be used to find out how much we actually - // skipped when Skip() fails. - int64 target_byte_count = streams_[0]->ByteCount() + count; - if (streams_[0]->Skip(count)) return true; - - // Hit the end of the stream. Figure out how many more bytes we still have - // to skip. - int64 final_byte_count = streams_[0]->ByteCount(); - GOOGLE_DCHECK_LT(final_byte_count, target_byte_count); - count = target_byte_count - final_byte_count; - - // That stream is done. Advance to the next one. - bytes_retired_ += final_byte_count; - ++streams_; - --stream_count_; - } - - return false; -} - -int64_t ConcatenatingInputStream::ByteCount() const { - if (stream_count_ == 0) { - return bytes_retired_; - } else { - return bytes_retired_ + streams_[0]->ByteCount(); - } -} - - -// =================================================================== - -} // namespace io -} // namespace protobuf -} // namespace google diff --git a/third_party/protobuf-lite/zero_copy_stream_impl_lite.cc b/third_party/protobuf-lite/zero_copy_stream_impl_lite.cc deleted file mode 100644 index 54c5db945..000000000 --- a/third_party/protobuf-lite/zero_copy_stream_impl_lite.cc +++ /dev/null @@ -1,467 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// 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 Inc. 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 THE COPYRIGHT -// OWNER OR CONTRIBUTORS 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. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include - -#include -#include - -#include -#include -#include -#include - -namespace google { -namespace protobuf { -namespace io { - -namespace { - -// Default block size for Copying{In,Out}putStreamAdaptor. -static const int kDefaultBlockSize = 8192; - -} // namespace - -// =================================================================== - -ArrayInputStream::ArrayInputStream(const void* data, int size, int block_size) - : data_(reinterpret_cast(data)), - size_(size), - block_size_(block_size > 0 ? block_size : size), - position_(0), - last_returned_size_(0) {} - -bool ArrayInputStream::Next(const void** data, int* size) { - if (position_ < size_) { - last_returned_size_ = std::min(block_size_, size_ - position_); - *data = data_ + position_; - *size = last_returned_size_; - position_ += last_returned_size_; - return true; - } else { - // We're at the end of the array. - last_returned_size_ = 0; // Don't let caller back up. - return false; - } -} - -void ArrayInputStream::BackUp(int count) { - GOOGLE_CHECK_GT(last_returned_size_, 0) - << "BackUp() can only be called after a successful Next()."; - GOOGLE_CHECK_LE(count, last_returned_size_); - GOOGLE_CHECK_GE(count, 0); - position_ -= count; - last_returned_size_ = 0; // Don't let caller back up further. -} - -bool ArrayInputStream::Skip(int count) { - GOOGLE_CHECK_GE(count, 0); - last_returned_size_ = 0; // Don't let caller back up. - if (count > size_ - position_) { - position_ = size_; - return false; - } else { - position_ += count; - return true; - } -} - -int64_t ArrayInputStream::ByteCount() const { return position_; } - - -// =================================================================== - -ArrayOutputStream::ArrayOutputStream(void* data, int size, int block_size) - : data_(reinterpret_cast(data)), - size_(size), - block_size_(block_size > 0 ? block_size : size), - position_(0), - last_returned_size_(0) {} - -bool ArrayOutputStream::Next(void** data, int* size) { - if (position_ < size_) { - last_returned_size_ = std::min(block_size_, size_ - position_); - *data = data_ + position_; - *size = last_returned_size_; - position_ += last_returned_size_; - return true; - } else { - // We're at the end of the array. - last_returned_size_ = 0; // Don't let caller back up. - return false; - } -} - -void ArrayOutputStream::BackUp(int count) { - GOOGLE_CHECK_GT(last_returned_size_, 0) - << "BackUp() can only be called after a successful Next()."; - GOOGLE_CHECK_LE(count, last_returned_size_); - GOOGLE_CHECK_GE(count, 0); - position_ -= count; - last_returned_size_ = 0; // Don't let caller back up further. -} - -int64_t ArrayOutputStream::ByteCount() const { return position_; } - -// =================================================================== - -StringOutputStream::StringOutputStream(std::string* target) : target_(target) {} - -bool StringOutputStream::Next(void** data, int* size) { - GOOGLE_CHECK(target_ != NULL); - size_t old_size = target_->size(); - - // Grow the string. - size_t new_size; - if (old_size < target_->capacity()) { - // Resize the string to match its capacity, since we can get away - // without a memory allocation this way. - new_size = target_->capacity(); - } else { - // Size has reached capacity, try to double it. - new_size = old_size * 2; - } - // Avoid integer overflow in returned '*size'. - new_size = std::min(new_size, old_size + std::numeric_limits::max()); - // Increase the size, also make sure that it is at least kMinimumSize. - STLStringResizeUninitialized( - target_, - std::max(new_size, - kMinimumSize + 0)); // "+ 0" works around GCC4 weirdness. - - *data = mutable_string_data(target_) + old_size; - *size = target_->size() - old_size; - return true; -} - -void StringOutputStream::BackUp(int count) { - GOOGLE_CHECK_GE(count, 0); - GOOGLE_CHECK(target_ != NULL); - GOOGLE_CHECK_LE(count, target_->size()); - target_->resize(target_->size() - count); -} - -int64_t StringOutputStream::ByteCount() const { - GOOGLE_CHECK(target_ != NULL); - return target_->size(); -} - -// =================================================================== - -int CopyingInputStream::Skip(int count) { - char junk[4096]; - int skipped = 0; - while (skipped < count) { - int bytes = Read(junk, std::min(count - skipped, - implicit_cast(sizeof(junk)))); - if (bytes <= 0) { - // EOF or read error. - return skipped; - } - skipped += bytes; - } - return skipped; -} - -CopyingInputStreamAdaptor::CopyingInputStreamAdaptor( - CopyingInputStream* copying_stream, int block_size) - : copying_stream_(copying_stream), - owns_copying_stream_(false), - failed_(false), - position_(0), - buffer_size_(block_size > 0 ? block_size : kDefaultBlockSize), - buffer_used_(0), - backup_bytes_(0) {} - -CopyingInputStreamAdaptor::~CopyingInputStreamAdaptor() { - if (owns_copying_stream_) { - delete copying_stream_; - } -} - -bool CopyingInputStreamAdaptor::Next(const void** data, int* size) { - if (failed_) { - // Already failed on a previous read. - return false; - } - - AllocateBufferIfNeeded(); - - if (backup_bytes_ > 0) { - // We have data left over from a previous BackUp(), so just return that. - *data = buffer_.get() + buffer_used_ - backup_bytes_; - *size = backup_bytes_; - backup_bytes_ = 0; - return true; - } - - // Read new data into the buffer. - buffer_used_ = copying_stream_->Read(buffer_.get(), buffer_size_); - if (buffer_used_ <= 0) { - // EOF or read error. We don't need the buffer anymore. - if (buffer_used_ < 0) { - // Read error (not EOF). - failed_ = true; - } - FreeBuffer(); - return false; - } - position_ += buffer_used_; - - *size = buffer_used_; - *data = buffer_.get(); - return true; -} - -void CopyingInputStreamAdaptor::BackUp(int count) { - GOOGLE_CHECK(backup_bytes_ == 0 && buffer_.get() != NULL) - << " BackUp() can only be called after Next()."; - GOOGLE_CHECK_LE(count, buffer_used_) - << " Can't back up over more bytes than were returned by the last call" - " to Next()."; - GOOGLE_CHECK_GE(count, 0) << " Parameter to BackUp() can't be negative."; - - backup_bytes_ = count; -} - -bool CopyingInputStreamAdaptor::Skip(int count) { - GOOGLE_CHECK_GE(count, 0); - - if (failed_) { - // Already failed on a previous read. - return false; - } - - // First skip any bytes left over from a previous BackUp(). - if (backup_bytes_ >= count) { - // We have more data left over than we're trying to skip. Just chop it. - backup_bytes_ -= count; - return true; - } - - count -= backup_bytes_; - backup_bytes_ = 0; - - int skipped = copying_stream_->Skip(count); - position_ += skipped; - return skipped == count; -} - -int64_t CopyingInputStreamAdaptor::ByteCount() const { - return position_ - backup_bytes_; -} - -void CopyingInputStreamAdaptor::AllocateBufferIfNeeded() { - if (buffer_.get() == NULL) { - buffer_.reset(new uint8[buffer_size_]); - } -} - -void CopyingInputStreamAdaptor::FreeBuffer() { - GOOGLE_CHECK_EQ(backup_bytes_, 0); - buffer_used_ = 0; - buffer_.reset(); -} - -// =================================================================== - -CopyingOutputStreamAdaptor::CopyingOutputStreamAdaptor( - CopyingOutputStream* copying_stream, int block_size) - : copying_stream_(copying_stream), - owns_copying_stream_(false), - failed_(false), - position_(0), - buffer_size_(block_size > 0 ? block_size : kDefaultBlockSize), - buffer_used_(0) {} - -CopyingOutputStreamAdaptor::~CopyingOutputStreamAdaptor() { - WriteBuffer(); - if (owns_copying_stream_) { - delete copying_stream_; - } -} - -bool CopyingOutputStreamAdaptor::Flush() { return WriteBuffer(); } - -bool CopyingOutputStreamAdaptor::Next(void** data, int* size) { - if (buffer_used_ == buffer_size_) { - if (!WriteBuffer()) return false; - } - - AllocateBufferIfNeeded(); - - *data = buffer_.get() + buffer_used_; - *size = buffer_size_ - buffer_used_; - buffer_used_ = buffer_size_; - return true; -} - -void CopyingOutputStreamAdaptor::BackUp(int count) { - GOOGLE_CHECK_GE(count, 0); - GOOGLE_CHECK_EQ(buffer_used_, buffer_size_) - << " BackUp() can only be called after Next()."; - GOOGLE_CHECK_LE(count, buffer_used_) - << " Can't back up over more bytes than were returned by the last call" - " to Next()."; - - buffer_used_ -= count; -} - -int64_t CopyingOutputStreamAdaptor::ByteCount() const { - return position_ + buffer_used_; -} - -bool CopyingOutputStreamAdaptor::WriteAliasedRaw(const void* data, int size) { - if (size >= buffer_size_) { - if (!Flush() || !copying_stream_->Write(data, size)) { - return false; - } - GOOGLE_DCHECK_EQ(buffer_used_, 0); - position_ += size; - return true; - } - - void* out; - int out_size; - while (true) { - if (!Next(&out, &out_size)) { - return false; - } - - if (size <= out_size) { - std::memcpy(out, data, size); - BackUp(out_size - size); - return true; - } - - std::memcpy(out, data, out_size); - data = static_cast(data) + out_size; - size -= out_size; - } - return true; -} - - -bool CopyingOutputStreamAdaptor::WriteBuffer() { - if (failed_) { - // Already failed on a previous write. - return false; - } - - if (buffer_used_ == 0) return true; - - if (copying_stream_->Write(buffer_.get(), buffer_used_)) { - position_ += buffer_used_; - buffer_used_ = 0; - return true; - } else { - failed_ = true; - FreeBuffer(); - return false; - } -} - -void CopyingOutputStreamAdaptor::AllocateBufferIfNeeded() { - if (buffer_ == NULL) { - buffer_.reset(new uint8[buffer_size_]); - } -} - -void CopyingOutputStreamAdaptor::FreeBuffer() { - buffer_used_ = 0; - buffer_.reset(); -} - -// =================================================================== - -LimitingInputStream::LimitingInputStream(ZeroCopyInputStream* input, - int64 limit) - : input_(input), limit_(limit) { - prior_bytes_read_ = input_->ByteCount(); -} - -LimitingInputStream::~LimitingInputStream() { - // If we overshot the limit, back up. - if (limit_ < 0) input_->BackUp(-limit_); -} - -bool LimitingInputStream::Next(const void** data, int* size) { - if (limit_ <= 0) return false; - if (!input_->Next(data, size)) return false; - - limit_ -= *size; - if (limit_ < 0) { - // We overshot the limit. Reduce *size to hide the rest of the buffer. - *size += limit_; - } - return true; -} - -void LimitingInputStream::BackUp(int count) { - if (limit_ < 0) { - input_->BackUp(count - limit_); - limit_ = count; - } else { - input_->BackUp(count); - limit_ += count; - } -} - -bool LimitingInputStream::Skip(int count) { - if (count > limit_) { - if (limit_ < 0) return false; - input_->Skip(limit_); - limit_ = 0; - return false; - } else { - if (!input_->Skip(count)) return false; - limit_ -= count; - return true; - } -} - -int64_t LimitingInputStream::ByteCount() const { - if (limit_ < 0) { - return input_->ByteCount() + limit_ - prior_bytes_read_; - } else { - return input_->ByteCount() - prior_bytes_read_; - } -} - - -// =================================================================== - -} // namespace io -} // namespace protobuf -} // namespace google diff --git a/third_party/upb/LICENSE b/third_party/upb/LICENSE new file mode 100644 index 000000000..bd79a1223 --- /dev/null +++ b/third_party/upb/LICENSE @@ -0,0 +1,25 @@ +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 any other + contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``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. diff --git a/third_party/upb/google/protobuf/arena.h b/third_party/upb/google/protobuf/arena.h new file mode 100644 index 000000000..f0c1516a7 --- /dev/null +++ b/third_party/upb/google/protobuf/arena.h @@ -0,0 +1,56 @@ +// Copyright 2016 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GOOGLE_PROTOBUF_ARENA_H_ +#define GOOGLE_PROTOBUF_ARENA_H_ + +#include +#include +#include + +namespace google { +namespace protobuf { + +class Arena { + public: + Arena() {} + ~Arena() { + for (auto& cleanup : cleanups_) { + cleanup(); + } + } + + template + static T* Create(Arena* arena, Args&&... args) { + if (arena) { + T* ptr = new T(std::forward(args)...); + arena->cleanups_.push_back([ptr]() { delete ptr; }); + return ptr; + } else { + return new T(std::forward(args)...); + } + } + + template + static T* CreateMaybeMessage(Arena* arena, Args&&... args) { + return Create(arena, std::forward(args)...); + } + private: + std::vector> cleanups_; +}; + +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_ARENA_H_ diff --git a/third_party/upb/google/protobuf/message_lite.h b/third_party/upb/google/protobuf/message_lite.h new file mode 100644 index 000000000..d2accbfb3 --- /dev/null +++ b/third_party/upb/google/protobuf/message_lite.h @@ -0,0 +1,26 @@ +// Copyright 2016 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H_ +#define GOOGLE_PROTOBUF_MESSAGE_LITE_H_ + +namespace google { +namespace protobuf { + +inline void ShutdownProtobufLibrary() {} + +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H_ diff --git a/third_party/upb/upb.c b/third_party/upb/upb.c new file mode 100644 index 000000000..2d0421eec --- /dev/null +++ b/third_party/upb/upb.c @@ -0,0 +1,15419 @@ +/* Amalgamated source file */ +#include "upb.h" + +/* + * This is where we define internal portability macros used across upb. + * + * All of these macros are undef'd in undef.inc to avoid leaking them to users. + * + * The correct usage is: + * + * #include "upb/foobar.h" + * #include "upb/baz.h" + * + * // MUST be last included header. + * #include "upb/port/def.inc" + * + * // Code for this file. + * // <...> + * + * // Can be omitted for .c files, required for .h. + * #include "upb/port/undef.inc" + * + * This file is private and must not be included by users! + */ + +#if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ + (defined(__cplusplus) && __cplusplus >= 201703L) || \ + (defined(_MSC_VER) && _MSC_VER >= 1900)) +#error upb requires C99 or C++17 or MSVC >= 2015. +#endif + +// Portable check for GCC minimum version: +// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html +#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) +#define UPB_GNUC_MIN(x, y) \ + (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y)) +#else +#define UPB_GNUC_MIN(x, y) 0 +#endif + +// Macros for checking for compiler attributes, defined here to avoid the +// problem described in +// https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html. +#ifdef __has_attribute +#define UPB_HAS_ATTRIBUTE(x) __has_attribute(x) +#else +#define UPB_HAS_ATTRIBUTE(x) 0 +#endif + +#if defined(__cplusplus) && defined(__has_cpp_attribute) +// NOTE: requiring __cplusplus above should not be necessary, but +// works around https://bugs.llvm.org/show_bug.cgi?id=23435. +#define UPB_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) +#else +#define UPB_HAS_CPP_ATTRIBUTE(x) 0 +#endif + +// Once in a while we want to use this macro in a C++-only portion of an +// otherwise C-compatible header, so we copy and paste this from ABSL. +#if UPB_HAS_CPP_ATTRIBUTE(deprecated) && UPB_HAS_CPP_ATTRIBUTE(clang::annotate) +#define UPB_DEPRECATE_AND_INLINE() [[deprecated, clang::annotate("inline-me")]] +#elif UPB_HAS_CPP_ATTRIBUTE(deprecated) +#define UPB_DEPRECATE_AND_INLINE() [[deprecated]] +#else +#define UPB_DEPRECATE_AND_INLINE() +#endif + +#ifdef __has_builtin +#define UPB_HAS_BUILTIN(x) __has_builtin(x) +#else +#define UPB_HAS_BUILTIN(x) 0 +#endif + +#ifdef __has_extension +#define UPB_HAS_EXTENSION(x) __has_extension(x) +#else +#define UPB_HAS_EXTENSION(x) 0 +#endif + +#ifdef __has_feature +#define UPB_HAS_FEATURE(x) __has_feature(x) +#else +#define UPB_HAS_FEATURE(x) 0 +#endif + +#include +#include +#include +#include +#include +#include + +#ifndef UINTPTR_MAX +Error, UINTPTR_MAX is undefined +#endif + +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif + +/* If we always read/write as a consistent type to each address, this shouldn't + * violate aliasing. + */ +#define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs))) + +// A flexible array member may have lower alignment requirements than the struct +// overall - in that case, it can overlap with the trailing padding of the rest +// of the struct, and a naive sizeof(base) + sizeof(flex) * count calculation +// will not take into account that overlap, and allocate more than is required. +#define UPB_SIZEOF_FLEX(type, member, count) \ + UPB_MAX(sizeof(type), offsetof(type, member[count])) + +#define UPB_SIZEOF_FLEX_WOULD_OVERFLOW(type, member, count) \ + (((SIZE_MAX - offsetof(type, member[0])) / \ + (offsetof(type, member[1]) - offsetof(type, member[0]))) < (size_t)count) + +#define UPB_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + +#define UPB_MAPTYPE_STRING 0 + +// UPB_EXPORT: always generate a public symbol. +#if defined(__GNUC__) || defined(__clang__) +#define UPB_EXPORT __attribute__((visibility("default"))) __attribute__((used)) +#else +#define UPB_EXPORT +#endif + +// UPB_INLINE: inline if possible, emit standalone code if required. +#ifdef __cplusplus +#define UPB_INLINE inline +#elif defined(__GNUC__) || defined(__clang__) +#define UPB_INLINE static __inline__ +#else +#define UPB_INLINE static +#endif + +// UPB_INLINE_IF_NOT_GCC: because gcc can be very noisy at times. +#if defined(__GNUC__) && !defined(__clang__) +#define UPB_INLINE_IF_NOT_GCC static +#else +#define UPB_INLINE_IF_NOT_GCC UPB_INLINE +#endif + +#ifdef UPB_BUILD_API +#define UPB_API UPB_EXPORT +#define UPB_API_INLINE UPB_EXPORT +#else +#define UPB_API +#define UPB_API_INLINE UPB_INLINE +#endif + +#ifdef EXPORT_UPBC +#define UPBC_API UPB_EXPORT +#else +#define UPBC_API +#endif + +#if UPB_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__) +#define UPB_ASAN 1 +#else +#define UPB_ASAN 0 +#endif + +#if UPB_HAS_FEATURE(hwaddress_sanitizer) +#define UPB_HWASAN 1 +#define UPB_HWASAN_POISON_TAG 17 +#define UPB_MALLOC_ALIGN 16 +#else +#define UPB_HWASAN 0 +#define UPB_MALLOC_ALIGN 8 +#endif + +#if UPB_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__) +#define UPB_TSAN 1 +#else +#define UPB_TSAN 0 +#endif + +#if UPB_HAS_FEATURE(memory_sanitizer) +#define UPB_MSAN 1 +#else +#define UPB_MSAN 0 +#endif + +// An unfortunate concession to C++17 and MSVC, which don't support zero-sized +// structs. +#if UPB_ASAN || UPB_HWASAN || UPB_TSAN +#define UPB_XSAN_MEMBER upb_Xsan xsan; +#define UPB_XSAN(st) (&(st)->xsan) +#define UPB_XSAN_STRUCT_SIZE 1 +#else +#define UPB_XSAN_MEMBER +#define UPB_XSAN(st) (NULL) +#define UPB_XSAN_STRUCT_SIZE 0 +#endif + +#define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align)) +#define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align)) +#define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, UPB_MALLOC_ALIGN) + +#if __STDC_VERSION__ >= 202311L || UPB_HAS_EXTENSION(cxx_alignof) || \ + defined(__cplusplus) +#define UPB_ALIGN_OF(type) alignof(type) +#elif __STDC_VERSION__ >= 201112L || UPB_HAS_EXTENSION(c_alignof) +#define UPB_ALIGN_OF(type) _Alignof(type) +#elif UPB_GNUC_MIN(2, 95) +#define UPB_ALIGN_OF(type) __alignof__(type) +#elif defined(_MSC_VER) +#define UPB_ALIGN_OF(type) __alignof(type) +#else +#define UPB_ALIGN_OF(type) \ + offsetof( \ + struct { \ + char c; \ + type member; \ + }, \ + member) +#endif + +#ifdef _MSC_VER +// Some versions of our Windows compiler don't support the C11 syntax. +#define UPB_ALIGN_AS(x) __declspec(align(x)) +#elif defined(__GNUC__) +#define UPB_ALIGN_AS(x) __attribute__((aligned(x))) +#else +#define UPB_ALIGN_AS(x) _Alignas(x) +#endif + +#if __STDC_VERSION__ >= 202311L || UPB_HAS_EXTENSION(cxx_static_assert) || \ + defined(__cplusplus) +#define UPB_STATIC_ASSERT(val, msg) static_assert((val), msg) +#elif __STDC_VERSION__ >= 201112L || UPB_HAS_EXTENSION(c_static_assert) || \ + UPB_GNUC_MIN(4, 6) +#define UPB_STATIC_ASSERT(val, msg) _Static_assert((val), msg) +#else +// Unfortunately this hack doesn't work inside struct declarations, but it works +// everywhere else +#define UPB_STATIC_ASSERT_CONCAT_IMPL(s1, s2) s1##s2 +#define UPB_STATIC_ASSERT_CONCAT(s1, s2) UPB_STATIC_ASSERT_CONCAT_IMPL(s1, s2) +#ifdef __COUNTER__ +#define UPB_STATIC_ASSERT(condition, message) \ + typedef char UPB_STATIC_ASSERT_CONCAT(static_assertion_failure_, \ + __COUNTER__)[(condition) ? 1 : -1] +#else +#define UPB_STATIC_ASSERT(condition, message) \ + typedef char UPB_STATIC_ASSERT_CONCAT(static_assertion_failure_, \ + __LINE__)[(condition) ? 1 : -1] +#endif +#endif + +// Hints to the compiler about likely/unlikely branches. +#if defined(__GNUC__) || defined(__clang__) +#define UPB_LIKELY(x) __builtin_expect((bool)(x), 1) +#define UPB_UNLIKELY(x) __builtin_expect((bool)(x), 0) +#else +#define UPB_LIKELY(x) (x) +#define UPB_UNLIKELY(x) (x) +#endif + +#if UPB_HAS_BUILTIN(__builtin_expect_with_probability) +#define UPB_UNPREDICTABLE(x) \ + __builtin_expect_with_probability((bool)(x), 1, 0.5) +#else +#define UPB_UNPREDICTABLE(x) (x) +#endif + +// Macros for function attributes on compilers that support them. +#if defined(__GNUC__) || defined(__clang__) +#define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) static +#define UPB_NOINLINE __attribute__((noinline)) +#define UPB_NORETURN __attribute__((__noreturn__)) +#define UPB_PRINTF(str, first_vararg) \ + __attribute__((format(printf, str, first_vararg))) +#elif defined(_MSC_VER) +#define UPB_NOINLINE +#define UPB_FORCEINLINE static +#define UPB_NORETURN __declspec(noreturn) +#define UPB_PRINTF(str, first_vararg) +#else /* !defined(__GNUC__) */ +#define UPB_FORCEINLINE static +#define UPB_NOINLINE +#define UPB_NORETURN +#define UPB_PRINTF(str, first_vararg) +#endif + +#if defined(__clang__) +#define UPB_NODEREF __attribute__((noderef)) +#else +#define UPB_NODEREF +#endif + +#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) +#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) + +#define UPB_UNUSED(var) (void)(var) + +// UPB_ASSUME(): in release mode, we tell the compiler to assume this is true. +#ifdef NDEBUG +#ifdef __GNUC__ +#define UPB_ASSUME(expr) \ + if (!(expr)) __builtin_unreachable() +#elif defined _MSC_VER +#define UPB_ASSUME(expr) \ + if (!(expr)) __assume(0) +#else +#define UPB_ASSUME(expr) \ + do { \ + } while (false && (expr)) +#endif +#else +#define UPB_ASSUME(expr) assert(expr) +#endif + +#if UPB_HAS_BUILTIN(__builtin_constant_p) && UPB_HAS_ATTRIBUTE(const) +#define UPB_MAYBE_ASSUME(pred, x) \ + if (__builtin_constant_p(pred) && pred) UPB_ASSUME(x) +#define UPB_ATTR_CONST __attribute__((const)) +#else +#define UPB_MAYBE_ASSUME(pred, x) +#define UPB_ATTR_CONST +#endif + +/* UPB_ASSERT(): in release mode, we use the expression without letting it be + * evaluated. This prevents "unused variable" warnings. */ +#ifdef NDEBUG +#define UPB_ASSERT(expr) \ + do { \ + } while (false && (expr)) +#else +#define UPB_ASSERT(expr) assert(expr) +#endif + +#if !defined(NDEBUG) && !defined(UPB_TSAN) +#define UPB_ENABLE_REF_CYCLE_CHECKS 1 +#else +#define UPB_ENABLE_REF_CYCLE_CHECKS 0 +#endif + +#if defined(__GNUC__) || defined(__clang__) +#define UPB_UNREACHABLE() \ + do { \ + assert(0); \ + __builtin_unreachable(); \ + } while (0) +#elif defined(_MSC_VER) +#define UPB_UNREACHABLE() \ + do { \ + assert(0); \ + __assume(0); \ + } while (0) +#else +#define UPB_UNREACHABLE() \ + do { \ + assert(0); \ + } while (0) +#endif + +#ifdef __ANDROID__ +#define UPB_DEFAULT_MAX_BLOCK_SIZE 8192 +#else +#define UPB_DEFAULT_MAX_BLOCK_SIZE 32768 +#endif + +/* UPB_SETJMP() / UPB_LONGJMP() */ +// Android uses a custom libc that does not implement all of posix, but it has +// had sigsetjmp/siglongjmp forever on arm and since API 12 on x86. Apple has +// sigsetjmp, but does not define the posix feature test macro. +#if defined(__APPLE__) || defined(_POSIX_C_SOURCE) || defined(__ANDROID__) +// avoid setting/restoring signal mask, which involves costly syscalls +#define UPB_SETJMP(buf) sigsetjmp(buf, 0) +#define UPB_LONGJMP(buf, val) siglongjmp(buf, val) +#elif defined(WASM_WAMR) +#define UPB_SETJMP(buf) 0 +#define UPB_LONGJMP(buf, val) abort() +#else +#define UPB_SETJMP(buf) setjmp(buf) +#define UPB_LONGJMP(buf, val) longjmp(buf, val) +#endif + +#if ((__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)) || \ + UPB_HAS_EXTENSION(c_atomic) || \ + defined(__GNUC__) // GCC supported atomics as an extension before it + // supported __has_extension +#define UPB_USE_C11_ATOMICS +#elif defined(_MSC_VER) +#define UPB_USE_MSC_ATOMICS +#endif + +#if defined(UPB_USE_C11_ATOMICS) +#define UPB_ATOMIC(T) _Atomic(T) +#elif defined(UPB_USE_MSC_ATOMICS) +#define UPB_ATOMIC(T) volatile T +#else +#define UPB_ATOMIC(T) T +#endif + +/* UPB_PTRADD(ptr, ofs): add pointer while avoiding "NULL + 0" UB */ +#define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr)) + +#define UPB_PRIVATE(x) x##_dont_copy_me__upb_internal_use_only + +#ifdef UPB_ALLOW_PRIVATE_ACCESS__FOR_BITS_ONLY +#define UPB_ONLYBITS(x) x +#else +#define UPB_ONLYBITS(x) UPB_PRIVATE(x) +#endif + +/* Configure whether fasttable is switched on or not. *************************/ + +#if UPB_HAS_ATTRIBUTE(musttail) +#define UPB_MUSTTAIL __attribute__((musttail)) +#else +#define UPB_MUSTTAIL +#endif + +#if UPB_HAS_ATTRIBUTE(preserve_most) && !defined(__i386__) +#define UPB_PRESERVE_MOST __attribute__((preserve_most)) +#else +#define UPB_PRESERVE_MOST +#endif + +#if UPB_HAS_ATTRIBUTE(preserve_none) +#define UPB_PRESERVE_NONE __attribute__((preserve_none)) +#else +#define UPB_PRESERVE_NONE +#endif + +#if defined(__aarch64__) && (defined(__GNUC__) || defined(__clang__)) +#define UPB_ARM64_ASM 1 +#else +#define UPB_ARM64_ASM 0 +#endif + +/* When compiling with branch protection, we need to ensure that all branch + * targets in assembly use the appropriate landing pad instruction. These + * instructions are backwards compatible with processors that don't have + * FEAT_BTI and are treated as nops. + */ +#if UPB_ARM64_ASM && defined(__ARM_FEATURE_BTI_DEFAULT) +#if __ARM_FEATURE_BTI_DEFAULT == 1 +#define UPB_ARM64_BTI_DEFAULT 1 +#else +#define UPB_ARM64_BTI_DEFAULT 0 +#endif +#else +#define UPB_ARM64_BTI_DEFAULT 0 +#endif + +/* This check is not fully robust: it does not require that we have "musttail" + * support available. We need tail calls to avoid consuming arbitrary amounts + * of stack space. + * + * GCC/Clang can mostly be trusted to generate tail calls as long as + * optimization is enabled, but, debug builds will not generate tail calls + * unless "musttail" is available. + * + * We should probably either: + * 1. require that the compiler supports musttail. + * 2. add some fallback code for when musttail isn't available (ie. return + * instead of tail calling). This is safe and portable, but this comes at + * a CPU cost. + */ +#if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__) +#define UPB_FASTTABLE_SUPPORTED 1 +#else +#define UPB_FASTTABLE_SUPPORTED 0 +#endif + +/* define UPB_ENABLE_FASTTABLE to force fast table support. + * This is useful when we want to ensure we are really getting fasttable, + * for example for testing or benchmarking. */ +#if defined(UPB_ENABLE_FASTTABLE) +#if !UPB_FASTTABLE_SUPPORTED +#error fasttable is x86-64/ARM64 only and requires GCC or Clang. +#endif +#define UPB_FASTTABLE 1 +/* Define UPB_TRY_ENABLE_FASTTABLE to use fasttable if possible. + * This is useful for releasing code that might be used on multiple platforms, + * for example the PHP or Ruby C extensions. */ +#elif defined(UPB_TRY_ENABLE_FASTTABLE) +#define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED +#else +#define UPB_FASTTABLE 0 +#endif + +/* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully + * 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 + +#if defined(__cplusplus) +#if defined(__clang__) || UPB_GNUC_MIN(6, 0) +// https://gcc.gnu.org/gcc-6/changes.html +#define UPB_DEPRECATED [[deprecated]] +#else +#define UPB_DEPRECATED +#endif +#else +#define UPB_DEPRECATED +#endif + +#if defined(UPB_IS_GOOGLE3) && \ + (!defined(UPB_BOOTSTRAP_STAGE) || UPB_BOOTSTRAP_STAGE != 0) +#define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init +#elif defined(UPB_IS_GOOGLE3) && defined(UPB_BOOTSTRAP_STAGE) && \ + UPB_BOOTSTRAP_STAGE == 0 +#define UPB_DESC_MINITABLE(sym) proto2__##sym##_msg_init() +#elif defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0 +#define UPB_DESC_MINITABLE(sym) google__protobuf__##sym##_msg_init() +#else +#define UPB_DESC_MINITABLE(sym) &google__protobuf__##sym##_msg_init +#endif + +#undef UPB_IS_GOOGLE3 + +#ifdef __clang__ +#define UPB_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("address"))) +#else +#define UPB_NO_SANITIZE_ADDRESS +#endif + +#if defined(__GNUC__) && (defined(__clang__) || UPB_GNUC_MIN(11, 0)) +#define UPB_RETAIN __attribute__((retain)) +#else +#define UPB_RETAIN +#endif + +// Linker arrays combine elements from multiple translation units into a single +// array that can be iterated over at runtime. +// +// It is an alternative to pre-main "registration" functions. +// +// Usage: +// +// // In N translation units. +// UPB_LINKARR_APPEND(foo_array) static int elems[3] = {1, 2, 3}; +// +// // At runtime: +// UPB_LINKARR_DECLARE(foo_array, int); +// +// void f() { +// const int* start = UPB_LINKARR_START(foo_array); +// const int* stop = UPB_LINKARR_STOP(foo_array); +// for (const int* p = start; p < stop; p++) { +// // Windows can introduce zero padding, so we have to skip zeroes. +// if (*p != 0) { +// vec.push_back(*p); +// } +// } +// } + +#if defined(__GNUC__) && !defined(__clang__) +// GCC can't handle mismatched retain attributes in the same section: +// https://github.com/protocolbuffers/protobuf/issues/26385 +// To work around this, we retain all linker array elements, even though this +// effectively disables tree-shaking of unused extensions when using GCC. +#define UPB_LINKARR_ATTR UPB_RETAIN +#else +#define UPB_LINKARR_ATTR +#endif + +#define UPB_LINKARR_SENTINEL UPB_RETAIN __attribute__((weak, used)) + +#if defined(__ELF__) || defined(__wasm__) + +#define UPB_LINKARR_APPEND(name) \ + __attribute__(( \ + section("linkarr_" #name))) UPB_LINKARR_ATTR UPB_NO_SANITIZE_ADDRESS +#define UPB_LINKARR_DECLARE(name, type) \ + extern type __start_linkarr_##name; \ + extern type __stop_linkarr_##name; \ + UPB_LINKARR_APPEND(name) \ + UPB_LINKARR_SENTINEL type UPB_linkarr_internal_empty_##name[1] +#define UPB_LINKARR_START(name) (&__start_linkarr_##name) +#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name) + +#elif defined(__MACH__) + +/* As described in: https://stackoverflow.com/a/22366882 */ +#define UPB_LINKARR_APPEND(name) \ + __attribute__(( \ + section("__DATA,__la_" #name))) UPB_LINKARR_ATTR UPB_NO_SANITIZE_ADDRESS +#define UPB_LINKARR_DECLARE(name, type) \ + extern type __start_linkarr_##name __asm( \ + "section$start$__DATA$__la_" #name); \ + extern type __stop_linkarr_##name __asm( \ + "section$end$__DATA$" \ + "__la_" #name); \ + UPB_LINKARR_APPEND(name) \ + UPB_LINKARR_SENTINEL type UPB_linkarr_internal_empty_##name[1] +#define UPB_LINKARR_START(name) (&__start_linkarr_##name) +#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name) + +#elif defined(_MSC_VER) + +/* See: + * https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155 + * https://devblogs.microsoft.com/oldnewthing/20181108-00/?p=100165 + * https://devblogs.microsoft.com/oldnewthing/20181109-00/?p=100175 */ +#define UPB_STRINGIFY_INTERNAL(x) #x +#define UPB_STRINGIFY(x) UPB_STRINGIFY_INTERNAL(x) +#define UPB_CONCAT(a, b, c) a##b##c +#define UPB_LINKARR_NAME(name, index) \ + UPB_STRINGIFY(UPB_CONCAT(la_, name, index)) +#define UPB_LINKARR_APPEND(name) \ + __pragma(section(UPB_LINKARR_NAME(name, $j), read)) \ + __declspec(allocate(UPB_LINKARR_NAME(name, $j))) +// clang-format off +#define UPB_LINKARR_DECLARE(name, type) \ + __pragma(message(UPB_LINKARR_NAME(name, $j))) \ + __pragma(section(UPB_LINKARR_NAME(name, $a), read)) \ + __pragma(section(UPB_LINKARR_NAME(name, $z), read)) \ + __declspec(allocate(UPB_LINKARR_NAME(name, $a)), selectany) \ + type __start_linkarr_##name; \ + __declspec(allocate(UPB_LINKARR_NAME(name, $z)), selectany) \ + type __stop_linkarr_##name; \ + UPB_LINKARR_APPEND(name) \ + __declspec(selectany) type UPB_linkarr_internal_empty_##name[1] = {0} +// clang-format on +#define UPB_LINKARR_START(name) (&__start_linkarr_##name) +#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name) + +#else + +// Linker arrays are not supported on this platform. Make macros no-ops. +#define UPB_LINKARR_APPEND(name) +#define UPB_LINKARR_DECLARE(name, type) +#define UPB_LINKARR_START(name) (NULL) +#define UPB_LINKARR_STOP(name) (NULL) + +#endif + +// Workaround for https://github.com/llvm/llvm-project/issues/167577 until it's +// fixed. Some function must exist for the constructor to work properly. +// TODO Remove this or gate it on a future version of clang. +#if defined(__clang__) && defined(__arm__) +#define _UPB_CONSTRUCTOR_PLACEHOLDER(unique_name) \ + __attribute__((used, visibility("hidden"))) void UPB_PRIVATE(unique_name)( \ + void) {} +#else +#define _UPB_CONSTRUCTOR_PLACEHOLDER(unique_name) +#endif + +#if defined(__ELF__) || defined(__wasm__) || defined(__MACH__) +#define UPB_CONSTRUCTOR(name, unique_name) \ + _UPB_CONSTRUCTOR_PLACEHOLDER(unique_name) \ + __attribute__((weak, visibility("hidden"), constructor)) void UPB_PRIVATE( \ + name)(void) +#elif defined(_MSC_VER) +/* + * See: https://stackoverflow.com/questions/1113409 + * + * The /include pragma suggested in the link above doesn't work in our case + * because it requires globally unique names. We need a different solution + * to prevent optimizers from removing the constructor. Our solution is to + * create a dummy exported weak symbol that prevent this stripping. + */ +#pragma section(".CRT$XCU", long, read) +#define UPB_CONSTRUCTOR(name, unique_name) \ + static void __cdecl UPB_PRIVATE(name)(void); \ + __declspec(allocate(".CRT$XCU"), selectany) void( \ + __cdecl * UPB_PRIVATE(name##_))(void) = UPB_PRIVATE(name); \ + __declspec(selectany, dllexport) void* UPB_PRIVATE(name##_force_linkage) = \ + &UPB_PRIVATE(name##_); \ + static void __cdecl UPB_PRIVATE(name)(void) + +#else +// No constructor support, nothing we can do except not break builds. +#define UPB_CONSTRUCTOR(name, unique_name) static void UPB_PRIVATE(name)(void) +#endif + +// +// Weak alias platform support. Theoretically this should be possible to do with +// only C using attributes like __attribute__((weak, alias("foo"))), but +// Clang doesn't support this properly on macOS. So we have to use assembly. +#if defined(__APPLE__) + +// TODO: once https://github.com/llvm/llvm-project/issues/167262 is fixed +// in the LLVM linker, we should have all weak variables point to a single +// "default" empty MiniTable instead of having each leaf define its own, like +// we do with ELF below. This will reduce binary size if many messages are tree +// shaken. +#define UPB_WEAK_SINGLETON_PLACEHOLDER_MINITABLE() +#define UPB_WEAK_PLACEHOLDER_MINITABLE(name) \ + __attribute__((weak)) const upb_MiniTable name = { \ + .UPB_PRIVATE(fields) = NULL, \ + .UPB_PRIVATE(size) = sizeof(struct upb_Message), \ + .UPB_PRIVATE(field_count) = 0, \ + .UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable, \ + .UPB_PRIVATE(dense_below) = 0, \ + .UPB_PRIVATE(table_mask) = -1, \ + .UPB_PRIVATE(required_count) = 0, \ + }; +#define UPB_WEAK_ALIAS(type, from, to) \ + extern type to; \ + __asm__(".globl _" #to); \ + __asm__(".private_extern _" #to); \ + __asm__(".set _" #to ", _" #from); \ + __asm__(".weak_definition _" #to); +#define UPB_STRONG_ALIAS(type, from, to) \ + __asm__(".globl _" #to); \ + __asm__(".private_extern _" #to); \ + __asm__(".set _" #to ", _" #from); + +#elif defined(__ELF__) + +// On ELF, weak aliases work properly, so we can have all weak MiniTables point +// to the same empty singleton MiniTable. This reduces code size if many +// MiniTables are tree shaken. +#define UPB_WEAK_SINGLETON_PLACEHOLDER_MINITABLE() \ + __attribute__((weak)) \ + const upb_MiniTable kUpb_WeakSingletonPlaceholderMiniTable = { \ + .UPB_PRIVATE(fields) = NULL, \ + .UPB_PRIVATE(size) = sizeof(struct upb_Message), \ + .UPB_PRIVATE(field_count) = 0, \ + .UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable, \ + .UPB_PRIVATE(dense_below) = 0, \ + .UPB_PRIVATE(table_mask) = -1, \ + .UPB_PRIVATE(required_count) = 0, \ + }; +#define UPB_WEAK_PLACEHOLDER_MINITABLE(name) +#define UPB_WEAK_ALIAS(type, from, to) \ + extern type to \ + __attribute__((weak, alias("kUpb_WeakSingletonPlaceholderMiniTable"))); +#define UPB_STRONG_ALIAS(type, from, to) \ + extern type to __attribute__((alias(#from))); + +#else +#define UPB_WEAK_SINGLETON_PLACEHOLDER_MINITABLE() +#define UPB_WEAK_PLACEHOLDER_MINITABLE(name) +#define UPB_WEAK_ALIAS(type, from, to) weak_alias_not_supported_on_this_platform +#define UPB_STRONG_ALIAS(type, from, to) \ + strong_alias_not_supported_on_this_platform +#endif + +// Future versions of upb will include breaking changes to some APIs. +// This macro can be set to enable these API changes ahead of time, so that +// user code can be updated before upgrading versions of protobuf. +#ifdef UPB_FUTURE_BREAKING_CHANGES + +#endif + +/* This file was generated by upb_generator from the input file: + * + * google/protobuf/descriptor.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#include + +// Must be last. + +extern const UPB_PRIVATE(upb_GeneratedExtensionListEntry)* UPB_PRIVATE(upb_generated_extension_list); +typedef struct { + upb_MiniTableField fields[1]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__FileDescriptorSet_msg_init_Fields; + +static const google__protobuf__FileDescriptorSet_msg_init_Fields google_protobuf_FileDescriptorSet__fields = {{ + {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FileDescriptorProto_msg_init}, +}}; + +const upb_MiniTable google__protobuf__FileDescriptorSet_msg_init = { + &google_protobuf_FileDescriptorSet__fields.fields[0], + 16, 1, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FileDescriptorSet", +#endif +}; + +typedef struct { + upb_MiniTableField fields[14]; + upb_MiniTableSubInternal subs[7]; +} google__protobuf__FileDescriptorProto_msg_init_Fields; + +static const google__protobuf__FileDescriptorProto_msg_init_Fields google_protobuf_FileDescriptorProto__fields = {{ + {1, UPB_SIZE(56, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(64, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(32, 104), 66, UPB_SIZE(25, 29), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {9, UPB_SIZE(36, 112), 67, UPB_SIZE(23, 28), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {12, UPB_SIZE(72, 48), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {14, UPB_SIZE(48, 12), 69, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__EnumDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__ServiceDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FieldDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FileOptions_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__SourceCodeInfo_msg_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, +}}; + +const upb_MiniTable google__protobuf__FileDescriptorProto_msg_init = { + &google_protobuf_FileDescriptorProto__fields.fields[0], + UPB_SIZE(80, 144), 14, kUpb_ExtMode_NonExtendable, 12, UPB_FASTTABLE_MASK(120), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FileDescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0020000001000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x004000003f00001a, &upb_DecodeFast_Bytes_Repeated_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x007800003f000050, &upb_DecodeFast_Varint32_Repeated_Tag1Byte}, + {0x008000003f000058, &upb_DecodeFast_Varint32_Repeated_Tag1Byte}, + {0x0030000004000062, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x008800003f00007a, &upb_DecodeFast_Bytes_Repeated_Tag1Byte}, + }) +}; + +typedef struct { + upb_MiniTableField fields[11]; + upb_MiniTableSubInternal subs[9]; +} google__protobuf__DescriptorProto_msg_init_Fields; + +static const google__protobuf__DescriptorProto_msg_init_Fields google_protobuf_DescriptorProto__fields = {{ + {1, UPB_SIZE(52, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(32, 72), 65, UPB_SIZE(20, 26), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {11, UPB_SIZE(48, 12), 66, UPB_SIZE(11, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FieldDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__EnumDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto__ExtensionRange_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FieldDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__MessageOptions_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__OneofDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto__ReservedRange_msg_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__SymbolVisibility_enum_init}, +}}; + +const upb_MiniTable google__protobuf__DescriptorProto_msg_init = { + &google_protobuf_DescriptorProto__fields.fields[0], + UPB_SIZE(64, 104), 11, kUpb_ExtMode_NonExtendable, 11, UPB_FASTTABLE_MASK(120), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.DescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x006000003f000052, &upb_DecodeFast_Bytes_Repeated_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__DescriptorProto__ExtensionRange_msg_init_Fields; + +static const google__protobuf__DescriptorProto__ExtensionRange_msg_init_Fields google_protobuf_DescriptorProto_ExtensionRange__fields = {{ + {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(20, 24), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__ExtensionRangeOptions_msg_init}, +}}; + +const upb_MiniTable google__protobuf__DescriptorProto__ExtensionRange_msg_init = { + &google_protobuf_DescriptorProto_ExtensionRange__fields.fields[0], + UPB_SIZE(24, 32), 3, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.DescriptorProto.ExtensionRange", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c000000000008, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0010000001000010, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[2]; +} google__protobuf__DescriptorProto__ReservedRange_msg_init_Fields; + +static const google__protobuf__DescriptorProto__ReservedRange_msg_init_Fields google_protobuf_DescriptorProto_ReservedRange__fields = {{ + {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable google__protobuf__DescriptorProto__ReservedRange_msg_init = { + &google_protobuf_DescriptorProto_ReservedRange__fields.fields[0], + 24, 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.DescriptorProto.ReservedRange", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c000000000008, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0010000001000010, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[4]; + upb_MiniTableSubInternal subs[4]; +} google__protobuf__ExtensionRangeOptions_msg_init_Fields; + +static const google__protobuf__ExtensionRangeOptions_msg_init_Fields google_protobuf_ExtensionRangeOptions__fields = {{ + {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 12), 64, UPB_SIZE(10, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {50, UPB_SIZE(20, 24), 65, UPB_SIZE(8, 10), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__ExtensionRangeOptions__Declaration_msg_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__ExtensionRangeOptions__VerificationState_enum_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__ExtensionRangeOptions_msg_init = { + &google_protobuf_ExtensionRangeOptions__fields.fields[0], + UPB_SIZE(32, 40), 4, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.ExtensionRangeOptions", +#endif +}; + +typedef struct { + upb_MiniTableField fields[5]; +} google__protobuf__ExtensionRangeOptions__Declaration_msg_init_Fields; + +static const google__protobuf__ExtensionRangeOptions__Declaration_msg_init_Fields google_protobuf_ExtensionRangeOptions_Declaration__fields = {{ + {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, 16, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(24, 32), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {5, 9, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {6, 10, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable google__protobuf__ExtensionRangeOptions__Declaration_msg_init = { + &google_protobuf_ExtensionRangeOptions_Declaration__fields.fields[0], + UPB_SIZE(32, 48), 5, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.ExtensionRangeOptions.Declaration", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c000000000008, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0010000001000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x002000000200001a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0009000003000028, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000a000004000030, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[11]; + upb_MiniTableSubInternal subs[3]; +} google__protobuf__FieldDescriptorProto_msg_init_Fields; + +static const google__protobuf__FieldDescriptorProto_msg_init_Fields google_protobuf_FieldDescriptorProto__fields = {{ + {1, 32, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(40, 48), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, 12, 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {4, 16, 67, UPB_SIZE(24, 25), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, 20, 68, UPB_SIZE(22, 24), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(48, 64), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(56, 80), 70, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(24, 112), 71, UPB_SIZE(14, 17), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {9, UPB_SIZE(28, 24), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {10, UPB_SIZE(64, 96), 73, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {17, 10, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__FieldDescriptorProto__Label_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FieldDescriptorProto__Type_enum_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions_msg_init}, +}}; + +const upb_MiniTable google__protobuf__FieldDescriptorProto_msg_init = { + &google_protobuf_FieldDescriptorProto__fields.fields[0], + UPB_SIZE(72, 120), 11, kUpb_ExtMode_NonExtendable, 10, UPB_FASTTABLE_MASK(248), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FieldDescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x002000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0030000001000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x000c000002000018, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0040000005000032, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x005000000600003a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0018000008000048, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0060000009000052, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000a00000a000188, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[2]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__OneofDescriptorProto_msg_init_Fields; + +static const google__protobuf__OneofDescriptorProto_msg_init_Fields google_protobuf_OneofDescriptorProto__fields = {{ + {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 32), 65, 3, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__OneofOptions_msg_init}, +}}; + +const upb_MiniTable google__protobuf__OneofDescriptorProto_msg_init = { + &google_protobuf_OneofDescriptorProto__fields.fields[0], + UPB_SIZE(24, 40), 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(8), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.OneofDescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + }) +}; + +typedef struct { + upb_MiniTableField fields[6]; + upb_MiniTableSubInternal subs[4]; +} google__protobuf__EnumDescriptorProto_msg_init_Fields; + +static const google__protobuf__EnumDescriptorProto_msg_init_Fields google_protobuf_EnumDescriptorProto__fields = {{ + {1, UPB_SIZE(32, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 40), 65, UPB_SIZE(13, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(28, 12), 66, UPB_SIZE(6, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__EnumValueDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__EnumOptions_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__SymbolVisibility_enum_init}, +}}; + +const upb_MiniTable google__protobuf__EnumDescriptorProto_msg_init = { + &google_protobuf_EnumDescriptorProto__fields.fields[0], + UPB_SIZE(40, 64), 6, kUpb_ExtMode_NonExtendable, 6, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.EnumDescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x003800003f00002a, &upb_DecodeFast_Bytes_Repeated_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[2]; +} google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init_Fields; + +static const google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init_Fields google_protobuf_EnumDescriptorProto_EnumReservedRange__fields = {{ + {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init = { + &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields.fields[0], + 24, 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.EnumDescriptorProto.EnumReservedRange", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c000000000008, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0010000001000010, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__EnumValueDescriptorProto_msg_init_Fields; + +static const google__protobuf__EnumValueDescriptorProto_msg_init_Fields google_protobuf_EnumValueDescriptorProto__fields = {{ + {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, 12, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 32), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__EnumValueOptions_msg_init}, +}}; + +const upb_MiniTable google__protobuf__EnumValueDescriptorProto_msg_init = { + &google_protobuf_EnumValueDescriptorProto__fields.fields[0], + UPB_SIZE(32, 40), 3, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.EnumValueDescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x000c000001000010, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[2]; +} google__protobuf__ServiceDescriptorProto_msg_init_Fields; + +static const google__protobuf__ServiceDescriptorProto_msg_init_Fields google_protobuf_ServiceDescriptorProto__fields = {{ + {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 40), 65, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__MethodDescriptorProto_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__ServiceOptions_msg_init}, +}}; + +const upb_MiniTable google__protobuf__ServiceDescriptorProto_msg_init = { + &google_protobuf_ServiceDescriptorProto__fields.fields[0], + UPB_SIZE(32, 48), 3, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(8), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.ServiceDescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + }) +}; + +typedef struct { + upb_MiniTableField fields[6]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__MethodDescriptorProto_msg_init_Fields; + +static const google__protobuf__MethodDescriptorProto_msg_init_Fields google_protobuf_MethodDescriptorProto__fields = {{ + {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(24, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(32, 48), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(12, 64), 67, 9, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {5, 9, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {6, 10, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__MethodOptions_msg_init}, +}}; + +const upb_MiniTable google__protobuf__MethodDescriptorProto_msg_init = { + &google_protobuf_MethodDescriptorProto__fields.fields[0], + UPB_SIZE(40, 72), 6, kUpb_ExtMode_NonExtendable, 6, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.MethodDescriptorProto", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0020000001000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x003000000200001a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0009000004000028, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000a000005000030, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[21]; + upb_MiniTableSubInternal subs[3]; +} google__protobuf__FileOptions_msg_init_Fields; + +static const google__protobuf__FileOptions_msg_init_Fields google_protobuf_FileOptions__fields = {{ + {1, UPB_SIZE(32, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {8, 40, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {9, 20, 66, UPB_SIZE(57, 58), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {10, 11, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {11, UPB_SIZE(48, 56), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {16, 12, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {17, 13, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {18, 14, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {20, 15, 72, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {23, 16, 73, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {27, 17, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {31, 18, 75, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {36, UPB_SIZE(56, 72), 76, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {37, UPB_SIZE(64, 88), 77, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {39, UPB_SIZE(72, 104), 78, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {40, UPB_SIZE(80, 120), 79, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {41, UPB_SIZE(88, 136), 80, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {44, UPB_SIZE(96, 152), 81, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {45, UPB_SIZE(104, 168), 82, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {50, UPB_SIZE(24, 184), 83, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__FileOptions__OptimizeMode_enum_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__FileOptions_msg_init = { + &google_protobuf_FileOptions__fields.fields[0], + UPB_SIZE(112, 200), 21, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(248), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FileOptions", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001800000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0028000001000042, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000b000003000050, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x003800000400005a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c000005000180, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x000d000006000188, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x000e000007000190, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000f0000080001a0, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x005800000d0002aa, &upb_DecodeFast_Bytes_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x00100000090001b8, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x007800000f0002c2, &upb_DecodeFast_Bytes_Scalar_Tag2Byte}, + {0x00880000100002ca, &upb_DecodeFast_Bytes_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001100000a0001d8, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x00980000110002e2, &upb_DecodeFast_Bytes_Scalar_Tag2Byte}, + {0x00a80000120002ea, &upb_DecodeFast_Bytes_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001200000b0001f8, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + }) +}; + +typedef struct { + upb_MiniTableField fields[7]; + upb_MiniTableSubInternal subs[2]; +} google__protobuf__MessageOptions_msg_init_Fields; + +static const google__protobuf__MessageOptions_msg_init_Fields google_protobuf_MessageOptions__fields = {{ + {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {7, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {11, 13, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {12, 16, 69, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__MessageOptions_msg_init = { + &google_protobuf_MessageOptions__fields.fields[0], + UPB_SIZE(24, 32), 7, kUpb_ExtMode_Extendable, 3, UPB_FASTTABLE_MASK(120), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.MessageOptions", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0009000000000008, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000a000001000010, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000b000002000018, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c000003000038, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000d000004000058, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[14]; + upb_MiniTableSubInternal subs[8]; +} google__protobuf__FieldOptions_msg_init_Fields; + +static const google__protobuf__FieldOptions_msg_init_Fields google_protobuf_FieldOptions__fields = {{ + {1, 16, 64, 42, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {5, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {6, 20, 68, UPB_SIZE(31, 32), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {10, 13, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {15, 14, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {16, 15, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {17, 24, 72, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {21, UPB_SIZE(36, 48), 73, UPB_SIZE(14, 19), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {22, UPB_SIZE(40, 56), 74, UPB_SIZE(12, 18), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__CType_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__JSType_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__OptionRetention_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__OptionTargetType_enum_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions__EditionDefault_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions__FeatureSupport_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__FieldOptions_msg_init = { + &google_protobuf_FieldOptions__fields.fields[0], + UPB_SIZE(48, 72), 14, kUpb_ExtMode_Extendable, 3, UPB_FASTTABLE_MASK(248), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FieldOptions", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000a000001000010, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000b000002000018, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000c000003000028, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000d000005000050, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000e000006000078, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000f000007000180, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[2]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__FieldOptions__EditionDefault_msg_init_Fields; + +static const google__protobuf__FieldOptions__EditionDefault_msg_init_Fields google_protobuf_FieldOptions_EditionDefault__fields = {{ + {2, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, 12, 65, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, +}}; + +const upb_MiniTable google__protobuf__FieldOptions__EditionDefault_msg_init = { + &google_protobuf_FieldOptions_EditionDefault__fields.fields[0], + UPB_SIZE(24, 32), 2, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FieldOptions.EditionDefault", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0010000000000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[5]; + upb_MiniTableSubInternal subs[3]; +} google__protobuf__FieldOptions__FeatureSupport_msg_init_Fields; + +static const google__protobuf__FieldOptions__FeatureSupport_msg_init_Fields google_protobuf_FieldOptions_FeatureSupport__fields = {{ + {1, 12, 64, UPB_SIZE(15, 16), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, 16, 65, UPB_SIZE(13, 15), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, 24, 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, 20, 67, UPB_SIZE(8, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(32, 40), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, +}}; + +const upb_MiniTable google__protobuf__FieldOptions__FeatureSupport_msg_init = { + &google_protobuf_FieldOptions_FeatureSupport__fields.fields[0], + UPB_SIZE(40, 56), 5, kUpb_ExtMode_NonExtendable, 5, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FieldOptions.FeatureSupport", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001800000200001a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x002800000400002a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[2]; + upb_MiniTableSubInternal subs[2]; +} google__protobuf__OneofOptions_msg_init_Fields; + +static const google__protobuf__OneofOptions_msg_init_Fields google_protobuf_OneofOptions__fields = {{ + {1, UPB_SIZE(12, 16), 64, 6, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__OneofOptions_msg_init = { + &google_protobuf_OneofOptions__fields.fields[0], + UPB_SIZE(24, 32), 2, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.OneofOptions", +#endif +}; + +typedef struct { + upb_MiniTableField fields[5]; + upb_MiniTableSubInternal subs[2]; +} google__protobuf__EnumOptions_msg_init_Fields; + +static const google__protobuf__EnumOptions_msg_init_Fields google_protobuf_EnumOptions__fields = {{ + {2, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {3, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {6, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(12, 16), 67, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__EnumOptions_msg_init = { + &google_protobuf_EnumOptions__fields.fields[0], + UPB_SIZE(24, 32), 5, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.EnumOptions", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0009000000000010, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x000a000001000018, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000b000002000030, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[5]; + upb_MiniTableSubInternal subs[3]; +} google__protobuf__EnumValueOptions_msg_init_Fields; + +static const google__protobuf__EnumValueOptions_msg_init_Fields google_protobuf_EnumValueOptions__fields = {{ + {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(12, 16), 65, UPB_SIZE(12, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, 10, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(16, 24), 67, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions__FeatureSupport_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__EnumValueOptions_msg_init = { + &google_protobuf_EnumValueOptions__fields.fields[0], + UPB_SIZE(24, 40), 5, kUpb_ExtMode_Extendable, 4, UPB_FASTTABLE_MASK(24), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.EnumValueOptions", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0009000000000008, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x000a000002000018, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + }) +}; + +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[2]; +} google__protobuf__ServiceOptions_msg_init_Fields; + +static const google__protobuf__ServiceOptions_msg_init_Fields google_protobuf_ServiceOptions__fields = {{ + {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {34, UPB_SIZE(12, 16), 65, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__ServiceOptions_msg_init = { + &google_protobuf_ServiceOptions__fields.fields[0], + UPB_SIZE(24, 32), 3, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(248), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.ServiceOptions", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0009000000000288, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[4]; + upb_MiniTableSubInternal subs[3]; +} google__protobuf__MethodOptions_msg_init_Fields; + +static const google__protobuf__MethodOptions_msg_init_Fields google_protobuf_MethodOptions__fields = {{ + {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, + {34, 12, 65, 9, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {35, 16, 66, UPB_SIZE(7, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__MethodOptions__IdempotencyLevel_enum_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init}, +}}; + +const upb_MiniTable google__protobuf__MethodOptions_msg_init = { + &google_protobuf_MethodOptions__fields.fields[0], + UPB_SIZE(24, 32), 4, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(248), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.MethodOptions", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0009000000000288, &upb_DecodeFast_Bool_Scalar_Tag2Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[7]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__UninterpretedOption_msg_init_Fields; + +static const google__protobuf__UninterpretedOption_msg_init_Fields google_protobuf_UninterpretedOption__fields = {{ + {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(40, 72), 65, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(48, 80), 66, kUpb_NoSub, 3, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(56, 88), 67, kUpb_NoSub, 1, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}, + {7, UPB_SIZE(24, 32), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {8, UPB_SIZE(32, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption__NamePart_msg_init}, +}}; + +const upb_MiniTable google__protobuf__UninterpretedOption_msg_init = { + &google_protobuf_UninterpretedOption__fields.fields[0], + UPB_SIZE(64, 96), 7, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(120), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.UninterpretedOption", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000001a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0048000001000020, &upb_DecodeFast_Varint64_Scalar_Tag1Byte}, + {0x0050000002000028, &upb_DecodeFast_Varint64_Scalar_Tag1Byte}, + {0x0058000003000031, &upb_DecodeFast_Fixed64_Scalar_Tag1Byte}, + {0x002000000400003a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0030000005000042, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[2]; +} google__protobuf__UninterpretedOption__NamePart_msg_init_Fields; + +static const google__protobuf__UninterpretedOption__NamePart_msg_init_Fields google_protobuf_UninterpretedOption_NamePart__fields = {{ + {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {2, 9, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable google__protobuf__UninterpretedOption__NamePart_msg_init = { + &google_protobuf_UninterpretedOption_NamePart__fields.fields[0], + UPB_SIZE(24, 32), 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(24), 2, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.UninterpretedOption.NamePart", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x001000000000000a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0009000001000010, &upb_DecodeFast_Bool_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[8]; + upb_MiniTableSubInternal subs[8]; +} google__protobuf__FeatureSet_msg_init_Fields; + +static const google__protobuf__FeatureSet_msg_init_Fields google_protobuf_FeatureSet__fields = {{ + {1, 12, 64, 24, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {2, 16, 65, UPB_SIZE(22, 23), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {3, 20, 66, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {4, 24, 67, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, 28, 68, UPB_SIZE(16, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {6, 32, 69, UPB_SIZE(14, 19), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {7, 36, 70, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {8, 40, 71, UPB_SIZE(10, 17), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__FieldPresence_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__EnumType_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__Utf8Validation_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__MessageEncoding_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__JsonFormat_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__EnforceNamingStyle_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__VisibilityFeature__DefaultSymbolVisibility_enum_init}, +}}; + +const upb_MiniTable google__protobuf__FeatureSet_msg_init = { + &google_protobuf_FeatureSet__fields.fields[0], + 48, 8, kUpb_ExtMode_Extendable, 8, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FeatureSet", +#endif +}; + +const upb_MiniTable google__protobuf__FeatureSet__VisibilityFeature_msg_init = { + NULL, + 8, 0, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FeatureSet.VisibilityFeature", +#endif +}; + +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[3]; +} google__protobuf__FeatureSetDefaults_msg_init_Fields; + +static const google__protobuf__FeatureSetDefaults_msg_init_Fields google_protobuf_FeatureSetDefaults__fields = {{ + {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(16, 12), 64, UPB_SIZE(7, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(20, 16), 65, UPB_SIZE(5, 8), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, +}}; + +const upb_MiniTable google__protobuf__FeatureSetDefaults_msg_init = { + &google_protobuf_FeatureSetDefaults__fields.fields[0], + UPB_SIZE(24, 32), 3, kUpb_ExtMode_NonExtendable, 1, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FeatureSetDefaults", +#endif +}; + +typedef struct { + upb_MiniTableField fields[3]; + upb_MiniTableSubInternal subs[3]; +} google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init_Fields; + +static const google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init_Fields google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault__fields = {{ + {3, 12, 64, UPB_SIZE(9, 10), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {4, 16, 65, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(20, 24), 66, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, + {.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init}, +}}; + +const upb_MiniTable google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init = { + &google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault__fields.fields[0], + UPB_SIZE(24, 32), 3, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.FeatureSetDefaults.FeatureSetEditionDefault", +#endif +}; + +typedef struct { + upb_MiniTableField fields[1]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__SourceCodeInfo_msg_init_Fields; + +static const google__protobuf__SourceCodeInfo_msg_init_Fields google_protobuf_SourceCodeInfo__fields = {{ + {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__SourceCodeInfo__Location_msg_init}, +}}; + +const upb_MiniTable google__protobuf__SourceCodeInfo_msg_init = { + &google_protobuf_SourceCodeInfo__fields.fields[0], + 16, 1, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.SourceCodeInfo", +#endif +}; + +typedef struct { + upb_MiniTableField fields[5]; +} google__protobuf__SourceCodeInfo__Location_msg_init_Fields; + +static const google__protobuf__SourceCodeInfo__Location_msg_init_Fields google_protobuf_SourceCodeInfo_Location__fields = {{ + {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(24, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {4, 32, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, +}}; + +const upb_MiniTable google__protobuf__SourceCodeInfo__Location_msg_init = { + &google_protobuf_SourceCodeInfo_Location__fields.fields[0], + UPB_SIZE(40, 72), 5, kUpb_ExtMode_NonExtendable, 4, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.SourceCodeInfo.Location", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x003000003f00000a, &upb_DecodeFast_Varint32_Packed_Tag1Byte}, + {0x003800003f000012, &upb_DecodeFast_Varint32_Packed_Tag1Byte}, + {0x001000000000001a, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0020000001000022, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x004000003f000032, &upb_DecodeFast_Bytes_Repeated_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +typedef struct { + upb_MiniTableField fields[1]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__GeneratedCodeInfo_msg_init_Fields; + +static const google__protobuf__GeneratedCodeInfo_msg_init_Fields google_protobuf_GeneratedCodeInfo__fields = {{ + {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(submsg) = &google__protobuf__GeneratedCodeInfo__Annotation_msg_init}, +}}; + +const upb_MiniTable google__protobuf__GeneratedCodeInfo_msg_init = { + &google_protobuf_GeneratedCodeInfo__fields.fields[0], + 16, 1, kUpb_ExtMode_NonExtendable, 1, UPB_FASTTABLE_MASK(255), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.GeneratedCodeInfo", +#endif +}; + +typedef struct { + upb_MiniTableField fields[5]; + upb_MiniTableSubInternal subs[1]; +} google__protobuf__GeneratedCodeInfo__Annotation_msg_init_Fields; + +static const google__protobuf__GeneratedCodeInfo__Annotation_msg_init_Fields google_protobuf_GeneratedCodeInfo_Annotation__fields = {{ + {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}, + {2, UPB_SIZE(28, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}, + {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {4, UPB_SIZE(20, 16), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + {5, UPB_SIZE(24, 20), 67, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}, + }, + { + {.UPB_PRIVATE(subenum) = &google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init}, +}}; + +const upb_MiniTable google__protobuf__GeneratedCodeInfo__Annotation_msg_init = { + &google_protobuf_GeneratedCodeInfo_Annotation__fields.fields[0], + UPB_SIZE(40, 48), 5, kUpb_ExtMode_NonExtendable, 5, UPB_FASTTABLE_MASK(56), 0, +#ifdef UPB_TRACING_ENABLED + "google.protobuf.GeneratedCodeInfo.Annotation", +#endif + UPB_FASTTABLE_INIT({ + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x002800003f00000a, &upb_DecodeFast_Varint32_Packed_Tag1Byte}, + {0x0018000000000012, &upb_DecodeFast_Bytes_Scalar_Tag1Byte}, + {0x000c000001000018, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0010000002000020, &upb_DecodeFast_Varint32_Scalar_Tag1Byte}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + {0x0000000000000000, &_upb_FastDecoder_DecodeGeneric}, + }) +}; + +const upb_MiniTableEnum google__protobuf__Edition_enum_init = { + 64, + 11, + { + 0x7, + 0x0, + 0x384, + 0x3e6, + 0x3e7, + 0x3e8, + 0x3e9, + 0x3ea, + 0x270f, + 0x1869d, + 0x1869e, + 0x1869f, + 0x7fffffff, + }, +}; + +const upb_MiniTableEnum google__protobuf__ExtensionRangeOptions__VerificationState_enum_init = { + 64, + 0, + { + 0x3, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__EnforceNamingStyle_enum_init = { + 64, + 0, + { + 0xf, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__EnumType_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__FieldPresence_enum_init = { + 64, + 0, + { + 0xf, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__JsonFormat_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__MessageEncoding_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__Utf8Validation_enum_init = { + 64, + 0, + { + 0xd, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FeatureSet__VisibilityFeature__DefaultSymbolVisibility_enum_init = { + 64, + 0, + { + 0x1f, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FieldDescriptorProto__Label_enum_init = { + 64, + 0, + { + 0xe, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FieldDescriptorProto__Type_enum_init = { + 64, + 0, + { + 0x7fffe, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FieldOptions__CType_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FieldOptions__JSType_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FieldOptions__OptionRetention_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FieldOptions__OptionTargetType_enum_init = { + 64, + 0, + { + 0x3ff, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__FileOptions__OptimizeMode_enum_init = { + 64, + 0, + { + 0xe, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__MethodOptions__IdempotencyLevel_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +const upb_MiniTableEnum google__protobuf__SymbolVisibility_enum_init = { + 64, + 0, + { + 0x7, + 0x0, + }, +}; + +static const upb_MiniTable *messages_layout[34] = { + &google__protobuf__FileDescriptorSet_msg_init, + &google__protobuf__FileDescriptorProto_msg_init, + &google__protobuf__DescriptorProto_msg_init, + &google__protobuf__DescriptorProto__ExtensionRange_msg_init, + &google__protobuf__DescriptorProto__ReservedRange_msg_init, + &google__protobuf__ExtensionRangeOptions_msg_init, + &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, + &google__protobuf__FieldDescriptorProto_msg_init, + &google__protobuf__OneofDescriptorProto_msg_init, + &google__protobuf__EnumDescriptorProto_msg_init, + &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, + &google__protobuf__EnumValueDescriptorProto_msg_init, + &google__protobuf__ServiceDescriptorProto_msg_init, + &google__protobuf__MethodDescriptorProto_msg_init, + &google__protobuf__FileOptions_msg_init, + &google__protobuf__MessageOptions_msg_init, + &google__protobuf__FieldOptions_msg_init, + &google__protobuf__FieldOptions__EditionDefault_msg_init, + &google__protobuf__FieldOptions__FeatureSupport_msg_init, + &google__protobuf__OneofOptions_msg_init, + &google__protobuf__EnumOptions_msg_init, + &google__protobuf__EnumValueOptions_msg_init, + &google__protobuf__ServiceOptions_msg_init, + &google__protobuf__MethodOptions_msg_init, + &google__protobuf__UninterpretedOption_msg_init, + &google__protobuf__UninterpretedOption__NamePart_msg_init, + &google__protobuf__FeatureSet_msg_init, + &google__protobuf__FeatureSet__VisibilityFeature_msg_init, + &google__protobuf__FeatureSetDefaults_msg_init, + &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, + &google__protobuf__SourceCodeInfo_msg_init, + &google__protobuf__SourceCodeInfo__Location_msg_init, + &google__protobuf__GeneratedCodeInfo_msg_init, + &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, +}; + +static const upb_MiniTableEnum *enums_layout[20] = { + &google__protobuf__Edition_enum_init, + &google__protobuf__ExtensionRangeOptions__VerificationState_enum_init, + &google__protobuf__FeatureSet__EnforceNamingStyle_enum_init, + &google__protobuf__FeatureSet__EnumType_enum_init, + &google__protobuf__FeatureSet__FieldPresence_enum_init, + &google__protobuf__FeatureSet__JsonFormat_enum_init, + &google__protobuf__FeatureSet__MessageEncoding_enum_init, + &google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init, + &google__protobuf__FeatureSet__Utf8Validation_enum_init, + &google__protobuf__FeatureSet__VisibilityFeature__DefaultSymbolVisibility_enum_init, + &google__protobuf__FieldDescriptorProto__Label_enum_init, + &google__protobuf__FieldDescriptorProto__Type_enum_init, + &google__protobuf__FieldOptions__CType_enum_init, + &google__protobuf__FieldOptions__JSType_enum_init, + &google__protobuf__FieldOptions__OptionRetention_enum_init, + &google__protobuf__FieldOptions__OptionTargetType_enum_init, + &google__protobuf__FileOptions__OptimizeMode_enum_init, + &google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init, + &google__protobuf__MethodOptions__IdempotencyLevel_enum_init, + &google__protobuf__SymbolVisibility_enum_init, +}; + +const upb_MiniTableFile google_protobuf_descriptor_proto_upb_file_layout = { + messages_layout, + enums_layout, + NULL, + 34, + 20, + 0, +}; + + + + +#include +#include +#include +#include +#include + +// Must be last. + +void upb_Status_Clear(upb_Status* status) { + if (!status) return; + status->ok = true; + status->msg[0] = '\0'; +} + +bool upb_Status_IsOk(const upb_Status* status) { return status->ok; } + +const char* upb_Status_ErrorMessage(const upb_Status* status) { + return status->msg; +} + +void upb_Status_SetErrorMessage(upb_Status* status, const char* msg) { + if (!status) return; + status->ok = false; + strncpy(status->msg, msg, _kUpb_Status_MaxMessage - 1); + status->msg[_kUpb_Status_MaxMessage - 1] = '\0'; +} + +void upb_Status_SetErrorFormat(upb_Status* status, const char* fmt, ...) { + va_list args; + va_start(args, fmt); + upb_Status_VSetErrorFormat(status, fmt, args); + va_end(args); +} + +void upb_Status_VSetErrorFormat(upb_Status* status, const char* fmt, + va_list args) { + if (!status) return; + status->ok = false; + vsnprintf(status->msg, sizeof(status->msg), fmt, args); + status->msg[_kUpb_Status_MaxMessage - 1] = '\0'; +} + +void upb_Status_VAppendErrorFormat(upb_Status* status, const char* fmt, + va_list args) { + size_t len; + if (!status) return; + status->ok = false; + len = strlen(status->msg); + vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args); + status->msg[_kUpb_Status_MaxMessage - 1] = '\0'; +} + +/* + * upb_table Implementation + * + * Implementation is heavily inspired by Lua's ltable.c. + */ + + +#include +#include + + +// Must be last. + +#define UPB_MAXARRSIZE 16 // 2**16 = 64k. + +// From Chromium. +#define ARRAY_SIZE(x) \ + ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x]))))) + +/* The minimum utilization of the array part of a mixed hash/array table. This + * is a speed/memory-usage tradeoff (though it's not straightforward because of + * cache effects). The lower this is, the more memory we'll use. */ +static const double MIN_DENSITY = 0.1; + +#if defined(__has_builtin) +#if __has_builtin(__builtin_popcount) +#define UPB_FAST_POPCOUNT32(i) __builtin_popcount(i) +#endif +#elif defined(__GNUC__) +#define UPB_FAST_POPCOUNT32(i) __builtin_popcount(i) +#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) +// Only use __popcnt on x86/x64 architectures for MSVC +#define UPB_FAST_POPCOUNT32(i) __popcnt(i) +#endif + +UPB_INLINE int _upb_popcnt32(uint32_t i) { +#ifdef UPB_FAST_POPCOUNT32 + return UPB_FAST_POPCOUNT32(i); +#else + int count = 0; + while (i != 0) { + count += i & 1; + i >>= 1; + } + return count; +#endif +} + +#undef UPB_FAST_POPCOUNT32 + +UPB_INLINE uint8_t _upb_log2_table_size(upb_table* t) { + return _upb_popcnt32(t->mask); +} + +static bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; } + +static int log2ceil(uint64_t v) { + int ret = 0; + bool pow2 = is_pow2(v); + while (v >>= 1) ret++; + ret = pow2 ? ret : ret + 1; // Ceiling. + return UPB_MIN(UPB_MAXARRSIZE, ret); +} + +/* A type to represent the lookup key of either a strtable, inttable or + * exttable. */ +typedef union { + uintptr_t num; + upb_StringView str; + struct { + const void* ptr; + uint32_t ext_num; + } ext; +} lookupkey_t; + +static lookupkey_t strkey2(const char* str, size_t len) { + return (lookupkey_t){.str = upb_StringView_FromDataAndSize(str, len)}; +} + +static lookupkey_t intkey(uintptr_t key) { return (lookupkey_t){.num = key}; } + +static lookupkey_t extkey(const void* ptr, uint32_t ext_num) { + return (lookupkey_t){.ext = {ptr, ext_num}}; +} + +// Conceptually the hash and equal functions should only take the key, not the +// value, but the extension table stores part of its logical key in the value +// slot. This is a sign that we have outgrown the original architecture. +typedef uint32_t hashfunc_t(upb_key key, upb_value val); +typedef bool eqlfunc_t(upb_key k1, upb_value v1, lookupkey_t k2); + +/* Base table (shared code) ***************************************************/ + +static uint32_t upb_inthash(uintptr_t key) { + UPB_STATIC_ASSERT(sizeof(uintptr_t) == 4 || sizeof(uintptr_t) == 8, + "Pointers don't fit"); + if (sizeof(uintptr_t) == 8) { + return (uint32_t)key ^ (uint32_t)(key >> 32); + } else { + return (uint32_t)key; + } +} + +static const upb_tabent* upb_getentry(const upb_table* t, uint32_t hash) { + return t->entries + (hash & t->mask); +} + +static bool isfull(upb_table* t) { + uint32_t size = upb_table_size(t); + // 0.875 load factor + return t->count == (size - (size >> 3)); +} + +static bool init(upb_table* t, uint8_t size_lg2, upb_Arena* a) { + if (size_lg2 >= 32) { + return false; + } + t->count = 0; + uint32_t size = 1 << size_lg2; + t->mask = size - 1; // 0 mask if size_lg2 is 0 + if (upb_table_size(t) > (SIZE_MAX / sizeof(upb_tabent))) { + return false; + } + size_t bytes = upb_table_size(t) * sizeof(upb_tabent); + if (bytes > 0) { + t->entries = upb_Arena_Malloc(a, bytes); + if (!t->entries) return false; + memset(t->entries, 0, bytes); + } else { + t->entries = NULL; + } + return true; +} + +static upb_tabent* emptyent(upb_table* t, upb_tabent* e) { + upb_tabent* begin = t->entries; + upb_tabent* end = begin + upb_table_size(t); + for (e = e + 1; e < end; e++) { + if (upb_tabent_isempty(e)) return e; + } + for (e = begin; e < end; e++) { + if (upb_tabent_isempty(e)) return e; + } + UPB_ASSERT(false); + return NULL; +} + +static upb_tabent* getentry_mutable(upb_table* t, uint32_t hash) { + return (upb_tabent*)upb_getentry(t, hash); +} + +static const upb_tabent* findentry(const upb_table* t, lookupkey_t key, + uint32_t hash, eqlfunc_t* eql) { + const upb_tabent* e; + + if (t->count == 0) return NULL; + e = upb_getentry(t, hash); + if (upb_tabent_isempty(e)) return NULL; + while (1) { + if (eql(e->key, e->val, key)) return e; + if ((e = e->next) == NULL) return NULL; + } +} + +static upb_tabent* findentry_mutable(upb_table* t, lookupkey_t key, + uint32_t hash, eqlfunc_t* eql) { + return (upb_tabent*)findentry(t, key, hash, eql); +} + +static bool lookup(const upb_table* t, lookupkey_t key, upb_value* v, + uint32_t hash, eqlfunc_t* eql) { + const upb_tabent* e = findentry(t, key, hash, eql); + if (e) { + if (v) *v = e->val; + return true; + } else { + return false; + } +} + +/* The given key must not already exist in the table. */ +static void insert(upb_table* t, lookupkey_t key, upb_key tabkey, upb_value val, + uint32_t hash, hashfunc_t* hashfunc, eqlfunc_t* eql) { + upb_tabent* mainpos_e; + upb_tabent* our_e; + + UPB_ASSERT(findentry(t, key, hash, eql) == NULL); + + t->count++; + mainpos_e = getentry_mutable(t, hash); + our_e = mainpos_e; + + if (upb_tabent_isempty(mainpos_e)) { + /* Our main position is empty; use it. */ + our_e->next = NULL; + } else { + /* Collision. */ + upb_tabent* new_e = emptyent(t, mainpos_e); + /* Head of collider's chain. */ + upb_tabent* chain = + getentry_mutable(t, hashfunc(mainpos_e->key, mainpos_e->val)); + if (chain == mainpos_e) { + /* Existing ent is in its main position (it has the same hash as us, and + * is the head of our chain). Insert to new ent and append to this chain. + */ + new_e->next = mainpos_e->next; + mainpos_e->next = new_e; + our_e = new_e; + } else { + /* Existing ent is not in its main position (it is a node in some other + * chain). This implies that no existing ent in the table has our hash. + * Evict it (updating its chain) and use its ent for head of our chain. */ + *new_e = *mainpos_e; /* copies next. */ + while (chain->next != mainpos_e) { + chain = (upb_tabent*)chain->next; + UPB_ASSERT(chain); + } + chain->next = new_e; + our_e = mainpos_e; + our_e->next = NULL; + } + } + our_e->key = tabkey; + our_e->val = val; + UPB_ASSERT(findentry(t, key, hash, eql) == our_e); +} + +static bool rm(upb_table* t, lookupkey_t key, upb_value* val, uint32_t hash, + eqlfunc_t* eql) { + upb_tabent* chain = getentry_mutable(t, hash); + if (upb_tabent_isempty(chain)) return false; + if (eql(chain->key, chain->val, key)) { + /* Element to remove is at the head of its chain. */ + t->count--; + if (val) *val = chain->val; + if (chain->next) { + upb_tabent* move = (upb_tabent*)chain->next; + *chain = *move; + move->key = upb_key_empty(); + } else { + chain->key = upb_key_empty(); + } + return true; + } else { + /* Element to remove is either in a non-head position or not in the + * table. */ + while (chain->next && !eql(chain->next->key, chain->next->val, key)) { + chain = (upb_tabent*)chain->next; + } + if (chain->next) { + /* Found element to remove. */ + upb_tabent* rm = (upb_tabent*)chain->next; + t->count--; + if (val) *val = chain->next->val; + rm->key = upb_key_empty(); + chain->next = rm->next; + return true; + } else { + /* Element to remove is not in the table. */ + return false; + } + } +} + +static size_t next(const upb_table* t, size_t i) { + do { + if (++i >= upb_table_size(t)) return SIZE_MAX - 1; /* Distinct from -1. */ + } while (upb_tabent_isempty(&t->entries[i])); + + return i; +} + +static size_t begin(const upb_table* t) { return next(t, -1); } + +/* upb_strtable ***************************************************************/ + +// A simple "subclass" of upb_table that only adds a hash function for strings. + +static upb_SizePrefixString* upb_SizePrefixString_Copy(upb_StringView s, + upb_Arena* a) { + // A 2GB string will fail at serialization time, but we accept up to 4GB in + // memory here. + if (s.size > UINT32_MAX) return NULL; + upb_SizePrefixString* str = + upb_Arena_Malloc(a, sizeof(uint32_t) + s.size + 1); + if (str == NULL) return NULL; + str->size = s.size; + char* data = (char*)str->data; + if (s.size) memcpy(data, s.data, s.size); + data[s.size] = '\0'; + return str; +} + +/* Adapted from ABSL's wyhash. */ + +static uint64_t UnalignedLoad64(const void* p) { + uint64_t val; + memcpy(&val, p, 8); + return val; +} + +static uint32_t UnalignedLoad32(const void* p) { + uint32_t val; + memcpy(&val, p, 4); + return val; +} + +#if defined(_MSC_VER) && defined(_M_X64) +#include +#endif + +/* Computes a * b, returning the low 64 bits of the result and storing the high + * 64 bits in |*high|. */ +static uint64_t upb_umul128(uint64_t v0, uint64_t v1, uint64_t* out_high) { +#ifdef __SIZEOF_INT128__ + __uint128_t p = v0; + p *= v1; + *out_high = (uint64_t)(p >> 64); + return (uint64_t)p; +#elif defined(_MSC_VER) && defined(_M_X64) + return _umul128(v0, v1, out_high); +#else + uint64_t a32 = v0 >> 32; + uint64_t a00 = v0 & 0xffffffff; + uint64_t b32 = v1 >> 32; + uint64_t b00 = v1 & 0xffffffff; + uint64_t high = a32 * b32; + uint64_t low = a00 * b00; + uint64_t mid1 = a32 * b00; + uint64_t mid2 = a00 * b32; + low += (mid1 << 32) + (mid2 << 32); + // Omit carry bit, for mixing we do not care about exact numerical precision. + high += (mid1 >> 32) + (mid2 >> 32); + *out_high = high; + return low; +#endif +} + +static uint64_t WyhashMix(uint64_t v0, uint64_t v1) { + uint64_t high; + uint64_t low = upb_umul128(v0, v1, &high); + return low ^ high; +} + +static uint64_t Wyhash(const void* data, size_t len, uint64_t seed, + const uint64_t salt[]) { + const uint8_t* ptr = (const uint8_t*)data; + uint64_t starting_length = (uint64_t)len; + uint64_t current_state = seed ^ salt[0]; + + if (len > 64) { + // If we have more than 64 bytes, we're going to handle chunks of 64 + // bytes at a time. We're going to build up two separate hash states + // which we will then hash together. + uint64_t duplicated_state = current_state; + + do { + uint64_t a = UnalignedLoad64(ptr); + uint64_t b = UnalignedLoad64(ptr + 8); + uint64_t c = UnalignedLoad64(ptr + 16); + uint64_t d = UnalignedLoad64(ptr + 24); + uint64_t e = UnalignedLoad64(ptr + 32); + uint64_t f = UnalignedLoad64(ptr + 40); + uint64_t g = UnalignedLoad64(ptr + 48); + uint64_t h = UnalignedLoad64(ptr + 56); + + uint64_t cs0 = WyhashMix(a ^ salt[1], b ^ current_state); + uint64_t cs1 = WyhashMix(c ^ salt[2], d ^ current_state); + current_state = (cs0 ^ cs1); + + uint64_t ds0 = WyhashMix(e ^ salt[3], f ^ duplicated_state); + uint64_t ds1 = WyhashMix(g ^ salt[4], h ^ duplicated_state); + duplicated_state = (ds0 ^ ds1); + + ptr += 64; + len -= 64; + } while (len > 64); + + current_state = current_state ^ duplicated_state; + } + + // We now have a data `ptr` with at most 64 bytes and the current state + // of the hashing state machine stored in current_state. + while (len > 16) { + uint64_t a = UnalignedLoad64(ptr); + uint64_t b = UnalignedLoad64(ptr + 8); + + current_state = WyhashMix(a ^ salt[1], b ^ current_state); + + ptr += 16; + len -= 16; + } + + // We now have a data `ptr` with at most 16 bytes. + uint64_t a = 0; + uint64_t b = 0; + if (len > 8) { + // When we have at least 9 and at most 16 bytes, set A to the first 64 + // bits of the input and B to the last 64 bits of the input. Yes, they will + // overlap in the middle if we are working with less than the full 16 + // bytes. + a = UnalignedLoad64(ptr); + b = UnalignedLoad64(ptr + len - 8); + } else if (len > 3) { + // If we have at least 4 and at most 8 bytes, set A to the first 32 + // bits and B to the last 32 bits. + a = UnalignedLoad32(ptr); + b = UnalignedLoad32(ptr + len - 4); + } else if (len > 0) { + // If we have at least 1 and at most 3 bytes, read all of the provided + // bits into A, with some adjustments. + a = ((ptr[0] << 16) | (ptr[len >> 1] << 8) | ptr[len - 1]); + b = 0; + } else { + a = 0; + b = 0; + } + + uint64_t w = WyhashMix(a ^ salt[1], b ^ current_state); + uint64_t z = salt[1] ^ starting_length; + return WyhashMix(w, z); +} + +const uint64_t kWyhashSalt[5] = { + 0x243F6A8885A308D3ULL, 0x13198A2E03707344ULL, 0xA4093822299F31D0ULL, + 0x082EFA98EC4E6C89ULL, 0x452821E638D01377ULL, +}; + +uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed) { + return Wyhash(p, n, seed, kWyhashSalt); +} + +static const void* const _upb_seed; + +// Returns a random seed for upb's hash function. This does not provide +// high-quality randomness, but it should be enough to prevent unit tests from +// relying on a deterministic map ordering. By returning the address of a +// variable, we are able to get some randomness for free provided that ASLR is +// enabled. +static uint64_t _upb_Seed(void) { return (uint64_t)&_upb_seed; } + +static uint32_t _upb_Hash_NoSeed(const char* p, size_t n) { + return _upb_Hash(p, n, _upb_Seed()); +} + +static uint32_t strhash(upb_key key, upb_value val) { + UPB_UNUSED(val); + return _upb_Hash_NoSeed(key.str->data, key.str->size); +} + +static bool streql(upb_key k1, upb_value v1, lookupkey_t k2) { + UPB_UNUSED(v1); + const upb_SizePrefixString* k1s = k1.str; + const upb_StringView k2s = k2.str; + return k1s->size == k2s.size && + (k1s->size == 0 || memcmp(k1s->data, k2s.data, k1s->size) == 0); +} + +/** Calculates the number of entries required to hold an expected number of + * values, within the table's load factor. */ +static size_t _upb_entries_needed_for(size_t expected_size) { + size_t need_entries = expected_size + 1 + expected_size / 7; + UPB_ASSERT(need_entries - (need_entries >> 3) >= expected_size); + return need_entries; +} + +bool upb_strtable_init(upb_strtable* t, size_t expected_size, upb_Arena* a) { + int size_lg2 = upb_Log2Ceiling(_upb_entries_needed_for(expected_size)); + return init(&t->t, size_lg2, a); +} + +void upb_strtable_clear(upb_strtable* t) { + size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent); + t->t.count = 0; + memset((char*)t->t.entries, 0, bytes); +} + +bool upb_strtable_resize(upb_strtable* t, size_t size_lg2, upb_Arena* a) { + upb_strtable new_table; + if (!init(&new_table.t, size_lg2, a)) return false; + + intptr_t iter = UPB_STRTABLE_BEGIN; + upb_StringView sv; + upb_value val; + while (upb_strtable_next2(t, &sv, &val, &iter)) { + // Unlike normal insert, does not copy string data or possibly reallocate + // the table + // The data pointer used in the table is guaranteed to point at a + // upb_SizePrefixString, we just need to back up by the size of the uint32_t + // length prefix. + const upb_SizePrefixString* keystr = + (const upb_SizePrefixString*)(sv.data - sizeof(uint32_t)); + UPB_ASSERT(keystr->data == sv.data); + UPB_ASSERT(keystr->size == sv.size); + + lookupkey_t lookupkey = {.str = sv}; + upb_key tabkey = {.str = keystr}; + uint32_t hash = _upb_Hash_NoSeed(sv.data, sv.size); + insert(&new_table.t, lookupkey, tabkey, val, hash, &strhash, &streql); + } + *t = new_table; + return true; +} + +bool upb_strtable_insert(upb_strtable* t, const char* k, size_t len, + upb_value v, upb_Arena* a) { + if (isfull(&t->t)) { + /* Need to resize. New table of double the size, add old elements to it. */ + if (!upb_strtable_resize(t, _upb_log2_table_size(&t->t) + 1, a)) { + return false; + } + } + + upb_StringView sv = upb_StringView_FromDataAndSize(k, len); + upb_SizePrefixString* size_prefix_string = upb_SizePrefixString_Copy(sv, a); + if (!size_prefix_string) return false; + + lookupkey_t lookupkey = {.str = sv}; + upb_key key = {.str = size_prefix_string}; + uint32_t hash = _upb_Hash_NoSeed(k, len); + insert(&t->t, lookupkey, key, v, hash, &strhash, &streql); + return true; +} + +bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len, + upb_value* v) { + uint32_t hash = _upb_Hash_NoSeed(key, len); + return lookup(&t->t, strkey2(key, len), v, hash, &streql); +} + +bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len, + upb_value* val) { + uint32_t hash = _upb_Hash_NoSeed(key, len); + return rm(&t->t, strkey2(key, len), val, hash, &streql); +} + +/* Iteration */ + +void upb_strtable_begin(upb_strtable_iter* i, const upb_strtable* t) { + i->t = t; + i->index = begin(&t->t); +} + +void upb_strtable_next(upb_strtable_iter* i) { + i->index = next(&i->t->t, i->index); +} + +bool upb_strtable_done(const upb_strtable_iter* i) { + if (!i->t) return true; + return i->index >= upb_table_size(&i->t->t) || + upb_tabent_isempty(str_tabent(i)); +} + +upb_StringView upb_strtable_iter_key(const upb_strtable_iter* i) { + UPB_ASSERT(!upb_strtable_done(i)); + return upb_key_strview(str_tabent(i)->key); +} + +upb_value upb_strtable_iter_value(const upb_strtable_iter* i) { + UPB_ASSERT(!upb_strtable_done(i)); + return str_tabent(i)->val; +} + +void upb_strtable_iter_setdone(upb_strtable_iter* i) { + i->t = NULL; + i->index = SIZE_MAX; +} + +bool upb_strtable_iter_isequal(const upb_strtable_iter* i1, + const upb_strtable_iter* i2) { + if (upb_strtable_done(i1) && upb_strtable_done(i2)) return true; + return i1->t == i2->t && i1->index == i2->index; +} + +bool upb_strtable_next2(const upb_strtable* t, upb_StringView* key, + upb_value* val, intptr_t* iter) { + size_t tab_idx = next(&t->t, *iter); + if (tab_idx < upb_table_size(&t->t)) { + upb_tabent* ent = &t->t.entries[tab_idx]; + *key = upb_key_strview(ent->key); + *val = ent->val; + *iter = tab_idx; + return true; + } + + return false; +} + +void upb_strtable_removeiter(upb_strtable* t, intptr_t* iter) { + intptr_t i = *iter; + upb_tabent* ent = &t->t.entries[i]; + upb_tabent* prev = NULL; + + // Linear search, not great. + upb_tabent* end = &t->t.entries[upb_table_size(&t->t)]; + for (upb_tabent* e = t->t.entries; e != end; e++) { + if (e->next == ent) { + prev = e; + break; + } + } + + if (prev) { + prev->next = ent->next; + } + + t->t.count--; + ent->key = upb_key_empty(); + ent->next = NULL; +} + +void upb_strtable_setentryvalue(upb_strtable* t, intptr_t iter, upb_value v) { + t->t.entries[iter].val = v; +} + +/* upb_exttable ***************************************************************/ + +static uint32_t _upb_exttable_hash(const void* ptr, uint32_t ext_num) { + uint64_t a = (uintptr_t)ptr; + uint64_t b = ext_num; + return (uint32_t)WyhashMix(a ^ kWyhashSalt[1], b ^ _upb_Seed()); +} + +static uint32_t exthash(upb_key key, upb_value val) { + const void* ptr = (const void*)key.num; + uint32_t ext_num = *(const uint32_t*)upb_value_getconstptr(val); + return _upb_exttable_hash(ptr, ext_num); +} + +static bool exteql(upb_key k1, upb_value v1, lookupkey_t k2) { + if ((const void*)k1.num == k2.ext.ptr) { + uint32_t ext_num1 = *(const uint32_t*)upb_value_getconstptr(v1); + return ext_num1 == k2.ext.ext_num; + } + return false; +} + +bool upb_exttable_init(upb_exttable* t, size_t expected_size, upb_Arena* a) { + int size_lg2 = upb_Log2Ceiling(_upb_entries_needed_for(expected_size)); + return init(&t->t, size_lg2, a); +} + +void upb_exttable_clear(upb_exttable* t) { + size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent); + t->t.count = 0; + memset((char*)t->t.entries, 0, bytes); +} + +bool upb_exttable_resize(upb_exttable* t, size_t size_lg2, upb_Arena* a) { + upb_exttable new_table; + if (!init(&new_table.t, size_lg2, a)) return false; + + size_t i; + for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) { + const upb_tabent* e = &t->t.entries[i]; + uint32_t hash = exthash(e->key, e->val); + uint32_t ext_num = *(const uint32_t*)upb_value_getconstptr(e->val); + lookupkey_t lookupkey = extkey((const void*)e->key.num, ext_num); + insert(&new_table.t, lookupkey, e->key, e->val, hash, &exthash, &exteql); + } + + *t = new_table; + return true; +} + +bool upb_exttable_insert(upb_exttable* t, const void* k, const uint32_t* v, + upb_Arena* a) { + UPB_ASSERT(k != NULL); + UPB_ASSERT(v != NULL); + UPB_ASSERT(*v != 0); + + if (isfull(&t->t)) { + if (!upb_exttable_resize(t, _upb_log2_table_size(&t->t) + 1, a)) { + return false; + } + } + + lookupkey_t lookupkey = extkey(k, *v); + upb_key key = {.num = (uintptr_t)k}; + upb_value val = upb_value_constptr(v); + uint32_t hash = _upb_exttable_hash(k, *v); + insert(&t->t, lookupkey, key, val, hash, &exthash, &exteql); + return true; +} + +const uint32_t* upb_exttable_lookup(const upb_exttable* t, const void* k, + uint32_t ext_number) { + uint32_t hash = _upb_exttable_hash(k, ext_number); + upb_value val; + if (lookup(&t->t, extkey(k, ext_number), &val, hash, &exteql)) { + return (const uint32_t*)upb_value_getconstptr(val); + } + return NULL; +} + +const uint32_t* upb_exttable_remove(upb_exttable* t, const void* k, + uint32_t ext_number) { + uint32_t hash = _upb_exttable_hash(k, ext_number); + upb_value val; + if (rm(&t->t, extkey(k, ext_number), &val, hash, &exteql)) { + return (const uint32_t*)upb_value_getconstptr(val); + } + return NULL; +} + +size_t upb_exttable_size(const upb_exttable* t) { return t->t.count; } + +/* upb_inttable ***************************************************************/ + +/* For inttables we use a hybrid structure where small keys are kept in an + * array and large keys are put in the hash table. */ + +// The sentinel value used in the dense array part. Note that callers must +// ensure that inttable is never used with a value of this sentinel type +// (pointers and u32 values will never be; i32 needs to be handled carefully +// to avoid sign-extending into this value). +static const upb_value kInttableSentinel = {.val = UINT64_MAX}; +static uint32_t presence_mask_arr_size(uint32_t array_size) { + return (array_size + 7) / 8; // sizeof(uint8_t) is always 1. +} + +static uint32_t inthash(upb_key key, upb_value val) { + UPB_UNUSED(val); + return upb_inthash(key.num); +} + +static bool inteql(upb_key k1, upb_value v1, lookupkey_t k2) { + UPB_UNUSED(v1); + return k1.num == k2.num; +} + +static upb_value* mutable_array(upb_inttable* t) { + return (upb_value*)t->array; +} + +static const upb_value* inttable_array_get(const upb_inttable* t, + uintptr_t key) { + UPB_ASSERT(key < t->array_size); + const upb_value* val = &t->array[key]; + return upb_inttable_arrhas(t, key) ? val : NULL; +} + +static upb_value* inttable_val(upb_inttable* t, uintptr_t key) { + if (key < t->array_size) { + return (upb_value*)inttable_array_get(t, key); + } else { + upb_tabent* e = + findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql); + return e ? &e->val : NULL; + } +} + +static const upb_value* inttable_val_const(const upb_inttable* t, + uintptr_t key) { + return inttable_val((upb_inttable*)t, key); +} + +size_t upb_inttable_count(const upb_inttable* t) { + return t->t.count + t->array_count; +} + +static void check(upb_inttable* t) { + UPB_UNUSED(t); +#if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG) + { + // This check is very expensive (makes inserts/deletes O(N)). + size_t count = 0; + intptr_t iter = UPB_INTTABLE_BEGIN; + uintptr_t key; + upb_value val; + while (upb_inttable_next(t, &key, &val, &iter)) { + UPB_ASSERT(upb_inttable_lookup(t, key, NULL)); + } + UPB_ASSERT(count == upb_inttable_count(t)); + } +#endif +} + +bool upb_inttable_sizedinit(upb_inttable* t, uint32_t asize, int hsize_lg2, + upb_Arena* a) { + if (!init(&t->t, hsize_lg2, a)) return false; + /* Always make the array part at least 1 long, so that we know key 0 + * won't be in the hash part, which simplifies things. */ + t->array_size = UPB_MAX(1, asize); + t->array_count = 0; +#if UINT32_MAX >= SIZE_MAX + if (UPB_UNLIKELY(SIZE_MAX / sizeof(upb_value) < t->array_size)) { + return false; + } +#endif + + // Allocate the array part and the presence mask array in one allocation. + size_t array_bytes = t->array_size * sizeof(upb_value); + uint32_t presence_bytes = presence_mask_arr_size(t->array_size); + uintptr_t total_bytes = array_bytes + presence_bytes; + if (UPB_UNLIKELY(total_bytes > SIZE_MAX)) { + return false; + } + void* alloc = upb_Arena_Malloc(a, total_bytes); + if (!alloc) { + return false; + } + t->array = alloc; + memset(mutable_array(t), 0xff, array_bytes); + t->presence_mask = (uint8_t*)alloc + array_bytes; + memset((uint8_t*)t->presence_mask, 0, presence_bytes); + + check(t); + return true; +} + +bool upb_inttable_init(upb_inttable* t, upb_Arena* a) { + // The init size of the table part to match that of strtable. + return upb_inttable_sizedinit(t, 0, 3, a); +} + +bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val, + upb_Arena* a) { + if (key < t->array_size) { + UPB_ASSERT(!upb_inttable_arrhas(t, key)); + t->array_count++; + mutable_array(t)[key] = val; + ((uint8_t*)t->presence_mask)[key / 8] |= (1 << (key % 8)); + } else { + if (isfull(&t->t)) { + /* Need to resize the hash part, but we re-use the array part. */ + size_t i; + upb_table new_table; + + if (!init(&new_table, _upb_log2_table_size(&t->t) + 1, a)) { + return false; + } + + for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) { + const upb_tabent* e = &t->t.entries[i]; + insert(&new_table, intkey(e->key.num), e->key, e->val, + inthash(e->key, e->val), &inthash, &inteql); + } + + UPB_ASSERT(t->t.count == new_table.count); + + t->t = new_table; + } + upb_key tabkey = {.num = key}; + insert(&t->t, intkey(key), tabkey, val, upb_inthash(key), &inthash, + &inteql); + } + check(t); + return true; +} + +bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v) { + const upb_value* table_v = inttable_val_const(t, key); + if (!table_v) return false; + if (v) *v = *table_v; + return true; +} + +bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val) { + upb_value* table_v = inttable_val(t, key); + if (!table_v) return false; + *table_v = val; + return true; +} + +bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val) { + bool success; + if (key < t->array_size) { + if (upb_inttable_arrhas(t, key)) { + t->array_count--; + if (val) { + *val = t->array[key]; + } + mutable_array(t)[key] = kInttableSentinel; + ((uint8_t*)t->presence_mask)[key / 8] &= ~(1 << (key % 8)); + success = true; + } else { + success = false; + } + } else { + success = rm(&t->t, intkey(key), val, upb_inthash(key), &inteql); + } + check(t); + return success; +} + +bool upb_inttable_compact(upb_inttable* t, upb_Arena* a) { + /* A power-of-two histogram of the table keys. */ + uint32_t counts[UPB_MAXARRSIZE + 1] = {0}; + + /* The max key in each bucket. */ + uintptr_t max[UPB_MAXARRSIZE + 1] = {0}; + + { + intptr_t iter = UPB_INTTABLE_BEGIN; + uintptr_t key; + upb_value val; + while (upb_inttable_next(t, &key, &val, &iter)) { + int bucket = log2ceil(key); + max[bucket] = UPB_MAX(max[bucket], key); + counts[bucket]++; + } + } + + /* Find the largest power of two that satisfies the MIN_DENSITY + * definition (while actually having some keys). */ + uint32_t arr_count = upb_inttable_count(t); + + // Scan all buckets except capped bucket + int size_lg2 = ARRAY_SIZE(counts) - 1; + for (; size_lg2 > 0; size_lg2--) { + if (counts[size_lg2] == 0) { + /* We can halve again without losing any entries. */ + continue; + } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) { + break; + } + + arr_count -= counts[size_lg2]; + } + + UPB_ASSERT(arr_count <= upb_inttable_count(t)); + + upb_inttable new_t; + { + /* Insert all elements into new, perfectly-sized table. */ + uintptr_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */ + uint32_t hash_count = upb_inttable_count(t) - arr_count; + size_t hash_size = hash_count ? _upb_entries_needed_for(hash_count) : 0; + int hashsize_lg2 = log2ceil(hash_size); + + if (!upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a)) { + return false; + } + + { + intptr_t iter = UPB_INTTABLE_BEGIN; + uintptr_t key; + upb_value val; + while (upb_inttable_next(t, &key, &val, &iter)) { + upb_inttable_insert(&new_t, key, val, a); + } + } + + UPB_ASSERT(new_t.array_size == arr_size); + } + *t = new_t; + return true; +} + +void upb_inttable_clear(upb_inttable* t) { + // Clear the array part. + size_t array_bytes = t->array_size * sizeof(upb_value); + t->array_count = 0; + // Clear the array by setting all bits to 1, as UINT64_MAX is the sentinel + // value for an empty array. + memset(mutable_array(t), 0xff, array_bytes); + // Clear the presence mask array. + memset((uint8_t*)t->presence_mask, 0, presence_mask_arr_size(t->array_size)); + // Clear the table part. + size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent); + t->t.count = 0; + memset((char*)t->t.entries, 0, bytes); +} + +// Iteration. + +bool upb_inttable_next(const upb_inttable* t, uintptr_t* key, upb_value* val, + intptr_t* iter) { + intptr_t i = *iter; + if ((size_t)(i + 1) <= t->array_size) { + while ((size_t)++i < t->array_size) { + const upb_value* ent = inttable_array_get(t, i); + if (ent) { + *key = i; + *val = *ent; + *iter = i; + return true; + } + } + i--; // Back up to exactly one position before the start of the table. + } + + size_t tab_idx = next(&t->t, i - t->array_size); + if (tab_idx < upb_table_size(&t->t)) { + upb_tabent* ent = &t->t.entries[tab_idx]; + *key = ent->key.num; + *val = ent->val; + *iter = tab_idx + t->array_size; + return true; + } else { + // We should set the iterator any way. When we are done, the iterator value + // is invalidated. `upb_inttable_done` will check on the iterator value to + // determine if the iteration is done. + *iter = INTPTR_MAX - 1; // To disambiguate from UPB_INTTABLE_BEGIN, to + // match the behavior of `upb_strtable_iter`. + return false; + } +} + +void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter) { + intptr_t i = *iter; + if ((size_t)i < t->array_size) { + t->array_count--; + mutable_array(t)[i].val = -1; + } else { + upb_tabent* ent = &t->t.entries[i - t->array_size]; + upb_tabent* prev = NULL; + + // Linear search, not great. + upb_tabent* end = &t->t.entries[upb_table_size(&t->t)]; + for (upb_tabent* e = t->t.entries; e != end; e++) { + if (e->next == ent) { + prev = e; + break; + } + } + + if (prev) { + prev->next = ent->next; + } + + t->t.count--; + ent->key = upb_key_empty(); + ent->next = NULL; + } +} + +void upb_inttable_setentryvalue(upb_inttable* t, intptr_t iter, upb_value v) { + if ((size_t)iter < t->array_size) { + mutable_array(t)[iter] = v; + } else { + upb_tabent* ent = &t->t.entries[iter - t->array_size]; + ent->val = v; + } +} + +bool upb_inttable_done(const upb_inttable* t, intptr_t iter) { + if ((uintptr_t)iter >= t->array_size + upb_table_size(&t->t)) { + return true; + } else if ((size_t)iter < t->array_size) { + return !upb_inttable_arrhas(t, iter); + } else { + return upb_tabent_isempty(&t->t.entries[iter - t->array_size]); + } +} + +uintptr_t upb_inttable_iter_key(const upb_inttable* t, intptr_t iter) { + UPB_ASSERT(!upb_inttable_done(t, iter)); + return (size_t)iter < t->array_size + ? iter + : t->t.entries[iter - t->array_size].key.num; +} + +upb_value upb_inttable_iter_value(const upb_inttable* t, intptr_t iter) { + UPB_ASSERT(!upb_inttable_done(t, iter)); + if ((size_t)iter < t->array_size) { + return t->array[iter]; + } else { + return t->t.entries[iter - t->array_size].val; + } +} + + +// Must be last. + +const char* upb_BufToUint64(const char* ptr, const char* end, uint64_t* val) { + uint64_t u64 = 0; + while (ptr < end) { + unsigned ch = *ptr - '0'; + if (ch >= 10) break; + if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) { + return NULL; // integer overflow + } + u64 *= 10; + u64 += ch; + ptr++; + } + + *val = u64; + return ptr; +} + +const char* upb_BufToInt64(const char* ptr, const char* end, int64_t* val, + bool* is_neg) { + bool neg = false; + uint64_t u64; + + if (ptr != end && *ptr == '-') { + ptr++; + neg = true; + } + + ptr = upb_BufToUint64(ptr, end, &u64); + if (!ptr || u64 > (uint64_t)INT64_MAX + neg) { + return NULL; // integer overflow + } + + *val = neg ? -u64 : u64; + if (is_neg) *is_neg = neg; + return ptr; +} + + +#include +#include +#include +#include + +// Must be last. + +/* Miscellaneous utilities ****************************************************/ + +static void upb_FixLocale(char* p) { + /* printf() is dependent on locales; sadly there is no easy and portable way + * to avoid this. This little post-processing step will translate 1,2 -> 1.2 + * since JSON needs the latter. Arguably a hack, but it is simple and the + * alternatives are far more complicated, platform-dependent, and/or larger + * in code size. */ + for (; *p; p++) { + if (*p == ',') *p = '.'; + } +} + +void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size) { + assert(size >= kUpb_RoundTripBufferSize); + if (isnan(val)) { + snprintf(buf, size, "%s", "nan"); + return; + } + snprintf(buf, size, "%.*g", DBL_DIG, val); + if (strtod(buf, NULL) != val) { + snprintf(buf, size, "%.*g", DBL_DIG + 2, val); + assert(strtod(buf, NULL) == val); + } + upb_FixLocale(buf); +} + +void _upb_EncodeRoundTripFloat(float val, char* buf, size_t size) { + assert(size >= kUpb_RoundTripBufferSize); + if (isnan(val)) { + snprintf(buf, size, "%s", "nan"); + return; + } + snprintf(buf, size, "%.*g", FLT_DIG, val); + if (strtof(buf, NULL) != val) { + snprintf(buf, size, "%.*g", FLT_DIG + 3, val); + assert(strtof(buf, NULL) == val); + } + upb_FixLocale(buf); +} + + +#include +#include + +// Must be last. + +// Determine the locale-specific radix character by calling sprintf() to print +// the number 1.5, then stripping off the digits. As far as I can tell, this +// is the only portable, thread-safe way to get the C library to divulge the +// locale's radix character. No, localeconv() is NOT thread-safe. + +static int GetLocaleRadix(char *data, size_t capacity) { + char temp[16]; + const int size = snprintf(temp, sizeof(temp), "%.1f", 1.5); + UPB_ASSERT(temp[0] == '1'); + UPB_ASSERT(temp[size - 1] == '5'); + if (size < capacity) { + return 0; + } + temp[size - 1] = '\0'; + strncpy(data, temp + 1, size); + return size - 2; +} + +// Populates a string identical to *input except that the character pointed to +// by pos (which should be '.') is replaced with the locale-specific radix. + +static void LocalizeRadix(const char *input, const char *pos, char *output, + int output_size) { + const int len1 = pos - input; + + char radix[8]; + const int len2 = GetLocaleRadix(radix, sizeof(radix)); + + const int n = output_size - len1 - len2 - 1; + if (n < 0) { + return; + } + + memcpy(output, input, len1); + memcpy(output + len1, radix, len2); + strncpy(output + len1 + len2, input + len1 + 1, n); + output[output_size - 1] = '\0'; +} + +double _upb_NoLocaleStrtod(const char *str, char **endptr) { + // We cannot simply set the locale to "C" temporarily with setlocale() + // as this is not thread-safe. Instead, we try to parse in the current + // locale first. If parsing stops at a '.' character, then this is a + // pretty good hint that we're actually in some other locale in which + // '.' is not the radix character. + + char *temp_endptr; + double result = strtod(str, &temp_endptr); + if (endptr != NULL) *endptr = temp_endptr; + if (*temp_endptr != '.') return result; + + // Parsing halted on a '.'. Perhaps we're in a different locale? Let's + // try to replace the '.' with a locale-specific radix character and + // try again. + + char localized[80]; + LocalizeRadix(str, temp_endptr, localized, sizeof localized); + char *localized_endptr; + result = strtod(localized, &localized_endptr); + if ((localized_endptr - &localized[0]) > (temp_endptr - str)) { + // This attempt got further, so replacing the decimal must have helped. + // Update endptr to point at the right location. + if (endptr != NULL) { + // size_diff is non-zero if the localized radix has multiple bytes. + int size_diff = strlen(localized) - strlen(str); + *endptr = (char *)str + (localized_endptr - &localized[0] - size_diff); + } + } + + return result; +} + + +// Must be last. + +int upb_Unicode_ToUTF8(uint32_t cp, char* out) { + if (cp <= 0x7f) { + out[0] = cp; + return 1; + } + if (cp <= 0x07ff) { + out[0] = (cp >> 6) | 0xc0; + out[1] = (cp & 0x3f) | 0x80; + return 2; + } + if (cp <= 0xffff) { + out[0] = (cp >> 12) | 0xe0; + out[1] = ((cp >> 6) & 0x3f) | 0x80; + out[2] = (cp & 0x3f) | 0x80; + return 3; + } + if (cp <= 0x10ffff) { + out[0] = (cp >> 18) | 0xf0; + out[1] = ((cp >> 12) & 0x3f) | 0x80; + out[2] = ((cp >> 6) & 0x3f) | 0x80; + out[3] = (cp & 0x3f) | 0x80; + return 4; + } + return 0; +} + + +#include + +// Must be last. + +static void* upb_global_allocfunc(upb_alloc* alloc, void* ptr, size_t oldsize, + size_t size, size_t* actual_size) { + UPB_UNUSED(alloc); + UPB_UNUSED(oldsize); + UPB_UNUSED(actual_size); + if (size == 0) { + free(ptr); + return NULL; + } else { + return realloc(ptr, size); + } +} + +upb_alloc upb_alloc_global = {&upb_global_allocfunc}; + + +#include + + +#ifdef UPB_TRACING_ENABLED +#include +#endif + +#include +#include + + +// Must be last. + +static UPB_ATOMIC(size_t) g_max_block_size = UPB_DEFAULT_MAX_BLOCK_SIZE; + +void upb_Arena_SetMaxBlockSize(size_t max) { + UPB_ASSERT(max <= UINT32_MAX); + upb_Atomic_Store(&g_max_block_size, max, memory_order_relaxed); +} + +typedef struct upb_MemBlock { + struct upb_MemBlock* next; + // Size of the actual allocation. + // Size of 0 means this is a upb_ArenaRef. + size_t size; + // Data follows. +} upb_MemBlock; + +// A special block type that indicates a reference to another arena. +// When this arena is freed, a ref is released on the referenced arena. +// size must be 0. +typedef struct upb_ArenaRef { + upb_MemBlock prefix; // size is always zero + const upb_Arena* arena; +#ifndef NDEBUG + const struct upb_ArenaRef* next_ref; +#endif +} upb_ArenaRef; + +typedef struct upb_ArenaInternal { + // upb_alloc* together with a low bit which signals if there is an initial + // block. + uintptr_t block_alloc; + + // Linked list of blocks to free/cleanup. + upb_MemBlock* blocks; + +#ifndef NDEBUG + // Stack of pointers to other arenas that this arena owns. + // Used for debug-only ref cycle checks. + UPB_ATOMIC(const upb_ArenaRef*) refs; +#endif + + // Size of the last block we allocated in the normal exponential scheme. + uint32_t last_block_size; + + // A hint that grows whenever we perform a "one-off" allocation into a a + // dedicated block. This helps us determine if these outlier blocks are + // actually common enough that we should switch back to the normal exponential + // scheme at the larger size. + uint32_t size_hint; + + // All non atomic members used during allocation must be above this point, and + // are used by _SwapIn/_SwapOut + + // Total space allocated in blocks, atomic only for SpaceAllocated + UPB_ATOMIC(uintptr_t) space_allocated; + + // The cleanup for the allocator. This is called after all the blocks are + // freed in an arena. + upb_AllocCleanupFunc* upb_alloc_cleanup; + + // When multiple arenas are fused together, each arena points to a parent + // arena (root points to itself). The root tracks how many live arenas + // reference it. + + // The low bit is tagged: + // 0: pointer to parent + // 1: count, left shifted by one + UPB_ATOMIC(uintptr_t) parent_or_count; + + // All nodes that are fused together are in a singly-linked list. + // == NULL at end of list. + UPB_ATOMIC(struct upb_ArenaInternal*) next; + + // - If the low bit is set, is a pointer to the tail of the list (populated + // for roots, set to self for roots with no fused arenas). This is best + // effort, and it may not always reflect the true tail, but it will always + // be a valid node in the list. This is useful for finding the list tail + // without having to walk the entire list. + // - If the low bit is not set, is a pointer to the previous node in the list, + // such that a->previous_or_tail->next == a. + UPB_ATOMIC(uintptr_t) previous_or_tail; + + // We use a different UPB_XSAN_MEMBER than the one in upb_Arena because the + // two are distinct synchronization domains. The upb_Arena.ptr member is + // not published in the allocation path, so it is not synchronized with + // respect to operations performed in this file such as Fuse, Free, + // SpaceAllocated, etc. This means that it is not safe to read or write + // the upb_Arena.ptr member in those functions. + UPB_XSAN_MEMBER +} upb_ArenaInternal; + +// All public + private state for an arena. +typedef struct { + upb_Arena head; + upb_ArenaInternal body; +} upb_ArenaState; + +typedef struct { + upb_ArenaInternal* root; + uintptr_t tagged_count; +} upb_ArenaRoot; + +static const size_t kUpb_MemblockReserve = + UPB_ALIGN_MALLOC(sizeof(upb_MemBlock)); + +static const size_t kUpb_ArenaRefReserve = + UPB_ALIGN_MALLOC(sizeof(upb_ArenaRef)); + +// Extracts the (upb_ArenaInternal*) from a (upb_Arena*) +static upb_ArenaInternal* upb_Arena_Internal(const upb_Arena* a) { + return &((upb_ArenaState*)a)->body; +} + +static bool _upb_Arena_IsTaggedRefcount(uintptr_t parent_or_count) { + return (parent_or_count & 1) == 1; +} + +static bool _upb_Arena_IsTaggedPointer(uintptr_t parent_or_count) { + return (parent_or_count & 1) == 0; +} + +static uintptr_t _upb_Arena_RefCountFromTagged(uintptr_t parent_or_count) { + UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count)); + return parent_or_count >> 1; +} + +static uintptr_t _upb_Arena_TaggedFromRefcount(uintptr_t refcount) { + uintptr_t parent_or_count = (refcount << 1) | 1; + UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count)); + return parent_or_count; +} + +static upb_ArenaInternal* _upb_Arena_PointerFromTagged( + uintptr_t parent_or_count) { + UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count)); + return (upb_ArenaInternal*)parent_or_count; +} + +static uintptr_t _upb_Arena_TaggedFromPointer(upb_ArenaInternal* ai) { + uintptr_t parent_or_count = (uintptr_t)ai; + UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count)); + return parent_or_count; +} + +static bool _upb_Arena_IsTaggedTail(uintptr_t previous_or_tail) { + return (previous_or_tail & 1) == 1; +} + +static bool _upb_Arena_IsTaggedPrevious(uintptr_t previous_or_tail) { + return (previous_or_tail & 1) == 0; +} + +static upb_ArenaInternal* _upb_Arena_TailFromTagged( + uintptr_t previous_or_tail) { + UPB_ASSERT(_upb_Arena_IsTaggedTail(previous_or_tail)); + return (upb_ArenaInternal*)(previous_or_tail ^ 1); +} + +static uintptr_t _upb_Arena_TaggedFromTail(upb_ArenaInternal* tail) { + uintptr_t previous_or_tail = (uintptr_t)tail | 1; + UPB_ASSERT(_upb_Arena_IsTaggedTail(previous_or_tail)); + return previous_or_tail; +} + +static upb_ArenaInternal* _upb_Arena_PreviousFromTagged( + uintptr_t previous_or_tail) { + UPB_ASSERT(_upb_Arena_IsTaggedPrevious(previous_or_tail)); + return (upb_ArenaInternal*)previous_or_tail; +} + +static uintptr_t _upb_Arena_TaggedFromPrevious(upb_ArenaInternal* ai) { + uintptr_t previous = (uintptr_t)ai; + UPB_ASSERT(_upb_Arena_IsTaggedPrevious(previous)); + return previous; +} + +static upb_alloc* _upb_ArenaInternal_BlockAlloc(upb_ArenaInternal* ai) { + return (upb_alloc*)(ai->block_alloc & ~0x1); +} + +static uintptr_t _upb_Arena_MakeBlockAlloc(upb_alloc* alloc, bool has_initial) { + uintptr_t alloc_uint = (uintptr_t)alloc; + UPB_ASSERT((alloc_uint & 1) == 0); + return alloc_uint | (has_initial ? 1 : 0); +} + +static bool _upb_ArenaInternal_HasInitialBlock(upb_ArenaInternal* ai) { + return ai->block_alloc & 0x1; +} + +#ifdef UPB_TRACING_ENABLED +static void (*_init_arena_trace_handler)(const upb_Arena*, size_t size) = NULL; +static void (*_fuse_arena_trace_handler)(const upb_Arena*, + const upb_Arena*) = NULL; +static void (*_free_arena_trace_handler)(const upb_Arena*) = NULL; + +void upb_Arena_SetTraceHandler( + void (*initArenaTraceHandler)(const upb_Arena*, size_t size), + void (*fuseArenaTraceHandler)(const upb_Arena*, const upb_Arena*), + void (*freeArenaTraceHandler)(const upb_Arena*)) { + _init_arena_trace_handler = initArenaTraceHandler; + _fuse_arena_trace_handler = fuseArenaTraceHandler; + _free_arena_trace_handler = freeArenaTraceHandler; +} + +void upb_Arena_LogInit(const upb_Arena* arena, size_t size) { + if (_init_arena_trace_handler) { + _init_arena_trace_handler(arena, size); + } +} +void upb_Arena_LogFuse(const upb_Arena* arena1, const upb_Arena* arena2) { + if (_fuse_arena_trace_handler) { + _fuse_arena_trace_handler(arena1, arena2); + } +} +void upb_Arena_LogFree(const upb_Arena* arena) { + if (_free_arena_trace_handler) { + _free_arena_trace_handler(arena); + } +} +#endif // UPB_TRACING_ENABLED + +// If the param a is already the root, provides no memory order of refcount. +// If it has a parent, then acquire memory order is provided for both the root +// and the refcount. Thread safe. +static upb_ArenaRoot _upb_Arena_FindRoot(upb_ArenaInternal* ai) { + uintptr_t poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_relaxed); + if (_upb_Arena_IsTaggedRefcount(poc)) { + // Fast, relaxed path - arenas that have never been fused to a parent only + // need relaxed memory order, since they're returning themselves and the + // refcount. + return (upb_ArenaRoot){.root = ai, .tagged_count = poc}; + } + // Slow path needs acquire order; reloading is cheaper than a fence on ARM + // (LDA vs DMB ISH). Even though this is a reread, we know it must be a tagged + // pointer because if this Arena isn't a root, it can't ever become one. + poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire); + do { + upb_ArenaInternal* next = _upb_Arena_PointerFromTagged(poc); + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(next)); + UPB_ASSERT(ai != next); + poc = upb_Atomic_Load(&next->parent_or_count, memory_order_acquire); + + if (_upb_Arena_IsTaggedPointer(poc)) { + // To keep complexity down, we lazily collapse levels of the tree. This + // keeps it flat in the final case, but doesn't cost much incrementally. + // + // Path splitting keeps time complexity down, see: + // https://en.wikipedia.org/wiki/Disjoint-set_data_structure + UPB_ASSERT(ai != _upb_Arena_PointerFromTagged(poc)); + upb_Atomic_Store(&ai->parent_or_count, poc, memory_order_release); + } + ai = next; + } while (_upb_Arena_IsTaggedPointer(poc)); + return (upb_ArenaRoot){.root = ai, .tagged_count = poc}; +} + +uintptr_t upb_Arena_SpaceAllocated(const upb_Arena* arena, + size_t* fused_count) { + upb_ArenaInternal* ai = upb_Arena_Internal(arena); + uintptr_t memsize = 0; + size_t local_fused_count = 0; + // Our root would get updated by any racing fuses before our target arena + // became reachable from the root via the linked list; in order to preserve + // monotonic output (any arena counted by a previous invocation is counted by + // this one), we instead iterate forwards and backwards so that we only see + // the results of completed fuses. + uintptr_t previous_or_tail = + upb_Atomic_Load(&ai->previous_or_tail, memory_order_acquire); + while (_upb_Arena_IsTaggedPrevious(previous_or_tail)) { + upb_ArenaInternal* previous = + _upb_Arena_PreviousFromTagged(previous_or_tail); + UPB_ASSERT(previous != ai); + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(previous)); + // Unfortunate macro behavior; prior to C11 when using nonstandard atomics + // this returns a void* and can't be used with += without an intermediate + // conversion to an integer. + // Relaxed is safe - no subsequent reads depend this one + uintptr_t allocated = + upb_Atomic_Load(&previous->space_allocated, memory_order_relaxed); + memsize += allocated; + previous_or_tail = + upb_Atomic_Load(&previous->previous_or_tail, memory_order_acquire); + local_fused_count++; + } + while (ai != NULL) { + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(ai)); + // Unfortunate macro behavior; prior to C11 when using nonstandard atomics + // this returns a void* and can't be used with += without an intermediate + // conversion to an integer. + // Relaxed is safe - no subsequent reads depend this one + uintptr_t allocated = + upb_Atomic_Load(&ai->space_allocated, memory_order_relaxed); + memsize += allocated; + ai = upb_Atomic_Load(&ai->next, memory_order_acquire); + local_fused_count++; + } + + if (fused_count) *fused_count = local_fused_count; + return memsize; +} + +uint32_t upb_Arena_DebugRefCount(const upb_Arena* a) { + uintptr_t tagged = _upb_Arena_FindRoot(upb_Arena_Internal(a)).tagged_count; + return (uint32_t)_upb_Arena_RefCountFromTagged(tagged); +} + +#if UPB_ENABLE_REF_CYCLE_CHECKS + +bool upb_Arena_HasRefChain(const upb_Arena* from, const upb_Arena* to) { + upb_ArenaInternal* ai = upb_Arena_Internal(from); + upb_ArenaInternal* current; + + if (upb_Arena_IsFused(from, to)) return true; + + // 1. Traverse backward to the start of a consistent segment. + uintptr_t previous_or_tail = + upb_Atomic_Load(&ai->previous_or_tail, memory_order_acquire); + while (_upb_Arena_IsTaggedPrevious(previous_or_tail)) { + ai = _upb_Arena_PreviousFromTagged(previous_or_tail); + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(ai)); + previous_or_tail = + upb_Atomic_Load(&ai->previous_or_tail, memory_order_acquire); + } + + // 2. Traverse forward through all arenas in the fuse group. + current = ai; + while (current != NULL) { + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(current)); + const upb_ArenaRef* ref = + upb_Atomic_Load(¤t->refs, memory_order_acquire); + while (ref != NULL) { + if (ref->arena == to || upb_Arena_HasRefChain(ref->arena, to)) { + return true; + } + ref = ref->next_ref; + } + current = upb_Atomic_Load(¤t->next, memory_order_acquire); + } + return false; +} + +#endif + +static upb_MemBlock* _upb_Arena_AllocBlockInternal(upb_alloc* alloc, + size_t size) { + UPB_ASSERT(size >= kUpb_MemblockReserve); + upb_SizedPtr alloc_result = upb_SizeReturningMalloc(alloc, size); + if (!alloc_result.p) return NULL; + upb_MemBlock* block = alloc_result.p; + block->size = alloc_result.n; + return block; +} + +static upb_MemBlock* _upb_Arena_AllocBlock(upb_Arena* a, size_t size) { + upb_ArenaInternal* ai = upb_Arena_Internal(a); + return _upb_Arena_AllocBlockInternal(_upb_ArenaInternal_BlockAlloc(ai), size); +} + +static void _upb_Arena_AddBlock(upb_Arena* a, upb_MemBlock* block) { + upb_ArenaInternal* ai = upb_Arena_Internal(a); + + // Atomic add not required here, as threads won't race allocating blocks, plus + // atomic fetch-add is slower than load/add/store on arm devices compiled + // targeting pre-v8.1. Relaxed order is safe as nothing depends on order of + // size allocated. + uintptr_t old_space_allocated = + upb_Atomic_Load(&ai->space_allocated, memory_order_relaxed); + upb_Atomic_Store(&ai->space_allocated, old_space_allocated + block->size, + memory_order_relaxed); + + block->next = ai->blocks; + ai->blocks = block; +} + +static void _upb_Arena_UseBlockInternal(upb_Arena* a, upb_MemBlock* block, + size_t offset) { + size_t block_size = block->size; + char* start = UPB_PTR_AT(block, kUpb_MemblockReserve + offset, char); + a->UPB_PRIVATE(ptr) = start; + a->UPB_PRIVATE(end) = UPB_PTR_AT(block, block_size, char); + UPB_PRIVATE(upb_Xsan_PoisonRegion)(start, a->UPB_PRIVATE(end) - start); + UPB_PRIVATE(upb_Xsan_Init)(UPB_XSAN(a)); + UPB_ASSERT(UPB_PRIVATE(_upb_ArenaHas)(a) >= + block_size - kUpb_MemblockReserve - offset); +} + +static void _upb_Arena_UseBlock(upb_Arena* a, upb_MemBlock* block) { + _upb_Arena_UseBlockInternal(a, block, 0); +} + +static bool _upb_Arena_WouldReduceFreeSpace(upb_Arena* a, size_t size, + size_t block_size) { + upb_ArenaInternal* ai = upb_Arena_Internal(a); + size_t current_free = + ai->blocks ? a->UPB_PRIVATE(end) - a->UPB_PRIVATE(ptr) : 0; + size_t future_free = block_size - kUpb_MemblockReserve - size; + return current_free >= future_free; +} + +// Fulfills the allocation request by allocating a new block. Returns NULL on +// allocation failure. +void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(upb_Arena* a, size_t size) { + upb_ArenaInternal* ai = upb_Arena_Internal(a); + if (!ai->block_alloc) return NULL; + + // Whether to satisfy the allocation from a one-off block which is right-sized + // for the current allocation. We do this if we suspect that the current + // allocation is an outlier that does not represent the typical size of + // allocations from this arena, or if we would reduce free space by + // using exponential growth. + bool one_off = false; + + // Relaxed order is safe here as we don't need any ordering with the setter. + size_t max_block_size = + upb_Atomic_Load(&g_max_block_size, memory_order_relaxed); + size_t block_size = UPB_MIN(ai->last_block_size * 2, max_block_size); + + if (size + kUpb_MemblockReserve > block_size) { + // A regular doubling would not yield a large enough block. Does size_hint + // indicate that we have consistently needed large blocks? + block_size = UPB_MIN(ai->size_hint * 2, max_block_size); + if (size + kUpb_MemblockReserve > block_size) { + // Even size_hint is not large enough, we will have to do a one-off. + one_off = true; + } + } + + // If switching to a block of this size would *reduce* available free space, + // we might as well make a one-off block instead. + one_off = one_off || _upb_Arena_WouldReduceFreeSpace(a, size, block_size); + + if (one_off) { + // Note: this may exceed the max block size, but that's okay. + block_size = size + kUpb_MemblockReserve; + } + + upb_MemBlock* block = _upb_Arena_AllocBlock(a, block_size); + if (!block) return NULL; + + _upb_Arena_AddBlock(a, block); + + // Recheck size, in case the allocator gave us a much larger block than we + // requested and we want to make it the new allocating region. + if (UPB_UNLIKELY(one_off) && + _upb_Arena_WouldReduceFreeSpace(a, size, block->size)) { + // Increase size_hint, so that a series of one-off allocations will + // eventually convince us to switch to exponential growth at the larger + // size. + ai->size_hint = UPB_MIN(ai->size_hint + (size >> 1), max_block_size >> 1); + char* allocated = UPB_PTR_AT(block, kUpb_MemblockReserve, char); + char* poison_start = allocated + size - UPB_PRIVATE(kUpb_Asan_GuardSize); + UPB_PRIVATE(upb_Xsan_PoisonRegion)( + poison_start, UPB_PTR_AT(block, block->size, char) - poison_start); + return allocated; + } else { + ai->last_block_size = UPB_MIN(block->size, UINT32_MAX); + ai->size_hint = ai->last_block_size; + _upb_Arena_UseBlock(a, block); + UPB_ASSERT(UPB_PRIVATE(_upb_ArenaHas)(a) >= size); + return upb_Arena_Malloc(a, size - UPB_PRIVATE(kUpb_Asan_GuardSize)); + } +} + +static upb_Arena* _upb_Arena_InitSlow(upb_alloc* alloc, size_t first_size) { + const size_t first_block_overhead = + UPB_ALIGN_MALLOC(kUpb_MemblockReserve + sizeof(upb_ArenaState)); + upb_ArenaState* a; + + if (!alloc) return NULL; + + // We need to malloc the initial block. + size_t block_size = + first_block_overhead + UPB_MAX(256, UPB_ALIGN_MALLOC(first_size) + + UPB_PRIVATE(kUpb_Asan_GuardSize)); + upb_MemBlock* block = _upb_Arena_AllocBlockInternal(alloc, block_size); + if (!block) return NULL; + + // Initialize the arena state in the first block. We "borrow" the memory from + // the block, because we can't yet call upb_Arena_Malloc. + a = UPB_PTR_AT(block, kUpb_MemblockReserve, upb_ArenaState); + a->body.block_alloc = _upb_Arena_MakeBlockAlloc(alloc, 0); + a->body.last_block_size = UPB_MIN(block->size, UINT32_MAX); + a->body.size_hint = UPB_MIN(block->size, UINT32_MAX); + upb_Atomic_Init(&a->body.parent_or_count, _upb_Arena_TaggedFromRefcount(1)); + upb_Atomic_Init(&a->body.next, NULL); + upb_Atomic_Init(&a->body.previous_or_tail, + _upb_Arena_TaggedFromTail(&a->body)); + upb_Atomic_Init(&a->body.space_allocated, 0); + a->body.blocks = NULL; +#ifndef NDEBUG + a->body.refs = NULL; +#endif + a->body.upb_alloc_cleanup = NULL; + UPB_PRIVATE(upb_Xsan_Init)(UPB_XSAN(&a->body)); + + _upb_Arena_AddBlock(&a->head, block); + _upb_Arena_UseBlockInternal(&a->head, block, + UPB_ALIGN_MALLOC(sizeof(upb_ArenaState))); + + return &a->head; +} + +upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc) { + UPB_STATIC_ASSERT(UPB_ARENA_SIZE_HACK >= sizeof(upb_ArenaState), + "Need to update UPB_ARENA_SIZE_HACK"); + upb_ArenaState* a; + + if (mem) { + /* Align initial pointer up so that we return properly-aligned pointers. */ + void* aligned = (void*)UPB_ALIGN_MALLOC((uintptr_t)mem); + size_t delta = (uintptr_t)aligned - (uintptr_t)mem; + n = delta <= n ? n - delta : 0; + mem = aligned; + } + if (UPB_UNLIKELY(n < sizeof(upb_ArenaState) || !mem)) { + upb_Arena* ret = _upb_Arena_InitSlow(alloc, mem ? 0 : n); +#ifdef UPB_TRACING_ENABLED + upb_Arena_LogInit(ret, n); +#endif + return ret; + } + + a = mem; + + upb_Atomic_Init(&a->body.parent_or_count, _upb_Arena_TaggedFromRefcount(1)); + upb_Atomic_Init(&a->body.next, NULL); + upb_Atomic_Init(&a->body.previous_or_tail, + _upb_Arena_TaggedFromTail(&a->body)); + upb_Atomic_Init(&a->body.space_allocated, 0); + a->body.blocks = NULL; +#ifndef NDEBUG + a->body.refs = NULL; +#endif + a->body.size_hint = 128; + a->body.last_block_size = 128; + a->body.upb_alloc_cleanup = NULL; + a->body.block_alloc = _upb_Arena_MakeBlockAlloc(alloc, 1); + a->head.UPB_PRIVATE(ptr) = (void*)UPB_ALIGN_MALLOC((uintptr_t)(a + 1)); + a->head.UPB_PRIVATE(end) = UPB_PTR_AT(mem, n, char); + UPB_PRIVATE(upb_Xsan_Init)(UPB_XSAN(&a->body)); +#ifdef UPB_TRACING_ENABLED + upb_Arena_LogInit(&a->head, n); +#endif + return &a->head; +} + +static void _upb_Arena_DoFree(upb_ArenaInternal* ai) { + UPB_ASSERT(_upb_Arena_RefCountFromTagged(ai->parent_or_count) == 1); + while (ai != NULL) { + UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(ai)); + // Load first since arena itself is likely from one of its blocks. Relaxed + // order is safe because fused arena ordering is provided by the reference + // count, and fuse is not permitted to race with the final decrement. + upb_ArenaInternal* next_arena = + (upb_ArenaInternal*)upb_Atomic_Load(&ai->next, memory_order_relaxed); + // Freeing may have memory barriers that confuse tsan, so assert immediately + // after load here + if (next_arena) { + UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(next_arena)); + } + upb_alloc* block_alloc = _upb_ArenaInternal_BlockAlloc(ai); + upb_MemBlock* block = ai->blocks; + upb_AllocCleanupFunc* alloc_cleanup = *ai->upb_alloc_cleanup; + while (block != NULL) { + // Load first since we are deleting block. + upb_MemBlock* next_block = block->next; + if (block->size == 0) { + // If the block is an arena ref, then we need to release our ref on the + // referenced arena. + upb_ArenaRef* ref = (upb_ArenaRef*)block; + upb_Arena_DecRefFor((upb_Arena*)ref->arena, ai); + } else { + upb_free_sized(block_alloc, block, block->size); + } + block = next_block; + } + if (alloc_cleanup != NULL) { + alloc_cleanup(block_alloc); + } + ai = next_arena; + } +} + +void upb_Arena_Free(upb_Arena* a) { + upb_ArenaInternal* ai = upb_Arena_Internal(a); + // Cannot be replaced with _upb_Arena_FindRoot, as that provides only a + // relaxed read of the refcount if ai is already the root. + uintptr_t poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire); +retry: + while (_upb_Arena_IsTaggedPointer(poc)) { + ai = _upb_Arena_PointerFromTagged(poc); + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(ai)); + poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire); + } + + // compare_exchange or fetch_sub are RMW operations, which are more + // expensive then direct loads. As an optimization, we only do RMW ops + // when we need to update things for other threads to see. + if (poc == _upb_Arena_TaggedFromRefcount(1)) { +#ifdef UPB_TRACING_ENABLED + upb_Arena_LogFree(a); +#endif + _upb_Arena_DoFree(ai); + return; + } + + if (upb_Atomic_CompareExchangeWeak( + &ai->parent_or_count, &poc, + _upb_Arena_TaggedFromRefcount(_upb_Arena_RefCountFromTagged(poc) - 1), + memory_order_release, memory_order_acquire)) { + // We were >1 and we decremented it successfully, so we are done. + return; + } + + // We failed our update, so someone has done something, retry the whole + // process, but the failed exchange reloaded `poc` for us. + goto retry; +} + +// Logically performs the following operation, in a way that is safe against +// racing fuses: +// ret = TAIL(parent) +// ret->next = child +// return ret +// +// The caller is therefore guaranteed that ret->next == child. +static upb_ArenaInternal* _upb_Arena_LinkForward( + upb_ArenaInternal* const parent, upb_ArenaInternal* child) { + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(parent)); + uintptr_t parent_previous_or_tail = + upb_Atomic_Load(&parent->previous_or_tail, memory_order_acquire); + + // Optimization: use parent->previous_or_tail to skip to TAIL(parent) in O(1) + // time when possible. This is the common case because we just fused into + // parent, suggesting that it should be a root with a cached tail. + // + // However, if there was a racing fuse, parent may no longer be a root, in + // which case we need to walk the entire list to find the tail. The tail + // pointer is also not guaranteed to be the true tail, so even when the + // optimization is taken, we still need to walk list nodes to find the true + // tail. + upb_ArenaInternal* parent_tail = + _upb_Arena_IsTaggedTail(parent_previous_or_tail) + ? _upb_Arena_TailFromTagged(parent_previous_or_tail) + : parent; + + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(parent_tail)); + upb_ArenaInternal* parent_tail_next = + upb_Atomic_Load(&parent_tail->next, memory_order_acquire); + + do { + // Walk the list to find the true tail (a node with next == NULL). + while (parent_tail_next != NULL) { + parent_tail = parent_tail_next; + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(parent_tail)); + parent_tail_next = + upb_Atomic_Load(&parent_tail->next, memory_order_acquire); + } + } while (!upb_Atomic_CompareExchangeWeak( // Replace a NULL next with child. + &parent_tail->next, &parent_tail_next, child, memory_order_release, + memory_order_acquire)); + + return parent_tail; +} + +// Updates parent->previous_or_tail = child->previous_or_tail in hopes that the +// latter represents the true tail of the newly-combined list. +// +// This is a best-effort operation that may set the tail to a stale value, and +// may fail to update the tail at all. +void _upb_Arena_UpdateParentTail(upb_ArenaInternal* parent, + upb_ArenaInternal* child) { + // We are guaranteed that child->previous_or_tail is tagged, because we have + // just transitioned child from root -> non-root, which is an exclusive + // operation that can only happen once. So we are the exclusive updater of + // child->previous_or_tail that can transition it from tagged to untagged. + // + // However, we are not guaranteed that child->previous_or_tail is the true + // tail. A racing fuse may have appended to child's list but not yet updated + // child->previous_or_tail. + uintptr_t child_previous_or_tail = + upb_Atomic_Load(&child->previous_or_tail, memory_order_acquire); + upb_ArenaInternal* new_parent_tail = + _upb_Arena_TailFromTagged(child_previous_or_tail); + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(new_parent_tail)); + + // If another thread fused with parent, such that it is no longer a root, + // don't overwrite their previous pointer with our tail. Relaxed order is fine + // here as we only inspect the tag bit. + uintptr_t parent_previous_or_tail = + upb_Atomic_Load(&parent->previous_or_tail, memory_order_relaxed); + if (_upb_Arena_IsTaggedTail(parent_previous_or_tail)) { + upb_Atomic_CompareExchangeStrong( + &parent->previous_or_tail, &parent_previous_or_tail, + _upb_Arena_TaggedFromTail(new_parent_tail), memory_order_release, + memory_order_relaxed); + } +} + +static void _upb_Arena_LinkBackward(upb_ArenaInternal* child, + upb_ArenaInternal* old_parent_tail) { + // Link child to parent going backwards, for SpaceAllocated. This transitions + // child->previous_or_tail from tail (tagged) to previous (untagged), after + // which its value is immutable. + // + // - We are guaranteed that no other threads are also attempting to perform + // this transition (tail -> previous), because we just updated + // old_parent_tail->next from NULL to non-NULL, an exclusive operation that + // can only happen once. + // + // - _upb_Arena_UpdateParentTail() uses CAS to ensure that it + // does not perform the reverse transition (previous -> tail). + // + // - We are guaranteed that old_parent_tail is the correct "previous" pointer, + // even in the presence of racing fuses that are adding more nodes to the + // list, because _upb_Arena_LinkForward() guarantees that: + // old_parent_tail->next == child. + upb_Atomic_Store(&child->previous_or_tail, + _upb_Arena_TaggedFromPrevious(old_parent_tail), + memory_order_release); +} + +static void _upb_Arena_DoFuseArenaLists(upb_ArenaInternal* const parent, + upb_ArenaInternal* child) { + upb_ArenaInternal* old_parent_tail = _upb_Arena_LinkForward(parent, child); + _upb_Arena_UpdateParentTail(parent, child); + _upb_Arena_LinkBackward(child, old_parent_tail); +} + +void upb_Arena_SetAllocCleanup(upb_Arena* a, upb_AllocCleanupFunc* func) { + UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(a)); + upb_ArenaInternal* ai = upb_Arena_Internal(a); + UPB_ASSERT(ai->upb_alloc_cleanup == NULL); + ai->upb_alloc_cleanup = func; +} + +// Thread safe. +static upb_ArenaInternal* _upb_Arena_DoFuse(upb_ArenaInternal** ai1, + upb_ArenaInternal** ai2, + uintptr_t* ref_delta) { + // `parent_or_count` has two distinct modes + // - parent pointer mode + // - refcount mode + // + // In parent pointer mode, it may change what pointer it refers to in the + // tree, but it will always approach a root. Any operation that walks the + // tree to the root may collapse levels of the tree concurrently. + upb_ArenaRoot r1 = _upb_Arena_FindRoot(*ai1); + upb_ArenaRoot r2 = _upb_Arena_FindRoot(*ai2); + + if (r1.root == r2.root) return r1.root; // Already fused. + + *ai1 = r1.root; + *ai2 = r2.root; + + // Avoid cycles by always fusing into the root with the lower address. + if ((uintptr_t)r1.root > (uintptr_t)r2.root) { + upb_ArenaRoot tmp = r1; + r1 = r2; + r2 = tmp; + } + + // The moment we install `r1` as the parent for `r2` all racing frees may + // immediately begin decrementing `r1`'s refcount (including pending + // increments to that refcount and their frees!). We need to add `r2`'s refs + // now, so that `r1` can withstand any unrefs that come from r2. + // + // Note that while it is possible for `r2`'s refcount to increase + // asynchronously, we will not actually do the reparenting operation below + // unless `r2`'s refcount is unchanged from when we read it. + // + // Note that we may have done this previously, either to this node or a + // different node, during a previous and failed DoFuse() attempt. But we will + // not lose track of these refs because we always add them to our overall + // delta. + uintptr_t r2_untagged_count = r2.tagged_count & ~1; + uintptr_t with_r2_refs = r1.tagged_count + r2_untagged_count; + if (!upb_Atomic_CompareExchangeStrong( + &r1.root->parent_or_count, &r1.tagged_count, with_r2_refs, + memory_order_release, memory_order_acquire)) { + return NULL; + } + + // Perform the actual fuse by removing the refs from `r2` and swapping in the + // parent pointer. + if (!upb_Atomic_CompareExchangeStrong( + &r2.root->parent_or_count, &r2.tagged_count, + _upb_Arena_TaggedFromPointer(r1.root), memory_order_release, + memory_order_acquire)) { + // We'll need to remove the excess refs we added to r1 previously. + *ref_delta += r2_untagged_count; + return NULL; + } + + // Now that the fuse has been performed (and can no longer fail) we need to + // append `r2` to `r1`'s linked list. + _upb_Arena_DoFuseArenaLists(r1.root, r2.root); + return r1.root; +} + +// Thread safe. +static bool _upb_Arena_FixupRefs(upb_ArenaInternal* new_root, + uintptr_t ref_delta) { + if (ref_delta == 0) return true; // No fixup required. + // Relaxed order is safe here as if the value is a pointer, we don't deref it + // or publish it anywhere else. The refcount does provide memory order + // between allocations on arenas and the eventual free and thus normally + // requires acquire/release; but in this case any edges provided by the refs + // we are cleaning up were already provided by the fuse operation itself. It's + // not valid for a decrement that could cause the overall fused arena to reach + // a zero refcount to race with this function, as that could result in a + // use-after-free anyway. + uintptr_t poc = + upb_Atomic_Load(&new_root->parent_or_count, memory_order_relaxed); + if (_upb_Arena_IsTaggedPointer(poc)) return false; + uintptr_t with_refs = poc - ref_delta; + UPB_ASSERT(!_upb_Arena_IsTaggedPointer(with_refs)); + // Relaxed order on success is safe here, for the same reasons as the relaxed + // read above. Relaxed order is safe on failure because the updated value is + // stored in a local variable which goes immediately out of scope; the retry + // loop will reread what it needs with proper memory order. + return upb_Atomic_CompareExchangeStrong(&new_root->parent_or_count, &poc, + with_refs, memory_order_relaxed, + memory_order_relaxed); +} + +bool upb_Arena_Fuse(const upb_Arena* a1, const upb_Arena* a2) { + if (a1 == a2) return true; // trivial fuse + +#ifdef UPB_TRACING_ENABLED + upb_Arena_LogFuse(a1, a2); +#endif + + upb_ArenaInternal* ai1 = upb_Arena_Internal(a1); + upb_ArenaInternal* ai2 = upb_Arena_Internal(a2); + + // Do not fuse initial blocks since we cannot lifetime extend them. + // Any other fuse scenario is allowed. + if (_upb_ArenaInternal_HasInitialBlock(ai1) || + _upb_ArenaInternal_HasInitialBlock(ai2)) { + return false; + } + + // The number of refs we ultimately need to transfer to the new root. + uintptr_t ref_delta = 0; + while (true) { + upb_ArenaInternal* new_root = _upb_Arena_DoFuse(&ai1, &ai2, &ref_delta); + if (new_root != NULL && _upb_Arena_FixupRefs(new_root, ref_delta)) { +#if UPB_ENABLE_REF_CYCLE_CHECKS + UPB_ASSERT(!upb_Arena_HasRefChain(a1, a2)); +#endif + return true; + } + } +} + +bool upb_Arena_IsFused(const upb_Arena* a, const upb_Arena* b) { + if (a == b) return true; // trivial fuse + upb_ArenaInternal* ra = _upb_Arena_FindRoot(upb_Arena_Internal(a)).root; + upb_ArenaInternal* rb = upb_Arena_Internal(b); + while (true) { + rb = _upb_Arena_FindRoot(rb).root; + if (ra == rb) return true; + upb_ArenaInternal* tmp = _upb_Arena_FindRoot(ra).root; + if (ra == tmp) return false; + // a's root changed since we last checked. Retry. + ra = tmp; + } +} + +bool upb_Arena_IncRefFor(const upb_Arena* a, const void* owner) { + upb_ArenaInternal* ai = upb_Arena_Internal(a); + if (_upb_ArenaInternal_HasInitialBlock(ai)) return false; + upb_ArenaRoot r; + r.root = ai; + +retry: + r = _upb_Arena_FindRoot(r.root); + if (upb_Atomic_CompareExchangeWeak( + &r.root->parent_or_count, &r.tagged_count, + _upb_Arena_TaggedFromRefcount( + _upb_Arena_RefCountFromTagged(r.tagged_count) + 1), + // Relaxed order is safe on success, incrementing the refcount + // need not perform any synchronization with the eventual free of the + // arena - that's provided by decrements. + memory_order_relaxed, + // Relaxed order is safe on failure as r.tagged_count is immediately + // overwritten by retrying the find root operation. + memory_order_relaxed)) { + // We incremented it successfully, so we are done. + return true; + } + // We failed update due to parent switching on the arena. + goto retry; +} + +void upb_Arena_DecRefFor(const upb_Arena* a, const void* owner) { + upb_Arena_Free((upb_Arena*)a); +} + +bool upb_Arena_RefArena(upb_Arena* from, const upb_Arena* to) { + UPB_ASSERT(!upb_Arena_IsFused(from, to)); + if (_upb_ArenaInternal_HasInitialBlock(upb_Arena_Internal(to))) { + // We can't increment a ref to `to`, so return early. + return false; + } + + upb_ArenaInternal* ai = upb_Arena_Internal(from); + upb_ArenaRef* ref = upb_Arena_Malloc(from, kUpb_ArenaRefReserve); + + if (!ref) { + return false; + } + + // When 'from' is freed, a ref on 'to' will be released. + // Intentionally ignore return value, since we already check up above if this + // call will succeed. + bool result = upb_Arena_IncRefFor(to, from); + UPB_ASSERT(result); + + // When we add a reference from `from` to `to`, we need to keep track of the + // ref in the `from` arena's linked list of refs. This allows us to + // walk all refs for `from` when `from` is freed, and thus allows us to + // decrement the refcount on `to` when `from` is freed. + ref->prefix.next = ai->blocks; + ref->prefix.size = 0; + ref->arena = to; + ai->blocks = (upb_MemBlock*)ref; + +#ifndef NDEBUG + // Add to the dedicated list of refs. + // This function is not thread-safe from `from`, so a simple load/store is + // sufficient. + ref->next_ref = upb_Atomic_Load(&ai->refs, memory_order_relaxed); + upb_Atomic_Store(&ai->refs, ref, memory_order_release); +#endif + +#if UPB_ENABLE_REF_CYCLE_CHECKS + UPB_ASSERT(!upb_Arena_HasRefChain(to, from)); // Forbid cycles. +#endif + + return true; +} + +#ifndef NDEBUG +bool upb_Arena_HasRef(const upb_Arena* from, const upb_Arena* to) { + const upb_ArenaInternal* ai = upb_Arena_Internal(from); + const upb_ArenaRef* ref = upb_Atomic_Load(&ai->refs, memory_order_acquire); + while (ref != NULL) { + if (upb_Arena_IsFused(ref->arena, to)) { + return true; + } + ref = ref->next_ref; + } + return false; +} +#endif + +upb_alloc* upb_Arena_GetUpbAlloc(upb_Arena* a) { + UPB_PRIVATE(upb_Xsan_AccessReadOnly)(UPB_XSAN(a)); + upb_ArenaInternal* ai = upb_Arena_Internal(a); + return _upb_ArenaInternal_BlockAlloc(ai); +} + +void UPB_PRIVATE(_upb_Arena_SwapIn)(upb_Arena* des, const upb_Arena* src) { + memcpy(des, src, offsetof(upb_ArenaState, body.space_allocated)); + upb_ArenaInternal* desi = upb_Arena_Internal(des); + upb_ArenaInternal* srci = upb_Arena_Internal(src); + uintptr_t new_space_allocated = + upb_Atomic_Load(&srci->space_allocated, memory_order_relaxed); + upb_Atomic_Store(&desi->space_allocated, new_space_allocated, + memory_order_relaxed); +} + +void UPB_PRIVATE(_upb_Arena_SwapOut)(upb_Arena* des, const upb_Arena* src) { + UPB_PRIVATE(_upb_Arena_SwapIn)(des, src); +} + +bool _upb_Arena_WasLastAlloc(struct upb_Arena* a, void* ptr, size_t oldsize) { + upb_ArenaInternal* ai = upb_Arena_Internal(a); + upb_MemBlock* block = ai->blocks; + // Skip any arena refs. + while (block != NULL && block->size == 0) { + block = block->next; + } + if (block == NULL) return false; + char* start = UPB_PTR_AT(block, kUpb_MemblockReserve, char); + return UPB_PRIVATE(upb_Xsan_PtrEq)(ptr, start) && + UPB_PRIVATE(_upb_Arena_AllocSpan)(oldsize) == + block->size - kUpb_MemblockReserve; +} + + + +// Must be last. + +bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTableField* f, + upb_Message* map_entry_message, upb_Arena* arena) { + UPB_ASSERT(!upb_Message_IsFrozen(map_entry_message)); + const upb_MiniTable* map_entry_mini_table = + upb_MiniTable_MapEntrySubMessage(f); + UPB_ASSERT(map_entry_mini_table); + const upb_MiniTableField* map_entry_key_field = + upb_MiniTable_MapKey(map_entry_mini_table); + const upb_MiniTableField* map_entry_value_field = + upb_MiniTable_MapValue(map_entry_mini_table); + // Map key/value cannot have explicit defaults, + // hence assuming a zero default is valid. + upb_MessageValue default_val = upb_MessageValue_Zero(); + upb_MessageValue map_entry_key = + upb_Message_GetField(map_entry_message, map_entry_key_field, default_val); + upb_MessageValue map_entry_value = upb_Message_GetField( + map_entry_message, map_entry_value_field, default_val); + return upb_Map_Set(map, map_entry_key, map_entry_value, arena); +} + + +#include +#include + + +// Must be last. + +upb_Array* upb_Array_New(upb_Arena* a, upb_CType type) { + const int lg2 = UPB_PRIVATE(_upb_CType_SizeLg2)(type); + return UPB_PRIVATE(_upb_Array_New)(a, 4, lg2); +} + +upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i) { + UPB_ASSERT(i < upb_Array_Size(arr)); + upb_MessageValue ret; + const char* data = upb_Array_DataPtr(arr); + const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr); + memcpy(&ret, data + (i << lg2), 1 << lg2); + return ret; +} + +upb_Message* upb_Array_GetMutable(upb_Array* arr, size_t i) { + UPB_ASSERT(i < upb_Array_Size(arr)); + size_t elem_size = sizeof(upb_Message*); + UPB_ASSERT(elem_size == (1 << UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr))); + char* data = upb_Array_MutableDataPtr(arr); + upb_Message* ret; + memcpy(&ret, data + (i * elem_size), elem_size); + UPB_ASSERT(!upb_Message_IsFrozen(ret)); + return ret; +} + +void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val) { + UPB_ASSERT(!upb_Array_IsFrozen(arr)); + UPB_ASSERT(i < upb_Array_Size(arr)); + char* data = upb_Array_MutableDataPtr(arr); + const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr); + memcpy(data + (i << lg2), &val, 1 << lg2); +} + +bool upb_Array_Append(upb_Array* arr, upb_MessageValue val, upb_Arena* arena) { + UPB_ASSERT(!upb_Array_IsFrozen(arr)); + UPB_ASSERT(arena); + if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + upb_Array_Set(arr, arr->UPB_PRIVATE(size) - 1, val); + return true; +} + +bool upb_Array_Copy(upb_Array* dst, const upb_Array* src, upb_Arena* arena) { + UPB_ASSERT(dst); + UPB_ASSERT(src); + UPB_ASSERT(!upb_Array_IsFrozen(dst)); + if (dst == src) return true; + size_t len = upb_Array_Size(src); + if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(dst, len, arena)) { + return false; + } + if (len == 0) return true; + const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(dst); + const int src_lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(src); + UPB_ASSERT(lg2 == src_lg2); + char* dst_data = upb_Array_MutableDataPtr(dst); + const char* src_data = upb_Array_DataPtr(src); + memcpy(dst_data, src_data, len << lg2); + return true; +} + +bool upb_Array_AppendAll(upb_Array* dst, const upb_Array* src, + upb_Arena* arena) { + UPB_ASSERT(!upb_Array_IsFrozen(dst)); + UPB_ASSERT(src); + size_t src_len = upb_Array_Size(src); + if (src_len == 0) return true; + size_t dst_len = upb_Array_Size(dst); + size_t len = dst_len + src_len; + if (UPB_UNLIKELY(len < dst_len)) return false; + if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(dst, len, arena)) { + return false; + } + const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(dst); + const int src_lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(src); + UPB_ASSERT(lg2 == src_lg2); + char* dst_data = upb_Array_MutableDataPtr(dst); + const char* src_data = upb_Array_DataPtr(src); + memcpy(dst_data + (dst_len << lg2), src_data, src_len << lg2); + return true; +} + +void upb_Array_Move(upb_Array* arr, size_t dst_idx, size_t src_idx, + size_t count) { + UPB_ASSERT(!upb_Array_IsFrozen(arr)); + const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr); + char* data = upb_Array_MutableDataPtr(arr); + memmove(&data[dst_idx << lg2], &data[src_idx << lg2], count << lg2); +} + +bool upb_Array_Insert(upb_Array* arr, size_t i, size_t count, + upb_Arena* arena) { + UPB_ASSERT(!upb_Array_IsFrozen(arr)); + UPB_ASSERT(arena); + UPB_ASSERT(i <= arr->UPB_PRIVATE(size)); + UPB_ASSERT(count + arr->UPB_PRIVATE(size) >= count); + const size_t oldsize = arr->UPB_PRIVATE(size); + if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + count, arena)) { + return false; + } + upb_Array_Move(arr, i + count, i, oldsize - i); + return true; +} + +/* + * i end arr->size + * |------------|XXXXXXXX|--------| + */ +void upb_Array_Delete(upb_Array* arr, size_t i, size_t count) { + UPB_ASSERT(!upb_Array_IsFrozen(arr)); + const size_t end = i + count; + UPB_ASSERT(i <= end); + UPB_ASSERT(end <= arr->UPB_PRIVATE(size)); + upb_Array_Move(arr, i, end, arr->UPB_PRIVATE(size) - end); + arr->UPB_PRIVATE(size) -= count; +} + +bool upb_Array_Resize(upb_Array* arr, size_t size, upb_Arena* arena) { + UPB_ASSERT(!upb_Array_IsFrozen(arr)); + const size_t oldsize = arr->UPB_PRIVATE(size); + if (UPB_UNLIKELY( + !UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena))) { + return false; + } + const size_t newsize = arr->UPB_PRIVATE(size); + if (newsize > oldsize) { + const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr); + char* data = upb_Array_MutableDataPtr(arr); + memset(data + (oldsize << lg2), 0, (newsize - oldsize) << lg2); + } + return true; +} + +bool UPB_PRIVATE(_upb_Array_Realloc)(upb_Array* array, size_t min_capacity, + upb_Arena* arena) { + size_t new_capacity = UPB_MAX(array->UPB_PRIVATE(capacity), 4); + const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array); + size_t old_bytes = array->UPB_PRIVATE(capacity) << lg2; + void* ptr = upb_Array_MutableDataPtr(array); + + // Log2 ceiling of size. + while (new_capacity < min_capacity) { + if (upb_ShlOverflow(&new_capacity, 1)) { + new_capacity = SIZE_MAX; + break; + } + } + + size_t new_bytes = new_capacity; + if (upb_ShlOverflow(&new_bytes, lg2)) { + return false; + } + ptr = upb_Arena_Realloc(arena, ptr, old_bytes, new_bytes); + if (!ptr) return false; + + UPB_PRIVATE(_upb_Array_SetTaggedPtr)(array, ptr, lg2); + array->UPB_PRIVATE(capacity) = new_capacity; + return true; +} + +void upb_Array_Freeze(upb_Array* arr, const upb_MiniTable* m) { + if (upb_Array_IsFrozen(arr)) return; + UPB_PRIVATE(_upb_Array_ShallowFreeze)(arr); + + if (m) { + const size_t size = upb_Array_Size(arr); + + for (size_t i = 0; i < size; i++) { + upb_MessageValue val = upb_Array_Get(arr, i); + upb_Message_Freeze((upb_Message*)val.msg_val, m); + } + } +} + + +#include +#include + + +// Must be last. + +bool upb_Message_NextExtensionReverse(const upb_Message* msg, + const upb_MiniTableExtension** result, + uintptr_t* iter) { + upb_MessageValue val; + return UPB_PRIVATE(_upb_Message_NextExtensionReverse)(msg, result, &val, + iter); +} + +const upb_MiniTableExtension* upb_Message_FindExtensionByNumber( + const upb_Message* msg, uint32_t field_number) { + uintptr_t iter = kUpb_Message_ExtensionBegin; + const upb_MiniTableExtension* result; + while (upb_Message_NextExtensionReverse(msg, &result, &iter)) { + if (upb_MiniTableExtension_Number(result) == field_number) return result; + } + return NULL; +} + + +#include +#include + + +// Must be last. + +// Strings/bytes are special-cased in maps. +char _upb_Map_CTypeSizeTable[12] = { + [kUpb_CType_Bool] = 1, + [kUpb_CType_Float] = 4, + [kUpb_CType_Int32] = 4, + [kUpb_CType_UInt32] = 4, + [kUpb_CType_Enum] = 4, + [kUpb_CType_Message] = sizeof(void*), + [kUpb_CType_Double] = 8, + [kUpb_CType_Int64] = 8, + [kUpb_CType_UInt64] = 8, + [kUpb_CType_String] = UPB_MAPTYPE_STRING, + [kUpb_CType_Bytes] = UPB_MAPTYPE_STRING, +}; + +upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, upb_CType value_type) { + return _upb_Map_New(a, _upb_Map_CTypeSize(key_type), + _upb_Map_CTypeSize(value_type)); +} + +size_t upb_Map_Size(const upb_Map* map) { return _upb_Map_Size(map); } + +bool upb_Map_Get(const upb_Map* map, upb_MessageValue key, + upb_MessageValue* val) { + return _upb_Map_Get(map, &key, map->key_size, val, map->val_size); +} + +struct upb_Message* upb_Map_GetMutable(upb_Map* map, upb_MessageValue key) { + UPB_ASSERT(map->val_size == sizeof(upb_Message*)); + upb_Message* val = NULL; + if (_upb_Map_Get(map, &key, map->key_size, &val, sizeof(upb_Message*))) { + return val; + } else { + return NULL; + } +} + +void upb_Map_Clear(upb_Map* map) { _upb_Map_Clear(map); } + +upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key, + upb_MessageValue val, upb_Arena* arena) { + UPB_ASSERT(arena); + return (upb_MapInsertStatus)_upb_Map_Insert(map, &key, map->key_size, &val, + map->val_size, arena); +} + +bool upb_Map_Delete(upb_Map* map, upb_MessageValue key, upb_MessageValue* val) { + upb_value v; + const bool removed = _upb_Map_Delete(map, &key, map->key_size, &v); + if (val) _upb_map_fromvalue(v, val, map->val_size); + return removed; +} + +bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key, + upb_MessageValue* val, size_t* iter) { + upb_value v; + bool ret; + if (map->UPB_PRIVATE(is_strtable)) { + upb_StringView strkey; + ret = upb_strtable_next2(&map->t.strtable, &strkey, &v, (intptr_t*)iter); + if (ret) { + _upb_map_fromkey(strkey, key, map->key_size); + } + } else { + uintptr_t intkey; + ret = upb_inttable_next(&map->t.inttable, &intkey, &v, (intptr_t*)iter); + if (ret) { + memcpy(key, &intkey, map->key_size); + } + } + if (ret) { + _upb_map_fromvalue(v, val, map->val_size); + } + return ret; +} + +UPB_API void upb_Map_SetEntryValue(upb_Map* map, size_t iter, + upb_MessageValue val) { + upb_value v; + _upb_map_tovalue(&val, map->val_size, &v, NULL); + if (map->UPB_PRIVATE(is_strtable)) { + upb_strtable_setentryvalue(&map->t.strtable, iter, v); + } else { + upb_inttable_setentryvalue(&map->t.inttable, iter, v); + } +} + +bool upb_MapIterator_Next(const upb_Map* map, size_t* iter) { + return _upb_map_next(map, iter); +} + +bool upb_MapIterator_Done(const upb_Map* map, size_t iter) { + UPB_ASSERT(iter != kUpb_Map_Begin); + if (map->UPB_PRIVATE(is_strtable)) { + upb_strtable_iter i; + i.t = &map->t.strtable; + i.index = iter; + return upb_strtable_done(&i); + } else { + return upb_inttable_done(&map->t.inttable, iter); + } +} + +// Returns the key and value for this entry of the map. +upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter) { + upb_MessageValue ret; + if (map->UPB_PRIVATE(is_strtable)) { + upb_strtable_iter i; + i.t = &map->t.strtable; + i.index = iter; + _upb_map_fromkey(upb_strtable_iter_key(&i), &ret, map->key_size); + } else { + uintptr_t intkey = upb_inttable_iter_key(&map->t.inttable, iter); + memcpy(&ret, &intkey, map->key_size); + } + return ret; +} + +upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter) { + upb_value v; + if (map->UPB_PRIVATE(is_strtable)) { + upb_strtable_iter i; + i.t = &map->t.strtable; + i.index = iter; + v = upb_strtable_iter_value(&i); + } else { + v = upb_inttable_iter_value(&map->t.inttable, iter); + } + + upb_MessageValue ret; + _upb_map_fromvalue(v, &ret, map->val_size); + return ret; +} + +void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m) { + if (upb_Map_IsFrozen(map)) return; + UPB_PRIVATE(_upb_Map_ShallowFreeze)(map); + + if (m) { + size_t iter = kUpb_Map_Begin; + upb_MessageValue key, val; + + while (upb_Map_Next(map, &key, &val, &iter)) { + upb_Message_Freeze((upb_Message*)val.msg_val, m); + } + } +} + +// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE ///////////////////////// + +upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size) { + upb_Map* map = upb_Arena_Malloc(a, sizeof(upb_Map)); + if (!map) return NULL; + + if (key_size <= sizeof(uintptr_t) && key_size != UPB_MAPTYPE_STRING) { + if (!upb_inttable_init(&map->t.inttable, a)) return NULL; + map->UPB_PRIVATE(is_strtable) = false; + } else { + if (!upb_strtable_init(&map->t.strtable, 4, a)) return NULL; + map->UPB_PRIVATE(is_strtable) = true; + } + map->key_size = key_size; + map->val_size = value_size; + map->UPB_PRIVATE(is_frozen) = false; + + return map; +} + + +#include +#include +#include + + +// Must be last. + +static int _upb_mapsorter_intkeys(const void* _a, const void* _b) { + const upb_tabent* const* a = _a; + const upb_tabent* const* b = _b; + uintptr_t a_key = (*a)->key.num; + uintptr_t b_key = (*b)->key.num; + return a_key < b_key ? -1 : a_key > b_key; +} + +static void _upb_mapsorter_getkeys(const void* _a, const void* _b, void* a_key, + void* b_key, size_t size) { + const upb_tabent* const* a = _a; + const upb_tabent* const* b = _b; + upb_StringView a_tabkey = upb_key_strview((*a)->key); + upb_StringView b_tabkey = upb_key_strview((*b)->key); + _upb_map_fromkey(a_tabkey, a_key, size); + _upb_map_fromkey(b_tabkey, b_key, size); +} + +static int _upb_mapsorter_cmpi64(const void* _a, const void* _b) { + int64_t a, b; + _upb_mapsorter_getkeys(_a, _b, &a, &b, 8); + return a < b ? -1 : a > b; +} + +static int _upb_mapsorter_cmpu64(const void* _a, const void* _b) { + uint64_t a, b; + _upb_mapsorter_getkeys(_a, _b, &a, &b, 8); + return a < b ? -1 : a > b; +} + +static int _upb_mapsorter_cmpi32(const void* _a, const void* _b) { + int32_t a, b; + _upb_mapsorter_getkeys(_a, _b, &a, &b, 4); + return a < b ? -1 : a > b; +} + +static int _upb_mapsorter_cmpu32(const void* _a, const void* _b) { + uint32_t a, b; + _upb_mapsorter_getkeys(_a, _b, &a, &b, 4); + return a < b ? -1 : a > b; +} + +static int _upb_mapsorter_cmpbool(const void* _a, const void* _b) { + bool a, b; + _upb_mapsorter_getkeys(_a, _b, &a, &b, 1); + return a < b ? -1 : a > b; +} + +static int _upb_mapsorter_cmpstr(const void* _a, const void* _b) { + upb_StringView a, b; + _upb_mapsorter_getkeys(_a, _b, &a, &b, UPB_MAPTYPE_STRING); + size_t common_size = UPB_MIN(a.size, b.size); + int cmp = memcmp(a.data, b.data, common_size); + if (cmp) return -cmp; + return a.size < b.size ? -1 : a.size > b.size; +} + +static int (*const compar[kUpb_FieldType_SizeOf])(const void*, const void*) = { + [kUpb_FieldType_Int64] = _upb_mapsorter_cmpi64, + [kUpb_FieldType_SFixed64] = _upb_mapsorter_cmpi64, + [kUpb_FieldType_SInt64] = _upb_mapsorter_cmpi64, + + [kUpb_FieldType_UInt64] = _upb_mapsorter_cmpu64, + [kUpb_FieldType_Fixed64] = _upb_mapsorter_cmpu64, + + [kUpb_FieldType_Int32] = _upb_mapsorter_cmpi32, + [kUpb_FieldType_SInt32] = _upb_mapsorter_cmpi32, + [kUpb_FieldType_SFixed32] = _upb_mapsorter_cmpi32, + [kUpb_FieldType_Enum] = _upb_mapsorter_cmpi32, + + [kUpb_FieldType_UInt32] = _upb_mapsorter_cmpu32, + [kUpb_FieldType_Fixed32] = _upb_mapsorter_cmpu32, + + [kUpb_FieldType_Bool] = _upb_mapsorter_cmpbool, + + [kUpb_FieldType_String] = _upb_mapsorter_cmpstr, + [kUpb_FieldType_Bytes] = _upb_mapsorter_cmpstr, +}; + +static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted, + int size) { + sorted->start = s->size; + sorted->pos = sorted->start; + sorted->end = sorted->start + size; + + if (sorted->end > s->cap) { + const int oldsize = s->cap * sizeof(*s->entries); + s->cap = upb_RoundUpToPowerOfTwo(sorted->end); + const int newsize = s->cap * sizeof(*s->entries); + s->entries = upb_grealloc(s->entries, oldsize, newsize); + if (!s->entries) return false; + } + + s->size = sorted->end; + return true; +} + +bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, + const upb_Map* map, _upb_sortedmap* sorted) { + int map_size; + if (map->UPB_PRIVATE(is_strtable)) { + map_size = _upb_Map_Size(map); + } else { + // For inttable, only sort the table entries, since the array part is + // already in a sorted order. + map_size = map->t.inttable.t.count; + } + + if (!_upb_mapsorter_resize(s, sorted, map_size)) return false; + + // Copy non-empty entries from the table to s->entries. + const void** dst = &s->entries[sorted->start]; + const upb_tabent* src; + const upb_tabent* end; + if (map->UPB_PRIVATE(is_strtable)) { + src = map->t.strtable.t.entries; + end = src + upb_table_size(&map->t.strtable.t); + } else { + // For inttable, only sort the table entries, since the array part is + // already in a sorted order. + src = map->t.inttable.t.entries; + end = src + upb_table_size(&map->t.inttable.t); + } + for (; src < end; src++) { + if (!upb_tabent_isempty(src)) { + *dst = src; + dst++; + } + } + UPB_ASSERT(dst == &s->entries[sorted->end]); + + // Sort entries according to the key type. + qsort(&s->entries[sorted->start], map_size, sizeof(*s->entries), + map->UPB_PRIVATE(is_strtable) ? compar[key_type] + : _upb_mapsorter_intkeys); + return true; +} + +static int _upb_mapsorter_cmpext(const void* _a, const void* _b) { + const upb_Extension* const* a = _a; + const upb_Extension* const* b = _b; + uint32_t a_num = upb_MiniTableExtension_Number((*a)->ext); + uint32_t b_num = upb_MiniTableExtension_Number((*b)->ext); + UPB_ASSERT(a_num != b_num); + return a_num < b_num ? -1 : 1; +} + +bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Message_Internal* in, + _upb_sortedmap* sorted) { + size_t count = 0; + for (size_t i = 0; i < in->size; i++) { + count += upb_TaggedAuxPtr_IsExtension(in->aux_data[i]); + } + if (!_upb_mapsorter_resize(s, sorted, count)) return false; + if (count == 0) return true; + const upb_Extension** entry = + (const upb_Extension**)&s->entries[sorted->start]; + for (size_t i = 0; i < in->size; i++) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i]; + if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + *entry++ = upb_TaggedAuxPtr_Extension(tagged_ptr); + } + } + qsort(&s->entries[sorted->start], count, sizeof(*s->entries), + _upb_mapsorter_cmpext); + return true; +} + + +#include +#include +#include +#include + + +// Must be last. + +upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* a) { + return _upb_Message_New(m, a); +} + +UPB_NOINLINE bool UPB_PRIVATE(_upb_Message_AddUnknownSlowPath)(upb_Message* msg, + const char* data, + size_t len, + upb_Arena* arena, + bool alias) { + { + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + // Alias fast path was already checked in the inline function that calls + // this one + if (!alias && in && in->size) { + upb_TaggedAuxPtr ptr = in->aux_data[in->size - 1]; + if (upb_TaggedAuxPtr_IsUnknown(ptr)) { + upb_StringView* existing = upb_TaggedAuxPtr_UnknownData(ptr); + if (!upb_TaggedAuxPtr_IsUnknownAliased(ptr)) { + // If part of the existing field was deleted at the beginning, we can + // reconstruct it by comparing the address of the end with the address + // of the entry itself; having the non-aliased tag means that the + // string_view and the data it points to are part of the same original + // upb_Arena_Malloc allocation, and the end of the string view + // represents the end of that allocation. + size_t prev_alloc_size = + (existing->data + existing->size) - (char*)existing; + if (SIZE_MAX - prev_alloc_size >= len) { + size_t new_alloc_size = prev_alloc_size + len; + if (upb_Arena_TryExtend(arena, existing, prev_alloc_size, + new_alloc_size)) { + memcpy(UPB_PTR_AT(existing, prev_alloc_size, void), data, len); + existing->size += len; + return true; + } + } + } + } + } + } + // TODO: b/376969853 - Add debug check that the unknown field is an overall + // valid proto field + if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, arena)) { + return false; + } + upb_StringView* view; + if (alias) { + view = upb_Arena_Malloc(arena, sizeof(upb_StringView)); + if (!view) return false; + view->data = data; + } else { + if (SIZE_MAX - sizeof(upb_StringView) < len) return false; + view = upb_Arena_Malloc(arena, sizeof(upb_StringView) + len); + if (!view) return false; + char* copy = UPB_PTR_AT(view, sizeof(upb_StringView), char); + memcpy(copy, data, len); + view->data = copy; + } + view->size = len; + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + in->aux_data[in->size++] = alias + ? upb_TaggedAuxPtr_MakeUnknownDataAliased(view) + : upb_TaggedAuxPtr_MakeUnknownData(view); + return true; +} + +bool UPB_PRIVATE(_upb_Message_AddUnknownV)(struct upb_Message* msg, + upb_Arena* arena, + upb_StringView data[], + size_t count) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + UPB_ASSERT(count > 0); + size_t total_len = 0; + for (size_t i = 0; i < count; i++) { + if (SIZE_MAX - total_len < data[i].size) { + return false; + } + total_len += data[i].size; + } + + { + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (in && in->size) { + upb_TaggedAuxPtr ptr = in->aux_data[in->size - 1]; + if (upb_TaggedAuxPtr_IsUnknown(ptr)) { + upb_StringView* existing = upb_TaggedAuxPtr_UnknownData(ptr); + if (!upb_TaggedAuxPtr_IsUnknownAliased(ptr)) { + size_t prev_alloc_size = + (existing->data + existing->size) - (char*)existing; + if (SIZE_MAX - prev_alloc_size >= total_len) { + size_t new_alloc_size = prev_alloc_size + total_len; + if (upb_Arena_TryExtend(arena, existing, prev_alloc_size, + new_alloc_size)) { + char* copy = UPB_PTR_AT(existing, prev_alloc_size, char); + for (size_t i = 0; i < count; i++) { + memcpy(copy, data[i].data, data[i].size); + copy += data[i].size; + } + existing->size += total_len; + return true; + } + } + } + } + } + } + + if (SIZE_MAX - sizeof(upb_StringView) < total_len) return false; + if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, arena)) return false; + + upb_StringView* view = + upb_Arena_Malloc(arena, sizeof(upb_StringView) + total_len); + if (!view) return false; + char* copy = UPB_PTR_AT(view, sizeof(upb_StringView), char); + view->data = copy; + view->size = total_len; + for (size_t i = 0; i < count; i++) { + memcpy(copy, data[i].data, data[i].size); + copy += data[i].size; + } + // TODO: b/376969853 - Add debug check that the unknown field is an overall + // valid proto field + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + in->aux_data[in->size++] = upb_TaggedAuxPtr_MakeUnknownData(view); + return true; +} + +void _upb_Message_DiscardUnknown_shallow(upb_Message* msg) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) return; + uint32_t size = 0; + for (uint32_t i = 0; i < in->size; i++) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i]; + if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + in->aux_data[size++] = tagged_ptr; + } + } + in->size = size; +} + +upb_Message_DeleteUnknownStatus upb_Message_DeleteUnknown(upb_Message* msg, + upb_StringView* data, + uintptr_t* iter, + upb_Arena* arena) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + UPB_ASSERT(*iter != kUpb_Message_UnknownBegin); + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + UPB_ASSERT(in); + UPB_ASSERT(*iter <= in->size); + upb_TaggedAuxPtr unknown_ptr = in->aux_data[*iter - 1]; + UPB_ASSERT(upb_TaggedAuxPtr_IsUnknown(unknown_ptr)); + upb_StringView* unknown = upb_TaggedAuxPtr_UnknownData(unknown_ptr); + if (unknown->data == data->data && unknown->size == data->size) { + // Remove whole field + in->aux_data[*iter - 1] = upb_TaggedAuxPtr_Null(); + } else if (unknown->data == data->data) { + // Strip prefix + unknown->data += data->size; + unknown->size -= data->size; + *data = *unknown; + return kUpb_DeleteUnknown_IterUpdated; + } else if (unknown->data + unknown->size == data->data + data->size) { + // Truncate existing field + unknown->size -= data->size; + if (!upb_TaggedAuxPtr_IsUnknownAliased(unknown_ptr)) { + in->aux_data[*iter - 1] = + upb_TaggedAuxPtr_MakeUnknownDataAliased(unknown); + } + } else { + UPB_ASSERT(unknown->data < data->data && + unknown->data + unknown->size > data->data + data->size); + // Split in the middle + upb_StringView* prefix = unknown; + upb_StringView* suffix = upb_Arena_Malloc(arena, sizeof(upb_StringView)); + if (!suffix) { + return kUpb_DeleteUnknown_AllocFail; + } + if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, arena)) { + return kUpb_DeleteUnknown_AllocFail; + } + in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (*iter != in->size) { + // Shift later entries down so that unknown field ordering is preserved + memmove(&in->aux_data[*iter + 1], &in->aux_data[*iter], + sizeof(upb_TaggedAuxPtr) * (in->size - *iter)); + } + in->aux_data[*iter] = upb_TaggedAuxPtr_MakeUnknownDataAliased(suffix); + if (!upb_TaggedAuxPtr_IsUnknownAliased(unknown_ptr)) { + in->aux_data[*iter - 1] = upb_TaggedAuxPtr_MakeUnknownDataAliased(prefix); + } + in->size++; + suffix->data = data->data + data->size; + suffix->size = (prefix->data + prefix->size) - suffix->data; + prefix->size = data->data - prefix->data; + } + return upb_Message_NextUnknown(msg, data, iter) + ? kUpb_DeleteUnknown_IterUpdated + : kUpb_DeleteUnknown_DeletedLast; +} + +size_t upb_Message_ExtensionCount(const upb_Message* msg) { + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) return 0; + const upb_MiniTableExtension* ext; + upb_MessageValue val; + uintptr_t iter = kUpb_Message_ExtensionBegin; + size_t count = 0; + while (upb_Message_NextExtension(msg, &ext, &val, &iter)) { + count++; + } + return count; +} + +void upb_Message_Freeze(upb_Message* msg, const upb_MiniTable* m) { + if (upb_Message_IsFrozen(msg)) return; + UPB_PRIVATE(_upb_Message_ShallowFreeze)(msg); + + // Base Fields. + const size_t field_count = upb_MiniTable_FieldCount(m); + + for (size_t i = 0; i < field_count; i++) { + const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i); + const upb_MiniTable* m2 = upb_MiniTable_SubMessage(f); + + switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) { + case kUpb_FieldMode_Array: { + upb_Array* arr = upb_Message_GetMutableArray(msg, f); + if (arr) upb_Array_Freeze(arr, m2); + break; + } + case kUpb_FieldMode_Map: { + upb_Map* map = upb_Message_GetMutableMap(msg, f); + if (map) { + const upb_MiniTableField* f2 = upb_MiniTable_MapValue(m2); + const upb_MiniTable* m3 = upb_MiniTable_SubMessage(f2); + upb_Map_Freeze(map, m3); + } + break; + } + case kUpb_FieldMode_Scalar: { + if (m2) { + upb_Message* msg2 = upb_Message_GetMutableMessage(msg, f); + if (msg2) upb_Message_Freeze(msg2, m2); + } + break; + } + } + } + + // Extensions. + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + // TODO: b/376969853 - use iterator API + uint32_t size = in ? in->size : 0; + for (size_t i = 0; i < size; i++) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i]; + if (!upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + continue; + } + const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr); + const upb_MiniTableExtension* e = ext->ext; + const upb_MiniTableField* f = &e->UPB_PRIVATE(field); + const upb_MiniTable* m2 = upb_MiniTableExtension_GetSubMessage(e); + + upb_MessageValue val; + memcpy(&val, &(ext->data), sizeof(upb_MessageValue)); + + switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) { + case kUpb_FieldMode_Array: { + upb_Array* arr = (upb_Array*)val.array_val; + if (arr) upb_Array_Freeze(arr, m2); + break; + } + case kUpb_FieldMode_Map: + UPB_UNREACHABLE(); // Maps cannot be extensions. + break; + case kUpb_FieldMode_Scalar: + if (upb_MiniTableField_IsSubMessage(f)) { + upb_Message* msg2 = (upb_Message*)val.msg_val; + if (msg2) upb_Message_Freeze(msg2, m2); + } + break; + } + } +} + + +#include +#include + + +// Must be last. + + +#ifdef __cplusplus +extern "C" { +#endif + +bool upb_Message_IsEmpty(const upb_Message* msg, const upb_MiniTable* m) { + if (upb_Message_ExtensionCount(msg)) return false; + + const upb_MiniTableField* f; + upb_MessageValue v; + size_t iter = kUpb_BaseField_Begin; + return !UPB_PRIVATE(_upb_Message_NextBaseField)(msg, m, &f, &v, &iter); +} + +static bool _upb_Array_IsEqual(const upb_Array* arr1, const upb_Array* arr2, + upb_CType ctype, const upb_MiniTable* m, + int options) { + // Check for trivial equality. + if (arr1 == arr2) return true; + + // Must have identical element counts. + const size_t size1 = arr1 ? upb_Array_Size(arr1) : 0; + const size_t size2 = arr2 ? upb_Array_Size(arr2) : 0; + if (size1 != size2) return false; + + for (size_t i = 0; i < size1; i++) { + const upb_MessageValue val1 = upb_Array_Get(arr1, i); + const upb_MessageValue val2 = upb_Array_Get(arr2, i); + + if (!upb_MessageValue_IsEqual(val1, val2, ctype, m, options)) return false; + } + + return true; +} + +static bool _upb_Map_IsEqual(const upb_Map* map1, const upb_Map* map2, + const upb_MiniTable* m, int options) { + // Check for trivial equality. + if (map1 == map2) return true; + + // Must have identical element counts. + size_t size1 = map1 ? upb_Map_Size(map1) : 0; + size_t size2 = map2 ? upb_Map_Size(map2) : 0; + if (size1 != size2) return false; + + const upb_MiniTableField* f = upb_MiniTable_MapValue(m); + const upb_MiniTable* m2_value = upb_MiniTable_SubMessage(f); + const upb_CType ctype = upb_MiniTableField_CType(f); + + upb_MessageValue key, val1, val2; + size_t iter = kUpb_Map_Begin; + while (upb_Map_Next(map1, &key, &val1, &iter)) { + if (!upb_Map_Get(map2, key, &val2)) return false; + if (!upb_MessageValue_IsEqual(val1, val2, ctype, m2_value, options)) + return false; + } + + return true; +} + +static bool _upb_Message_BaseFieldsAreEqual(const upb_Message* msg1, + const upb_Message* msg2, + const upb_MiniTable* m, + int options) { + // Iterate over all base fields for each message. + // The order will always match if the messages are equal. + size_t iter1 = kUpb_BaseField_Begin; + size_t iter2 = kUpb_BaseField_Begin; + + for (;;) { + const upb_MiniTableField *f1, *f2; + upb_MessageValue val1, val2; + + const bool got1 = + UPB_PRIVATE(_upb_Message_NextBaseField)(msg1, m, &f1, &val1, &iter1); + const bool got2 = + UPB_PRIVATE(_upb_Message_NextBaseField)(msg2, m, &f2, &val2, &iter2); + + if (got1 != got2) return false; // Must have identical field counts. + if (!got1) return true; // Loop termination condition. + if (f1 != f2) return false; // Must have identical fields set. + + const upb_MiniTable* subm = upb_MiniTable_SubMessage(f1); + const upb_CType ctype = upb_MiniTableField_CType(f1); + + bool eq; + switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f1)) { + case kUpb_FieldMode_Array: + eq = _upb_Array_IsEqual(val1.array_val, val2.array_val, ctype, subm, + options); + break; + case kUpb_FieldMode_Map: + eq = _upb_Map_IsEqual(val1.map_val, val2.map_val, subm, options); + break; + case kUpb_FieldMode_Scalar: + eq = upb_MessageValue_IsEqual(val1, val2, ctype, subm, options); + break; + } + if (!eq) return false; + } +} + +static bool _upb_Message_ExtensionsAreEqual(const upb_Message* msg1, + const upb_Message* msg2, + const upb_MiniTable* m, + int options) { + const upb_MiniTableExtension* e; + upb_MessageValue val1; + + // Iterate over all extensions for msg1, and search msg2 for each extension. + size_t count1 = 0; + size_t iter1 = kUpb_Message_ExtensionBegin; + while (upb_Message_NextExtension(msg1, &e, &val1, &iter1)) { + const upb_Extension* ext2 = UPB_PRIVATE(_upb_Message_Getext)(msg2, e); + if (!ext2) return false; + + count1++; + + const upb_MessageValue val2 = ext2->data; + const upb_MiniTableField* f = &e->UPB_PRIVATE(field); + const upb_MiniTable* subm = upb_MiniTableField_IsSubMessage(f) + ? upb_MiniTableExtension_GetSubMessage(e) + : NULL; + const upb_CType ctype = upb_MiniTableField_CType(f); + + bool eq; + switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) { + case kUpb_FieldMode_Array: + eq = _upb_Array_IsEqual(val1.array_val, val2.array_val, ctype, subm, + options); + break; + case kUpb_FieldMode_Map: + UPB_UNREACHABLE(); // Maps cannot be extensions. + break; + case kUpb_FieldMode_Scalar: { + eq = upb_MessageValue_IsEqual(val1, val2, ctype, subm, options); + break; + } + } + if (!eq) return false; + } + + // Must have identical extension counts (this catches the case where msg2 + // has extensions that msg1 doesn't). + if (count1 != upb_Message_ExtensionCount(msg2)) return false; + + return true; +} + +bool upb_Message_IsEqual(const upb_Message* msg1, const upb_Message* msg2, + const upb_MiniTable* m, int options) { + if (UPB_UNLIKELY(msg1 == msg2)) return true; + + if (!_upb_Message_BaseFieldsAreEqual(msg1, msg2, m, options)) return false; + if (!_upb_Message_ExtensionsAreEqual(msg1, msg2, m, options)) return false; + + if (!(options & kUpb_CompareOption_IncludeUnknownFields)) return true; + + // The wire encoder enforces a maximum depth of 100 so we match that here. + return UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(msg1, msg2, 100) == + kUpb_UnknownCompareResult_Equal; +} + + +#include +#include + + +// Must be last. + +typedef struct upb_UnknownFields upb_UnknownFields; + +typedef struct { + uint32_t tag; + union { + uint64_t varint; + uint64_t uint64; + uint32_t uint32; + upb_StringView delimited; + upb_UnknownFields* group; + } data; +} upb_UnknownField; + +struct upb_UnknownFields { + size_t size; + size_t capacity; + upb_UnknownField* fields; +}; + +typedef struct { + upb_EpsCopyInputStream stream; + upb_Arena* arena; + upb_UnknownField* tmp; + size_t tmp_size; + int depth; + upb_UnknownCompareResult status; + jmp_buf err; +} upb_UnknownField_Context; + +typedef struct { + upb_UnknownField* arr_base; + upb_UnknownField* arr_ptr; + upb_UnknownField* arr_end; + uint32_t last_tag; + bool sorted; +} upb_UnknownFields_Builder; + +UPB_NORETURN static void upb_UnknownFields_OutOfMemory( + upb_UnknownField_Context* ctx) { + ctx->status = kUpb_UnknownCompareResult_OutOfMemory; + UPB_LONGJMP(ctx->err, 1); +} + +static void upb_UnknownFields_Grow(upb_UnknownField_Context* ctx, + upb_UnknownField** base, + upb_UnknownField** ptr, + upb_UnknownField** end) { + size_t old = (*ptr - *base); + size_t new = UPB_MAX(4, old * 2); + + *base = upb_Arena_Realloc(ctx->arena, *base, old * sizeof(**base), + new * sizeof(**base)); + if (!*base) upb_UnknownFields_OutOfMemory(ctx); + + *ptr = *base + old; + *end = *base + new; +} + +// We have to implement our own sort here, since qsort() is not an in-order +// sort. Here we use merge sort, the simplest in-order sort. +static void upb_UnknownFields_Merge(upb_UnknownField* arr, size_t start, + size_t mid, size_t end, + upb_UnknownField* tmp) { + memcpy(tmp, &arr[start], (end - start) * sizeof(*tmp)); + + upb_UnknownField* ptr1 = tmp; + upb_UnknownField* end1 = &tmp[mid - start]; + upb_UnknownField* ptr2 = &tmp[mid - start]; + upb_UnknownField* end2 = &tmp[end - start]; + upb_UnknownField* out = &arr[start]; + + while (ptr1 < end1 && ptr2 < end2) { + if (ptr1->tag <= ptr2->tag) { + *out++ = *ptr1++; + } else { + *out++ = *ptr2++; + } + } + + if (ptr1 < end1) { + memcpy(out, ptr1, (end1 - ptr1) * sizeof(*out)); + } else if (ptr2 < end2) { + memcpy(out, ptr1, (end2 - ptr2) * sizeof(*out)); + } +} + +static void upb_UnknownFields_SortRecursive(upb_UnknownField* arr, size_t start, + size_t end, upb_UnknownField* tmp) { + if (end - start > 1) { + size_t mid = start + ((end - start) / 2); + upb_UnknownFields_SortRecursive(arr, start, mid, tmp); + upb_UnknownFields_SortRecursive(arr, mid, end, tmp); + upb_UnknownFields_Merge(arr, start, mid, end, tmp); + } +} + +static void upb_UnknownFields_Sort(upb_UnknownField_Context* ctx, + upb_UnknownFields* fields) { + if (ctx->tmp_size < fields->size) { + const int oldsize = ctx->tmp_size * sizeof(*ctx->tmp); + ctx->tmp_size = UPB_MAX(8, ctx->tmp_size); + while (ctx->tmp_size < fields->size) ctx->tmp_size *= 2; + const int newsize = ctx->tmp_size * sizeof(*ctx->tmp); + ctx->tmp = upb_grealloc(ctx->tmp, oldsize, newsize); + } + upb_UnknownFields_SortRecursive(fields->fields, 0, fields->size, ctx->tmp); +} + +static upb_UnknownFields* upb_UnknownFields_BuildFromBuffer( + upb_UnknownField_Context* ctx, const char** buf); + +// Combines two unknown fields into one. +static void upb_CombineUnknownFields(upb_UnknownField_Context* ctx, + upb_UnknownFields_Builder* builder, + const char** buf) { + upb_UnknownField* arr_base = builder->arr_base; + upb_UnknownField* arr_ptr = builder->arr_ptr; + upb_UnknownField* arr_end = builder->arr_end; + const char* ptr = *buf; + uint32_t last_tag = builder->last_tag; + bool sorted = builder->sorted; + + // Parse the unknown field data. It is an invariant of the data structure that + // unknown field data is valid, so parse errors here should be impossible. + while (!upb_EpsCopyInputStream_IsDone(&ctx->stream, &ptr)) { + uint32_t tag; + ptr = upb_WireReader_ReadTag(ptr, &tag, &ctx->stream); + UPB_ASSERT(tag <= UINT32_MAX); + int wire_type = upb_WireReader_GetWireType(tag); + if (wire_type == kUpb_WireType_EndGroup) break; + if (tag < last_tag) sorted = false; + last_tag = tag; + + if (arr_ptr == arr_end) { + upb_UnknownFields_Grow(ctx, &arr_base, &arr_ptr, &arr_end); + } + upb_UnknownField* field = arr_ptr; + field->tag = tag; + arr_ptr++; + + switch (wire_type) { + case kUpb_WireType_Varint: + ptr = upb_WireReader_ReadVarint(ptr, &field->data.varint, &ctx->stream); + UPB_ASSERT(ptr); + break; + case kUpb_WireType_64Bit: + ptr = + upb_WireReader_ReadFixed64(ptr, &field->data.uint64, &ctx->stream); + UPB_ASSERT(ptr); + break; + case kUpb_WireType_32Bit: + ptr = + upb_WireReader_ReadFixed32(ptr, &field->data.uint32, &ctx->stream); + UPB_ASSERT(ptr); + break; + case kUpb_WireType_Delimited: { + int size; + upb_StringView sv; + ptr = upb_WireReader_ReadSize(ptr, &size, &ctx->stream); + UPB_ASSERT(ptr); + ptr = upb_EpsCopyInputStream_ReadStringAlwaysAlias(&ctx->stream, ptr, + size, &sv); + UPB_ASSERT(ptr); + field->data.delimited.data = sv.data; + field->data.delimited.size = sv.size; + break; + } + case kUpb_WireType_StartGroup: + if (--ctx->depth < 0) { + ctx->status = kUpb_UnknownCompareResult_MaxDepthExceeded; + UPB_LONGJMP(ctx->err, 1); + } + field->data.group = upb_UnknownFields_BuildFromBuffer(ctx, &ptr); + ctx->depth++; + break; + default: + UPB_UNREACHABLE(); + } + } + *buf = ptr; + builder->arr_base = arr_base; + builder->arr_ptr = arr_ptr; + builder->arr_end = arr_end; + builder->sorted = sorted; + builder->last_tag = last_tag; +} + +static upb_UnknownFields* upb_UnknownFields_DoBuild( + upb_UnknownField_Context* ctx, upb_UnknownFields_Builder* builder) { + upb_UnknownFields* ret = upb_Arena_Malloc(ctx->arena, sizeof(*ret)); + if (!ret) upb_UnknownFields_OutOfMemory(ctx); + ret->fields = builder->arr_base; + ret->size = builder->arr_ptr - builder->arr_base; + ret->capacity = builder->arr_end - builder->arr_base; + if (!builder->sorted) { + upb_UnknownFields_Sort(ctx, ret); + } + return ret; +} + +// Builds a upb_UnknownFields data structure from the binary data in buf. +static upb_UnknownFields* upb_UnknownFields_BuildFromBuffer( + upb_UnknownField_Context* ctx, const char** buf) { + upb_UnknownFields_Builder builder = { + .arr_base = NULL, + .arr_ptr = NULL, + .arr_end = NULL, + .sorted = true, + .last_tag = 0, + }; + const char* ptr = *buf; + upb_CombineUnknownFields(ctx, &builder, &ptr); + upb_UnknownFields* fields = upb_UnknownFields_DoBuild(ctx, &builder); + *buf = ptr; + return fields; +} + +// Builds a upb_UnknownFields data structure from the unknown fields of a +// upb_Message. +static upb_UnknownFields* upb_UnknownFields_Build(upb_UnknownField_Context* ctx, + const upb_Message* msg) { + upb_UnknownFields_Builder builder = { + .arr_base = NULL, + .arr_ptr = NULL, + .arr_end = NULL, + .sorted = true, + .last_tag = 0, + }; + uintptr_t iter = kUpb_Message_UnknownBegin; + upb_StringView view; + while (upb_Message_NextUnknown(msg, &view, &iter)) { + upb_EpsCopyInputStream_Init(&ctx->stream, &view.data, view.size); + upb_CombineUnknownFields(ctx, &builder, &view.data); + UPB_ASSERT(upb_EpsCopyInputStream_IsDone(&ctx->stream, &view.data) && + !upb_EpsCopyInputStream_IsError(&ctx->stream)); + } + upb_UnknownFields* fields = upb_UnknownFields_DoBuild(ctx, &builder); + return fields; +} + +// Compares two sorted upb_UnknownFields structures for equality. +static bool upb_UnknownFields_IsEqual(const upb_UnknownFields* uf1, + const upb_UnknownFields* uf2) { + if (uf1->size != uf2->size) return false; + for (size_t i = 0, n = uf1->size; i < n; i++) { + upb_UnknownField* f1 = &uf1->fields[i]; + upb_UnknownField* f2 = &uf2->fields[i]; + if (f1->tag != f2->tag) return false; + int wire_type = f1->tag & 7; + switch (wire_type) { + case kUpb_WireType_Varint: + if (f1->data.varint != f2->data.varint) return false; + break; + case kUpb_WireType_64Bit: + if (f1->data.uint64 != f2->data.uint64) return false; + break; + case kUpb_WireType_32Bit: + if (f1->data.uint32 != f2->data.uint32) return false; + break; + case kUpb_WireType_Delimited: + if (!upb_StringView_IsEqual(f1->data.delimited, f2->data.delimited)) { + return false; + } + break; + case kUpb_WireType_StartGroup: + if (!upb_UnknownFields_IsEqual(f1->data.group, f2->data.group)) { + return false; + } + break; + default: + UPB_UNREACHABLE(); + } + } + return true; +} + +static upb_UnknownCompareResult upb_UnknownField_DoCompare( + upb_UnknownField_Context* ctx, const upb_Message* msg1, + const upb_Message* msg2) { + upb_UnknownCompareResult ret; + // First build both unknown fields into a sorted data structure (similar + // to the UnknownFieldSet in C++). + upb_UnknownFields* uf1 = upb_UnknownFields_Build(ctx, msg1); + upb_UnknownFields* uf2 = upb_UnknownFields_Build(ctx, msg2); + + // Now perform the equality check on the sorted structures. + if (upb_UnknownFields_IsEqual(uf1, uf2)) { + ret = kUpb_UnknownCompareResult_Equal; + } else { + ret = kUpb_UnknownCompareResult_NotEqual; + } + return ret; +} + +static upb_UnknownCompareResult upb_UnknownField_Compare( + upb_UnknownField_Context* const ctx, const upb_Message* msg1, + const upb_Message* msg2) { + upb_UnknownCompareResult ret; + if (UPB_SETJMP(ctx->err) == 0) { + ret = upb_UnknownField_DoCompare(ctx, msg1, msg2); + } else { + ret = ctx->status; + UPB_ASSERT(ret != kUpb_UnknownCompareResult_Equal); + } + + upb_Arena_Free(ctx->arena); + upb_gfree(ctx->tmp); + return ret; +} + +upb_UnknownCompareResult UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)( + const upb_Message* msg1, const upb_Message* msg2, int max_depth) { + bool msg1_empty = !upb_Message_HasUnknown(msg1); + bool msg2_empty = !upb_Message_HasUnknown(msg2); + if (msg1_empty && msg2_empty) return kUpb_UnknownCompareResult_Equal; + if (msg1_empty || msg2_empty) return kUpb_UnknownCompareResult_NotEqual; + + upb_UnknownField_Context ctx = { + .arena = upb_Arena_New(), + .depth = max_depth, + .tmp = NULL, + .tmp_size = 0, + .status = kUpb_UnknownCompareResult_Equal, + }; + + if (!ctx.arena) return kUpb_UnknownCompareResult_OutOfMemory; + + return upb_UnknownField_Compare(&ctx, msg1, msg2); +} + + +#include +#include +#include + + +// Must be last. + +static upb_StringView upb_Clone_StringView(upb_StringView str, + upb_Arena* arena) { + if (str.size == 0) { + return upb_StringView_FromDataAndSize(NULL, 0); + } + void* cloned_data = upb_Arena_Malloc(arena, str.size); + upb_StringView cloned_str = + upb_StringView_FromDataAndSize(cloned_data, str.size); + memcpy(cloned_data, str.data, str.size); + return cloned_str; +} + +static bool upb_Clone_MessageValue(void* value, upb_CType value_type, + const upb_MiniTable* sub, upb_Arena* arena) { + switch (value_type) { + case kUpb_CType_Bool: + case kUpb_CType_Float: + case kUpb_CType_Int32: + case kUpb_CType_UInt32: + case kUpb_CType_Enum: + case kUpb_CType_Double: + case kUpb_CType_Int64: + case kUpb_CType_UInt64: + return true; + case kUpb_CType_String: + case kUpb_CType_Bytes: { + upb_StringView source = *(upb_StringView*)value; + size_t size = source.size; + void* cloned_data = upb_Arena_Malloc(arena, size); + if (cloned_data == NULL) { + return false; + } + *(upb_StringView*)value = + upb_StringView_FromDataAndSize(cloned_data, size); + memcpy(cloned_data, source.data, size); + return true; + } break; + case kUpb_CType_Message: { + UPB_ASSERT(sub); + const upb_Message* source = *(upb_Message**)value; + UPB_ASSERT(source); + upb_Message* clone = upb_Message_DeepClone(source, sub, arena); + *(upb_Message**)value = clone; + return clone != NULL; + } break; + } + UPB_UNREACHABLE(); +} + +upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type, + upb_CType value_type, + const upb_MiniTable* map_entry_table, + upb_Arena* arena) { + upb_Map* cloned_map = _upb_Map_New(arena, map->key_size, map->val_size); + if (cloned_map == NULL) { + return NULL; + } + upb_MessageValue key, val; + size_t iter = kUpb_Map_Begin; + while (upb_Map_Next(map, &key, &val, &iter)) { + const upb_MiniTableField* value_field = + upb_MiniTable_MapValue(map_entry_table); + const upb_MiniTable* value_sub = + upb_MiniTableField_CType(value_field) == kUpb_CType_Message + ? upb_MiniTable_GetSubMessageTable(value_field) + : NULL; + upb_CType value_field_type = upb_MiniTableField_CType(value_field); + if (!upb_Clone_MessageValue(&val, value_field_type, value_sub, arena)) { + return NULL; + } + if (!upb_Map_Set(cloned_map, key, val, arena)) { + return NULL; + } + } + return cloned_map; +} + +static upb_Map* upb_Message_Map_DeepClone(const upb_Map* map, + const upb_MiniTable* mini_table, + const upb_MiniTableField* f, + upb_Message* clone, + upb_Arena* arena) { + UPB_ASSERT(!upb_Message_IsFrozen(clone)); + const upb_MiniTable* map_entry_table = upb_MiniTable_MapEntrySubMessage(f); + UPB_ASSERT(map_entry_table); + + const upb_MiniTableField* key_field = upb_MiniTable_MapKey(map_entry_table); + const upb_MiniTableField* value_field = + upb_MiniTable_MapValue(map_entry_table); + + upb_Map* cloned_map = upb_Map_DeepClone( + map, upb_MiniTableField_CType(key_field), + upb_MiniTableField_CType(value_field), map_entry_table, arena); + if (!cloned_map) { + return NULL; + } + upb_Message_SetBaseField(clone, f, &cloned_map); + return cloned_map; +} + +upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type, + const upb_MiniTable* sub, upb_Arena* arena) { + const size_t size = upb_Array_Size(array); + const int lg2 = UPB_PRIVATE(_upb_CType_SizeLg2)(value_type); + upb_Array* cloned_array = UPB_PRIVATE(_upb_Array_New)(arena, size, lg2); + if (!cloned_array) { + return NULL; + } + if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(cloned_array, size, arena)) { + return NULL; + } + for (size_t i = 0; i < size; ++i) { + upb_MessageValue val = upb_Array_Get(array, i); + if (!upb_Clone_MessageValue(&val, value_type, sub, arena)) { + return NULL; + } + upb_Array_Set(cloned_array, i, val); + } + return cloned_array; +} + +static bool upb_Message_Array_DeepClone(const upb_Array* array, + const upb_MiniTable* mini_table, + const upb_MiniTableField* field, + upb_Message* clone, upb_Arena* arena) { + UPB_ASSERT(!upb_Message_IsFrozen(clone)); + UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field); + upb_Array* cloned_array = + upb_Array_DeepClone(array, upb_MiniTableField_CType(field), + upb_MiniTableField_CType(field) == kUpb_CType_Message + ? upb_MiniTable_GetSubMessageTable(field) + : NULL, + arena); + + // Clear out upb_Array* due to parent memcpy. + upb_Message_SetBaseField(clone, field, &cloned_array); + return true; +} + +static bool upb_Clone_ExtensionValue( + const upb_MiniTableExtension* mini_table_ext, const upb_Extension* source, + upb_Extension* dest, upb_Arena* arena) { + dest->data = source->data; + return upb_Clone_MessageValue( + &dest->data, upb_MiniTableExtension_CType(mini_table_ext), + upb_MiniTableExtension_GetSubMessage(mini_table_ext), arena); +} + +upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src, + const upb_MiniTable* mini_table, + upb_Arena* arena) { + UPB_ASSERT(!upb_Message_IsFrozen(dst)); + upb_StringView empty_string = upb_StringView_FromDataAndSize(NULL, 0); + // Only copy message area skipping upb_Message_Internal. + memcpy(dst + 1, src + 1, mini_table->UPB_PRIVATE(size) - sizeof(upb_Message)); + for (int i = 0; i < upb_MiniTable_FieldCount(mini_table); ++i) { + const upb_MiniTableField* field = + upb_MiniTable_GetFieldByIndex(mini_table, i); + if (upb_MiniTableField_IsScalar(field)) { + switch (upb_MiniTableField_CType(field)) { + case kUpb_CType_Message: { + const upb_Message* sub_message = upb_Message_GetMessage(src, field); + if (sub_message != NULL) { + const upb_MiniTable* sub_message_table = + upb_MiniTable_GetSubMessageTable(field); + upb_Message* dst_sub_message = + upb_Message_DeepClone(sub_message, sub_message_table, arena); + if (dst_sub_message == NULL) { + return NULL; + } + upb_Message_SetBaseFieldMessage(dst, field, dst_sub_message); + } + } break; + case kUpb_CType_String: + case kUpb_CType_Bytes: { + upb_StringView str = upb_Message_GetString(src, field, empty_string); + if (str.size != 0) { + if (!upb_Message_SetString( + dst, field, upb_Clone_StringView(str, arena), arena)) { + return NULL; + } + } + } break; + default: + // Scalar, already copied. + break; + } + } else { + if (upb_MiniTableField_IsMap(field)) { + const upb_Map* map = upb_Message_GetMap(src, field); + if (map != NULL) { + if (!upb_Message_Map_DeepClone(map, mini_table, field, dst, arena)) { + return NULL; + } + } + } else { + const upb_Array* array = upb_Message_GetArray(src, field); + if (array != NULL) { + if (!upb_Message_Array_DeepClone(array, mini_table, field, dst, + arena)) { + return NULL; + } + } + } + } + } + // Clone extensions. + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(src); + if (!in) return dst; + + for (size_t i = 0; i < in->size; i++) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i]; + if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + // Clone extension + const upb_Extension* msg_ext = upb_TaggedAuxPtr_Extension(tagged_ptr); + const upb_MiniTableField* field = &msg_ext->ext->UPB_PRIVATE(field); + upb_Extension* dst_ext = UPB_PRIVATE(_upb_Message_GetOrCreateExtension)( + dst, msg_ext->ext, arena); + if (!dst_ext) return NULL; + if (upb_MiniTableField_IsScalar(field)) { + if (!upb_Clone_ExtensionValue(msg_ext->ext, msg_ext, dst_ext, arena)) { + return NULL; + } + } else { + upb_Array* msg_array = (upb_Array*)msg_ext->data.array_val; + UPB_ASSERT(msg_array); + upb_Array* cloned_array = upb_Array_DeepClone( + msg_array, upb_MiniTableField_CType(field), + upb_MiniTableExtension_GetSubMessage(msg_ext->ext), arena); + if (!cloned_array) { + return NULL; + } + dst_ext->data.array_val = cloned_array; + } + } else if (upb_TaggedAuxPtr_IsUnknown(tagged_ptr)) { + // Clone unknown + upb_StringView* unknown = upb_TaggedAuxPtr_UnknownData(tagged_ptr); + // Make a copy into destination arena. + if (!UPB_PRIVATE(_upb_Message_AddUnknown)( + dst, unknown->data, unknown->size, arena, kUpb_AddUnknown_Copy)) { + return NULL; + } + } + } + + return dst; +} + +bool upb_Message_DeepCopy(upb_Message* dst, const upb_Message* src, + const upb_MiniTable* mini_table, upb_Arena* arena) { + UPB_ASSERT(!upb_Message_IsFrozen(dst)); + upb_Message_Clear(dst, mini_table); + return _upb_Message_Copy(dst, src, mini_table, arena) != NULL; +} + +// Deep clones a message using the provided target arena. +// +// Returns NULL on failure. +upb_Message* upb_Message_DeepClone(const upb_Message* msg, + const upb_MiniTable* m, upb_Arena* arena) { + upb_Message* clone = upb_Message_New(m, arena); + return _upb_Message_Copy(clone, msg, m, arena); +} + +// Performs a shallow copy. TODO: Extend to handle unknown fields. +void upb_Message_ShallowCopy(upb_Message* dst, const upb_Message* src, + const upb_MiniTable* m) { + UPB_ASSERT(!upb_Message_IsFrozen(dst)); + memcpy(dst, src, m->UPB_PRIVATE(size)); +} + +// Performs a shallow clone. Ignores unknown fields. +upb_Message* upb_Message_ShallowClone(const upb_Message* msg, + const upb_MiniTable* m, + upb_Arena* arena) { + upb_Message* clone = upb_Message_New(m, arena); + upb_Message_ShallowCopy(clone, msg, m); + return clone; +} + +#include "stddef.h" + +// Must be last. + +bool upb_Message_MergeFrom(upb_Message* dst, const upb_Message* src, + const upb_MiniTable* mt, + const upb_ExtensionRegistry* extreg, + upb_Arena* arena) { + char* buf = NULL; + size_t size = 0; + // This tmp arena is used to hold the bytes for `src` serialized. This bends + // the typical "no hidden allocations" design of upb, but under a properly + // optimized implementation this extra allocation would not be necessary and + // so we don't want to unnecessarily have the bad API or bloat the passed-in + // arena with this very-short-term allocation. + upb_Arena* encode_arena = upb_Arena_New(); + upb_EncodeStatus e_status = upb_Encode(src, mt, 0, encode_arena, &buf, &size); + if (e_status != kUpb_EncodeStatus_Ok) { + upb_Arena_Free(encode_arena); + return false; + } + upb_DecodeStatus d_status = upb_Decode(buf, size, dst, mt, extreg, 0, arena); + if (d_status != kUpb_DecodeStatus_Ok) { + upb_Arena_Free(encode_arena); + return false; + } + upb_Arena_Free(encode_arena); + return true; +} + + +#include +#include + + +// Must be last. + +const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)( + const struct upb_Message* msg, const upb_MiniTableExtension* e) { + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) return NULL; + + for (size_t i = 0; i < in->size; i++) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i]; + if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr); + if (ext->ext == e) { + return ext; + } + } + } + + return NULL; +} + +upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)( + struct upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* a) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Extension* ext = (upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e); + if (ext) return ext; + + if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, a)) return NULL; + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + ext = upb_Arena_Malloc(a, sizeof(upb_Extension)); + if (!ext) return NULL; + memset(ext, 0, sizeof(upb_Extension)); + ext->ext = e; + in->aux_data[in->size++] = upb_TaggedAuxPtr_MakeExtension(ext); + return ext; +} + + +#include +#include +#include +#include + + +// Must be last. + +// The latest win32 SDKs have an invalid definition of NAN. +// https://developercommunity.visualstudio.com/t/NAN-is-no-longer-compile-time-constant-i/10688907 +// +// Unfortunately, the `0.0 / 0.0` workaround doesn't work in Clang under C23, so +// try __builtin_nan first, if that exists. +#ifdef _WIN32 +#ifdef __has_builtin +#if __has_builtin(__builtin_nan) +#define UPB_NAN __builtin_nan("0") +#endif +#if __has_builtin(__builtin_inf) +#define UPB_INFINITY __builtin_inf() +#endif +#endif +#ifndef UPB_NAN +#define UPB_NAN 0.0 / 0.0 +#endif +#ifndef UPB_INFINITY +#define UPB_INFINITY 1.0 / 0.0 +#endif +#else +// For !_WIN32, assume math.h works. +#define UPB_NAN NAN +#define UPB_INFINITY INFINITY +#endif + +const float kUpb_FltInfinity = UPB_INFINITY; +const double kUpb_Infinity = UPB_INFINITY; +const double kUpb_NaN = UPB_NAN; + +static size_t _upb_Message_SizeOfInternal(uint32_t count) { + return UPB_SIZEOF_FLEX(upb_Message_Internal, aux_data, count); +} + +bool UPB_PRIVATE(_upb_Message_ReserveSlot)(struct upb_Message* msg, + upb_Arena* a) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) { + // No internal data, allocate from scratch. + uint32_t capacity = 4; + in = upb_Arena_Malloc(a, _upb_Message_SizeOfInternal(capacity)); + if (!in) return false; + in->size = 0; + in->capacity = capacity; + UPB_PRIVATE(_upb_Message_SetInternal)(msg, in); + } else if (in->capacity == in->size) { + if (in->size == UINT32_MAX) return false; + // Internal data is too small, reallocate. + size_t needed_pow2 = upb_RoundUpToPowerOfTwo(in->size + 1); + if (needed_pow2 > UINT32_MAX) return false; + uint32_t new_capacity = needed_pow2; + if (UPB_SIZEOF_FLEX_WOULD_OVERFLOW(upb_Message_Internal, aux_data, + new_capacity)) { + return false; + } + in = upb_Arena_Realloc(a, in, _upb_Message_SizeOfInternal(in->capacity), + _upb_Message_SizeOfInternal(new_capacity)); + if (!in) return false; + in->capacity = new_capacity; + UPB_PRIVATE(_upb_Message_SetInternal)(msg, in); + } + UPB_ASSERT(in->capacity - in->size >= 1); + return true; +} + +#ifdef UPB_TRACING_ENABLED +static void (*_message_trace_handler)(const upb_MiniTable*, const upb_Arena*); + +void upb_Message_LogNewMessage(const upb_MiniTable* m, const upb_Arena* arena) { + if (_message_trace_handler) { + _message_trace_handler(m, arena); + } +} + +void upb_Message_SetNewMessageTraceHandler(void (*handler)(const upb_MiniTable*, + const upb_Arena*)) { + _message_trace_handler = handler; +} +#endif // UPB_TRACING_ENABLED + + +#include + + +// Must be last. + +bool UPB_PRIVATE(_upb_Message_NextBaseField)(const upb_Message* msg, + const upb_MiniTable* m, + const upb_MiniTableField** out_f, + upb_MessageValue* out_v, + size_t* iter) { + const size_t count = upb_MiniTable_FieldCount(m); + size_t i = *iter; + + while (++i < count) { + const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i); + const void* src = UPB_PRIVATE(_upb_Message_DataPtr)(msg, f); + + upb_MessageValue val; + UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, &val, src); + + // Skip field if unset or empty. + if (upb_MiniTableField_HasPresence(f)) { + if (!upb_Message_HasBaseField(msg, f)) continue; + } else { + if (UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(f, src)) continue; + + if (upb_MiniTableField_IsArray(f)) { + if (upb_Array_Size(val.array_val) == 0) continue; + } else if (upb_MiniTableField_IsMap(f)) { + if (upb_Map_Size(val.map_val) == 0) continue; + } + } + + *out_f = f; + *out_v = val; + *iter = i; + return true; + } + + return false; +} + + +#include +#include + + +// Must be last. + +typedef struct { + upb_MdDecoder base; + upb_Arena* arena; + upb_MiniTableEnum* enum_table; + uint32_t enum_value_count; + uint32_t enum_data_count; + uint32_t enum_data_capacity; +} upb_MdEnumDecoder; + +static size_t upb_MiniTableEnum_Size(uint32_t count) { + return UPB_SIZEOF_FLEX(upb_MiniTableEnum, UPB_PRIVATE(data), count); +} + +static upb_MiniTableEnum* _upb_MiniTable_AddEnumDataMember(upb_MdEnumDecoder* d, + uint32_t val) { + if (d->enum_data_count == d->enum_data_capacity) { + size_t old_sz = upb_MiniTableEnum_Size(d->enum_data_capacity); + if (d->enum_data_capacity > UINT32_MAX / 2) { + upb_MdDecoder_ErrorJmp(&d->base, "Out of memory"); + } + uint32_t new_capacity = UPB_MAX(2, d->enum_data_capacity * 2); + if (UPB_SIZEOF_FLEX_WOULD_OVERFLOW(upb_MiniTableEnum, UPB_PRIVATE(data), + new_capacity)) { + upb_MdDecoder_ErrorJmp(&d->base, "Out of memory"); + } + size_t new_sz = upb_MiniTableEnum_Size(new_capacity); + d->enum_table = upb_Arena_Realloc(d->arena, d->enum_table, old_sz, new_sz); + upb_MdDecoder_CheckOutOfMemory(&d->base, d->enum_table); + d->enum_data_capacity = new_capacity; + } + d->enum_table->UPB_PRIVATE(data)[d->enum_data_count++] = val; + return d->enum_table; +} + +static void upb_MiniTableEnum_BuildValue(upb_MdEnumDecoder* d, uint32_t val) { + upb_MiniTableEnum* table = d->enum_table; + d->enum_value_count++; + if (table->UPB_PRIVATE(value_count) || + (val > 512 && d->enum_value_count < val / 32)) { + if (table->UPB_PRIVATE(value_count) == 0) { + UPB_ASSERT(d->enum_data_count == table->UPB_PRIVATE(mask_limit) / 32); + } + table = _upb_MiniTable_AddEnumDataMember(d, val); + table->UPB_PRIVATE(value_count)++; + } else { + uint32_t new_mask_limit = ((val / 32) + 1) * 32; + while (table->UPB_PRIVATE(mask_limit) < new_mask_limit) { + table = _upb_MiniTable_AddEnumDataMember(d, 0); + table->UPB_PRIVATE(mask_limit) += 32; + } + table->UPB_PRIVATE(data)[val / 32] |= 1ULL << (val % 32); + } +} + +static upb_MiniTableEnum* upb_MtDecoder_DoBuildMiniTableEnum( + upb_MdEnumDecoder* d, const char* data, size_t len) { + // If the string is non-empty then it must begin with a version tag. + if (len) { + if (*data != kUpb_EncodedVersion_EnumV1) { + upb_MdDecoder_ErrorJmp(&d->base, "Invalid enum version: %c", *data); + } + data++; + len--; + } + + upb_MdDecoder_CheckOutOfMemory(&d->base, d->enum_table); + + // Guarantee at least 64 bits of mask without checking mask size. + d->enum_table->UPB_PRIVATE(mask_limit) = 64; + d->enum_table = _upb_MiniTable_AddEnumDataMember(d, 0); + d->enum_table = _upb_MiniTable_AddEnumDataMember(d, 0); + + d->enum_table->UPB_PRIVATE(value_count) = 0; + + const char* ptr = data; + uint32_t base = 0; + + while (ptr < d->base.end) { + char ch = *ptr++; + if (ch <= kUpb_EncodedValue_MaxEnumMask) { + uint32_t mask = _upb_FromBase92(ch); + for (int i = 0; i < 5; i++, base++, mask >>= 1) { + if (mask & 1) upb_MiniTableEnum_BuildValue(d, base); + } + } else if (kUpb_EncodedValue_MinSkip <= ch && + ch <= kUpb_EncodedValue_MaxSkip) { + uint32_t skip; + ptr = upb_MdDecoder_DecodeBase92Varint(&d->base, ptr, ch, + kUpb_EncodedValue_MinSkip, + kUpb_EncodedValue_MaxSkip, &skip); + base += skip; + } else { + upb_MdDecoder_ErrorJmp(&d->base, "Unexpected character: %c", ch); + } + } + + return d->enum_table; +} + +static upb_MiniTableEnum* upb_MtDecoder_BuildMiniTableEnum( + upb_MdEnumDecoder* const decoder, const char* const data, + size_t const len) { + if (UPB_SETJMP(decoder->base.err) != 0) return NULL; + return upb_MtDecoder_DoBuildMiniTableEnum(decoder, data, len); +} + +upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data, size_t len, + upb_Arena* arena, + upb_Status* status) { + uint32_t initial_capacity = 2; + upb_MdEnumDecoder decoder = { + .base = + { + .end = UPB_PTRADD(data, len), + .status = status, + }, + .arena = arena, + .enum_table = + upb_Arena_Malloc(arena, upb_MiniTableEnum_Size(initial_capacity)), + .enum_value_count = 0, + .enum_data_count = 0, + .enum_data_capacity = initial_capacity, + }; + + return upb_MtDecoder_BuildMiniTableEnum(&decoder, data, len); +} + + +#include +#include +#include +#include +#include + + +// Our awkward dance for including fasttable only when it is enabled. +#if UPB_FASTTABLE +#define UPB_INCLUDE_FAST_DECODE +#endif + +#ifdef UPB_INCLUDE_FAST_DECODE +#endif + +#undef UPB_INCLUDE_FAST_DECODE + +// Must be last. + +// We reserve unused hasbits to make room for upb_Message fields. +#define kUpb_Reserved_Hasbytes sizeof(struct upb_Message) + +// 64 is the first hasbit that we currently use. +#define kUpb_Reserved_Hasbits (kUpb_Reserved_Hasbytes * 8) + +#define kUpb_OneOfLayoutItem_IndexSentinel ((uint16_t)-1) + +// Stores the field number of the present value of the oneof +#define kUpb_OneOf_CaseFieldRep (kUpb_FieldRep_4Byte) + +// The maximum field number that can be encoded on the wire. +// Note that this limit does not apply to MessageSet, which can have field +// numbers up to INT32_MAX. +#define kUpb_MaxFieldNumber ((1 << 29) - 1) + +typedef struct { + // Index of the corresponding field. The field's offset will be the index of + // the next field in a linked list. + uint16_t field_index; + // This enum is stored in bytes to avoid trailing padding while preserving + // two-byte alignment. + uint8_t /* upb_FieldRep*/ rep; +} upb_OneOfLayoutItem; + +typedef struct { + upb_OneOfLayoutItem* data; + size_t size; + size_t buf_capacity_bytes; +} upb_OneOfLayoutItemVector; + +typedef struct { + upb_MdDecoder base; + upb_MiniTableField* fields; + upb_MiniTablePlatform platform; + upb_OneOfLayoutItemVector oneofs; + upb_Arena* arena; + // Initially tracks the count of each field rep type; then, during assignment, + // tracks the base offset for the next processed field of the given rep. + uint16_t rep_counts_offsets[kUpb_FieldRep_Max + 1]; + uint32_t sub_count; + bool is_extension; + + // When building fasttables, we don't know the total size of the table until + // we're done decoding, so we allocate a temporary table on the stack and + // allocate the real thing from the arena later. + upb_MiniTable table; +} upb_MtDecoder; + +// In each field's offset, we temporarily store a presence classifier: +enum PresenceClass { + kNoPresence = 0, + kHasbitPresence = 1, + kRequiredPresence = 2, + kOneofBase = 3, + // Negative values refer to a specific oneof with that number. Positive + // values >= kOneofBase indicate that this field is in a oneof, and specify + // the next field in this oneof's linked list. +}; + +static bool upb_MtDecoder_FieldIsPackable(upb_MiniTableField* field) { + return (field->UPB_PRIVATE(mode) & kUpb_FieldMode_Array) && + upb_FieldType_IsPackable(field->UPB_PRIVATE(descriptortype)); +} + +typedef struct { + uint16_t submsg_count; + uint16_t subenum_count; +} upb_SubCounts; + +static void upb_MiniTable_SetTypeAndSub(upb_MtDecoder* d, + upb_MiniTableField* field, + upb_FieldType type, + uint64_t msg_modifiers, + bool is_proto3_enum) { + if (is_proto3_enum) { + UPB_ASSERT(type == kUpb_FieldType_Enum); + type = kUpb_FieldType_Int32; + field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsAlternate; + } else if (type == kUpb_FieldType_String && + !(msg_modifiers & kUpb_MessageModifier_ValidateUtf8)) { + type = kUpb_FieldType_Bytes; + field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsAlternate; + } + + field->UPB_PRIVATE(descriptortype) = type; + + if (upb_MtDecoder_FieldIsPackable(field) && + (msg_modifiers & kUpb_MessageModifier_DefaultIsPacked)) { + field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsPacked; + } + + // We initially set `submsg_ofs` to the index of the sub in the list of subs. + // Later, we'll update it to be a relative byte offset. + if (type == kUpb_FieldType_Message || type == kUpb_FieldType_Group || + type == kUpb_FieldType_Enum) { + field->UPB_PRIVATE(submsg_ofs) = d->sub_count++; + } else { + field->UPB_PRIVATE(submsg_ofs) = kUpb_NoSub; + } +} + +static const char kUpb_EncodedToType[] = { + [kUpb_EncodedType_Double] = kUpb_FieldType_Double, + [kUpb_EncodedType_Float] = kUpb_FieldType_Float, + [kUpb_EncodedType_Int64] = kUpb_FieldType_Int64, + [kUpb_EncodedType_UInt64] = kUpb_FieldType_UInt64, + [kUpb_EncodedType_Int32] = kUpb_FieldType_Int32, + [kUpb_EncodedType_Fixed64] = kUpb_FieldType_Fixed64, + [kUpb_EncodedType_Fixed32] = kUpb_FieldType_Fixed32, + [kUpb_EncodedType_Bool] = kUpb_FieldType_Bool, + [kUpb_EncodedType_String] = kUpb_FieldType_String, + [kUpb_EncodedType_Group] = kUpb_FieldType_Group, + [kUpb_EncodedType_Message] = kUpb_FieldType_Message, + [kUpb_EncodedType_Bytes] = kUpb_FieldType_Bytes, + [kUpb_EncodedType_UInt32] = kUpb_FieldType_UInt32, + [kUpb_EncodedType_OpenEnum] = kUpb_FieldType_Enum, + [kUpb_EncodedType_SFixed32] = kUpb_FieldType_SFixed32, + [kUpb_EncodedType_SFixed64] = kUpb_FieldType_SFixed64, + [kUpb_EncodedType_SInt32] = kUpb_FieldType_SInt32, + [kUpb_EncodedType_SInt64] = kUpb_FieldType_SInt64, + [kUpb_EncodedType_ClosedEnum] = kUpb_FieldType_Enum, +}; + +static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch, + upb_MiniTableField* field, + uint64_t msg_modifiers) { + static const char kUpb_EncodedToFieldRep[] = { + [kUpb_EncodedType_Double] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_Float] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_Int64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_UInt64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_Int32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_Fixed64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_Fixed32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_Bool] = kUpb_FieldRep_1Byte, + [kUpb_EncodedType_String] = kUpb_FieldRep_StringView, + [kUpb_EncodedType_Bytes] = kUpb_FieldRep_StringView, + [kUpb_EncodedType_UInt32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_OpenEnum] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_SFixed32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_SFixed64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_SInt32] = kUpb_FieldRep_4Byte, + [kUpb_EncodedType_SInt64] = kUpb_FieldRep_8Byte, + [kUpb_EncodedType_ClosedEnum] = kUpb_FieldRep_4Byte, + }; + + char pointer_rep = d->platform == kUpb_MiniTablePlatform_32Bit + ? kUpb_FieldRep_4Byte + : kUpb_FieldRep_8Byte; + + int8_t type = _upb_FromBase92(ch); + if (ch >= _upb_ToBase92(kUpb_EncodedType_RepeatedBase)) { + type -= kUpb_EncodedType_RepeatedBase; + field->UPB_PRIVATE(mode) = kUpb_FieldMode_Array; + field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift; + field->UPB_PRIVATE(offset) = kNoPresence; + } else { + field->UPB_PRIVATE(mode) = kUpb_FieldMode_Scalar; + field->UPB_PRIVATE(offset) = kHasbitPresence; + if (type == kUpb_EncodedType_Group || type == kUpb_EncodedType_Message) { + field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift; + } else if ((unsigned long)type >= sizeof(kUpb_EncodedToFieldRep)) { + upb_MdDecoder_ErrorJmp(&d->base, "Invalid field type: %d", (int)type); + } else { + field->UPB_PRIVATE(mode) |= kUpb_EncodedToFieldRep[type] + << kUpb_FieldRep_Shift; + } + } + if ((unsigned long)type >= sizeof(kUpb_EncodedToType)) { + upb_MdDecoder_ErrorJmp(&d->base, "Invalid field type: %d", (int)type); + } + upb_MiniTable_SetTypeAndSub(d, field, kUpb_EncodedToType[type], msg_modifiers, + type == kUpb_EncodedType_OpenEnum); +} + +static void upb_MtDecoder_ModifyField(upb_MtDecoder* d, + uint32_t message_modifiers, + uint32_t field_modifiers, + upb_MiniTableField* field) { + if (field_modifiers & kUpb_EncodedFieldModifier_FlipPacked) { + if (!upb_MtDecoder_FieldIsPackable(field)) { + upb_MdDecoder_ErrorJmp(&d->base, + "Cannot flip packed on unpackable field %" PRIu32, + upb_MiniTableField_Number(field)); + } + field->UPB_PRIVATE(mode) ^= kUpb_LabelFlags_IsPacked; + } + + if (field_modifiers & kUpb_EncodedFieldModifier_FlipValidateUtf8) { + if (field->UPB_PRIVATE(descriptortype) != kUpb_FieldType_Bytes || + !(field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsAlternate)) { + upb_MdDecoder_ErrorJmp(&d->base, + "Cannot flip ValidateUtf8 on field %" PRIu32 + ", type=%d, mode=%d", + upb_MiniTableField_Number(field), + (int)field->UPB_PRIVATE(descriptortype), + (int)field->UPB_PRIVATE(mode)); + } + field->UPB_PRIVATE(descriptortype) = kUpb_FieldType_String; + field->UPB_PRIVATE(mode) &= ~kUpb_LabelFlags_IsAlternate; + } + + bool singular = field_modifiers & kUpb_EncodedFieldModifier_IsProto3Singular; + bool required = field_modifiers & kUpb_EncodedFieldModifier_IsRequired; + + // Validate. + if ((singular || required) && field->UPB_PRIVATE(offset) != kHasbitPresence) { + upb_MdDecoder_ErrorJmp(&d->base, + "Invalid modifier(s) for repeated field %" PRIu32, + upb_MiniTableField_Number(field)); + } + if (singular && required) { + upb_MdDecoder_ErrorJmp( + &d->base, "Field %" PRIu32 " cannot be both singular and required", + upb_MiniTableField_Number(field)); + } + + if (singular && upb_MiniTableField_IsSubMessage(field)) { + upb_MdDecoder_ErrorJmp(&d->base, + "Field %" PRIu32 " cannot be a singular submessage", + upb_MiniTableField_Number(field)); + } + + if (singular) field->UPB_PRIVATE(offset) = kNoPresence; + if (required) { + field->UPB_PRIVATE(offset) = kRequiredPresence; + } +} + +static void upb_MtDecoder_PushOneof(upb_MtDecoder* d, + upb_OneOfLayoutItem item) { + if (item.field_index == kUpb_OneOfLayoutItem_IndexSentinel) { + upb_MdDecoder_ErrorJmp(&d->base, "Empty oneof"); + } + if ((d->oneofs.size + 1) * sizeof(*d->oneofs.data) > + d->oneofs.buf_capacity_bytes) { + size_t new_cap = UPB_MAX(8, d->oneofs.size * 2) * sizeof(*d->oneofs.data); + d->oneofs.data = + upb_grealloc(d->oneofs.data, d->oneofs.buf_capacity_bytes, new_cap); + upb_MdDecoder_CheckOutOfMemory(&d->base, d->oneofs.data); + d->oneofs.buf_capacity_bytes = new_cap; + } + item.field_index -= kOneofBase; + + d->rep_counts_offsets[kUpb_OneOf_CaseFieldRep]++; + d->rep_counts_offsets[item.rep]++; + d->oneofs.data[d->oneofs.size++] = item; +} + +static size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep, + upb_MiniTablePlatform platform) { + enum { string_view_size_32 = 8, string_view_size_64 = 16 }; + UPB_STATIC_ASSERT(sizeof(upb_StringView) == + UPB_SIZE(string_view_size_32, string_view_size_64), + "StringView size mismatch"); + static const uint8_t kRepToSize32[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_StringView] = string_view_size_32, + [kUpb_FieldRep_8Byte] = 8, + }; + static const uint8_t kRepToSize64[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_StringView] = string_view_size_64, + [kUpb_FieldRep_8Byte] = 8, + }; + return platform == kUpb_MiniTablePlatform_32Bit ? kRepToSize32[rep] + : kRepToSize64[rep]; +} + +static size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep, + upb_MiniTablePlatform platform) { + enum { string_view_align_32 = 4, string_view_align_64 = 8 }; + UPB_STATIC_ASSERT(UPB_ALIGN_OF(upb_StringView) == + UPB_SIZE(string_view_align_32, string_view_align_64), + "StringView size mismatch"); + + static const uint8_t kRepToAlign32[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_StringView] = string_view_align_32, + [kUpb_FieldRep_8Byte] = 8, + }; + static const uint8_t kRepToAlign64[] = { + [kUpb_FieldRep_1Byte] = 1, + [kUpb_FieldRep_4Byte] = 4, + [kUpb_FieldRep_StringView] = string_view_align_64, + [kUpb_FieldRep_8Byte] = 8, + }; + return platform == kUpb_MiniTablePlatform_32Bit ? kRepToAlign32[rep] + : kRepToAlign64[rep]; +} + +static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d, + const char* ptr, + char first_ch, + upb_OneOfLayoutItem* item) { + uint32_t field_num; + ptr = upb_MdDecoder_DecodeBase92Varint( + &d->base, ptr, first_ch, kUpb_EncodedValue_MinOneofField, + kUpb_EncodedValue_MaxOneofField, &field_num); + upb_MiniTableField* f = + (void*)upb_MiniTable_FindFieldByNumber(&d->table, field_num); + + if (!f) { + upb_MdDecoder_ErrorJmp(&d->base, + "Couldn't add field number %" PRIu32 + " to oneof, no such field number.", + field_num); + } + if (f->UPB_PRIVATE(offset) != kHasbitPresence) { + upb_MdDecoder_ErrorJmp( + &d->base, + "Cannot add repeated, required, or singular field %" PRIu32 + " to oneof.", + field_num); + } + + // Oneof storage must be large enough to accommodate the largest member. + int rep = f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift; + size_t new_size = upb_MtDecoder_SizeOfRep(rep, d->platform); + size_t new_align = upb_MtDecoder_AlignOfRep(rep, d->platform); + size_t current_size = upb_MtDecoder_SizeOfRep(item->rep, d->platform); + size_t current_align = upb_MtDecoder_AlignOfRep(item->rep, d->platform); + + if (new_size > current_size || + (new_size == current_size && new_align > current_align)) { + UPB_ASSERT(new_align >= current_align); + item->rep = rep; + } else { + UPB_ASSERT(current_align >= new_align); + } + // Prepend this field to the linked list. + f->UPB_PRIVATE(offset) = item->field_index; + item->field_index = (f - d->fields) + kOneofBase; + return ptr; +} + +static const char* upb_MtDecoder_DecodeOneofs(upb_MtDecoder* d, + const char* ptr) { + upb_OneOfLayoutItem item = { + .rep = 0, .field_index = kUpb_OneOfLayoutItem_IndexSentinel}; + while (ptr < d->base.end) { + char ch = *ptr++; + if (ch == kUpb_EncodedValue_FieldSeparator) { + // Field separator, no action needed. + } else if (ch == kUpb_EncodedValue_OneofSeparator) { + // End of oneof. + upb_MtDecoder_PushOneof(d, item); + item.field_index = + kUpb_OneOfLayoutItem_IndexSentinel; // Move to next oneof. + } else { + ptr = upb_MtDecoder_DecodeOneofField(d, ptr, ch, &item); + } + } + + // Push final oneof. + upb_MtDecoder_PushOneof(d, item); + return ptr; +} + +static const char* upb_MtDecoder_ParseModifier(upb_MtDecoder* d, + const char* ptr, char first_ch, + upb_MiniTableField* last_field, + uint64_t* msg_modifiers) { + uint32_t mod; + ptr = upb_MdDecoder_DecodeBase92Varint(&d->base, ptr, first_ch, + kUpb_EncodedValue_MinModifier, + kUpb_EncodedValue_MaxModifier, &mod); + if (last_field) { + upb_MtDecoder_ModifyField(d, *msg_modifiers, mod, last_field); + } else { + if (d->is_extension) { + upb_MdDecoder_ErrorJmp(&d->base, + "Extensions cannot have message modifiers"); + } + *msg_modifiers = mod; + } + + return ptr; +} + +size_t upb_MtDecoder_PtrSize(upb_MtDecoder* d) { + return d->platform == kUpb_MiniTablePlatform_32Bit ? 4 : 8; +} + +static void upb_MtDecoder_AllocateSubs(upb_MtDecoder* d, + upb_MiniTableSubInternal* subs) { + // The `ofs` variable tracks byte offset between the current field and the + // current entry in the `subs` array. Whenever we move to the next entry in + // the `fields` array, the offset decreases by the size of the field, but + // whenever we move to the next entry in the `subs` array, the offset + // *increases* by the size of the entry in the `subs` array. + UPB_ASSERT((char*)subs >= (char*)d->fields); + size_t ofs = (char*)subs - (char*)d->fields; + uintptr_t ptr_size = upb_MtDecoder_PtrSize(d); + for (int i = 0; i < d->table.UPB_PRIVATE(field_count); + i++, ofs -= sizeof(upb_MiniTableField)) { + upb_MiniTableField* f = &d->fields[i]; + if (f->UPB_PRIVATE(submsg_ofs) == kUpb_NoSub) continue; + size_t u32_ofs = ofs / kUpb_SubmsgOffsetBytes; + UPB_ASSERT((ofs % 4) == 0); + UPB_ASSERT((i * sizeof(upb_MiniTableField) + ofs) % ptr_size == 0); + if (u32_ofs > UINT16_MAX) { + upb_MdDecoder_ErrorJmp(&d->base, "Submessage offset overflow"); + } + f->UPB_PRIVATE(submsg_ofs) = u32_ofs; + ofs += ptr_size; + } +} + +static const char* upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr, + size_t len, void* fields, + size_t field_size, + uint16_t* field_count) { + uint64_t msg_modifiers = 0; + uint32_t last_field_number = 0; + upb_MiniTableField* last_field = NULL; + bool need_dense_below = !d->is_extension; + + d->base.end = UPB_PTRADD(ptr, len); + + while (ptr < d->base.end) { + char ch = *ptr++; + if (ch <= kUpb_EncodedValue_MaxField) { + if (d->is_extension && last_field) { + // For extensions, consume only a single field and then return. + return --ptr; + } + upb_MiniTableField* field = fields; + *field_count += 1; + fields = (char*)fields + field_size; + uint32_t number = ++last_field_number; + if (number == 0 || (number > kUpb_MaxFieldNumber && !d->is_extension)) { + upb_MdDecoder_ErrorJmp(&d->base, "Invalid field number: %" PRIu32, + number); + } + field->UPB_PRIVATE(number) = number; + last_field = field; + upb_MiniTable_SetField(d, ch, field, msg_modifiers); + } else if (kUpb_EncodedValue_MinModifier <= ch && + ch <= kUpb_EncodedValue_MaxModifier) { + ptr = upb_MtDecoder_ParseModifier(d, ptr, ch, last_field, &msg_modifiers); + if (msg_modifiers & kUpb_MessageModifier_IsExtendable) { + d->table.UPB_PRIVATE(ext) |= kUpb_ExtMode_Extendable; + } + } else if (ch == kUpb_EncodedValue_End) { + if (d->is_extension) { + upb_MdDecoder_ErrorJmp(&d->base, "Extensions cannot have oneofs."); + } + ptr = upb_MtDecoder_DecodeOneofs(d, ptr); + } else if (kUpb_EncodedValue_MinSkip <= ch && + ch <= kUpb_EncodedValue_MaxSkip) { + if (need_dense_below) { + d->table.UPB_PRIVATE(dense_below) = d->table.UPB_PRIVATE(field_count); + need_dense_below = false; + } + uint32_t skip; + ptr = upb_MdDecoder_DecodeBase92Varint(&d->base, ptr, ch, + kUpb_EncodedValue_MinSkip, + kUpb_EncodedValue_MaxSkip, &skip); + last_field_number += skip; + last_field_number--; // Next field seen will increment. + } else { + upb_MdDecoder_ErrorJmp(&d->base, "Invalid char: %c", ch); + } + } + + if (need_dense_below) { + d->table.UPB_PRIVATE(dense_below) = d->table.UPB_PRIVATE(field_count); + } + + return ptr; +} + +static void upb_MtDecoder_ParseMessage(upb_MtDecoder* d, const char* data, + size_t len) { + const size_t bytes_per_field = + sizeof(upb_MiniTableField) + sizeof(upb_MiniTableSubInternal); + // Buffer length is an upper bound on the number of fields. We will return + // what we don't use. + if ((SIZE_MAX - 4) / bytes_per_field < len) { + upb_MdDecoder_ErrorJmp(&d->base, "MiniDescriptor is too large"); + } + // Max size used per field is a upb_MiniTableField and a + // upb_MiniTableSubInternal. There could also be up to 4 bytes of padding, + // since sizeof(upb_MiniTableField) == 12 and + // alignof(upb_MiniTableSubInternal) == 8. + UPB_STATIC_ASSERT(UPB_ALIGN_OF(upb_MiniTableSubInternal) - + UPB_ALIGN_OF(upb_MiniTableField) <= + 4, + "alignment difference is too large"); + const size_t initial_bytes = bytes_per_field * len + 4; + d->fields = upb_Arena_Malloc(d->arena, initial_bytes); + upb_MdDecoder_CheckOutOfMemory(&d->base, d->fields); + + d->table.UPB_PRIVATE(field_count) = 0; + d->table.UPB_PRIVATE(fields) = d->fields; + upb_MtDecoder_Parse(d, data, len, d->fields, sizeof(*d->fields), + &d->table.UPB_PRIVATE(field_count)); + size_t field_bytes = + UPB_ALIGN_UP(d->table.UPB_PRIVATE(field_count) * sizeof(*d->fields), + upb_MtDecoder_PtrSize(d)); + upb_MiniTableSubInternal* subs = + UPB_PTR_AT(d->fields, field_bytes, upb_MiniTableSubInternal); + memset(subs, 0, sizeof(upb_MiniTableSubInternal) * d->sub_count); + + // We now know how much space we actually used, so shrink the allocation to + // that size. + size_t final_bytes = + field_bytes + sizeof(upb_MiniTableSubInternal) * d->sub_count; + upb_Arena_ShrinkLast(d->arena, d->fields, initial_bytes, final_bytes); + upb_MtDecoder_AllocateSubs(d, subs); +} + +static void upb_MtDecoder_CalculateAlignments(upb_MtDecoder* d) { + // Add alignment counts for non-oneof fields (oneofs were added already) + int n = d->table.UPB_PRIVATE(field_count); + for (int i = 0; i < n; i++) { + upb_MiniTableField* f = &d->fields[i]; + if (f->UPB_PRIVATE(offset) >= kOneofBase) continue; + d->rep_counts_offsets[f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift]++; + } + + // Reserve properly aligned space for each type of field representation + // present in this message. When we iterate over the fields, they will obtain + // their offset from within the region matching their alignment requirements. + size_t base = d->table.UPB_PRIVATE(size); + // Start with the lowest alignment requirement, going up, because: + // 1. If there are presence bits, we won't be aligned to start, but adding + // some lower-alignment fields may get us closer without wasting space to + // padding. + // 2. The allocator enforces 8 byte alignment, so moving intermediate padding + // to trailing padding doesn't save us anything. + for (upb_FieldRep rep = kUpb_FieldRep_1Byte; rep <= kUpb_FieldRep_Max; + rep++) { + uint16_t count = d->rep_counts_offsets[rep]; + if (count) { + base = UPB_ALIGN_UP(base, upb_MtDecoder_AlignOfRep(rep, d->platform)); + // This entry now tracks the base offset for this field representation + // type, instead of the count + d->rep_counts_offsets[rep] = base; + base += upb_MtDecoder_SizeOfRep(rep, d->platform) * count; + } + } + static const size_t max = UINT16_MAX; + if (base > max) { + upb_MdDecoder_ErrorJmp( + &d->base, "Message size exceeded maximum size of %zu bytes", max); + } + d->table.UPB_PRIVATE(size) = (uint16_t)base; +} + +static size_t upb_MiniTable_DivideRoundUp(size_t n, size_t d) { + return (n + d - 1) / d; +} + +static void upb_MtDecoder_AssignHasbits(upb_MtDecoder* d) { + int n = d->table.UPB_PRIVATE(field_count); + size_t last_hasbit = kUpb_Reserved_Hasbits - 1; + + // First assign required fields, which must have the lowest hasbits. + for (int i = 0; i < n; i++) { + upb_MiniTableField* field = + (upb_MiniTableField*)&d->table.UPB_PRIVATE(fields)[i]; + if (field->UPB_PRIVATE(offset) == kRequiredPresence) { + field->presence = ++last_hasbit; + } else if (field->UPB_PRIVATE(offset) == kNoPresence) { + field->presence = 0; + } + } + if (last_hasbit >= kUpb_Reserved_Hasbits + 63) { + upb_MdDecoder_ErrorJmp(&d->base, "Too many required fields"); + } + + d->table.UPB_PRIVATE(required_count) = + last_hasbit - (kUpb_Reserved_Hasbits - 1); + UPB_ASSERT(d->table.UPB_PRIVATE(required_count) < 64); + + // Next assign non-required hasbit fields. + for (int i = 0; i < n; i++) { + upb_MiniTableField* field = + (upb_MiniTableField*)&d->table.UPB_PRIVATE(fields)[i]; + if (field->UPB_PRIVATE(offset) == kHasbitPresence) { + field->presence = ++last_hasbit; + } + } + + d->table.UPB_PRIVATE(size) = + last_hasbit ? upb_MiniTable_DivideRoundUp(last_hasbit + 1, 8) : 0; +} + +static size_t upb_MtDecoder_Place(upb_MtDecoder* d, upb_FieldRep rep) { + size_t size = upb_MtDecoder_SizeOfRep(rep, d->platform); + size_t offset = d->rep_counts_offsets[rep]; + d->rep_counts_offsets[rep] += size; + return offset; +} + +static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { + upb_MiniTableField* field_end = + UPB_PTRADD(d->fields, d->table.UPB_PRIVATE(field_count)); + for (upb_MiniTableField* field = d->fields; field < field_end; field++) { + if (field->UPB_PRIVATE(offset) >= kOneofBase) continue; + field->UPB_PRIVATE(offset) = + upb_MtDecoder_Place(d, field->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift); + } + + upb_OneOfLayoutItem* oneof_end = UPB_PTRADD(d->oneofs.data, d->oneofs.size); + + for (upb_OneOfLayoutItem* item = d->oneofs.data; item < oneof_end; item++) { + upb_MiniTableField* f = &d->fields[item->field_index]; + uint16_t case_offset = upb_MtDecoder_Place(d, kUpb_OneOf_CaseFieldRep); + uint16_t data_offset = upb_MtDecoder_Place(d, item->rep); + while (true) { + f->presence = ~case_offset; + uint16_t next_offset = f->UPB_PRIVATE(offset); + f->UPB_PRIVATE(offset) = data_offset; + if (next_offset == kUpb_OneOfLayoutItem_IndexSentinel) break; + UPB_ASSERT(next_offset - kOneofBase < d->table.UPB_PRIVATE(field_count)); + f = &d->fields[next_offset - kOneofBase]; + } + } + + // Since messages are always allocated on arenas, we can save repeatedly + // realigning by doing alignment at minitable construction time. We don't want + // to align to UPB_MALLOC_ALIGN because it can change with sanitizers, and if + // we're generating code we don't want to calculate size differently depending + // on the proto compiler's host or build configuration. + d->table.UPB_PRIVATE(size) = + UPB_ALIGN_UP(d->table.UPB_PRIVATE(size), kUpb_Message_Align); +} + +static void upb_MtDecoder_ValidateEntryField(upb_MtDecoder* d, + const upb_MiniTableField* f, + uint32_t expected_num) { + const char* name = expected_num == 1 ? "key" : "val"; + const uint32_t f_number = upb_MiniTableField_Number(f); + if (f_number != expected_num) { + upb_MdDecoder_ErrorJmp(&d->base, + "map %s did not have expected number (%d vs %d)", + name, expected_num, f_number); + } + + if (!upb_MiniTableField_IsScalar(f)) { + upb_MdDecoder_ErrorJmp( + &d->base, "map %s cannot be repeated or map, or be in oneof", name); + } + + uint32_t not_ok_types; + if (expected_num == 1) { + not_ok_types = (1 << kUpb_FieldType_Float) | (1 << kUpb_FieldType_Double) | + (1 << kUpb_FieldType_Message) | (1 << kUpb_FieldType_Group) | + (1 << kUpb_FieldType_Bytes) | (1 << kUpb_FieldType_Enum); + } else { + not_ok_types = 1 << kUpb_FieldType_Group; + } + + if ((1 << upb_MiniTableField_Type(f)) & not_ok_types) { + upb_MdDecoder_ErrorJmp(&d->base, "map %s cannot have type %d", name, + (int)f->UPB_PRIVATE(descriptortype)); + } +} + +static void upb_MtDecoder_ParseMap(upb_MtDecoder* d, const char* data, + size_t len) { + upb_MtDecoder_ParseMessage(d, data, len); + upb_MtDecoder_AssignHasbits(d); + + if (UPB_UNLIKELY(d->table.UPB_PRIVATE(field_count) != 2)) { + upb_MdDecoder_ErrorJmp(&d->base, "%hu fields in map", + d->table.UPB_PRIVATE(field_count)); + UPB_UNREACHABLE(); + } + + if (d->oneofs.size != 0) { + upb_MdDecoder_ErrorJmp(&d->base, "Map entry cannot have oneof"); + } + + upb_MtDecoder_ValidateEntryField(d, &d->table.UPB_PRIVATE(fields)[0], 1); + upb_MtDecoder_ValidateEntryField(d, &d->table.UPB_PRIVATE(fields)[1], 2); + + d->fields[0].UPB_PRIVATE(offset) = offsetof(upb_MapEntry, k); + d->fields[1].UPB_PRIVATE(offset) = offsetof(upb_MapEntry, v); + d->table.UPB_PRIVATE(size) = sizeof(upb_MapEntry); + + // Map entries have a special bit set to signal it's a map entry, used in + // upb_MiniTable_SetSubMessage() below. + d->table.UPB_PRIVATE(ext) |= kUpb_ExtMode_IsMapEntry; +} + +static void upb_MtDecoder_ParseMessageSet(upb_MtDecoder* d, const char* data, + size_t len) { + if (len > 0) { + upb_MdDecoder_ErrorJmp(&d->base, "Invalid message set encode length: %zu", + len); + } + + upb_MiniTable* ret = &d->table; + ret->UPB_PRIVATE(size) = kUpb_Reserved_Hasbytes; + ret->UPB_PRIVATE(field_count) = 0; + ret->UPB_PRIVATE(ext) = kUpb_ExtMode_IsMessageSet; + ret->UPB_PRIVATE(dense_below) = 0; + ret->UPB_PRIVATE(table_mask) = -1; + ret->UPB_PRIVATE(required_count) = 0; +} + +static upb_MiniTable* upb_MtDecoder_DoBuildMiniTableWithBuf( + upb_MtDecoder* decoder, const char* data, size_t len, void** buf, + size_t* buf_size) { + decoder->table.UPB_PRIVATE(size) = kUpb_Reserved_Hasbytes; + decoder->table.UPB_PRIVATE(field_count) = 0; + decoder->table.UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable; + decoder->table.UPB_PRIVATE(dense_below) = 0; + decoder->table.UPB_PRIVATE(table_mask) = -1; + decoder->table.UPB_PRIVATE(required_count) = 0; +#ifdef UPB_TRACING_ENABLED + // MiniTables built from MiniDescriptors will not be able to vend the message + // name unless it is explicitly set with upb_MiniTable_SetFullName(). + decoder->table.UPB_PRIVATE(full_name) = 0; +#endif + + // Strip off and verify the version tag. + if (!len--) goto done; + const char vers = *data++; + + switch (vers) { + case kUpb_EncodedVersion_MapV1: + upb_MtDecoder_ParseMap(decoder, data, len); + break; + + case kUpb_EncodedVersion_MessageV1: + upb_MtDecoder_ParseMessage(decoder, data, len); + upb_MtDecoder_AssignHasbits(decoder); + upb_MtDecoder_CalculateAlignments(decoder); + upb_MtDecoder_AssignOffsets(decoder); + break; + + case kUpb_EncodedVersion_MessageSetV1: + upb_MtDecoder_ParseMessageSet(decoder, data, len); + break; + + default: + upb_MdDecoder_ErrorJmp(&decoder->base, "Invalid message version: %c", + vers); + } + +done: + *buf = decoder->oneofs.data; + *buf_size = decoder->oneofs.buf_capacity_bytes; + size_t mt_size = sizeof(upb_MiniTable); + +#if UPB_FASTTABLE + upb_DecodeFast_TableEntry fasttable[32]; + int fasttable_size = upb_DecodeFast_BuildTable(&decoder->table, fasttable); + mt_size += fasttable_size * sizeof(fasttable[0]); +#endif + + upb_MiniTable* ret = upb_Arena_Malloc(decoder->arena, mt_size); + memcpy(ret, &decoder->table, sizeof(*ret)); + +#if UPB_FASTTABLE + ret->UPB_PRIVATE(table_mask) = upb_DecodeFast_GetTableMask(fasttable_size); + for (int i = 0; i < fasttable_size; i++) { + ret->UPB_PRIVATE(fasttable)[i].field_data = fasttable[i].function_data; + ret->UPB_PRIVATE(fasttable)[i].field_parser = + upb_DecodeFast_GetFunctionPointer(fasttable[i].function_idx); + } +#endif + UPB_PRIVATE(upb_MiniTable_CheckInvariants)(ret); + return ret; +} + +static upb_MiniTable* upb_MtDecoder_BuildMiniTableWithBuf( + upb_MtDecoder* const decoder, const char* const data, const size_t len, + void** const buf, size_t* const buf_size) { + if (UPB_SETJMP(decoder->base.err) != 0) { + *buf = decoder->oneofs.data; + *buf_size = decoder->oneofs.buf_capacity_bytes; + return NULL; + } + + return upb_MtDecoder_DoBuildMiniTableWithBuf(decoder, data, len, buf, + buf_size); +} + +upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, void** buf, + size_t* buf_size, + upb_Status* status) { + upb_MtDecoder decoder = { + .base = {.status = status}, + .platform = platform, + .sub_count = 0, + .is_extension = false, + .oneofs = + { + .data = *buf, + .buf_capacity_bytes = *buf_size, + .size = 0, + }, + .arena = arena, + }; + + return upb_MtDecoder_BuildMiniTableWithBuf(&decoder, data, len, buf, + buf_size); +} + +static const char* upb_MtDecoder_DoBuildMiniTableExtension( + upb_MtDecoder* decoder, const char* data, size_t len, + upb_MiniTableExtension* ext, const upb_MiniTable* extendee, + upb_MiniTableSub sub) { + if (!(extendee->UPB_PRIVATE(ext) & + (kUpb_ExtMode_Extendable | kUpb_ExtMode_IsMessageSet))) { + upb_MdDecoder_ErrorJmp(&decoder->base, "Extendee is not extendable"); + } + + // If the string is non-empty then it must begin with a version tag. + if (len) { + if (*data != kUpb_EncodedVersion_ExtensionV1) { + upb_MdDecoder_ErrorJmp(&decoder->base, "Invalid ext version: %c", *data); + } + data++; + len--; + } + + uint16_t count = 0; + const char* ret = + upb_MtDecoder_Parse(decoder, data, len, ext, sizeof(*ext), &count); + if (!ret || count != 1) return NULL; + + upb_MiniTableField* f = &ext->UPB_PRIVATE(field); + + f->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsExtension; + f->UPB_PRIVATE(offset) = 0; + f->presence = 0; + + // In upb_MiniTableExtension, the `sub` member is a pointer-sized member that + // directly follows the `field` member. + f->UPB_PRIVATE(submsg_ofs) = + UPB_ALIGN_UP(sizeof(upb_MiniTableField), upb_MtDecoder_PtrSize(decoder)) / + kUpb_SubmsgOffsetBytes; + + if (extendee->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMessageSet) { + // Extensions of MessageSet must be messages. + if (!upb_MiniTableField_IsSubMessage(f)) return NULL; + + // Extensions of MessageSet must be non-repeating. + if (upb_MiniTableField_IsArray(f)) return NULL; + } + + ext->UPB_PRIVATE(extendee) = extendee; + ext->UPB_PRIVATE(sub) = sub; + + return ret; +} + +static const char* upb_MtDecoder_BuildMiniTableExtension( + upb_MtDecoder* const decoder, const char* const data, const size_t len, + upb_MiniTableExtension* const ext, const upb_MiniTable* const extendee, + const upb_MiniTableSub sub) { + if (UPB_SETJMP(decoder->base.err) != 0) return NULL; + return upb_MtDecoder_DoBuildMiniTableExtension(decoder, data, len, ext, + extendee, sub); +} + +const char* _upb_MiniTableExtension_Init(const char* data, size_t len, + upb_MiniTableExtension* ext, + const upb_MiniTable* extendee, + upb_MiniTableSub sub, + upb_MiniTablePlatform platform, + upb_Status* status) { + upb_MtDecoder decoder = { + .base = {.status = status}, + .arena = NULL, + .platform = platform, + .sub_count = 0, + .is_extension = true, + }; + + return upb_MtDecoder_BuildMiniTableExtension(&decoder, data, len, ext, + extendee, sub); +} + +upb_MiniTableExtension* _upb_MiniTableExtension_Build( + const char* data, size_t len, const upb_MiniTable* extendee, + upb_MiniTableSub sub, upb_MiniTablePlatform platform, upb_Arena* arena, + upb_Status* status) { + upb_MiniTableExtension* ext = + upb_Arena_Malloc(arena, sizeof(upb_MiniTableExtension)); + if (UPB_UNLIKELY(!ext)) return NULL; + + const char* ptr = _upb_MiniTableExtension_Init(data, len, ext, extendee, sub, + platform, status); + if (UPB_UNLIKELY(!ptr)) return NULL; + + return ext; +} + +upb_MiniTable* _upb_MiniTable_Build(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, upb_Status* status) { + void* buf = NULL; + size_t size = 0; + upb_MiniTable* ret = upb_MiniTable_BuildWithBuf(data, len, platform, arena, + &buf, &size, status); + upb_gfree(buf); + return ret; +} + + +#include +#include +#include + + +// Must be last. + +bool upb_MiniTable_SetSubMessage(upb_MiniTable* table, + upb_MiniTableField* field, + const upb_MiniTable* sub) { + UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field && + (uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) + + table->UPB_PRIVATE(field_count))); + UPB_ASSERT(sub); + + const bool sub_is_map = sub->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry; + + switch (field->UPB_PRIVATE(descriptortype)) { + case kUpb_FieldType_Message: + if (sub_is_map) { + const bool table_is_map = + table->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry; + if (UPB_UNLIKELY(table_is_map)) return false; + + field->UPB_PRIVATE(mode) = + (field->UPB_PRIVATE(mode) & ~kUpb_FieldMode_Mask) | + kUpb_FieldMode_Map; + } + break; + + case kUpb_FieldType_Group: + if (UPB_UNLIKELY(sub_is_map)) return false; + break; + + default: + return false; + } + + upb_MiniTableSubInternal* table_sub = + UPB_PTR_AT(field, field->UPB_PRIVATE(submsg_ofs) * kUpb_SubmsgOffsetBytes, + upb_MiniTableSubInternal); + // TODO: Add this assert back once YouTube is updated to not call + // this function repeatedly. + // UPB_ASSERT(upb_MiniTable_GetSubMessageTable(table, field) == NULL); + table_sub->UPB_PRIVATE(submsg) = sub; + return true; +} + +bool upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTableField* field, + const upb_MiniTableEnum* sub) { + UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field && + (uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) + + table->UPB_PRIVATE(field_count))); + UPB_ASSERT(sub); + + if (field->UPB_PRIVATE(descriptortype) != kUpb_FieldType_Enum) { + return false; + } + + if ((table->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry) && + !upb_MiniTableEnum_CheckValue(sub, 0)) { + // An enum used in a map must include 0 as a value. This matches a check + // performed in protoc ("Enum value in map must define 0 as the first + // value"). Protoc should ensure that we never get here. + // + // This ends up being important if we receive wire messages where a map + // entry omits the value (and thus defaults to 0). + return false; + } + + upb_MiniTableSubInternal* table_sub = + UPB_PTR_AT(field, field->UPB_PRIVATE(submsg_ofs) * kUpb_SubmsgOffsetBytes, + upb_MiniTableSubInternal); + *table_sub = upb_MiniTableSub_FromEnum(sub); + return true; +} + +uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* m, + const upb_MiniTableField** subs) { + uint32_t msg_count = 0; + uint32_t enum_count = 0; + + for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { + const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i); + if (upb_MiniTableField_CType(f) == kUpb_CType_Message) { + *subs = f; + ++subs; + msg_count++; + } + } + + for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { + const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i); + if (upb_MiniTableField_IsClosedEnum(f)) { + *subs = f; + ++subs; + enum_count++; + } + } + + return (msg_count << 16) | enum_count; +} + +// The list of sub_tables and sub_enums must exactly match the number and order +// of sub-message fields and sub-enum fields given by upb_MiniTable_GetSubList() +// above. +bool upb_MiniTable_Link(upb_MiniTable* m, const upb_MiniTable** sub_tables, + size_t sub_table_count, + const upb_MiniTableEnum** sub_enums, + size_t sub_enum_count) { + uint32_t msg_count = 0; + uint32_t enum_count = 0; + + for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { + upb_MiniTableField* f = + (upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i); + if (upb_MiniTableField_CType(f) == kUpb_CType_Message) { + const upb_MiniTable* sub = sub_tables[msg_count++]; + if (msg_count > sub_table_count) return false; + if (sub && !upb_MiniTable_SetSubMessage(m, f, sub)) return false; + } + } + + for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) { + upb_MiniTableField* f = + (upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i); + if (upb_MiniTableField_IsClosedEnum(f)) { + const upb_MiniTableEnum* sub = sub_enums[enum_count++]; + if (enum_count > sub_enum_count) return false; + if (sub && !upb_MiniTable_SetSubEnum(m, f, sub)) return false; + } + } + + return (msg_count == sub_table_count) && (enum_count == sub_enum_count); +} + + +const char _kUpb_ToBase92[] = { + ' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', + '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', + 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', + 'Z', '[', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', + 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '{', '|', '}', '~', +}; + +const int8_t _kUpb_FromBase92[] = { + 0, 1, -1, 2, 3, 4, 5, -1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, -1, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, +}; + + +#include +#include +#include + + +// Must be last. + +typedef struct { + uint64_t present_values_mask; + uint32_t last_written_value; +} upb_MtDataEncoderInternal_EnumState; + +typedef struct { + uint64_t msg_modifiers; + uint32_t last_field_num; + enum { + kUpb_OneofState_NotStarted, + kUpb_OneofState_StartedOneof, + kUpb_OneofState_EmittedOneofField, + } oneof_state; +} upb_MtDataEncoderInternal_MsgState; + +typedef struct { + char* buf_start; // Only for checking kUpb_MtDataEncoder_MinSize. + union { + upb_MtDataEncoderInternal_EnumState enum_state; + upb_MtDataEncoderInternal_MsgState msg_state; + } state; +} upb_MtDataEncoderInternal; + +static upb_MtDataEncoderInternal* upb_MtDataEncoder_GetInternal( + upb_MtDataEncoder* e, char* buf_start) { + UPB_ASSERT(sizeof(upb_MtDataEncoderInternal) <= sizeof(e->internal)); + upb_MtDataEncoderInternal* ret = (upb_MtDataEncoderInternal*)e->internal; + ret->buf_start = buf_start; + return ret; +} + +static char* upb_MtDataEncoder_PutRaw(upb_MtDataEncoder* e, char* ptr, + char ch) { + upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal; + UPB_ASSERT(ptr - in->buf_start < kUpb_MtDataEncoder_MinSize); + if (ptr == e->end) return NULL; + *ptr++ = ch; + return ptr; +} + +static char* upb_MtDataEncoder_Put(upb_MtDataEncoder* e, char* ptr, char ch) { + return upb_MtDataEncoder_PutRaw(e, ptr, _upb_ToBase92(ch)); +} + +static char* upb_MtDataEncoder_PutBase92Varint(upb_MtDataEncoder* e, char* ptr, + uint32_t val, int min, int max) { + int shift = upb_Log2Ceiling(_upb_FromBase92(max) - _upb_FromBase92(min) + 1); + UPB_ASSERT(shift <= 6); + uint32_t mask = (1 << shift) - 1; + do { + uint32_t bits = val & mask; + ptr = upb_MtDataEncoder_Put(e, ptr, bits + _upb_FromBase92(min)); + if (!ptr) return NULL; + val >>= shift; + } while (val); + return ptr; +} + +char* upb_MtDataEncoder_PutModifier(upb_MtDataEncoder* e, char* ptr, + uint64_t mod) { + if (mod) { + ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, mod, + kUpb_EncodedValue_MinModifier, + kUpb_EncodedValue_MaxModifier); + } + return ptr; +} + +char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr, + upb_FieldType type, uint32_t field_num, + uint64_t field_mod) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + in->state.msg_state.msg_modifiers = 0; + in->state.msg_state.last_field_num = 0; + in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted; + + ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_ExtensionV1); + if (!ptr) return NULL; + + return upb_MtDataEncoder_PutField(e, ptr, type, field_num, field_mod); +} + +char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr, + upb_FieldType key_type, + upb_FieldType value_type, uint64_t key_mod, + uint64_t value_mod) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + in->state.msg_state.msg_modifiers = 0; + in->state.msg_state.last_field_num = 0; + in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted; + + ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MapV1); + if (!ptr) return NULL; + + ptr = upb_MtDataEncoder_PutField(e, ptr, key_type, 1, key_mod); + if (!ptr) return NULL; + + return upb_MtDataEncoder_PutField(e, ptr, value_type, 2, value_mod); +} + +char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr) { + (void)upb_MtDataEncoder_GetInternal(e, ptr); + return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageSetV1); +} + +char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr, + uint64_t msg_mod) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + in->state.msg_state.msg_modifiers = msg_mod; + in->state.msg_state.last_field_num = 0; + in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted; + + ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageV1); + if (!ptr) return NULL; + + return upb_MtDataEncoder_PutModifier(e, ptr, msg_mod); +} + +static char* _upb_MtDataEncoder_MaybePutFieldSkip(upb_MtDataEncoder* e, + char* ptr, + uint32_t field_num) { + upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal; + if (field_num <= in->state.msg_state.last_field_num) return NULL; + if (in->state.msg_state.last_field_num + 1 != field_num) { + // Put skip. + UPB_ASSERT(field_num > in->state.msg_state.last_field_num); + uint32_t skip = field_num - in->state.msg_state.last_field_num; + ptr = upb_MtDataEncoder_PutBase92Varint( + e, ptr, skip, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip); + if (!ptr) return NULL; + } + in->state.msg_state.last_field_num = field_num; + return ptr; +} + +static char* _upb_MtDataEncoder_PutFieldType(upb_MtDataEncoder* e, char* ptr, + upb_FieldType type, + uint64_t field_mod) { + static const char kUpb_TypeToEncoded[] = { + [kUpb_FieldType_Double] = kUpb_EncodedType_Double, + [kUpb_FieldType_Float] = kUpb_EncodedType_Float, + [kUpb_FieldType_Int64] = kUpb_EncodedType_Int64, + [kUpb_FieldType_UInt64] = kUpb_EncodedType_UInt64, + [kUpb_FieldType_Int32] = kUpb_EncodedType_Int32, + [kUpb_FieldType_Fixed64] = kUpb_EncodedType_Fixed64, + [kUpb_FieldType_Fixed32] = kUpb_EncodedType_Fixed32, + [kUpb_FieldType_Bool] = kUpb_EncodedType_Bool, + [kUpb_FieldType_String] = kUpb_EncodedType_String, + [kUpb_FieldType_Group] = kUpb_EncodedType_Group, + [kUpb_FieldType_Message] = kUpb_EncodedType_Message, + [kUpb_FieldType_Bytes] = kUpb_EncodedType_Bytes, + [kUpb_FieldType_UInt32] = kUpb_EncodedType_UInt32, + [kUpb_FieldType_Enum] = kUpb_EncodedType_OpenEnum, + [kUpb_FieldType_SFixed32] = kUpb_EncodedType_SFixed32, + [kUpb_FieldType_SFixed64] = kUpb_EncodedType_SFixed64, + [kUpb_FieldType_SInt32] = kUpb_EncodedType_SInt32, + [kUpb_FieldType_SInt64] = kUpb_EncodedType_SInt64, + }; + + int encoded_type = kUpb_TypeToEncoded[type]; + + if (field_mod & kUpb_FieldModifier_IsClosedEnum) { + UPB_ASSERT(type == kUpb_FieldType_Enum); + encoded_type = kUpb_EncodedType_ClosedEnum; + } + + if (field_mod & kUpb_FieldModifier_IsRepeated) { + // Repeated fields shift the type number up (unlike other modifiers which + // are bit flags). + encoded_type += kUpb_EncodedType_RepeatedBase; + } + + return upb_MtDataEncoder_Put(e, ptr, encoded_type); +} + +static char* _upb_MtDataEncoder_MaybePutModifiers(upb_MtDataEncoder* e, + char* ptr, upb_FieldType type, + uint64_t field_mod) { + upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal; + uint32_t encoded_modifiers = 0; + if ((field_mod & kUpb_FieldModifier_IsRepeated) && + upb_FieldType_IsPackable(type)) { + bool field_is_packed = field_mod & kUpb_FieldModifier_IsPacked; + bool default_is_packed = in->state.msg_state.msg_modifiers & + kUpb_MessageModifier_DefaultIsPacked; + if (field_is_packed != default_is_packed) { + encoded_modifiers |= kUpb_EncodedFieldModifier_FlipPacked; + } + } + + if (type == kUpb_FieldType_String) { + bool field_validates_utf8 = field_mod & kUpb_FieldModifier_ValidateUtf8; + bool message_validates_utf8 = + in->state.msg_state.msg_modifiers & kUpb_MessageModifier_ValidateUtf8; + if (field_validates_utf8 != message_validates_utf8) { + // Old binaries do not recognize the field modifier. We need the failure + // mode to be too lax rather than too strict. Our caller should have + // handled this (see _upb_MessageDef_ValidateUtf8()). + assert(!message_validates_utf8); + encoded_modifiers |= kUpb_EncodedFieldModifier_FlipValidateUtf8; + } + } + + if (field_mod & kUpb_FieldModifier_IsProto3Singular) { + encoded_modifiers |= kUpb_EncodedFieldModifier_IsProto3Singular; + } + + if (field_mod & kUpb_FieldModifier_IsRequired) { + encoded_modifiers |= kUpb_EncodedFieldModifier_IsRequired; + } + + return upb_MtDataEncoder_PutModifier(e, ptr, encoded_modifiers); +} + +char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr, + upb_FieldType type, uint32_t field_num, + uint64_t field_mod) { + upb_MtDataEncoder_GetInternal(e, ptr); + + ptr = _upb_MtDataEncoder_MaybePutFieldSkip(e, ptr, field_num); + if (!ptr) return NULL; + + ptr = _upb_MtDataEncoder_PutFieldType(e, ptr, type, field_mod); + if (!ptr) return NULL; + + return _upb_MtDataEncoder_MaybePutModifiers(e, ptr, type, field_mod); +} + +char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + if (in->state.msg_state.oneof_state == kUpb_OneofState_NotStarted) { + ptr = upb_MtDataEncoder_Put(e, ptr, _upb_FromBase92(kUpb_EncodedValue_End)); + } else { + ptr = upb_MtDataEncoder_Put( + e, ptr, _upb_FromBase92(kUpb_EncodedValue_OneofSeparator)); + } + in->state.msg_state.oneof_state = kUpb_OneofState_StartedOneof; + return ptr; +} + +char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr, + uint32_t field_num) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + if (in->state.msg_state.oneof_state == kUpb_OneofState_EmittedOneofField) { + ptr = upb_MtDataEncoder_Put( + e, ptr, _upb_FromBase92(kUpb_EncodedValue_FieldSeparator)); + if (!ptr) return NULL; + } + ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, field_num, _upb_ToBase92(0), + _upb_ToBase92(63)); + in->state.msg_state.oneof_state = kUpb_OneofState_EmittedOneofField; + return ptr; +} + +char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + in->state.enum_state.present_values_mask = 0; + in->state.enum_state.last_written_value = 0; + + return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_EnumV1); +} + +static char* upb_MtDataEncoder_FlushDenseEnumMask(upb_MtDataEncoder* e, + char* ptr) { + upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal; + ptr = upb_MtDataEncoder_Put(e, ptr, in->state.enum_state.present_values_mask); + in->state.enum_state.present_values_mask = 0; + in->state.enum_state.last_written_value += 5; + return ptr; +} + +char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr, + uint32_t val) { + // TODO: optimize this encoding. + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + UPB_ASSERT(val >= in->state.enum_state.last_written_value); + uint32_t delta = val - in->state.enum_state.last_written_value; + if (delta >= 5 && in->state.enum_state.present_values_mask) { + ptr = upb_MtDataEncoder_FlushDenseEnumMask(e, ptr); + if (!ptr) { + return NULL; + } + delta -= 5; + } + + if (delta >= 5) { + ptr = upb_MtDataEncoder_PutBase92Varint( + e, ptr, delta, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip); + in->state.enum_state.last_written_value += delta; + delta = 0; + } + + UPB_ASSERT((in->state.enum_state.present_values_mask >> delta) == 0); + in->state.enum_state.present_values_mask |= 1ULL << delta; + return ptr; +} + +char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr) { + upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr); + if (!in->state.enum_state.present_values_mask) return ptr; + return upb_MtDataEncoder_FlushDenseEnumMask(e, ptr); +} + + +#include +#include +#include + + +// Must be last. + +struct upb_ExtensionRegistry { + upb_exttable exts; + upb_Arena* arena; +}; + +upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena) { + upb_ExtensionRegistry* r = upb_Arena_Malloc(arena, sizeof(*r)); + if (!r) return NULL; + r->arena = arena; + if (!upb_exttable_init(&r->exts, 8, arena)) return NULL; + return r; +} + +UPB_API upb_ExtensionRegistryStatus upb_ExtensionRegistry_Add( + upb_ExtensionRegistry* r, const upb_MiniTableExtension* e) { + UPB_STATIC_ASSERT( + offsetof(upb_MiniTableExtension, + UPB_PRIVATE(field).UPB_PRIVATE(number)) == 0, + "Extension must be first-member-of-struct convertable with uint32_t"); + uint32_t fieldnum = upb_MiniTableExtension_Number(e); + const upb_MiniTable* extendee = upb_MiniTableExtension_Extendee(e); + + const uint32_t kMaxFieldNumber = (1 << 29) - 1; + if (fieldnum == 0 || + (fieldnum > kMaxFieldNumber && !upb_MiniTable_IsMessageSet(extendee))) { + return kUpb_ExtensionRegistryStatus_InvalidExtension; + } + + if (upb_exttable_lookup(&r->exts, extendee, fieldnum) != NULL) { + return kUpb_ExtensionRegistryStatus_DuplicateEntry; + } + + if (!upb_exttable_insert(&r->exts, extendee, (const uint32_t*)e, r->arena)) { + return kUpb_ExtensionRegistryStatus_OutOfMemory; + } + return kUpb_ExtensionRegistryStatus_Ok; +} + +upb_ExtensionRegistryStatus upb_ExtensionRegistry_AddArray( + upb_ExtensionRegistry* r, const upb_MiniTableExtension** e, size_t count) { + const upb_MiniTableExtension** start = e; + const upb_MiniTableExtension** end = UPB_PTRADD(e, count); + upb_ExtensionRegistryStatus status = kUpb_ExtensionRegistryStatus_Ok; + for (; e < end; e++) { + status = upb_ExtensionRegistry_Add(r, *e); + if (status != kUpb_ExtensionRegistryStatus_Ok) goto failure; + } + return kUpb_ExtensionRegistryStatus_Ok; + +failure: + // Back out the entries previously added. + for (end = e, e = start; e < end; e++) { + const upb_MiniTableExtension* ext = *e; + upb_exttable_remove(&r->exts, upb_MiniTableExtension_Extendee(ext), + upb_MiniTableExtension_Number(ext)); + } + UPB_ASSERT(status != kUpb_ExtensionRegistryStatus_Ok); + return status; +} + +const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup( + const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num) { + const uint32_t* v = upb_exttable_lookup(&r->exts, t, num); + return (const upb_MiniTableExtension*)v; +} + +size_t upb_ExtensionRegistry_Size(const upb_ExtensionRegistry* r) { + return upb_exttable_size(&r->exts); +} + + +#include +#include + + +// Must be last. + +#if UPB_TSAN +#include +#endif // UPB_TSAN + +const UPB_PRIVATE(upb_GeneratedExtensionListEntry) * + UPB_PRIVATE(upb_generated_extension_list) = NULL; + +typedef struct upb_GeneratedRegistry { + UPB_ATOMIC(upb_GeneratedRegistryRef*) ref; + UPB_ATOMIC(int32_t) ref_count; +} upb_GeneratedRegistry; + +static upb_GeneratedRegistry* _upb_generated_registry(void) { + static upb_GeneratedRegistry r = {NULL, 0}; + return &r; +} + +static bool _upb_GeneratedRegistry_AddAllLinkedExtensions( + upb_ExtensionRegistry* r) { + const UPB_PRIVATE(upb_GeneratedExtensionListEntry)* entry = + UPB_PRIVATE(upb_generated_extension_list); + while (entry != NULL) { + const upb_MiniTableExtension** current = entry->start; + for (current = entry->start; current != entry->stop; ++current) { + const upb_MiniTableExtension* ext = *current; + // Sentinels and padding introduced by the linker can result in zeroed + // entries, so simply skip them. + if (*current == NULL) { + // MSVC introduces padding that might not be sized exactly the same as + // the linker array element, but it should be properly aligned, so just + // skipping empty elements should be safe. (If the size and align of + // the array elements was different, we'd have to do something more + // complicated). + continue; + } + + if (upb_ExtensionRegistry_Add(r, ext) != + kUpb_ExtensionRegistryStatus_Ok) { + return false; + } + } + entry = entry->next; + } + return true; +} + +// Constructs a new GeneratedRegistryRef, adding all linked extensions to the +// registry or returning NULL on failure. +static upb_GeneratedRegistryRef* _upb_GeneratedRegistry_New(void) { + upb_Arena* arena = NULL; + upb_ExtensionRegistry* extreg = NULL; + upb_GeneratedRegistryRef* ref = upb_gmalloc(sizeof(upb_GeneratedRegistryRef)); + if (ref == NULL) goto err; + arena = upb_Arena_New(); + if (arena == NULL) goto err; + extreg = upb_ExtensionRegistry_New(arena); + if (extreg == NULL) goto err; + + ref->UPB_PRIVATE(arena) = arena; + ref->UPB_PRIVATE(registry) = extreg; + + if (!_upb_GeneratedRegistry_AddAllLinkedExtensions(extreg)) goto err; + + return ref; + +err: + if (arena != NULL) upb_Arena_Free(arena); + if (ref != NULL) upb_gfree(ref); + return NULL; +} + +const upb_GeneratedRegistryRef* upb_GeneratedRegistry_Load(void) { + upb_GeneratedRegistry* registry = _upb_generated_registry(); + + // Loop until we successfully acquire a reference. This loop should only + // kick in under extremely high contention, and it should be guaranteed to + // succeed. + while (true) { + int32_t count = upb_Atomic_Load(®istry->ref_count, memory_order_acquire); + + // Try to increment the refcount, but only if it's not zero. + while (count > 0) { + if (upb_Atomic_CompareExchangeStrong(®istry->ref_count, &count, + count + 1, memory_order_acquire, + memory_order_relaxed)) { + // Successfully incremented. We can now safely load and return the + // pointer. + const upb_GeneratedRegistryRef* ref = + upb_Atomic_Load(®istry->ref, memory_order_acquire); + UPB_ASSERT(ref != NULL); + return ref; + } + // CAS failed, `count` was updated. Loop will retry. + } + + // If we're here, the count was 0. Time for the slow path. + // Double-check that the pointer is NULL before trying to create. + upb_GeneratedRegistryRef* ref = + upb_Atomic_Load(®istry->ref, memory_order_acquire); + if (ref == NULL) { + // Pointer is NULL, try to create and publish a new registry. + upb_GeneratedRegistryRef* new_ref = _upb_GeneratedRegistry_New(); + if (new_ref == NULL) return NULL; // OOM + + // Try to CAS the pointer from NULL to our new_ref. + if (upb_Atomic_CompareExchangeStrong(®istry->ref, &ref, new_ref, + memory_order_release, + memory_order_acquire)) { + // We won the race. Set the ref count to 1. + upb_Atomic_Store(®istry->ref_count, 1, memory_order_release); + return new_ref; + } else { + // We lost the race. `ref` now holds the pointer from the winning + // thread. Clean up our unused one and loop to try again to get a + // reference. + upb_Arena_Free(new_ref->UPB_PRIVATE(arena)); + upb_gfree(new_ref); + } + } + // If we are here, either we lost the CAS race, or the pointer was already + // non-NULL. In either case, we loop to the top and try to increment the + // refcount of the existing object. + +#if UPB_TSAN + // Yield to give other threads a chance to increment the refcount. This is + // especially an issue for TSAN builds, which are prone to locking up from + // the thread with the upb_Atomic_Store call above getting starved. + sched_yield(); +#endif // UPB_TSAN + } +} + +void upb_GeneratedRegistry_Release(const upb_GeneratedRegistryRef* r) { + if (r == NULL) return; + + upb_GeneratedRegistry* registry = _upb_generated_registry(); + + int ref_count = upb_Atomic_Sub(®istry->ref_count, 1, memory_order_acq_rel); + UPB_ASSERT(registry->ref_count >= 0); + + // A ref_count of 1 means that we decremented the refcount to 0. + if (ref_count == 1) { + upb_GeneratedRegistryRef* ref = + upb_Atomic_Exchange(®istry->ref, NULL, memory_order_acq_rel); + if (ref != NULL) { + // This is the last reference and we won any potential race to store NULL, + // so we need to clean up. + upb_Arena_Free(ref->UPB_PRIVATE(arena)); + upb_gfree(ref); + } + } +} + +const upb_ExtensionRegistry* upb_GeneratedRegistry_Get( + const upb_GeneratedRegistryRef* r) { + if (r == NULL) return NULL; + return r->UPB_PRIVATE(registry); +} + + +#include +#include + + +// Must be last. + +const upb_MiniTableField* upb_MiniTable_GetOneof(const upb_MiniTable* m, + const upb_MiniTableField* f) { + if (UPB_UNLIKELY(!upb_MiniTableField_IsInOneof(f))) { + return NULL; + } + const upb_MiniTableField* ptr = &m->UPB_PRIVATE(fields)[0]; + const upb_MiniTableField* end = + &m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)]; + for (; ptr < end; ptr++) { + if (ptr->presence == (*f).presence) { + return ptr; + } + } + return NULL; +} + +bool upb_MiniTable_NextOneofField(const upb_MiniTable* m, + const upb_MiniTableField** f) { + const upb_MiniTableField* ptr = *f; + const upb_MiniTableField* end = + &m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)]; + while (++ptr < end) { + if (ptr->presence == (*f)->presence) { + *f = ptr; + return true; + } + } + return false; +} + + +#include + + +// Must be last. + +// A MiniTable for a statically tree shaken message. Messages that use this +// MiniTable are guaranteed to remain unlinked; unlike the empty message, this +// MiniTable is never replaced, which greatly simplifies everything, because the +// type of a sub-message is always known, without consulting a tagged bit. +const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_StaticallyTreeShaken) = { + .UPB_PRIVATE(fields) = NULL, + .UPB_PRIVATE(size) = sizeof(struct upb_Message), + .UPB_PRIVATE(field_count) = 0, + .UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable, + .UPB_PRIVATE(dense_below) = 0, + .UPB_PRIVATE(table_mask) = -1, + .UPB_PRIVATE(required_count) = 0, +}; + + +#include +#include +#include +#include +#include + + +// Must be last. + +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; + const upb_GeneratedRegistryRef* generated_extreg; + const google_protobuf_FeatureSetDefaults* feature_set_defaults; + upb_MiniTablePlatform platform; + void* scratch_data; + size_t scratch_size; + size_t bytes_loaded; + bool disable_closed_enum_checking; + bool disable_implicit_field_presence; +}; + +void upb_DefPool_Free(upb_DefPool* s) { + upb_GeneratedRegistry_Release(s->generated_extreg); + upb_Arena_Free(s->arena); + upb_gfree(s->scratch_data); + upb_gfree(s); +} + +static const char serialized_defaults[] = UPB_INTERNAL_UPB_EDITION_DEFAULTS; + +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->disable_closed_enum_checking = false; + s->disable_implicit_field_presence = false; + + 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; + + s->generated_extreg = upb_GeneratedRegistry_Load(); + if (!s->generated_extreg) goto err; + + s->platform = kUpb_MiniTablePlatform_Native; + + upb_Status status; + if (!upb_DefPool_SetFeatureSetDefaults( + s, serialized_defaults, sizeof(serialized_defaults) - 1, &status)) { + goto err; + } + + if (!s->feature_set_defaults) goto err; + + return s; + +err: + upb_DefPool_Free(s); + return NULL; +} + +void upb_DefPool_DisableClosedEnumChecking(upb_DefPool* s) { + UPB_ASSERT(upb_strtable_count(&s->files) == 0); + s->disable_closed_enum_checking = true; +} + +bool upb_DefPool_ClosedEnumCheckingDisabled(const upb_DefPool* s) { + return s->disable_closed_enum_checking; +} + +void upb_DefPool_DisableImplicitFieldPresence(upb_DefPool* s) { + UPB_ASSERT(upb_strtable_count(&s->files) == 0); + s->disable_implicit_field_presence = true; +} + +bool upb_DefPool_ImplicitFieldPresenceDisabled(const upb_DefPool* s) { + return s->disable_implicit_field_presence; +} + +const google_protobuf_FeatureSetDefaults* upb_DefPool_FeatureSetDefaults( + const upb_DefPool* s) { + return s->feature_set_defaults; +} + +bool upb_DefPool_SetFeatureSetDefaults(upb_DefPool* s, + const char* serialized_defaults, + size_t serialized_len, + upb_Status* status) { + const google_protobuf_FeatureSetDefaults* defaults = google_protobuf_FeatureSetDefaults_parse( + serialized_defaults, serialized_len, s->arena); + if (!defaults) { + upb_Status_SetErrorFormat(status, "Failed to parse defaults"); + return false; + } + if (upb_strtable_count(&s->files) > 0) { + upb_Status_SetErrorFormat(status, + "Feature set defaults can't be changed once the " + "pool has started building"); + return false; + } + int min_edition = google_protobuf_FeatureSetDefaults_minimum_edition(defaults); + int max_edition = google_protobuf_FeatureSetDefaults_maximum_edition(defaults); + if (min_edition > max_edition) { + upb_Status_SetErrorFormat(status, "Invalid edition range %s to %s", + upb_FileDef_EditionName(min_edition), + upb_FileDef_EditionName(max_edition)); + return false; + } + size_t size; + const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* const* + default_list = google_protobuf_FeatureSetDefaults_defaults(defaults, &size); + int prev_edition = google_protobuf_EDITION_UNKNOWN; + for (size_t i = 0; i < size; ++i) { + int edition = google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_edition( + default_list[i]); + if (edition == google_protobuf_EDITION_UNKNOWN) { + upb_Status_SetErrorFormat(status, "Invalid edition UNKNOWN specified"); + return false; + } + if (edition <= prev_edition) { + upb_Status_SetErrorFormat(status, + "Feature set defaults are not strictly " + "increasing, %s is greater than or equal to %s", + upb_FileDef_EditionName(prev_edition), + upb_FileDef_EditionName(edition)); + return false; + } + prev_edition = edition; + } + + // Copy the defaults into the pool. + s->feature_set_defaults = defaults; + return true; +} + +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; +} + +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_FindMessageByNameWithSize(s, sym, strlen(sym)); +} + +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_FindEnumByNameWithSize(s, sym, strlen(sym)); +} + +const upb_EnumDef* upb_DefPool_FindEnumByNameWithSize(const upb_DefPool* s, + const char* sym, + size_t len) { + return _upb_DefPool_Unpack(s, sym, len, UPB_DEFTYPE_ENUM); +} + +const upb_EnumValueDef* upb_DefPool_FindEnumValueByName(const upb_DefPool* s, + const char* sym) { + return upb_DefPool_FindEnumValueByNameWithSize(s, sym, strlen(sym)); +} + +const upb_EnumValueDef* upb_DefPool_FindEnumValueByNameWithSize( + const upb_DefPool* s, const char* sym, size_t len) { + return _upb_DefPool_Unpack(s, sym, len, 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); + if (_upb_MessageDef_InMessageSet(m)) { + for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) { + const upb_FieldDef* ext = upb_MessageDef_NestedExtension(m, i); + if (upb_FieldDef_MessageSubDef(ext) == m) { + return ext; + } + } + } + return 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: 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 google_protobuf_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_strtable_init(&builder->feature_cache, 16, + builder->tmp_arena) || + !(builder->legacy_features = + google_protobuf_FeatureSet_new(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 google_protobuf_FileDescriptorProto* file_proto, + const upb_MiniTableFile* layout, upb_Status* status) { + const upb_StringView name = google_protobuf_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, + .tmp_buf = NULL, + .tmp_buf_size = 0, + .layout = layout, + .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 google_protobuf_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; + google_protobuf_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 = google_protobuf_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 = upb_gmalloc(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); +} + +const upb_ExtensionRegistry* _upb_DefPool_GeneratedExtensionRegistry( + const upb_DefPool* s) { + return upb_GeneratedRegistry_Get(s->generated_extreg); +} + + +// Must be last. + +upb_deftype_t _upb_DefType_Type(upb_value v) { + const uintptr_t num = (uintptr_t)upb_value_getconstptr(v); + return num & UPB_DEFTYPE_MASK; +} + +upb_value _upb_DefType_Pack(const void* ptr, upb_deftype_t type) { + uintptr_t num = (uintptr_t)ptr; + UPB_ASSERT((num & UPB_DEFTYPE_MASK) == 0); + num |= type; + return upb_value_constptr((const void*)num); +} + +const void* _upb_DefType_Unpack(upb_value v, upb_deftype_t type) { + uintptr_t num = (uintptr_t)upb_value_getconstptr(v); + return (num & UPB_DEFTYPE_MASK) == type + ? (const void*)(num & ~UPB_DEFTYPE_MASK) + : NULL; +} + + +// Must be last. + +bool _upb_DescState_Grow(upb_DescState* d, upb_Arena* a) { + const size_t oldbufsize = d->bufsize; + const int used = d->ptr - d->buf; + + if (!d->buf) { + d->buf = upb_Arena_Malloc(a, d->bufsize); + if (!d->buf) return false; + d->ptr = d->buf; + d->e.end = d->buf + d->bufsize; + } + + if (oldbufsize - used < kUpb_MtDataEncoder_MinSize) { + d->bufsize *= 2; + d->buf = upb_Arena_Realloc(a, d->buf, oldbufsize, d->bufsize); + if (!d->buf) return false; + d->ptr = d->buf + used; + d->e.end = d->buf + d->bufsize; + } + + return true; +} + + +#include +#include +#include + + +// Must be last. + +struct upb_EnumDef { + UPB_ALIGN_AS(8) const google_protobuf_EnumOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + const upb_MiniTableEnum* layout; // Only for proto2. + const upb_FileDef* file; + const upb_MessageDef* containing_type; // Could be merged with "file". + const char* full_name; + upb_strtable ntoi; + upb_inttable iton; + const upb_EnumValueDef* values; + const upb_EnumReservedRange* res_ranges; + const upb_StringView* res_names; + int value_count; + int res_range_count; + int res_name_count; + int32_t defaultval; + google_protobuf_SymbolVisibility visibility; + bool is_sorted; // Whether all of the values are defined in ascending order. +}; + +upb_EnumDef* _upb_EnumDef_At(const upb_EnumDef* e, int i) { + return (upb_EnumDef*)&e[i]; +} + +const upb_MiniTableEnum* _upb_EnumDef_MiniTable(const upb_EnumDef* e) { + return e->layout; +} + +bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a) { + const char* name = upb_EnumValueDef_Name(v); + const upb_value val = upb_value_constptr(v); + bool ok = upb_strtable_insert(&e->ntoi, name, strlen(name), val, a); + if (!ok) return false; + + // Multiple enumerators can have the same number, first one wins. + const int number = upb_EnumValueDef_Number(v); + if (!upb_inttable_lookup(&e->iton, number, NULL)) { + return upb_inttable_insert(&e->iton, number, val, a); + } + return true; +} + +const google_protobuf_EnumOptions* upb_EnumDef_Options(const upb_EnumDef* e) { + return e->opts; +} + +bool upb_EnumDef_HasOptions(const upb_EnumDef* e) { + return e->opts != (void*)kUpbDefOptDefault; +} + +const google_protobuf_FeatureSet* upb_EnumDef_ResolvedFeatures(const upb_EnumDef* e) { + return e->resolved_features; +} + +google_protobuf_SymbolVisibility upb_EnumDef_Visibility(const upb_EnumDef* e) { + return e->visibility; +} + +const char* upb_EnumDef_FullName(const upb_EnumDef* e) { return e->full_name; } + +const char* upb_EnumDef_Name(const upb_EnumDef* e) { + return _upb_DefBuilder_FullToShort(e->full_name); +} + +const upb_FileDef* upb_EnumDef_File(const upb_EnumDef* e) { return e->file; } + +const upb_MessageDef* upb_EnumDef_ContainingType(const upb_EnumDef* e) { + return e->containing_type; +} + +int32_t upb_EnumDef_Default(const upb_EnumDef* e) { + UPB_ASSERT(upb_EnumDef_FindValueByNumber(e, e->defaultval)); + return e->defaultval; +} + +int upb_EnumDef_ReservedRangeCount(const upb_EnumDef* e) { + return e->res_range_count; +} + +const upb_EnumReservedRange* upb_EnumDef_ReservedRange(const upb_EnumDef* e, + int i) { + UPB_ASSERT(0 <= i && i < e->res_range_count); + return _upb_EnumReservedRange_At(e->res_ranges, i); +} + +int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e) { + return e->res_name_count; +} + +upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i) { + UPB_ASSERT(0 <= i && i < e->res_name_count); + return e->res_names[i]; +} + +int upb_EnumDef_ValueCount(const upb_EnumDef* e) { return e->value_count; } + +const upb_EnumValueDef* upb_EnumDef_FindValueByName(const upb_EnumDef* e, + const char* name) { + return upb_EnumDef_FindValueByNameWithSize(e, name, strlen(name)); +} + +const upb_EnumValueDef* upb_EnumDef_FindValueByNameWithSize( + const upb_EnumDef* e, const char* name, size_t size) { + upb_value v; + return upb_strtable_lookup2(&e->ntoi, name, size, &v) + ? upb_value_getconstptr(v) + : NULL; +} + +const upb_EnumValueDef* upb_EnumDef_FindValueByNumber(const upb_EnumDef* e, + int32_t num) { + upb_value v; + return upb_inttable_lookup(&e->iton, num, &v) ? upb_value_getconstptr(v) + : NULL; +} + +bool upb_EnumDef_CheckNumber(const upb_EnumDef* e, int32_t num) { + // We could use upb_EnumDef_FindValueByNumber(e, num) != NULL, but we expect + // this to be faster (especially for small numbers). + return upb_MiniTableEnum_CheckValue(e->layout, num); +} + +const upb_EnumValueDef* upb_EnumDef_Value(const upb_EnumDef* e, int i) { + UPB_ASSERT(0 <= i && i < e->value_count); + return _upb_EnumValueDef_At(e->values, i); +} + +static bool upb_EnumDef_IsSpecifiedAsClosed(const upb_EnumDef* e) { + return google_protobuf_FeatureSet_enum_type(e->resolved_features) == + google_protobuf_FeatureSet_CLOSED; +} + +bool upb_EnumDef_IsClosed(const upb_EnumDef* e) { + if (_upb_FileDef_ClosedEnumCheckingDisabled(e->file)) return false; + return upb_EnumDef_IsSpecifiedAsClosed(e); +} + +bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a, + upb_StringView* out) { + upb_DescState s; + _upb_DescState_Init(&s); + + const upb_EnumValueDef** sorted = NULL; + if (!e->is_sorted) { + sorted = _upb_EnumValueDefs_Sorted(e->values, e->value_count, a); + if (!sorted) return false; + } + + if (!_upb_DescState_Grow(&s, a)) return false; + s.ptr = upb_MtDataEncoder_StartEnum(&s.e, s.ptr); + + // Duplicate values are allowed but we only encode each value once. + uint32_t previous = 0; + + for (int i = 0; i < e->value_count; i++) { + const uint32_t current = + upb_EnumValueDef_Number(sorted ? sorted[i] : upb_EnumDef_Value(e, i)); + if (i != 0 && previous == current) continue; + + if (!_upb_DescState_Grow(&s, a)) return false; + s.ptr = upb_MtDataEncoder_PutEnumValue(&s.e, s.ptr, current); + previous = current; + } + + if (!_upb_DescState_Grow(&s, a)) return false; + s.ptr = upb_MtDataEncoder_EndEnum(&s.e, s.ptr); + + // There will always be room for this '\0' in the encoder buffer because + // kUpb_MtDataEncoder_MinSize is overkill for upb_MtDataEncoder_EndEnum(). + UPB_ASSERT(s.ptr < s.buf + s.bufsize); + *s.ptr = '\0'; + + out->data = s.buf; + out->size = s.ptr - s.buf; + return true; +} + +static upb_MiniTableEnum* create_enumlayout(upb_DefBuilder* ctx, + const upb_EnumDef* e) { + upb_StringView sv; + bool ok = upb_EnumDef_MiniDescriptorEncode(e, ctx->tmp_arena, &sv); + if (!ok) _upb_DefBuilder_Errf(ctx, "OOM while building enum MiniDescriptor"); + + upb_Status status; + upb_MiniTableEnum* layout = + upb_MiniTableEnum_Build(sv.data, sv.size, ctx->arena, &status); + if (!layout) + _upb_DefBuilder_Errf(ctx, "Error building enum MiniTable: %s", status.msg); + return layout; +} + +static upb_StringView* _upb_EnumReservedNames_New( + upb_DefBuilder* ctx, int n, const upb_StringView* protos) { + upb_StringView* sv = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_StringView, n); + for (int i = 0; i < n; i++) { + sv[i].data = + upb_strdup2(protos[i].data, protos[i].size, _upb_DefBuilder_Arena(ctx)); + sv[i].size = protos[i].size; + } + return sv; +} + +static void create_enumdef(upb_DefBuilder* ctx, const char* prefix, + const google_protobuf_EnumDescriptorProto* enum_proto, + const google_protobuf_FeatureSet* parent_features, + upb_EnumDef* e) { + const google_protobuf_EnumValueDescriptorProto* const* values; + const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* res_ranges; + const upb_StringView* res_names; + upb_StringView name; + size_t n_value, n_res_range, n_res_name; + + UPB_DEF_SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto); + e->resolved_features = _upb_DefBuilder_ResolveFeatures( + ctx, parent_features, google_protobuf_EnumOptions_features(e->opts)); + + // Must happen before _upb_DefBuilder_Add() + e->file = _upb_DefBuilder_File(ctx); + + name = google_protobuf_EnumDescriptorProto_name(enum_proto); + + e->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name); + _upb_DefBuilder_Add(ctx, e->full_name, + _upb_DefType_Pack(e, UPB_DEFTYPE_ENUM)); + + values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n_value); + + if (n_value == 0) { + _upb_DefBuilder_Errf(ctx, "enums must contain at least one value (%s)", + e->full_name); + } + + e->defaultval = google_protobuf_EnumValueDescriptorProto_number(values[0]); + + // When the special UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN is enabled, we have to + // exempt closed enums from this check, even when we are treating them as + // open. + // + // We rely on the fact that the proto compiler will have already ensured that + // implicit presence fields do not use closed enums, even if we are treating + // them as open. + if (!upb_EnumDef_IsSpecifiedAsClosed(e) && e->defaultval != 0) { + _upb_DefBuilder_Errf(ctx, + "for open enums, the first value must be zero (%s)", + upb_EnumDef_FullName(e)); + } + + bool ok = upb_strtable_init(&e->ntoi, n_value, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + ok = upb_inttable_init(&e->iton, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + e->value_count = n_value; + e->values = _upb_EnumValueDefs_New(ctx, prefix, n_value, values, + e->resolved_features, e, &e->is_sorted); + + res_ranges = + google_protobuf_EnumDescriptorProto_reserved_range(enum_proto, &n_res_range); + e->res_range_count = n_res_range; + e->res_ranges = _upb_EnumReservedRanges_New(ctx, n_res_range, res_ranges, e); + + res_names = google_protobuf_EnumDescriptorProto_reserved_name(enum_proto, &n_res_name); + e->res_name_count = n_res_name; + e->res_names = _upb_EnumReservedNames_New(ctx, n_res_name, res_names); + + e->visibility = google_protobuf_EnumDescriptorProto_visibility(enum_proto); + + if (!upb_inttable_compact(&e->iton, ctx->arena)) _upb_DefBuilder_OomErr(ctx); + + if (upb_EnumDef_IsClosed(e)) { + if (ctx->layout) { + e->layout = upb_MiniTableFile_Enum(ctx->layout, ctx->enum_count++); + } else { + e->layout = create_enumlayout(ctx, e); + } + } else { + e->layout = NULL; + } +} + +upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n, + const google_protobuf_EnumDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, + const upb_MessageDef* containing_type) { + _upb_DefType_CheckPadding(sizeof(upb_EnumDef)); + + // If a containing type is defined then get the full name from that. + // Otherwise use the package name from the file def. + const char* name = containing_type ? upb_MessageDef_FullName(containing_type) + : _upb_FileDef_RawPackage(ctx->file); + + upb_EnumDef* e = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_EnumDef, n); + for (int i = 0; i < n; i++) { + create_enumdef(ctx, name, protos[i], parent_features, &e[i]); + e[i].containing_type = containing_type; + } + return e; +} + + +#include + + +// Must be last. + +struct upb_EnumReservedRange { + int32_t start; + int32_t end; +}; + +upb_EnumReservedRange* _upb_EnumReservedRange_At(const upb_EnumReservedRange* r, + int i) { + return (upb_EnumReservedRange*)&r[i]; +} + +int32_t upb_EnumReservedRange_Start(const upb_EnumReservedRange* r) { + return r->start; +} +int32_t upb_EnumReservedRange_End(const upb_EnumReservedRange* r) { + return r->end; +} + +upb_EnumReservedRange* _upb_EnumReservedRanges_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* protos, + const upb_EnumDef* e) { + upb_EnumReservedRange* r = + UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_EnumReservedRange, n); + + for (int i = 0; i < n; i++) { + const int32_t start = + google_protobuf_EnumDescriptorProto_EnumReservedRange_start(protos[i]); + const int32_t end = + google_protobuf_EnumDescriptorProto_EnumReservedRange_end(protos[i]); + + // A full validation would also check that each range is disjoint, and that + // none of the fields overlap with the extension ranges, but we are just + // sanity checking here. + + // Note: Not a typo! Unlike extension ranges and message reserved ranges, + // the end value of an enum reserved range is *inclusive*! + if (end < start) { + _upb_DefBuilder_Errf(ctx, "Reserved range (%d, %d) is invalid, enum=%s\n", + (int)start, (int)end, upb_EnumDef_FullName(e)); + } + + r[i].start = start; + r[i].end = end; + } + + return r; +} + + +#include +#include +#include + + +// Must be last. + +struct upb_EnumValueDef { + UPB_ALIGN_AS(8) const google_protobuf_EnumValueOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + const upb_EnumDef* parent; + const char* full_name; + int32_t number; +}; + +upb_EnumValueDef* _upb_EnumValueDef_At(const upb_EnumValueDef* v, int i) { + return (upb_EnumValueDef*)&v[i]; +} + +static int _upb_EnumValueDef_Compare(const void* p1, const void* p2) { + const uint32_t v1 = (*(const upb_EnumValueDef**)p1)->number; + const uint32_t v2 = (*(const upb_EnumValueDef**)p2)->number; + return (v1 < v2) ? -1 : (v1 > v2); +} + +const upb_EnumValueDef** _upb_EnumValueDefs_Sorted(const upb_EnumValueDef* v, + size_t n, upb_Arena* a) { + if (SIZE_MAX / sizeof(void*) < n) return NULL; + // TODO: Try to replace this arena alloc with a persistent scratch buffer. + upb_EnumValueDef** out = + (upb_EnumValueDef**)upb_Arena_Malloc(a, n * sizeof(void*)); + if (!out) return NULL; + + for (int i = 0; i < n; i++) { + out[i] = (upb_EnumValueDef*)&v[i]; + } + qsort(out, n, sizeof(void*), _upb_EnumValueDef_Compare); + + return (const upb_EnumValueDef**)out; +} + +const google_protobuf_EnumValueOptions* upb_EnumValueDef_Options( + const upb_EnumValueDef* v) { + return v->opts; +} + +bool upb_EnumValueDef_HasOptions(const upb_EnumValueDef* v) { + return v->opts != (void*)kUpbDefOptDefault; +} + +const google_protobuf_FeatureSet* upb_EnumValueDef_ResolvedFeatures( + const upb_EnumValueDef* e) { + return e->resolved_features; +} + +const upb_EnumDef* upb_EnumValueDef_Enum(const upb_EnumValueDef* v) { + return v->parent; +} + +const char* upb_EnumValueDef_FullName(const upb_EnumValueDef* v) { + return v->full_name; +} + +const char* upb_EnumValueDef_Name(const upb_EnumValueDef* v) { + return _upb_DefBuilder_FullToShort(v->full_name); +} + +int32_t upb_EnumValueDef_Number(const upb_EnumValueDef* v) { return v->number; } + +uint32_t upb_EnumValueDef_Index(const upb_EnumValueDef* v) { + // Compute index in our parent's array. + return v - upb_EnumDef_Value(v->parent, 0); +} + +static void create_enumvaldef(upb_DefBuilder* ctx, const char* prefix, + const google_protobuf_EnumValueDescriptorProto* val_proto, + const google_protobuf_FeatureSet* parent_features, + upb_EnumDef* e, upb_EnumValueDef* v) { + UPB_DEF_SET_OPTIONS(v->opts, EnumValueDescriptorProto, EnumValueOptions, + val_proto); + v->resolved_features = _upb_DefBuilder_ResolveFeatures( + ctx, parent_features, google_protobuf_EnumValueOptions_features(v->opts)); + + upb_StringView name = google_protobuf_EnumValueDescriptorProto_name(val_proto); + + v->parent = e; // Must happen prior to _upb_DefBuilder_Add() + v->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name); + v->number = google_protobuf_EnumValueDescriptorProto_number(val_proto); + _upb_DefBuilder_Add(ctx, v->full_name, + _upb_DefType_Pack(v, UPB_DEFTYPE_ENUMVAL)); + + bool ok = _upb_EnumDef_Insert(e, v, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); +} + +// Allocate and initialize an array of |n| enum value defs owned by |e|. +upb_EnumValueDef* _upb_EnumValueDefs_New( + upb_DefBuilder* ctx, const char* prefix, int n, + const google_protobuf_EnumValueDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, upb_EnumDef* e, bool* is_sorted) { + _upb_DefType_CheckPadding(sizeof(upb_EnumValueDef)); + + upb_EnumValueDef* v = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_EnumValueDef, n); + + *is_sorted = true; + uint32_t previous = 0; + for (int i = 0; i < n; i++) { + create_enumvaldef(ctx, prefix, protos[i], parent_features, e, &v[i]); + + const uint32_t current = v[i].number; + if (previous > current) *is_sorted = false; + previous = current; + } + + return v; +} + + +#include + + +// Must be last. + +struct upb_ExtensionRange { + const google_protobuf_ExtensionRangeOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + int32_t start; + int32_t end; +}; + +upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i) { + return (upb_ExtensionRange*)&r[i]; +} + +const google_protobuf_ExtensionRangeOptions* upb_ExtensionRange_Options( + const upb_ExtensionRange* r) { + return r->opts; +} + +bool upb_ExtensionRange_HasOptions(const upb_ExtensionRange* r) { + return r->opts != (void*)kUpbDefOptDefault; +} + +int32_t upb_ExtensionRange_Start(const upb_ExtensionRange* r) { + return r->start; +} + +int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r) { return r->end; } + +upb_ExtensionRange* _upb_ExtensionRanges_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_DescriptorProto_ExtensionRange* const* protos, + const google_protobuf_FeatureSet* parent_features, const upb_MessageDef* m) { + upb_ExtensionRange* r = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_ExtensionRange, n); + + for (int i = 0; i < n; i++) { + UPB_DEF_SET_OPTIONS(r[i].opts, DescriptorProto_ExtensionRange, + ExtensionRangeOptions, protos[i]); + r[i].resolved_features = _upb_DefBuilder_ResolveFeatures( + ctx, parent_features, google_protobuf_ExtensionRangeOptions_features(r[i].opts)); + + const int32_t start = + google_protobuf_DescriptorProto_ExtensionRange_start(protos[i]); + const int32_t end = google_protobuf_DescriptorProto_ExtensionRange_end(protos[i]); + const int32_t max = + google_protobuf_MessageOptions_message_set_wire_format(upb_MessageDef_Options(m)) + ? INT32_MAX + : kUpb_MaxFieldNumber + 1; + + // A full validation would also check that each range is disjoint, and that + // none of the fields overlap with the extension ranges, but we are just + // sanity checking here. + if (start < 1 || end <= start || end > max) { + _upb_DefBuilder_Errf(ctx, + "Extension range (%d, %d) is invalid, message=%s\n", + (int)start, (int)end, upb_MessageDef_FullName(m)); + } + + r[i].start = start; + r[i].end = end; + } + + return r; +} + + +#include +#include +#include +#include +#include +#include + + +// Must be last. + +#define UPB_FIELD_TYPE_UNSPECIFIED 0 + +typedef struct { + size_t len; + char str[1]; // Null-terminated string data follows. +} str_t; + +struct upb_FieldDef { + UPB_ALIGN_AS(8) const google_protobuf_FieldOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + const upb_FileDef* file; + const upb_MessageDef* msgdef; + const char* full_name; + const char* json_name; + union { + int64_t sint; + uint64_t uint; + double dbl; + float flt; + bool boolean; + str_t* str; + void* msg; // Always NULL. + } defaultval; + union { + const upb_OneofDef* oneof; + const upb_MessageDef* extension_scope; + } scope; + union { + const upb_MessageDef* msgdef; + const upb_EnumDef* enumdef; + const google_protobuf_FieldDescriptorProto* unresolved; + } sub; + uint32_t number_; + uint16_t index_; + uint16_t layout_index; // Index into msgdef->layout->fields or file->exts + bool has_default; + bool has_json_name; + bool has_presence; + bool is_extension; + bool is_proto3_optional; + upb_FieldType type_; + upb_Label label_; +}; + +upb_FieldDef* _upb_FieldDef_At(const upb_FieldDef* f, int i) { + return (upb_FieldDef*)&f[i]; +} + +const google_protobuf_FieldOptions* upb_FieldDef_Options(const upb_FieldDef* f) { + return f->opts; +} + +bool upb_FieldDef_HasOptions(const upb_FieldDef* f) { + return f->opts != (void*)kUpbDefOptDefault; +} + +const google_protobuf_FeatureSet* upb_FieldDef_ResolvedFeatures(const upb_FieldDef* f) { + return f->resolved_features; +} + +const char* upb_FieldDef_FullName(const upb_FieldDef* f) { + return f->full_name; +} + +upb_CType upb_FieldDef_CType(const upb_FieldDef* f) { + return upb_FieldType_CType(f->type_); +} + +upb_FieldType upb_FieldDef_Type(const upb_FieldDef* f) { return f->type_; } + +uint32_t upb_FieldDef_Index(const upb_FieldDef* f) { return f->index_; } + +uint32_t upb_FieldDef_LayoutIndex(const upb_FieldDef* f) { + return f->layout_index; +} + +upb_Label upb_FieldDef_Label(const upb_FieldDef* f) { return f->label_; } + +uint32_t upb_FieldDef_Number(const upb_FieldDef* f) { return f->number_; } + +bool upb_FieldDef_IsExtension(const upb_FieldDef* f) { return f->is_extension; } + +bool _upb_FieldDef_IsPackable(const upb_FieldDef* f) { + return upb_FieldDef_IsRepeated(f) && upb_FieldDef_IsPrimitive(f); +} + +bool upb_FieldDef_IsPacked(const upb_FieldDef* f) { + return _upb_FieldDef_IsPackable(f) && + google_protobuf_FeatureSet_repeated_field_encoding(f->resolved_features) == + google_protobuf_FeatureSet_PACKED; +} + +const char* upb_FieldDef_Name(const upb_FieldDef* f) { + return _upb_DefBuilder_FullToShort(f->full_name); +} + +const char* upb_FieldDef_JsonName(const upb_FieldDef* f) { + return f->json_name; +} + +bool upb_FieldDef_HasJsonName(const upb_FieldDef* f) { + return f->has_json_name; +} + +const upb_FileDef* upb_FieldDef_File(const upb_FieldDef* f) { return f->file; } + +const upb_MessageDef* upb_FieldDef_ContainingType(const upb_FieldDef* f) { + return f->msgdef; +} + +const upb_MessageDef* upb_FieldDef_ExtensionScope(const upb_FieldDef* f) { + return f->is_extension ? f->scope.extension_scope : NULL; +} + +const upb_OneofDef* upb_FieldDef_ContainingOneof(const upb_FieldDef* f) { + return f->is_extension ? NULL : f->scope.oneof; +} + +const upb_OneofDef* upb_FieldDef_RealContainingOneof(const upb_FieldDef* f) { + const upb_OneofDef* oneof = upb_FieldDef_ContainingOneof(f); + if (!oneof || upb_OneofDef_IsSynthetic(oneof)) return NULL; + return oneof; +} + +upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f) { + upb_MessageValue ret; + + if (upb_FieldDef_IsRepeated(f) || upb_FieldDef_IsSubMessage(f)) { + return (upb_MessageValue){.msg_val = NULL}; + } + + switch (upb_FieldDef_CType(f)) { + case kUpb_CType_Bool: + return (upb_MessageValue){.bool_val = f->defaultval.boolean}; + case kUpb_CType_Int64: + return (upb_MessageValue){.int64_val = f->defaultval.sint}; + case kUpb_CType_UInt64: + return (upb_MessageValue){.uint64_val = f->defaultval.uint}; + case kUpb_CType_Enum: + case kUpb_CType_Int32: + return (upb_MessageValue){.int32_val = (int32_t)f->defaultval.sint}; + case kUpb_CType_UInt32: + return (upb_MessageValue){.uint32_val = (uint32_t)f->defaultval.uint}; + case kUpb_CType_Float: + return (upb_MessageValue){.float_val = f->defaultval.flt}; + case kUpb_CType_Double: + return (upb_MessageValue){.double_val = f->defaultval.dbl}; + case kUpb_CType_String: + case kUpb_CType_Bytes: { + str_t* str = f->defaultval.str; + if (str) { + return (upb_MessageValue){ + .str_val = (upb_StringView){.data = str->str, .size = str->len}}; + } else { + return (upb_MessageValue){ + .str_val = (upb_StringView){.data = NULL, .size = 0}}; + } + } + default: + UPB_UNREACHABLE(); + } + + return ret; +} + +const upb_MessageDef* upb_FieldDef_MessageSubDef(const upb_FieldDef* f) { + return upb_FieldDef_IsSubMessage(f) ? f->sub.msgdef : NULL; +} + +const upb_EnumDef* upb_FieldDef_EnumSubDef(const upb_FieldDef* f) { + return upb_FieldDef_IsEnum(f) ? f->sub.enumdef : NULL; +} + +const upb_MiniTableField* upb_FieldDef_MiniTable(const upb_FieldDef* f) { + if (upb_FieldDef_IsExtension(f)) { + const upb_FileDef* file = upb_FieldDef_File(f); + return (upb_MiniTableField*)_upb_FileDef_ExtensionMiniTable( + file, f->layout_index); + } else { + const upb_MiniTable* layout = upb_MessageDef_MiniTable(f->msgdef); + return &layout->UPB_PRIVATE(fields)[f->layout_index]; + } +} + +const upb_MiniTableExtension* upb_FieldDef_MiniTableExtension( + const upb_FieldDef* f) { + UPB_ASSERT(upb_FieldDef_IsExtension(f)); + const upb_FileDef* file = upb_FieldDef_File(f); + return _upb_FileDef_ExtensionMiniTable(file, f->layout_index); +} + +bool _upb_FieldDef_IsClosedEnum(const upb_FieldDef* f) { + if (f->type_ != kUpb_FieldType_Enum) return false; + return upb_EnumDef_IsClosed(f->sub.enumdef); +} + +bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f) { + return f->is_proto3_optional; +} + +int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f) { return f->layout_index; } + +bool _upb_FieldDef_ValidateUtf8(const upb_FieldDef* f) { + if (upb_FieldDef_Type(f) != kUpb_FieldType_String) return false; + return google_protobuf_FeatureSet_utf8_validation(f->resolved_features) == + google_protobuf_FeatureSet_VERIFY; +} + +bool _upb_FieldDef_IsGroupLike(const upb_FieldDef* f) { + // Groups are always tag-delimited. + if (f->type_ != kUpb_FieldType_Group) { + return false; + } + + const upb_MessageDef* msg = upb_FieldDef_MessageSubDef(f); + + // Group fields always are always the lowercase type name. + const char* mname = upb_MessageDef_Name(msg); + const char* fname = upb_FieldDef_Name(f); + size_t name_size = strlen(fname); + if (name_size != strlen(mname)) return false; + for (size_t i = 0; i < name_size; ++i) { + if ((mname[i] | 0x20) != fname[i]) { + // Case-insensitive ascii comparison. + return false; + } + } + + if (upb_MessageDef_File(msg) != upb_FieldDef_File(f)) { + return false; + } + + // Group messages are always defined in the same scope as the field. File + // level extensions will compare NULL == NULL here, which is why the file + // comparison above is necessary to ensure both come from the same file. + return upb_FieldDef_IsExtension(f) ? upb_FieldDef_ExtensionScope(f) == + upb_MessageDef_ContainingType(msg) + : upb_FieldDef_ContainingType(f) == + upb_MessageDef_ContainingType(msg); +} + +uint64_t _upb_FieldDef_Modifiers(const upb_FieldDef* f) { + uint64_t out = upb_FieldDef_IsPacked(f) ? kUpb_FieldModifier_IsPacked : 0; + + if (upb_FieldDef_IsRepeated(f)) { + out |= kUpb_FieldModifier_IsRepeated; + } else if (upb_FieldDef_IsRequired(f)) { + out |= kUpb_FieldModifier_IsRequired; + } else if (!upb_FieldDef_HasPresence(f)) { + out |= kUpb_FieldModifier_IsProto3Singular; + } + + if (_upb_FieldDef_IsClosedEnum(f)) { + out |= kUpb_FieldModifier_IsClosedEnum; + } + + if (_upb_FieldDef_ValidateUtf8(f)) { + out |= kUpb_FieldModifier_ValidateUtf8; + } + + return out; +} + +bool upb_FieldDef_HasDefault(const upb_FieldDef* f) { return f->has_default; } +bool upb_FieldDef_HasPresence(const upb_FieldDef* f) { return f->has_presence; } + +bool upb_FieldDef_HasSubDef(const upb_FieldDef* f) { + return upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsEnum(f); +} + +bool upb_FieldDef_IsEnum(const upb_FieldDef* f) { + return upb_FieldDef_CType(f) == kUpb_CType_Enum; +} + +bool upb_FieldDef_IsMap(const upb_FieldDef* f) { + return upb_FieldDef_IsRepeated(f) && upb_FieldDef_IsSubMessage(f) && + upb_MessageDef_IsMapEntry(upb_FieldDef_MessageSubDef(f)); +} + +bool upb_FieldDef_IsOptional(const upb_FieldDef* f) { + return upb_FieldDef_Label(f) == kUpb_Label_Optional; +} + +bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f) { + return !upb_FieldDef_IsString(f) && !upb_FieldDef_IsSubMessage(f); +} + +bool upb_FieldDef_IsRepeated(const upb_FieldDef* f) { + return upb_FieldDef_Label(f) == kUpb_Label_Repeated; +} + +bool upb_FieldDef_IsRequired(const upb_FieldDef* f) { + return google_protobuf_FeatureSet_field_presence(f->resolved_features) == + google_protobuf_FeatureSet_LEGACY_REQUIRED; +} + +bool upb_FieldDef_IsString(const upb_FieldDef* f) { + return upb_FieldDef_CType(f) == kUpb_CType_String || + upb_FieldDef_CType(f) == kUpb_CType_Bytes; +} + +bool upb_FieldDef_IsSubMessage(const upb_FieldDef* f) { + return upb_FieldDef_CType(f) == kUpb_CType_Message; +} + +static bool between(int32_t x, int32_t low, int32_t high) { + return x >= low && x <= high; +} + +bool upb_FieldDef_checklabel(int32_t label) { return between(label, 1, 3); } +bool upb_FieldDef_checktype(int32_t type) { return between(type, 1, 11); } +bool upb_FieldDef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); } + +bool upb_FieldDef_checkdescriptortype(int32_t type) { + return between(type, 1, 18); +} + +static bool streql2(const char* a, size_t n, const char* b) { + return n == strlen(b) && memcmp(a, b, n) == 0; +} + +// Implement the transformation as described in the spec: +// 1. upper case all letters after an underscore. +// 2. remove all underscores. +static char* make_json_name(const char* name, size_t size, upb_Arena* a) { + char* out = upb_Arena_Malloc(a, size + 1); // +1 is to add a trailing '\0' + if (out == NULL) return NULL; + + bool ucase_next = false; + char* des = out; + for (size_t i = 0; i < size; i++) { + if (name[i] == '_') { + ucase_next = true; + } else { + *des++ = ucase_next ? toupper(name[i]) : name[i]; + ucase_next = false; + } + } + *des++ = '\0'; + return out; +} + +static str_t* newstr(upb_DefBuilder* ctx, const char* data, size_t len) { + str_t* ret = _upb_DefBuilder_Alloc(ctx, sizeof(*ret) + len); + if (!ret) _upb_DefBuilder_OomErr(ctx); + ret->len = len; + if (len) memcpy(ret->str, data, len); + ret->str[len] = '\0'; + return ret; +} + +static str_t* unescape(upb_DefBuilder* ctx, const upb_FieldDef* f, + const char* data, size_t len) { + // Size here is an upper bound; escape sequences could ultimately shrink it. + str_t* ret = _upb_DefBuilder_Alloc(ctx, sizeof(*ret) + len); + char* dst = &ret->str[0]; + const char* src = data; + const char* end = data + len; + + while (src < end) { + if (*src == '\\') { + src++; + *dst++ = _upb_DefBuilder_ParseEscape(ctx, f, &src, end); + } else { + *dst++ = *src++; + } + } + + ret->len = dst - &ret->str[0]; + return ret; +} + +static void parse_default(upb_DefBuilder* ctx, const char* str, size_t len, + upb_FieldDef* f) { + char* end; + char nullz[64]; + errno = 0; + + switch (upb_FieldDef_CType(f)) { + case kUpb_CType_Int32: + case kUpb_CType_Int64: + case kUpb_CType_UInt32: + case kUpb_CType_UInt64: + case kUpb_CType_Double: + case kUpb_CType_Float: + // Standard C number parsing functions expect null-terminated strings. + if (len >= sizeof(nullz) - 1) { + _upb_DefBuilder_Errf(ctx, "Default too long: %.*s", (int)len, str); + } + memcpy(nullz, str, len); + nullz[len] = '\0'; + str = nullz; + break; + default: + break; + } + + switch (upb_FieldDef_CType(f)) { + case kUpb_CType_Int32: { + long val = strtol(str, &end, 0); + if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end) { + goto invalid; + } + f->defaultval.sint = val; + break; + } + case kUpb_CType_Enum: { + const upb_EnumDef* e = f->sub.enumdef; + const upb_EnumValueDef* ev = + upb_EnumDef_FindValueByNameWithSize(e, str, len); + if (!ev) { + goto invalid; + } + f->defaultval.sint = upb_EnumValueDef_Number(ev); + break; + } + case kUpb_CType_Int64: { + long long val = strtoll(str, &end, 0); + if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) { + goto invalid; + } + f->defaultval.sint = val; + break; + } + case kUpb_CType_UInt32: { + unsigned long val = strtoul(str, &end, 0); + if (val > UINT32_MAX || errno == ERANGE || *end) { + goto invalid; + } + f->defaultval.uint = val; + break; + } + case kUpb_CType_UInt64: { + unsigned long long val = strtoull(str, &end, 0); + if (val > UINT64_MAX || errno == ERANGE || *end) { + goto invalid; + } + f->defaultval.uint = val; + break; + } + case kUpb_CType_Double: { + double val = strtod(str, &end); + if (errno == ERANGE || *end) { + goto invalid; + } + f->defaultval.dbl = val; + break; + } + case kUpb_CType_Float: { + float val = strtof(str, &end); + if (errno == ERANGE || *end) { + goto invalid; + } + f->defaultval.flt = val; + break; + } + case kUpb_CType_Bool: { + if (streql2(str, len, "false")) { + f->defaultval.boolean = false; + } else if (streql2(str, len, "true")) { + f->defaultval.boolean = true; + } else { + goto invalid; + } + break; + } + case kUpb_CType_String: + f->defaultval.str = newstr(ctx, str, len); + break; + case kUpb_CType_Bytes: + f->defaultval.str = unescape(ctx, f, str, len); + break; + case kUpb_CType_Message: + /* Should not have a default value. */ + _upb_DefBuilder_Errf(ctx, "Message should not have a default (%s)", + upb_FieldDef_FullName(f)); + } + + return; + +invalid: + _upb_DefBuilder_Errf(ctx, "Invalid default '%.*s' for field %s of type %d", + (int)len, str, upb_FieldDef_FullName(f), + (int)upb_FieldDef_Type(f)); +} + +static void set_default_default(upb_DefBuilder* ctx, upb_FieldDef* f, + bool must_be_empty) { + switch (upb_FieldDef_CType(f)) { + case kUpb_CType_Int32: + case kUpb_CType_Int64: + f->defaultval.sint = 0; + break; + case kUpb_CType_UInt64: + case kUpb_CType_UInt32: + f->defaultval.uint = 0; + break; + case kUpb_CType_Double: + case kUpb_CType_Float: + f->defaultval.dbl = 0; + break; + case kUpb_CType_String: + case kUpb_CType_Bytes: + f->defaultval.str = newstr(ctx, NULL, 0); + break; + case kUpb_CType_Bool: + f->defaultval.boolean = false; + break; + case kUpb_CType_Enum: { + f->defaultval.sint = upb_EnumDef_Default(f->sub.enumdef); + if (must_be_empty && f->defaultval.sint != 0) { + _upb_DefBuilder_Errf(ctx, + "Implicit presence field (%s) cannot use an enum " + "type with a non-zero default (%s)", + f->full_name, + upb_EnumDef_FullName(f->sub.enumdef)); + } + break; + } + case kUpb_CType_Message: + break; + } +} + +static bool _upb_FieldDef_InferLegacyFeatures( + upb_DefBuilder* ctx, upb_FieldDef* f, + const google_protobuf_FieldDescriptorProto* proto, + const google_protobuf_FieldOptions* options, google_protobuf_Edition edition, + google_protobuf_FeatureSet* features) { + bool ret = false; + + if (google_protobuf_FieldDescriptorProto_label(proto) == kUpb_Label_Required) { + if (edition == google_protobuf_EDITION_PROTO3) { + _upb_DefBuilder_Errf(ctx, "proto3 fields cannot be required (%s)", + f->full_name); + } + int val = google_protobuf_FeatureSet_LEGACY_REQUIRED; + google_protobuf_FeatureSet_set_field_presence(features, val); + ret = true; + } + + if (google_protobuf_FieldDescriptorProto_type(proto) == kUpb_FieldType_Group) { + int val = google_protobuf_FeatureSet_DELIMITED; + google_protobuf_FeatureSet_set_message_encoding(features, val); + ret = true; + } + + if (google_protobuf_FieldOptions_has_packed(options)) { + int val = google_protobuf_FieldOptions_packed(options) ? google_protobuf_FeatureSet_PACKED + : google_protobuf_FeatureSet_EXPANDED; + google_protobuf_FeatureSet_set_repeated_field_encoding(features, val); + ret = true; + } + + return ret; +} + +static void _upb_FieldDef_Create(upb_DefBuilder* ctx, const char* prefix, + const google_protobuf_FeatureSet* parent_features, + const google_protobuf_FieldDescriptorProto* field_proto, + upb_MessageDef* m, upb_FieldDef* f) { + // Must happen before _upb_DefBuilder_Add() + f->file = _upb_DefBuilder_File(ctx); + + const upb_StringView name = google_protobuf_FieldDescriptorProto_name(field_proto); + f->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name); + f->number_ = google_protobuf_FieldDescriptorProto_number(field_proto); + f->is_proto3_optional = + google_protobuf_FieldDescriptorProto_proto3_optional(field_proto); + f->msgdef = m; + f->scope.oneof = NULL; + + UPB_DEF_SET_OPTIONS(f->opts, FieldDescriptorProto, FieldOptions, field_proto); + + google_protobuf_Edition edition = upb_FileDef_Edition(f->file); + const google_protobuf_FeatureSet* unresolved_features = + google_protobuf_FieldOptions_features(f->opts); + bool implicit = false; + + if (_upb_DefBuilder_IsLegacyEdition(edition)) { + upb_Message_Clear(UPB_UPCAST(ctx->legacy_features), + UPB_DESC_MINITABLE(FeatureSet)); + if (_upb_FieldDef_InferLegacyFeatures(ctx, f, field_proto, f->opts, edition, + ctx->legacy_features)) { + implicit = true; + unresolved_features = ctx->legacy_features; + } + } + + if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) { + int oneof_index = google_protobuf_FieldDescriptorProto_oneof_index(field_proto); + + if (!m) { + _upb_DefBuilder_Errf(ctx, "oneof field (%s) has no containing msg", + f->full_name); + } + + if (oneof_index < 0 || oneof_index >= upb_MessageDef_OneofCount(m)) { + _upb_DefBuilder_Errf(ctx, "oneof_index out of range (%s)", f->full_name); + } + + upb_OneofDef* oneof = (upb_OneofDef*)upb_MessageDef_Oneof(m, oneof_index); + f->scope.oneof = oneof; + parent_features = upb_OneofDef_ResolvedFeatures(oneof); + + _upb_OneofDef_Insert(ctx, oneof, f, name.data, name.size); + } + + f->resolved_features = _upb_DefBuilder_DoResolveFeatures( + ctx, parent_features, unresolved_features, implicit); + + f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto); + if (f->label_ == kUpb_Label_Optional && + // TODO: remove once we can deprecate kUpb_Label_Required. + google_protobuf_FeatureSet_field_presence(f->resolved_features) == + google_protobuf_FeatureSet_LEGACY_REQUIRED) { + f->label_ = kUpb_Label_Required; + } + + if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) { + _upb_DefBuilder_Errf(ctx, "field has no name"); + } + + f->has_json_name = google_protobuf_FieldDescriptorProto_has_json_name(field_proto); + if (f->has_json_name) { + const upb_StringView sv = + google_protobuf_FieldDescriptorProto_json_name(field_proto); + f->json_name = upb_strdup2(sv.data, sv.size, ctx->arena); + } else { + f->json_name = make_json_name(name.data, name.size, ctx->arena); + } + if (!f->json_name) _upb_DefBuilder_OomErr(ctx); + + const bool has_type = google_protobuf_FieldDescriptorProto_has_type(field_proto); + const bool has_type_name = + google_protobuf_FieldDescriptorProto_has_type_name(field_proto); + + f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto); + + if (has_type) { + switch (f->type_) { + case kUpb_FieldType_Message: + case kUpb_FieldType_Group: + case kUpb_FieldType_Enum: + if (!has_type_name) { + _upb_DefBuilder_Errf(ctx, "field of type %d requires type name (%s)", + (int)f->type_, f->full_name); + } + break; + default: + if (has_type_name) { + _upb_DefBuilder_Errf( + ctx, "invalid type for field with type_name set (%s, %d)", + f->full_name, (int)f->type_); + } + } + } + + if ((!has_type && has_type_name) || f->type_ == kUpb_FieldType_Message) { + f->type_ = + UPB_FIELD_TYPE_UNSPECIFIED; // We'll assign this in resolve_subdef() + } else { + if (f->type_ < kUpb_FieldType_Double || f->type_ > kUpb_FieldType_SInt64) { + _upb_DefBuilder_Errf(ctx, "invalid type for field %s (%d)", f->full_name, + f->type_); + } + } + + if (f->label_ < kUpb_Label_Optional || f->label_ > kUpb_Label_Repeated) { + _upb_DefBuilder_Errf(ctx, "invalid label for field %s (%d)", f->full_name, + f->label_); + } + + /* We can't resolve the subdef or (in the case of extensions) the containing + * message yet, because it may not have been defined yet. We stash a pointer + * to the field_proto until later when we can properly resolve it. */ + f->sub.unresolved = field_proto; + + if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) { + if (upb_FieldDef_Label(f) != kUpb_Label_Optional) { + _upb_DefBuilder_Errf(ctx, "fields in oneof must have OPTIONAL label (%s)", + f->full_name); + } + } + + f->has_presence = + (!upb_FieldDef_IsRepeated(f)) && + (f->is_extension || _upb_FileDef_ImplicitFieldPresenceDisabled(f->file) || + (f->type_ == kUpb_FieldType_Message || + f->type_ == kUpb_FieldType_Group || upb_FieldDef_ContainingOneof(f) || + google_protobuf_FeatureSet_field_presence(f->resolved_features) != + google_protobuf_FeatureSet_IMPLICIT)); +} + +static void _upb_FieldDef_CreateExt( + upb_DefBuilder* ctx, const char* prefix, + const google_protobuf_FeatureSet* parent_features, + const google_protobuf_FieldDescriptorProto* field_proto, upb_MessageDef* m, + upb_FieldDef* f) { + f->is_extension = true; + _upb_FieldDef_Create(ctx, prefix, parent_features, field_proto, m, f); + + if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) { + _upb_DefBuilder_Errf(ctx, "oneof_index provided for extension field (%s)", + f->full_name); + } + + f->scope.extension_scope = m; + _upb_DefBuilder_Add(ctx, f->full_name, _upb_DefType_Pack(f, UPB_DEFTYPE_EXT)); + f->layout_index = ctx->ext_count++; + + if (ctx->layout) { + UPB_ASSERT(upb_MiniTableExtension_Number( + upb_FieldDef_MiniTableExtension(f)) == f->number_); + } +} + +static void _upb_FieldDef_CreateNotExt( + upb_DefBuilder* ctx, const char* prefix, + const google_protobuf_FeatureSet* parent_features, + const google_protobuf_FieldDescriptorProto* field_proto, upb_MessageDef* m, + upb_FieldDef* f) { + f->is_extension = false; + _upb_FieldDef_Create(ctx, prefix, parent_features, field_proto, m, f); + + if (!google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) { + if (f->is_proto3_optional) { + _upb_DefBuilder_Errf( + ctx, + "non-extension field (%s) with proto3_optional was not in a oneof", + f->full_name); + } + } + + _upb_MessageDef_InsertField(ctx, m, f); +} + +upb_FieldDef* _upb_Extensions_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_FieldDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, const char* prefix, + upb_MessageDef* m) { + _upb_DefType_CheckPadding(sizeof(upb_FieldDef)); + upb_FieldDef* defs = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_FieldDef, n); + + for (int i = 0; i < n; i++) { + upb_FieldDef* f = &defs[i]; + + _upb_FieldDef_CreateExt(ctx, prefix, parent_features, protos[i], m, f); + f->index_ = i; + } + + return defs; +} + +upb_FieldDef* _upb_FieldDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_FieldDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, const char* prefix, + upb_MessageDef* m, bool* is_sorted) { + _upb_DefType_CheckPadding(sizeof(upb_FieldDef)); + upb_FieldDef* defs = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_FieldDef, n); + + uint32_t previous = 0; + for (int i = 0; i < n; i++) { + upb_FieldDef* f = &defs[i]; + + _upb_FieldDef_CreateNotExt(ctx, prefix, parent_features, protos[i], m, f); + f->index_ = i; + if (!ctx->layout) { + // Speculate that the def fields are sorted. We will always sort the + // MiniTable fields, so if defs are sorted then indices will match. + // + // If this is incorrect, we will overwrite later. + f->layout_index = i; + } + + const uint32_t current = f->number_; + if (previous > current) *is_sorted = false; + previous = current; + } + + return defs; +} + +static void resolve_subdef(upb_DefBuilder* ctx, const char* prefix, + upb_FieldDef* f) { + const google_protobuf_FieldDescriptorProto* field_proto = f->sub.unresolved; + upb_StringView name = google_protobuf_FieldDescriptorProto_type_name(field_proto); + bool has_name = google_protobuf_FieldDescriptorProto_has_type_name(field_proto); + switch ((int)f->type_) { + case UPB_FIELD_TYPE_UNSPECIFIED: { + // Type was not specified and must be inferred. + UPB_ASSERT(has_name); + upb_deftype_t type; + const void* def = + _upb_DefBuilder_ResolveAny(ctx, f->full_name, prefix, name, &type); + switch (type) { + case UPB_DEFTYPE_ENUM: + f->sub.enumdef = def; + f->type_ = kUpb_FieldType_Enum; + break; + case UPB_DEFTYPE_MSG: + f->sub.msgdef = def; + f->type_ = kUpb_FieldType_Message; + // TODO: remove once we can deprecate + // kUpb_FieldType_Group. + if (google_protobuf_FeatureSet_message_encoding(f->resolved_features) == + google_protobuf_FeatureSet_DELIMITED && + !upb_MessageDef_IsMapEntry(def) && + !(f->msgdef && upb_MessageDef_IsMapEntry(f->msgdef))) { + f->type_ = kUpb_FieldType_Group; + } + f->has_presence = !upb_FieldDef_IsRepeated(f); + break; + default: + _upb_DefBuilder_Errf(ctx, "Couldn't resolve type name for field %s", + f->full_name); + } + break; + } + case kUpb_FieldType_Message: + case kUpb_FieldType_Group: + UPB_ASSERT(has_name); + f->sub.msgdef = _upb_DefBuilder_Resolve(ctx, f->full_name, prefix, name, + UPB_DEFTYPE_MSG); + break; + case kUpb_FieldType_Enum: + UPB_ASSERT(has_name); + f->sub.enumdef = _upb_DefBuilder_Resolve(ctx, f->full_name, prefix, name, + UPB_DEFTYPE_ENUM); + break; + default: + // No resolution necessary. + break; + } +} + +static int _upb_FieldDef_Compare(const void* p1, const void* p2) { + const uint32_t v1 = (*(upb_FieldDef**)p1)->number_; + const uint32_t v2 = (*(upb_FieldDef**)p2)->number_; + return (v1 < v2) ? -1 : (v1 > v2); +} + +// _upb_FieldDefs_Sorted() is mostly a pure function of its inputs, but has one +// critical side effect that we depend on: it sets layout_index appropriately +// for non-sorted lists of fields. +const upb_FieldDef** _upb_FieldDefs_Sorted(const upb_FieldDef* f, int n, + upb_Arena* a) { + // TODO: Replace this arena alloc with a persistent scratch buffer. + upb_FieldDef** out = (upb_FieldDef**)upb_Arena_Malloc(a, n * sizeof(void*)); + if (!out) return NULL; + + for (int i = 0; i < n; i++) { + out[i] = (upb_FieldDef*)&f[i]; + } + qsort(out, n, sizeof(void*), _upb_FieldDef_Compare); + + for (int i = 0; i < n; i++) { + out[i]->layout_index = i; + } + return (const upb_FieldDef**)out; +} + +bool upb_FieldDef_MiniDescriptorEncode(const upb_FieldDef* f, upb_Arena* a, + upb_StringView* out) { + UPB_ASSERT(f->is_extension); + + upb_DescState s; + _upb_DescState_Init(&s); + + const int number = upb_FieldDef_Number(f); + const uint64_t modifiers = _upb_FieldDef_Modifiers(f); + + if (!_upb_DescState_Grow(&s, a)) return false; + s.ptr = upb_MtDataEncoder_EncodeExtension(&s.e, s.ptr, f->type_, number, + modifiers); + *s.ptr = '\0'; + + out->data = s.buf; + out->size = s.ptr - s.buf; + return true; +} + +static void resolve_extension(upb_DefBuilder* ctx, const char* prefix, + upb_FieldDef* f, + const google_protobuf_FieldDescriptorProto* field_proto) { + if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) { + _upb_DefBuilder_Errf(ctx, "extension for field '%s' had no extendee", + f->full_name); + } + + upb_StringView name = google_protobuf_FieldDescriptorProto_extendee(field_proto); + const upb_MessageDef* m = + _upb_DefBuilder_Resolve(ctx, f->full_name, prefix, name, UPB_DEFTYPE_MSG); + f->msgdef = m; + + if (!_upb_MessageDef_IsValidExtensionNumber(m, f->number_)) { + _upb_DefBuilder_Errf( + ctx, + "field number %u in extension %s has no extension range in message %s", + (unsigned)f->number_, f->full_name, upb_MessageDef_FullName(m)); + } +} + +void _upb_FieldDef_BuildMiniTableExtension(upb_DefBuilder* ctx, + const upb_FieldDef* f) { + const upb_MiniTableExtension* ext = upb_FieldDef_MiniTableExtension(f); + + if (ctx->layout) { + UPB_ASSERT(upb_FieldDef_Number(f) == upb_MiniTableExtension_Number(ext)); + } else { + upb_StringView desc; + if (!upb_FieldDef_MiniDescriptorEncode(f, ctx->tmp_arena, &desc)) { + _upb_DefBuilder_OomErr(ctx); + } + + upb_MiniTableExtension* mut_ext = (upb_MiniTableExtension*)ext; + upb_MiniTableSub sub = {NULL}; + if (upb_FieldDef_IsSubMessage(f)) { + const upb_MiniTable* submsg = upb_MessageDef_MiniTable(f->sub.msgdef); + sub = upb_MiniTableSub_FromMessage(submsg); + } else if (_upb_FieldDef_IsClosedEnum(f)) { + const upb_MiniTableEnum* subenum = _upb_EnumDef_MiniTable(f->sub.enumdef); + sub = upb_MiniTableSub_FromEnum(subenum); + } + bool ok2 = _upb_MiniTableExtension_Init(desc.data, desc.size, mut_ext, + upb_MessageDef_MiniTable(f->msgdef), + sub, ctx->platform, ctx->status); + if (!ok2) _upb_DefBuilder_Errf(ctx, "Could not build extension mini table"); + } + + bool ok = _upb_DefPool_InsertExt(ctx->symtab, ext, f); + if (!ok) _upb_DefBuilder_OomErr(ctx); +} + +static void resolve_default(upb_DefBuilder* ctx, upb_FieldDef* f, + const google_protobuf_FieldDescriptorProto* field_proto) { + // Implicit presence fields should always have an effective default of 0. + // This should naturally fall out of the validations that protoc performs: + // - Implicit presence fields cannot specify a default value. + // - Implicit presence fields for for enums can only use open enums, which + // are required to have zero as their default. + // - Even if we are treating all enums as open, the proto compiler will + // still reject using a nominally closed enum with an implicit presence + // field. + bool must_be_empty = !f->has_presence && !upb_FieldDef_IsRepeated(f); + + // Have to delay resolving of the default value until now because of the enum + // case, since enum defaults are specified with a label. + if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) { + upb_StringView defaultval = + google_protobuf_FieldDescriptorProto_default_value(field_proto); + + if (must_be_empty) { + _upb_DefBuilder_Errf(ctx, + "fields with implicit presence cannot have " + "explicit defaults (%s)", + f->full_name); + } + + if (upb_FileDef_Edition(f->file) == google_protobuf_EDITION_PROTO3) { + _upb_DefBuilder_Errf(ctx, + "proto3 fields cannot have explicit defaults (%s)", + f->full_name); + } + + if (upb_FieldDef_IsSubMessage(f)) { + _upb_DefBuilder_Errf(ctx, + "message fields cannot have explicit defaults (%s)", + f->full_name); + } + + parse_default(ctx, defaultval.data, defaultval.size, f); + f->has_default = true; + } else { + set_default_default(ctx, f, must_be_empty); + f->has_default = false; + } +} + +void _upb_FieldDef_Resolve(upb_DefBuilder* ctx, const char* prefix, + upb_FieldDef* f) { + // We have to stash this away since resolve_subdef() may overwrite it. + const google_protobuf_FieldDescriptorProto* field_proto = f->sub.unresolved; + + resolve_subdef(ctx, prefix, f); + resolve_default(ctx, f, field_proto); + + if (f->is_extension) { + resolve_extension(ctx, prefix, f, field_proto); + } +} + + +#include +#include +#include + + +// Must be last. + +struct upb_FileDef { + const google_protobuf_FileOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + const char* name; + const char* package; + google_protobuf_Edition edition; + + const upb_FileDef** deps; + const int32_t* public_deps; + const int32_t* weak_deps; + const upb_MessageDef* top_lvl_msgs; + const upb_EnumDef* top_lvl_enums; + const upb_FieldDef* top_lvl_exts; + const upb_ServiceDef* services; + const upb_MiniTableExtension** ext_layouts; + const upb_DefPool* symtab; + + int dep_count; + int public_dep_count; + int weak_dep_count; + int top_lvl_msg_count; + int top_lvl_enum_count; + int top_lvl_ext_count; + int service_count; + int ext_count; // All exts in the file. +}; + +UPB_API const char* upb_FileDef_EditionName(int edition) { + // TODO Synchronize this with descriptor.proto better. + switch (edition) { + case google_protobuf_EDITION_PROTO2: + return "PROTO2"; + case google_protobuf_EDITION_PROTO3: + return "PROTO3"; + case google_protobuf_EDITION_2023: + return "2023"; + default: + return "UNKNOWN"; + } +} + +const google_protobuf_FileOptions* upb_FileDef_Options(const upb_FileDef* f) { + return f->opts; +} + +const google_protobuf_FeatureSet* upb_FileDef_ResolvedFeatures(const upb_FileDef* f) { + return f->resolved_features; +} + +bool upb_FileDef_HasOptions(const upb_FileDef* f) { + return f->opts != (void*)kUpbDefOptDefault; +} + +const char* upb_FileDef_Name(const upb_FileDef* f) { return f->name; } + +const char* upb_FileDef_Package(const upb_FileDef* f) { + return f->package ? f->package : ""; +} + +google_protobuf_Edition upb_FileDef_Edition(const upb_FileDef* f) { return f->edition; } + +const char* _upb_FileDef_RawPackage(const upb_FileDef* f) { return f->package; } + +int upb_FileDef_TopLevelMessageCount(const upb_FileDef* f) { + return f->top_lvl_msg_count; +} + +int upb_FileDef_DependencyCount(const upb_FileDef* f) { return f->dep_count; } + +int upb_FileDef_PublicDependencyCount(const upb_FileDef* f) { + return f->public_dep_count; +} + +int upb_FileDef_WeakDependencyCount(const upb_FileDef* f) { + return f->weak_dep_count; +} + +const int32_t* _upb_FileDef_PublicDependencyIndexes(const upb_FileDef* f) { + return f->public_deps; +} + +const int32_t* _upb_FileDef_WeakDependencyIndexes(const upb_FileDef* f) { + return f->weak_deps; +} + +bool _upb_FileDef_ClosedEnumCheckingDisabled(const upb_FileDef* f) { + return upb_DefPool_ClosedEnumCheckingDisabled(f->symtab); +} + +bool _upb_FileDef_ImplicitFieldPresenceDisabled(const upb_FileDef* f) { + return upb_DefPool_ImplicitFieldPresenceDisabled(f->symtab); +} + +int upb_FileDef_TopLevelEnumCount(const upb_FileDef* f) { + return f->top_lvl_enum_count; +} + +int upb_FileDef_TopLevelExtensionCount(const upb_FileDef* f) { + return f->top_lvl_ext_count; +} + +int upb_FileDef_ServiceCount(const upb_FileDef* f) { return f->service_count; } + +const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i) { + UPB_ASSERT(0 <= i && i < f->dep_count); + return f->deps[i]; +} + +const upb_FileDef* upb_FileDef_PublicDependency(const upb_FileDef* f, int i) { + UPB_ASSERT(0 <= i && i < f->public_dep_count); + return f->deps[f->public_deps[i]]; +} + +const upb_FileDef* upb_FileDef_WeakDependency(const upb_FileDef* f, int i) { + UPB_ASSERT(0 <= i && i < f->public_dep_count); + return f->deps[f->weak_deps[i]]; +} + +const upb_MessageDef* upb_FileDef_TopLevelMessage(const upb_FileDef* f, int i) { + UPB_ASSERT(0 <= i && i < f->top_lvl_msg_count); + return _upb_MessageDef_At(f->top_lvl_msgs, i); +} + +const upb_EnumDef* upb_FileDef_TopLevelEnum(const upb_FileDef* f, int i) { + UPB_ASSERT(0 <= i && i < f->top_lvl_enum_count); + return _upb_EnumDef_At(f->top_lvl_enums, i); +} + +const upb_FieldDef* upb_FileDef_TopLevelExtension(const upb_FileDef* f, int i) { + UPB_ASSERT(0 <= i && i < f->top_lvl_ext_count); + return _upb_FieldDef_At(f->top_lvl_exts, i); +} + +const upb_ServiceDef* upb_FileDef_Service(const upb_FileDef* f, int i) { + UPB_ASSERT(0 <= i && i < f->service_count); + return _upb_ServiceDef_At(f->services, i); +} + +const upb_DefPool* upb_FileDef_Pool(const upb_FileDef* f) { return f->symtab; } + +const upb_MiniTableExtension* _upb_FileDef_ExtensionMiniTable( + const upb_FileDef* f, int i) { + return f->ext_layouts[i]; +} + +// Note: Import cycles are not allowed so this will terminate. +bool upb_FileDef_Resolves(const upb_FileDef* f, const char* path) { + if (!strcmp(f->name, path)) return true; + + for (int i = 0; i < upb_FileDef_PublicDependencyCount(f); i++) { + const upb_FileDef* dep = upb_FileDef_PublicDependency(f, i); + if (upb_FileDef_Resolves(dep, path)) return true; + } + return false; +} + +static char* _strviewdup(upb_DefBuilder* ctx, upb_StringView view) { + char* ret = upb_strdup2(view.data, view.size, _upb_DefBuilder_Arena(ctx)); + if (!ret) _upb_DefBuilder_OomErr(ctx); + return ret; +} + +static bool streql_view(upb_StringView view, const char* b) { + return view.size == strlen(b) && memcmp(view.data, b, view.size) == 0; +} + +static int count_exts_in_msg(const google_protobuf_DescriptorProto* msg_proto) { + size_t n; + google_protobuf_DescriptorProto_extension(msg_proto, &n); + int ext_count = n; + + const google_protobuf_DescriptorProto* const* nested_msgs = + google_protobuf_DescriptorProto_nested_type(msg_proto, &n); + for (size_t i = 0; i < n; i++) { + ext_count += count_exts_in_msg(nested_msgs[i]); + } + + return ext_count; +} + +const google_protobuf_FeatureSet* _upb_FileDef_FindEdition(upb_DefBuilder* ctx, + int edition) { + const google_protobuf_FeatureSetDefaults* defaults = + upb_DefPool_FeatureSetDefaults(ctx->symtab); + + int min = google_protobuf_FeatureSetDefaults_minimum_edition(defaults); + int max = google_protobuf_FeatureSetDefaults_maximum_edition(defaults); + if (edition < min) { + _upb_DefBuilder_Errf(ctx, + "Edition %s is earlier than the minimum edition %s " + "given in the defaults", + upb_FileDef_EditionName(edition), + upb_FileDef_EditionName(min)); + return NULL; + } + if (edition > max && edition != google_protobuf_EDITION_UNSTABLE) { + _upb_DefBuilder_Errf(ctx, + "Edition %s is later than the maximum edition %s " + "given in the defaults", + upb_FileDef_EditionName(edition), + upb_FileDef_EditionName(max)); + return NULL; + } + + size_t n; + const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* const* d = + google_protobuf_FeatureSetDefaults_defaults(defaults, &n); + const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* result = NULL; + for (size_t i = 0; i < n; i++) { + if (google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_edition(d[i]) > + edition) { + break; + } + result = d[i]; + } + if (result == NULL) { + _upb_DefBuilder_Errf(ctx, "No valid default found for edition %s", + upb_FileDef_EditionName(edition)); + return NULL; + } + + // Merge the fixed and overridable features to get the edition's default + // feature set. + const google_protobuf_FeatureSet* fixed = + google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_fixed_features(result); + const google_protobuf_FeatureSet* overridable = + google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_overridable_features( + result); + if (!fixed && !overridable) { + _upb_DefBuilder_Errf(ctx, "No valid default found for edition %s", + upb_FileDef_EditionName(edition)); + return NULL; + } else if (!fixed) { + return overridable; + } + return _upb_DefBuilder_DoResolveFeatures(ctx, fixed, overridable, + /*is_implicit=*/true); +} + +// Allocate and initialize one file def, and add it to the context object. +void _upb_FileDef_Create(upb_DefBuilder* ctx, + const google_protobuf_FileDescriptorProto* file_proto) { + upb_FileDef* file = _upb_DefBuilder_Alloc(ctx, sizeof(upb_FileDef)); + ctx->file = file; + + const google_protobuf_DescriptorProto* const* msgs; + const google_protobuf_EnumDescriptorProto* const* enums; + const google_protobuf_FieldDescriptorProto* const* exts; + const google_protobuf_ServiceDescriptorProto* const* services; + const upb_StringView* strs; + const int32_t* public_deps; + const int32_t* weak_deps; + size_t n; + + file->symtab = ctx->symtab; + + // Count all extensions in the file, to build a flat array of layouts. + google_protobuf_FileDescriptorProto_extension(file_proto, &n); + int ext_count = n; + msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n); + for (size_t i = 0; i < n; i++) { + ext_count += count_exts_in_msg(msgs[i]); + } + file->ext_count = ext_count; + + if (ctx->layout) { + // We are using the ext layouts that were passed in. + file->ext_layouts = ctx->layout->UPB_PRIVATE(exts); + const int mt_ext_count = upb_MiniTableFile_ExtensionCount(ctx->layout); + if (mt_ext_count != file->ext_count) { + _upb_DefBuilder_Errf(ctx, + "Extension count did not match layout (%d vs %d)", + mt_ext_count, file->ext_count); + } + } else { + // We are building ext layouts from scratch. + file->ext_layouts = _upb_DefBuilder_Alloc( + ctx, sizeof(*file->ext_layouts) * file->ext_count); + upb_MiniTableExtension* ext = + UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MiniTableExtension, file->ext_count); + for (int i = 0; i < file->ext_count; i++) { + file->ext_layouts[i] = &ext[i]; + } + } + + upb_StringView name = google_protobuf_FileDescriptorProto_name(file_proto); + file->name = _strviewdup(ctx, name); + if (strlen(file->name) != name.size) { + _upb_DefBuilder_Errf(ctx, "File name contained embedded NULL"); + } + + upb_StringView package = google_protobuf_FileDescriptorProto_package(file_proto); + + if (package.size) { + _upb_DefBuilder_CheckIdentFull(ctx, package); + file->package = _strviewdup(ctx, package); + } else { + file->package = NULL; + } + + upb_StringView syntax = google_protobuf_FileDescriptorProto_syntax(file_proto); + + if (google_protobuf_FileDescriptorProto_has_edition(file_proto)) { + if (!streql_view(syntax, "editions")) { + _upb_DefBuilder_Errf(ctx, + "Setting edition requires that syntax=\"editions\", " + "but syntax is \"" UPB_STRINGVIEW_FORMAT "\"", + UPB_STRINGVIEW_ARGS(syntax)); + } + file->edition = google_protobuf_FileDescriptorProto_edition(file_proto); + } else if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) { + if (streql_view(syntax, "proto2")) { + file->edition = google_protobuf_EDITION_PROTO2; + } else if (streql_view(syntax, "proto3")) { + file->edition = google_protobuf_EDITION_PROTO3; + } else if (streql_view(syntax, "editions")) { + _upb_DefBuilder_Errf( + ctx, "File has syntax=\"editions\", but no edition is specified"); + } else { + _upb_DefBuilder_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'", + UPB_STRINGVIEW_ARGS(syntax)); + } + } else { + // The legacy default when no edition or syntax is specified is proto2. + file->edition = google_protobuf_EDITION_PROTO2; + } + + // Read options. + UPB_DEF_SET_OPTIONS(file->opts, FileDescriptorProto, FileOptions, file_proto); + + // Resolve features. + const google_protobuf_FeatureSet* edition_defaults = + _upb_FileDef_FindEdition(ctx, file->edition); + const google_protobuf_FeatureSet* unresolved = google_protobuf_FileOptions_features(file->opts); + file->resolved_features = + _upb_DefBuilder_ResolveFeatures(ctx, edition_defaults, unresolved); + + // Verify dependencies. + strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n); + file->dep_count = n; + file->deps = UPB_DEFBUILDER_ALLOCARRAY(ctx, const upb_FileDef*, n); + + for (size_t i = 0; i < n; i++) { + upb_StringView str = strs[i]; + file->deps[i] = + upb_DefPool_FindFileByNameWithSize(ctx->symtab, str.data, str.size); + if (!file->deps[i]) { + _upb_DefBuilder_Errf(ctx, + "Depends on file '" UPB_STRINGVIEW_FORMAT + "', but it has not been loaded", + UPB_STRINGVIEW_ARGS(str)); + } + } + + public_deps = google_protobuf_FileDescriptorProto_public_dependency(file_proto, &n); + file->public_dep_count = n; + file->public_deps = UPB_DEFBUILDER_ALLOCARRAY(ctx, int32_t, n); + int32_t* mutable_public_deps = (int32_t*)file->public_deps; + for (size_t i = 0; i < n; i++) { + if (public_deps[i] >= file->dep_count) { + _upb_DefBuilder_Errf(ctx, "public_dep %d is out of range", + (int)public_deps[i]); + } + mutable_public_deps[i] = public_deps[i]; + } + + weak_deps = google_protobuf_FileDescriptorProto_weak_dependency(file_proto, &n); + file->weak_dep_count = n; + file->weak_deps = UPB_DEFBUILDER_ALLOCARRAY(ctx, const int32_t, n); + int32_t* mutable_weak_deps = (int32_t*)file->weak_deps; + for (size_t i = 0; i < n; i++) { + if (weak_deps[i] >= file->dep_count) { + _upb_DefBuilder_Errf(ctx, "weak_dep %d is out of range", + (int)weak_deps[i]); + } + mutable_weak_deps[i] = weak_deps[i]; + } + + // Create enums. + enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n); + file->top_lvl_enum_count = n; + file->top_lvl_enums = + _upb_EnumDefs_New(ctx, n, enums, file->resolved_features, NULL); + + // Create extensions. + exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n); + file->top_lvl_ext_count = n; + file->top_lvl_exts = _upb_Extensions_New( + ctx, n, exts, file->resolved_features, file->package, NULL); + + // Create messages. + msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n); + file->top_lvl_msg_count = n; + file->top_lvl_msgs = + _upb_MessageDefs_New(ctx, n, msgs, file->resolved_features, NULL); + + // Create services. + services = google_protobuf_FileDescriptorProto_service(file_proto, &n); + file->service_count = n; + file->services = + _upb_ServiceDefs_New(ctx, n, services, file->resolved_features); + + // Now that all names are in the table, build layouts and resolve refs. + + for (int i = 0; i < file->top_lvl_msg_count; i++) { + upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i); + _upb_MessageDef_Resolve(ctx, m); + } + + for (int i = 0; i < file->top_lvl_ext_count; i++) { + upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i); + _upb_FieldDef_Resolve(ctx, file->package, f); + } + + for (int i = 0; i < file->top_lvl_msg_count; i++) { + upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i); + _upb_MessageDef_CreateMiniTable(ctx, (upb_MessageDef*)m); + } + + for (int i = 0; i < file->top_lvl_ext_count; i++) { + upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i); + _upb_FieldDef_BuildMiniTableExtension(ctx, f); + } + + for (int i = 0; i < file->top_lvl_msg_count; i++) { + upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i); + _upb_MessageDef_LinkMiniTable(ctx, m); + } + + if (file->ext_count) { + upb_ExtensionRegistryStatus status = upb_ExtensionRegistry_AddArray( + _upb_DefPool_ExtReg(ctx->symtab), file->ext_layouts, file->ext_count); + if (status != kUpb_ExtensionRegistryStatus_Ok) { + if (status == kUpb_ExtensionRegistryStatus_OutOfMemory) { + _upb_DefBuilder_OomErr(ctx); + } + + UPB_ASSERT(status == kUpb_ExtensionRegistryStatus_DuplicateEntry); + _upb_DefBuilder_Errf(ctx, "duplicate extension entry"); + } + } +} + + +#include +#include +#include +#include + + +// Must be last. + +/* The upb core does not generally have a concept of default instances. However + * for descriptor options we make an exception since the max size is known and + * modest (<200 bytes). All types can share a default instance since it is + * initialized to zeroes. + * + * We have to allocate an extra pointer for upb's internal metadata. */ +static UPB_ALIGN_AS(8) const + char opt_default_buf[_UPB_MAXOPT_SIZE + sizeof(void*)] = {0}; +const char* kUpbDefOptDefault = &opt_default_buf[sizeof(void*)]; + +const char* _upb_DefBuilder_FullToShort(const char* fullname) { + const char* p; + + if (fullname == NULL) { + return NULL; + } else if ((p = strrchr(fullname, '.')) == NULL) { + /* No '.' in the name, return the full string. */ + return fullname; + } else { + /* Return one past the last '.'. */ + return p + 1; + } +} + +void _upb_DefBuilder_FailJmp(upb_DefBuilder* ctx) { UPB_LONGJMP(ctx->err, 1); } + +void _upb_DefBuilder_Errf(upb_DefBuilder* ctx, const char* fmt, ...) { + va_list argp; + va_start(argp, fmt); + upb_Status_VSetErrorFormat(ctx->status, fmt, argp); + va_end(argp); + _upb_DefBuilder_FailJmp(ctx); +} + +void _upb_DefBuilder_OomErr(upb_DefBuilder* ctx) { + upb_Status_SetErrorMessage(ctx->status, "out of memory"); + _upb_DefBuilder_FailJmp(ctx); +} + +// Verify a relative identifier string. The loop is branchless for speed. +static void _upb_DefBuilder_CheckIdentNotFull(upb_DefBuilder* ctx, + upb_StringView name) { + bool good = name.size > 0; + + for (size_t i = 0; i < name.size; i++) { + const char c = name.data[i]; + const char d = c | 0x20; // force lowercase + const bool is_alpha = (('a' <= d) & (d <= 'z')) | (c == '_'); + const bool is_numer = ('0' <= c) & (c <= '9') & (i != 0); + + good &= is_alpha | is_numer; + } + + if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, false); +} + +const char* _upb_DefBuilder_MakeFullName(upb_DefBuilder* ctx, + const char* prefix, + upb_StringView name) { + _upb_DefBuilder_CheckIdentNotFull(ctx, name); + if (prefix) { + // ret = prefix + '.' + name; + size_t n = strlen(prefix); + char* ret = _upb_DefBuilder_Alloc(ctx, n + name.size + 2); + strcpy(ret, prefix); + ret[n] = '.'; + memcpy(&ret[n + 1], name.data, name.size); + ret[n + 1 + name.size] = '\0'; + return ret; + } else { + char* ret = upb_strdup2(name.data, name.size, ctx->arena); + if (!ret) _upb_DefBuilder_OomErr(ctx); + return ret; + } +} + +static bool remove_component(char* base, size_t* len) { + if (*len == 0) return false; + + for (size_t i = *len - 1; i > 0; i--) { + if (base[i] == '.') { + *len = i; + return true; + } + } + + *len = 0; + return true; +} + +const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx, + const char* from_name_dbg, + const char* base, upb_StringView sym, + upb_deftype_t* type) { + if (sym.size == 0) goto notfound; + upb_value v; + if (sym.data[0] == '.') { + // Symbols starting with '.' are absolute, so we do a single lookup. + // Slice to omit the leading '.' + if (!_upb_DefPool_LookupSym(ctx->symtab, sym.data + 1, sym.size - 1, &v)) { + goto notfound; + } + } else { + // Remove components from base until we find an entry or run out. + size_t baselen = base ? strlen(base) : 0; + char* tmp = upb_gmalloc(sym.size + baselen + 1); + while (1) { + char* p = tmp; + if (baselen) { + memcpy(p, base, baselen); + p[baselen] = '.'; + p += baselen + 1; + } + memcpy(p, sym.data, sym.size); + p += sym.size; + if (_upb_DefPool_LookupSym(ctx->symtab, tmp, p - tmp, &v)) { + break; + } + if (!remove_component(tmp, &baselen)) { + upb_gfree(tmp); + goto notfound; + } + } + upb_gfree(tmp); + } + + *type = _upb_DefType_Type(v); + return _upb_DefType_Unpack(v, *type); + +notfound: + _upb_DefBuilder_Errf(ctx, "couldn't resolve name '" UPB_STRINGVIEW_FORMAT "'", + UPB_STRINGVIEW_ARGS(sym)); +} + +const void* _upb_DefBuilder_Resolve(upb_DefBuilder* ctx, + const char* from_name_dbg, const char* base, + upb_StringView sym, upb_deftype_t type) { + upb_deftype_t found_type; + const void* ret = + _upb_DefBuilder_ResolveAny(ctx, from_name_dbg, base, sym, &found_type); + if (ret && found_type != type) { + _upb_DefBuilder_Errf(ctx, + "type mismatch when resolving %s: couldn't find " + "name " UPB_STRINGVIEW_FORMAT " with type=%d", + from_name_dbg, UPB_STRINGVIEW_ARGS(sym), (int)type); + } + return ret; +} + +// Per ASCII this will lower-case a letter. If the result is a letter, the +// input was definitely a letter. If the output is not a letter, this may +// have transformed the character unpredictably. +static char upb_ascii_lower(char ch) { return ch | 0x20; } + +// isalpha() etc. from are locale-dependent, which we don't want. +static bool upb_isbetween(uint8_t c, uint8_t low, uint8_t high) { + return low <= c && c <= high; +} + +static bool upb_isletter(char c) { + char lower = upb_ascii_lower(c); + return upb_isbetween(lower, 'a', 'z') || c == '_'; +} + +static bool upb_isalphanum(char c) { + return upb_isletter(c) || upb_isbetween(c, '0', '9'); +} + +static bool TryGetChar(const char** src, const char* end, char* ch) { + if (*src == end) return false; + *ch = **src; + *src += 1; + return true; +} + +static int TryGetHexDigit(const char** src, const char* end) { + char ch; + if (!TryGetChar(src, end, &ch)) return -1; + if ('0' <= ch && ch <= '9') { + return ch - '0'; + } + ch = upb_ascii_lower(ch); + if ('a' <= ch && ch <= 'f') { + return ch - 'a' + 0xa; + } + *src -= 1; // Char wasn't actually a hex digit. + return -1; +} + +static char upb_DefBuilder_ParseHexEscape(upb_DefBuilder* ctx, + const upb_FieldDef* f, + const char** src, const char* end) { + int hex_digit = TryGetHexDigit(src, end); + if (hex_digit < 0) { + _upb_DefBuilder_Errf( + ctx, "\\x must be followed by at least one hex digit (field='%s')", + upb_FieldDef_FullName(f)); + return 0; + } + unsigned int ret = hex_digit; + while ((hex_digit = TryGetHexDigit(src, end)) >= 0) { + ret = (ret << 4) | hex_digit; + } + if (ret > 0xff) { + _upb_DefBuilder_Errf(ctx, "Value of hex escape in field %s exceeds 8 bits", + upb_FieldDef_FullName(f)); + return 0; + } + return ret; +} + +static char TryGetOctalDigit(const char** src, const char* end) { + char ch; + if (!TryGetChar(src, end, &ch)) return -1; + if ('0' <= ch && ch <= '7') { + return ch - '0'; + } + *src -= 1; // Char wasn't actually an octal digit. + return -1; +} + +static char upb_DefBuilder_ParseOctalEscape(upb_DefBuilder* ctx, + const upb_FieldDef* f, + const char** src, const char* end) { + char ch = 0; + for (int i = 0; i < 3; i++) { + char digit; + if ((digit = TryGetOctalDigit(src, end)) >= 0) { + ch = (ch << 3) | digit; + } + } + return ch; +} + +char _upb_DefBuilder_ParseEscape(upb_DefBuilder* ctx, const upb_FieldDef* f, + const char** src, const char* end) { + char ch; + if (!TryGetChar(src, end, &ch)) { + _upb_DefBuilder_Errf(ctx, "unterminated escape sequence in field %s", + upb_FieldDef_FullName(f)); + return 0; + } + switch (ch) { + case 'a': + return '\a'; + case 'b': + return '\b'; + case 'f': + return '\f'; + case 'n': + return '\n'; + case 'r': + return '\r'; + case 't': + return '\t'; + case 'v': + return '\v'; + case '\\': + return '\\'; + case '\'': + return '\''; + case '\"': + return '\"'; + case '?': + return '\?'; + case 'x': + case 'X': + return upb_DefBuilder_ParseHexEscape(ctx, f, src, end); + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + *src -= 1; + return upb_DefBuilder_ParseOctalEscape(ctx, f, src, end); + } + _upb_DefBuilder_Errf(ctx, "Unknown escape sequence: \\%c", ch); +} + +void _upb_DefBuilder_CheckIdentSlow(upb_DefBuilder* ctx, upb_StringView name, + bool full) { + const char* str = name.data; + const size_t len = name.size; + bool start = true; + for (size_t i = 0; i < len; i++) { + const char c = str[i]; + if (c == '.') { + if (start || !full) { + _upb_DefBuilder_Errf( + ctx, "invalid name: unexpected '.' (" UPB_STRINGVIEW_FORMAT ")", + UPB_STRINGVIEW_ARGS(name)); + } + start = true; + } else if (start) { + if (!upb_isletter(c)) { + _upb_DefBuilder_Errf(ctx, + "invalid name: path components must start with a " + "letter (" UPB_STRINGVIEW_FORMAT ")", + UPB_STRINGVIEW_ARGS(name)); + } + start = false; + } else if (!upb_isalphanum(c)) { + _upb_DefBuilder_Errf( + ctx, + "invalid name: non-alphanumeric character (" UPB_STRINGVIEW_FORMAT + ")", + UPB_STRINGVIEW_ARGS(name)); + } + } + if (start) { + _upb_DefBuilder_Errf(ctx, + "invalid name: empty part (" UPB_STRINGVIEW_FORMAT ")", + UPB_STRINGVIEW_ARGS(name)); + } + + // We should never reach this point. + UPB_ASSERT(false); +} + +upb_StringView _upb_DefBuilder_MakeKey(upb_DefBuilder* ctx, + const google_protobuf_FeatureSet* parent, + upb_StringView key) { + size_t need = key.size + sizeof(void*); + if (ctx->tmp_buf_size < need) { + ctx->tmp_buf_size = UPB_MAX(64, upb_RoundUpToPowerOfTwo(need)); + ctx->tmp_buf = upb_Arena_Malloc(ctx->tmp_arena, ctx->tmp_buf_size); + if (!ctx->tmp_buf) _upb_DefBuilder_OomErr(ctx); + } + + memcpy(ctx->tmp_buf, &parent, sizeof(void*)); + memcpy(ctx->tmp_buf + sizeof(void*), key.data, key.size); + return upb_StringView_FromDataAndSize(ctx->tmp_buf, need); +} + +bool _upb_DefBuilder_GetOrCreateFeatureSet(upb_DefBuilder* ctx, + const google_protobuf_FeatureSet* parent, + upb_StringView key, + google_protobuf_FeatureSet** set) { + upb_StringView k = _upb_DefBuilder_MakeKey(ctx, parent, key); + upb_value v; + if (upb_strtable_lookup2(&ctx->feature_cache, k.data, k.size, &v)) { + *set = upb_value_getptr(v); + return false; + } + + *set = (google_protobuf_FeatureSet*)upb_Message_DeepClone( + UPB_UPCAST(parent), UPB_DESC_MINITABLE(FeatureSet), ctx->arena); + if (!*set) _upb_DefBuilder_OomErr(ctx); + + v = upb_value_ptr(*set); + if (!upb_strtable_insert(&ctx->feature_cache, k.data, k.size, v, + ctx->tmp_arena)) { + _upb_DefBuilder_OomErr(ctx); + } + + return true; +} + +const google_protobuf_FeatureSet* _upb_DefBuilder_DoResolveFeatures( + upb_DefBuilder* ctx, const google_protobuf_FeatureSet* parent, + const google_protobuf_FeatureSet* child, bool is_implicit) { + assert(parent); + if (!child) return parent; + + if (!is_implicit && + _upb_DefBuilder_IsLegacyEdition(upb_FileDef_Edition(ctx->file))) { + _upb_DefBuilder_Errf(ctx, "Features can only be specified for editions"); + } + + google_protobuf_FeatureSet* resolved; + size_t child_size; + const char* child_bytes = + google_protobuf_FeatureSet_serialize(child, ctx->tmp_arena, &child_size); + if (!child_bytes) _upb_DefBuilder_OomErr(ctx); + + upb_StringView key = upb_StringView_FromDataAndSize(child_bytes, child_size); + if (!_upb_DefBuilder_GetOrCreateFeatureSet(ctx, parent, key, &resolved)) { + return resolved; + } + + upb_DecodeStatus dec_status = + upb_Decode(child_bytes, child_size, UPB_UPCAST(resolved), + UPB_DESC_MINITABLE(FeatureSet), NULL, 0, ctx->arena); + if (dec_status != kUpb_DecodeStatus_Ok) _upb_DefBuilder_OomErr(ctx); + + return resolved; +} + + +#include + + +// Must be last. + +char* upb_strdup2(const char* s, size_t len, upb_Arena* a) { + size_t n; + char* p; + + // Prevent overflow errors. + if (len == SIZE_MAX) return NULL; + + // Always null-terminate, even if binary data; but don't rely on the input to + // have a null-terminating byte since it may be a raw binary buffer. + n = len + 1; + p = upb_Arena_Malloc(a, n); + if (p) { + if (len != 0) memcpy(p, s, len); + p[len] = 0; + } + return p; +} + + +#include +#include + + +// Must be last. + +bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f) { + const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f); + UPB_ASSERT(upb_FieldDef_HasPresence(f)); + + if (upb_MiniTableField_IsExtension(m_f)) { + return upb_Message_HasExtension(msg, (const upb_MiniTableExtension*)m_f); + } else { + return upb_Message_HasBaseField(msg, m_f); + } +} + +const upb_FieldDef* upb_Message_WhichOneofByDef(const upb_Message* msg, + const upb_OneofDef* o) { + const upb_FieldDef* f = upb_OneofDef_Field(o, 0); + if (upb_OneofDef_IsSynthetic(o)) { + UPB_ASSERT(upb_OneofDef_FieldCount(o) == 1); + return upb_Message_HasFieldByDef(msg, f) ? f : NULL; + } else { + const upb_MiniTableField* field = upb_FieldDef_MiniTable(f); + uint32_t oneof_case = upb_Message_WhichOneofFieldNumber(msg, field); + f = oneof_case ? upb_OneofDef_LookupNumber(o, oneof_case) : NULL; + UPB_ASSERT((f != NULL) == (oneof_case != 0)); + return f; + } +} + +upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg, + const upb_FieldDef* f) { + upb_MessageValue default_val = upb_FieldDef_Default(f); + return upb_Message_GetField(msg, upb_FieldDef_MiniTable(f), default_val); +} + +upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg, + const upb_FieldDef* f, + upb_Arena* a) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + UPB_ASSERT(upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsRepeated(f)); + if (upb_FieldDef_HasPresence(f) && !upb_Message_HasFieldByDef(msg, f)) { + // We need to skip the upb_Message_GetFieldByDef() call in this case. + goto make; + } + + upb_MessageValue val = upb_Message_GetFieldByDef(msg, f); + if (val.array_val) { + return (upb_MutableMessageValue){.array = (upb_Array*)val.array_val}; + } + + upb_MutableMessageValue ret; +make: + if (!a) return (upb_MutableMessageValue){.array = NULL}; + if (upb_FieldDef_IsMap(f)) { + const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f); + const upb_FieldDef* key = + upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_KeyFieldNumber); + const upb_FieldDef* value = + upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_ValueFieldNumber); + ret.map = + upb_Map_New(a, upb_FieldDef_CType(key), upb_FieldDef_CType(value)); + } else if (upb_FieldDef_IsRepeated(f)) { + ret.array = upb_Array_New(a, upb_FieldDef_CType(f)); + } else { + UPB_ASSERT(upb_FieldDef_IsSubMessage(f)); + const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f); + ret.msg = upb_Message_New(upb_MessageDef_MiniTable(m), a); + } + + val.array_val = ret.array; + upb_Message_SetFieldByDef(msg, f, val, a); + + return ret; +} + +bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f, + upb_MessageValue val, upb_Arena* a) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f); + + if (upb_MiniTableField_IsExtension(m_f)) { + return upb_Message_SetExtension(msg, (const upb_MiniTableExtension*)m_f, + &val, a); + } else { + upb_Message_SetBaseField(msg, m_f, &val); + return true; + } +} + +void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f); + + if (upb_MiniTableField_IsExtension(m_f)) { + upb_Message_ClearExtension(msg, (const upb_MiniTableExtension*)m_f); + } else { + upb_Message_ClearBaseField(msg, m_f); + } +} + +void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Message_Clear(msg, upb_MessageDef_MiniTable(m)); +} + +bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m, + const upb_DefPool* ext_pool, const upb_FieldDef** out_f, + upb_MessageValue* out_val, size_t* iter) { + const upb_MiniTable* mt = upb_MessageDef_MiniTable(m); + size_t i = *iter; + size_t n = upb_MiniTable_FieldCount(mt); + upb_MessageValue zero = upb_MessageValue_Zero(); + UPB_UNUSED(ext_pool); + + // Iterate over normal fields, returning the first one that is set. + while (++i < n) { + const upb_MiniTableField* field = upb_MiniTable_GetFieldByIndex(mt, i); + upb_MessageValue val = upb_Message_GetField(msg, field, zero); + + // Skip field if unset or empty. + if (upb_MiniTableField_HasPresence(field)) { + if (!upb_Message_HasBaseField(msg, field)) continue; + } else { + switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(field)) { + case kUpb_FieldMode_Map: + if (!val.map_val || upb_Map_Size(val.map_val) == 0) continue; + break; + case kUpb_FieldMode_Array: + if (!val.array_val || upb_Array_Size(val.array_val) == 0) continue; + break; + case kUpb_FieldMode_Scalar: + if (UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(field, &val)) + continue; + break; + } + } + + *out_val = val; + *out_f = + upb_MessageDef_FindFieldByNumber(m, upb_MiniTableField_Number(field)); + *iter = i; + return true; + } + + if (ext_pool) { + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) return false; + + for (; (i - n) < in->size; i++) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i - n]; + if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr); + memcpy(out_val, &ext->data, sizeof(*out_val)); + *out_f = upb_DefPool_FindExtensionByMiniTable(ext_pool, ext->ext); + *iter = i; + return true; + } + } + } + + *iter = i; + return false; +} + +bool _upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m, + const upb_DefPool* ext_pool, int depth) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + size_t iter = kUpb_Message_Begin; + const upb_FieldDef* f; + upb_MessageValue val; + bool ret = true; + + if (--depth == 0) return false; + + _upb_Message_DiscardUnknown_shallow(msg); + + while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) { + const upb_MessageDef* subm = upb_FieldDef_MessageSubDef(f); + if (!subm) continue; + if (upb_FieldDef_IsMap(f)) { + const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(subm, 2); + const upb_MessageDef* val_m = upb_FieldDef_MessageSubDef(val_f); + upb_Map* map = (upb_Map*)val.map_val; + size_t iter = kUpb_Map_Begin; + + if (!val_m) continue; + + upb_MessageValue map_key, map_val; + while (upb_Map_Next(map, &map_key, &map_val, &iter)) { + if (!_upb_Message_DiscardUnknown((upb_Message*)map_val.msg_val, val_m, + ext_pool, depth)) { + ret = false; + } + } + } else if (upb_FieldDef_IsRepeated(f)) { + const upb_Array* arr = val.array_val; + size_t i, n = upb_Array_Size(arr); + for (i = 0; i < n; i++) { + upb_MessageValue elem = upb_Array_Get(arr, i); + if (!_upb_Message_DiscardUnknown((upb_Message*)elem.msg_val, subm, + ext_pool, depth)) { + ret = false; + } + } + } else { + if (!_upb_Message_DiscardUnknown((upb_Message*)val.msg_val, subm, + ext_pool, depth)) { + ret = false; + } + } + } + + return ret; +} + +bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m, + const upb_DefPool* ext_pool, int maxdepth) { + return _upb_Message_DiscardUnknown(msg, m, ext_pool, maxdepth); +} + + +#include +#include +#include + + +// Must be last. + +struct upb_MessageDef { + UPB_ALIGN_AS(8) const google_protobuf_MessageOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + const upb_MiniTable* layout; + const upb_FileDef* file; + const upb_MessageDef* containing_type; + const char* full_name; + + // Tables for looking up fields by number and name. + upb_inttable itof; + upb_strtable ntof; + + // Looking up fields by json name. + upb_strtable jtof; + + /* All nested defs. + * MEM: We could save some space here by putting nested defs in a contiguous + * region and calculating counts from offsets or vice-versa. */ + const upb_FieldDef* fields; + const upb_OneofDef* oneofs; + const upb_ExtensionRange* ext_ranges; + const upb_StringView* res_names; + const upb_MessageDef* nested_msgs; + const upb_MessageReservedRange* res_ranges; + const upb_EnumDef* nested_enums; + const upb_FieldDef* nested_exts; + + // TODO: These counters don't need anywhere near 32 bits. + int field_count; + int real_oneof_count; + int oneof_count; + int ext_range_count; + int res_range_count; + int res_name_count; + int nested_msg_count; + int nested_enum_count; + int nested_ext_count; + bool in_message_set; + bool is_sorted; + upb_WellKnown well_known_type; + google_protobuf_SymbolVisibility visibility; +}; + +static void assign_msg_wellknowntype(upb_MessageDef* m) { + const char* name = m->full_name; + if (name == NULL) { + m->well_known_type = kUpb_WellKnown_Unspecified; + return; + } + if (!strcmp(name, "google.protobuf.Any")) { + m->well_known_type = kUpb_WellKnown_Any; + } else if (!strcmp(name, "google.protobuf.FieldMask")) { + m->well_known_type = kUpb_WellKnown_FieldMask; + } else if (!strcmp(name, "google.protobuf.Duration")) { + m->well_known_type = kUpb_WellKnown_Duration; + } else if (!strcmp(name, "google.protobuf.Timestamp")) { + m->well_known_type = kUpb_WellKnown_Timestamp; + } else if (!strcmp(name, "google.protobuf.DoubleValue")) { + m->well_known_type = kUpb_WellKnown_DoubleValue; + } else if (!strcmp(name, "google.protobuf.FloatValue")) { + m->well_known_type = kUpb_WellKnown_FloatValue; + } else if (!strcmp(name, "google.protobuf.Int64Value")) { + m->well_known_type = kUpb_WellKnown_Int64Value; + } else if (!strcmp(name, "google.protobuf.UInt64Value")) { + m->well_known_type = kUpb_WellKnown_UInt64Value; + } else if (!strcmp(name, "google.protobuf.Int32Value")) { + m->well_known_type = kUpb_WellKnown_Int32Value; + } else if (!strcmp(name, "google.protobuf.UInt32Value")) { + m->well_known_type = kUpb_WellKnown_UInt32Value; + } else if (!strcmp(name, "google.protobuf.BoolValue")) { + m->well_known_type = kUpb_WellKnown_BoolValue; + } else if (!strcmp(name, "google.protobuf.StringValue")) { + m->well_known_type = kUpb_WellKnown_StringValue; + } else if (!strcmp(name, "google.protobuf.BytesValue")) { + m->well_known_type = kUpb_WellKnown_BytesValue; + } else if (!strcmp(name, "google.protobuf.Value")) { + m->well_known_type = kUpb_WellKnown_Value; + } else if (!strcmp(name, "google.protobuf.ListValue")) { + m->well_known_type = kUpb_WellKnown_ListValue; + } else if (!strcmp(name, "google.protobuf.Struct")) { + m->well_known_type = kUpb_WellKnown_Struct; + } else { + m->well_known_type = kUpb_WellKnown_Unspecified; + } +} + +upb_MessageDef* _upb_MessageDef_At(const upb_MessageDef* m, int i) { + return (upb_MessageDef*)&m[i]; +} + +bool _upb_MessageDef_IsValidExtensionNumber(const upb_MessageDef* m, int n) { + for (int i = 0; i < m->ext_range_count; i++) { + const upb_ExtensionRange* r = upb_MessageDef_ExtensionRange(m, i); + if (upb_ExtensionRange_Start(r) <= n && n < upb_ExtensionRange_End(r)) { + return true; + } + } + return false; +} + +const google_protobuf_MessageOptions* upb_MessageDef_Options(const upb_MessageDef* m) { + return m->opts; +} + +bool upb_MessageDef_HasOptions(const upb_MessageDef* m) { + return m->opts != (void*)kUpbDefOptDefault; +} + +const google_protobuf_FeatureSet* upb_MessageDef_ResolvedFeatures( + const upb_MessageDef* m) { + return m->resolved_features; +} + +const char* upb_MessageDef_FullName(const upb_MessageDef* m) { + return m->full_name; +} + +const upb_FileDef* upb_MessageDef_File(const upb_MessageDef* m) { + return m->file; +} + +const upb_MessageDef* upb_MessageDef_ContainingType(const upb_MessageDef* m) { + return m->containing_type; +} + +const char* upb_MessageDef_Name(const upb_MessageDef* m) { + return _upb_DefBuilder_FullToShort(m->full_name); +} + +const upb_FieldDef* upb_MessageDef_FindFieldByNumber(const upb_MessageDef* m, + uint32_t i) { + upb_value val; + return upb_inttable_lookup(&m->itof, i, &val) ? upb_value_getconstptr(val) + : NULL; +} + +const upb_FieldDef* upb_MessageDef_FindFieldByNameWithSize( + const upb_MessageDef* m, const char* name, size_t size) { + upb_value val; + + if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) { + return NULL; + } + + return _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD); +} + +const upb_OneofDef* upb_MessageDef_FindOneofByNameWithSize( + const upb_MessageDef* m, const char* name, size_t size) { + upb_value val; + + if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) { + return NULL; + } + + return _upb_DefType_Unpack(val, UPB_DEFTYPE_ONEOF); +} + +bool _upb_MessageDef_Insert(upb_MessageDef* m, const char* name, size_t len, + upb_value v, upb_Arena* a) { + return upb_strtable_insert(&m->ntof, name, len, v, a); +} + +bool upb_MessageDef_FindByNameWithSize(const upb_MessageDef* m, + const char* name, size_t len, + const upb_FieldDef** out_f, + const upb_OneofDef** out_o) { + upb_value val; + + if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) { + return false; + } + + const upb_FieldDef* f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD); + const upb_OneofDef* o = _upb_DefType_Unpack(val, UPB_DEFTYPE_ONEOF); + if (out_f) *out_f = f; + if (out_o) *out_o = o; + return f || o; /* False if this was a JSON name. */ +} + +const upb_FieldDef* upb_MessageDef_FindByJsonNameWithSize( + const upb_MessageDef* m, const char* name, size_t size) { + upb_value val; + + if (upb_strtable_lookup2(&m->jtof, name, size, &val)) { + return upb_value_getconstptr(val); + } + + if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) { + return NULL; + } + + return _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD); +} + +int upb_MessageDef_ExtensionRangeCount(const upb_MessageDef* m) { + return m->ext_range_count; +} + +int upb_MessageDef_ReservedRangeCount(const upb_MessageDef* m) { + return m->res_range_count; +} + +int upb_MessageDef_ReservedNameCount(const upb_MessageDef* m) { + return m->res_name_count; +} + +int upb_MessageDef_FieldCount(const upb_MessageDef* m) { + return m->field_count; +} + +int upb_MessageDef_OneofCount(const upb_MessageDef* m) { + return m->oneof_count; +} + +int upb_MessageDef_RealOneofCount(const upb_MessageDef* m) { + return m->real_oneof_count; +} + +int upb_MessageDef_NestedMessageCount(const upb_MessageDef* m) { + return m->nested_msg_count; +} + +int upb_MessageDef_NestedEnumCount(const upb_MessageDef* m) { + return m->nested_enum_count; +} + +int upb_MessageDef_NestedExtensionCount(const upb_MessageDef* m) { + return m->nested_ext_count; +} + +const upb_MiniTable* upb_MessageDef_MiniTable(const upb_MessageDef* m) { + return m->layout; +} + +const upb_ExtensionRange* upb_MessageDef_ExtensionRange(const upb_MessageDef* m, + int i) { + UPB_ASSERT(0 <= i && i < m->ext_range_count); + return _upb_ExtensionRange_At(m->ext_ranges, i); +} + +const upb_MessageReservedRange* upb_MessageDef_ReservedRange( + const upb_MessageDef* m, int i) { + UPB_ASSERT(0 <= i && i < m->res_range_count); + return _upb_MessageReservedRange_At(m->res_ranges, i); +} + +upb_StringView upb_MessageDef_ReservedName(const upb_MessageDef* m, int i) { + UPB_ASSERT(0 <= i && i < m->res_name_count); + return m->res_names[i]; +} + +const upb_FieldDef* upb_MessageDef_Field(const upb_MessageDef* m, int i) { + UPB_ASSERT(0 <= i && i < m->field_count); + return _upb_FieldDef_At(m->fields, i); +} + +const upb_OneofDef* upb_MessageDef_Oneof(const upb_MessageDef* m, int i) { + UPB_ASSERT(0 <= i && i < m->oneof_count); + return _upb_OneofDef_At(m->oneofs, i); +} + +const upb_MessageDef* upb_MessageDef_NestedMessage(const upb_MessageDef* m, + int i) { + UPB_ASSERT(0 <= i && i < m->nested_msg_count); + return &m->nested_msgs[i]; +} + +const upb_EnumDef* upb_MessageDef_NestedEnum(const upb_MessageDef* m, int i) { + UPB_ASSERT(0 <= i && i < m->nested_enum_count); + return _upb_EnumDef_At(m->nested_enums, i); +} + +const upb_FieldDef* upb_MessageDef_NestedExtension(const upb_MessageDef* m, + int i) { + UPB_ASSERT(0 <= i && i < m->nested_ext_count); + return _upb_FieldDef_At(m->nested_exts, i); +} + +upb_WellKnown upb_MessageDef_WellKnownType(const upb_MessageDef* m) { + return m->well_known_type; +} + +UPB_API google_protobuf_SymbolVisibility +upb_MessageDef_Visibility(const upb_MessageDef* m) { + return m->visibility; +} + +bool _upb_MessageDef_InMessageSet(const upb_MessageDef* m) { + return m->in_message_set; +} + +const upb_FieldDef* upb_MessageDef_FindFieldByName(const upb_MessageDef* m, + const char* name) { + return upb_MessageDef_FindFieldByNameWithSize(m, name, strlen(name)); +} + +const upb_OneofDef* upb_MessageDef_FindOneofByName(const upb_MessageDef* m, + const char* name) { + return upb_MessageDef_FindOneofByNameWithSize(m, name, strlen(name)); +} + +bool upb_MessageDef_IsMapEntry(const upb_MessageDef* m) { + return google_protobuf_MessageOptions_map_entry(m->opts); +} + +bool upb_MessageDef_IsMessageSet(const upb_MessageDef* m) { + return google_protobuf_MessageOptions_message_set_wire_format(m->opts); +} + +static upb_MiniTable* _upb_MessageDef_MakeMiniTable(upb_DefBuilder* ctx, + const upb_MessageDef* m) { + upb_StringView desc; + // Note: this will assign layout_index for fields, so upb_FieldDef_MiniTable() + // is safe to call only after this call. + bool ok = upb_MessageDef_MiniDescriptorEncode(m, ctx->tmp_arena, &desc); + if (!ok) _upb_DefBuilder_OomErr(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, ctx->platform, ctx->arena, scratch_data, + scratch_size, ctx->status); + if (!ret) _upb_DefBuilder_FailJmp(ctx); + + return ret; +} + +void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m) { + for (int i = 0; i < m->field_count; i++) { + upb_FieldDef* f = (upb_FieldDef*)upb_MessageDef_Field(m, i); + _upb_FieldDef_Resolve(ctx, m->full_name, f); + } + + m->in_message_set = false; + for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) { + upb_FieldDef* ext = (upb_FieldDef*)upb_MessageDef_NestedExtension(m, i); + _upb_FieldDef_Resolve(ctx, m->full_name, ext); + if (upb_FieldDef_Type(ext) == kUpb_FieldType_Message && + upb_FieldDef_Label(ext) == kUpb_Label_Optional && + upb_FieldDef_MessageSubDef(ext) == m && + google_protobuf_MessageOptions_message_set_wire_format( + upb_MessageDef_Options(upb_FieldDef_ContainingType(ext)))) { + m->in_message_set = true; + } + } + + for (int i = 0; i < upb_MessageDef_NestedMessageCount(m); i++) { + upb_MessageDef* n = (upb_MessageDef*)upb_MessageDef_NestedMessage(m, i); + _upb_MessageDef_Resolve(ctx, n); + } +} + +void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m, + const upb_FieldDef* f) { + const int32_t field_number = upb_FieldDef_Number(f); + + if (field_number <= 0 || field_number > kUpb_MaxFieldNumber) { + _upb_DefBuilder_Errf(ctx, "invalid field number (%u)", field_number); + } + + const char* json_name = upb_FieldDef_JsonName(f); + const char* shortname = upb_FieldDef_Name(f); + const size_t shortnamelen = strlen(shortname); + + upb_value v = upb_value_constptr(f); + + upb_value existing_v; + if (upb_strtable_lookup(&m->ntof, shortname, &existing_v)) { + _upb_DefBuilder_Errf(ctx, "duplicate field name (%s)", shortname); + } + + const upb_value field_v = _upb_DefType_Pack(f, UPB_DEFTYPE_FIELD); + bool ok = + _upb_MessageDef_Insert(m, shortname, shortnamelen, field_v, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + bool skip_json_conflicts = + google_protobuf_MessageOptions_deprecated_legacy_json_field_conflicts( + upb_MessageDef_Options(m)); + if (!skip_json_conflicts && strcmp(shortname, json_name) != 0 && + google_protobuf_FeatureSet_json_format(m->resolved_features) == + google_protobuf_FeatureSet_ALLOW && + upb_strtable_lookup(&m->ntof, json_name, &v)) { + _upb_DefBuilder_Errf( + ctx, "duplicate json_name for (%s) with original field name (%s)", + shortname, json_name); + } + + if (upb_strtable_lookup(&m->jtof, json_name, &v)) { + if (!skip_json_conflicts) { + _upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name); + } + } else { + const size_t json_size = strlen(json_name); + ok = upb_strtable_insert(&m->jtof, json_name, json_size, + upb_value_constptr(f), ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + } + + if (upb_inttable_lookup(&m->itof, field_number, NULL)) { + _upb_DefBuilder_Errf(ctx, "duplicate field number (%u)", field_number); + } + + ok = upb_inttable_insert(&m->itof, field_number, v, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); +} + +void _upb_MessageDef_CreateMiniTable(upb_DefBuilder* ctx, upb_MessageDef* m) { + if (ctx->layout == NULL) { + m->layout = _upb_MessageDef_MakeMiniTable(ctx, m); + } else { + m->layout = upb_MiniTableFile_Message(ctx->layout, ctx->msg_count++); + UPB_ASSERT(m->field_count == upb_MiniTable_FieldCount(m->layout)); + + // We don't need the result of this call, but it will assign layout_index + // for all the fields in O(n lg n) time. + _upb_FieldDefs_Sorted(m->fields, m->field_count, ctx->tmp_arena); + } + + for (int i = 0; i < m->nested_msg_count; i++) { + upb_MessageDef* nested = + (upb_MessageDef*)upb_MessageDef_NestedMessage(m, i); + _upb_MessageDef_CreateMiniTable(ctx, nested); + } +} + +void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx, + const upb_MessageDef* m) { + for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) { + const upb_FieldDef* ext = upb_MessageDef_NestedExtension(m, i); + _upb_FieldDef_BuildMiniTableExtension(ctx, ext); + } + + for (int i = 0; i < m->nested_msg_count; i++) { + _upb_MessageDef_LinkMiniTable(ctx, upb_MessageDef_NestedMessage(m, i)); + } + + if (ctx->layout || ctx->platform != kUpb_MiniTablePlatform_Native) return; + + for (int i = 0; i < m->field_count; i++) { + const upb_FieldDef* f = upb_MessageDef_Field(m, i); + const upb_MessageDef* sub_m = upb_FieldDef_MessageSubDef(f); + const upb_EnumDef* sub_e = upb_FieldDef_EnumSubDef(f); + const int layout_index = _upb_FieldDef_LayoutIndex(f); + upb_MiniTable* mt = (upb_MiniTable*)upb_MessageDef_MiniTable(m); + + UPB_ASSERT(layout_index < m->field_count); + upb_MiniTableField* mt_f = + (upb_MiniTableField*)&m->layout->UPB_PRIVATE(fields)[layout_index]; + if (sub_m) { + UPB_ASSERT(mt_f); + UPB_ASSERT(sub_m->layout); + if (UPB_UNLIKELY(!upb_MiniTable_SetSubMessage(mt, mt_f, sub_m->layout))) { + _upb_DefBuilder_Errf(ctx, "invalid submsg for (%s)", m->full_name); + } + } else if (_upb_FieldDef_IsClosedEnum(f)) { + const upb_MiniTableEnum* mt_e = _upb_EnumDef_MiniTable(sub_e); + if (UPB_UNLIKELY(!upb_MiniTable_SetSubEnum(mt, mt_f, mt_e))) { + _upb_DefBuilder_Errf(ctx, "invalid subenum for (%s)", m->full_name); + } + } + } + +#ifndef NDEBUG + for (int i = 0; i < m->field_count; i++) { + const upb_FieldDef* f = upb_MessageDef_Field(m, i); + const int layout_index = _upb_FieldDef_LayoutIndex(f); + UPB_ASSERT(layout_index < upb_MiniTable_FieldCount(m->layout)); + const upb_MiniTableField* mt_f = + &m->layout->UPB_PRIVATE(fields)[layout_index]; + UPB_ASSERT(upb_FieldDef_Type(f) == upb_MiniTableField_Type(mt_f)); + UPB_ASSERT(upb_FieldDef_CType(f) == upb_MiniTableField_CType(mt_f)); + UPB_ASSERT(upb_FieldDef_HasPresence(f) == + upb_MiniTableField_HasPresence(mt_f)); + } +#endif +} + +// Returns whether packable repeated fields in the message should be considered +// packed by default. This is used only for the purpose of encoding +// MiniDescriptors, so we just return true if there are more packed fields than +// unpacked. This optimizes for smaller MiniDescriptors. +static bool _upb_MessageDef_DefaultIsPacked(const upb_MessageDef* m) { + int packed = 0; + int unpacked = 0; + for (int i = 0; i < m->field_count; i++) { + const upb_FieldDef* f = upb_MessageDef_Field(m, i); + if (_upb_FieldDef_IsPackable(f)) { + if (upb_FieldDef_IsPacked(f)) { + ++packed; + } else { + ++unpacked; + } + } + } + return packed > unpacked; +} + +static bool _upb_MessageDef_ValidateUtf8(const upb_MessageDef* m) { + bool has_string = false; + for (int i = 0; i < m->field_count; i++) { + const upb_FieldDef* f = upb_MessageDef_Field(m, i); + // Old binaries do not recognize the field-level "FlipValidateUtf8" wire + // modifier, so we do not actually have field-level control for old + // binaries. Given this, we judge that the better failure mode is to be + // more lax than intended, rather than more strict. To achieve this, we + // only mark the message with the ValidateUtf8 modifier if *all* fields + // validate UTF-8. + if (!_upb_FieldDef_ValidateUtf8(f)) return false; + if (upb_FieldDef_Type(f) == kUpb_FieldType_String) has_string = true; + } + return has_string; +} + +static uint64_t _upb_MessageDef_Modifiers(const upb_MessageDef* m) { + uint64_t out = 0; + + if (_upb_MessageDef_DefaultIsPacked(m)) { + out |= kUpb_MessageModifier_DefaultIsPacked; + } + + if (_upb_MessageDef_ValidateUtf8(m)) { + out |= kUpb_MessageModifier_ValidateUtf8; + } + + if (m->ext_range_count) { + out |= kUpb_MessageModifier_IsExtendable; + } + + return out; +} + +static bool _upb_MessageDef_EncodeMap(upb_DescState* s, const upb_MessageDef* m, + upb_Arena* a) { + if (m->field_count != 2) return false; + + const upb_FieldDef* key_field = upb_MessageDef_Field(m, 0); + const upb_FieldDef* val_field = upb_MessageDef_Field(m, 1); + if (key_field == NULL || val_field == NULL) return false; + + UPB_ASSERT(_upb_FieldDef_LayoutIndex(key_field) == 0); + UPB_ASSERT(_upb_FieldDef_LayoutIndex(val_field) == 1); + + s->ptr = upb_MtDataEncoder_EncodeMap( + &s->e, s->ptr, upb_FieldDef_Type(key_field), upb_FieldDef_Type(val_field), + _upb_FieldDef_Modifiers(key_field), _upb_FieldDef_Modifiers(val_field)); + return true; +} + +static bool _upb_MessageDef_EncodeMessage(upb_DescState* s, + const upb_MessageDef* m, + upb_Arena* a) { + const upb_FieldDef** sorted = NULL; + if (!m->is_sorted) { + sorted = _upb_FieldDefs_Sorted(m->fields, m->field_count, a); + if (!sorted) return false; + } + + s->ptr = upb_MtDataEncoder_StartMessage(&s->e, s->ptr, + _upb_MessageDef_Modifiers(m)); + + for (int i = 0; i < m->field_count; i++) { + const upb_FieldDef* f = sorted ? sorted[i] : upb_MessageDef_Field(m, i); + const upb_FieldType type = upb_FieldDef_Type(f); + const int number = upb_FieldDef_Number(f); + const uint64_t modifiers = _upb_FieldDef_Modifiers(f); + + if (!_upb_DescState_Grow(s, a)) return false; + s->ptr = upb_MtDataEncoder_PutField(&s->e, s->ptr, type, number, modifiers); + } + + for (int i = 0; i < m->real_oneof_count; i++) { + if (!_upb_DescState_Grow(s, a)) return false; + s->ptr = upb_MtDataEncoder_StartOneof(&s->e, s->ptr); + + const upb_OneofDef* o = upb_MessageDef_Oneof(m, i); + const int field_count = upb_OneofDef_FieldCount(o); + for (int j = 0; j < field_count; j++) { + const int number = upb_FieldDef_Number(upb_OneofDef_Field(o, j)); + + if (!_upb_DescState_Grow(s, a)) return false; + s->ptr = upb_MtDataEncoder_PutOneofField(&s->e, s->ptr, number); + } + } + + return true; +} + +static bool _upb_MessageDef_EncodeMessageSet(upb_DescState* s, + const upb_MessageDef* m, + upb_Arena* a) { + s->ptr = upb_MtDataEncoder_EncodeMessageSet(&s->e, s->ptr); + + return true; +} + +bool upb_MessageDef_MiniDescriptorEncode(const upb_MessageDef* m, upb_Arena* a, + upb_StringView* out) { + upb_DescState s; + _upb_DescState_Init(&s); + + if (!_upb_DescState_Grow(&s, a)) return false; + + if (upb_MessageDef_IsMapEntry(m)) { + if (!_upb_MessageDef_EncodeMap(&s, m, a)) return false; + } else if (google_protobuf_MessageOptions_message_set_wire_format(m->opts)) { + if (!_upb_MessageDef_EncodeMessageSet(&s, m, a)) return false; + } else { + if (!_upb_MessageDef_EncodeMessage(&s, m, a)) return false; + } + + if (!_upb_DescState_Grow(&s, a)) return false; + *s.ptr = '\0'; + + out->data = s.buf; + out->size = s.ptr - s.buf; + return true; +} + +static upb_StringView* _upb_ReservedNames_New(upb_DefBuilder* ctx, int n, + const upb_StringView* protos) { + upb_StringView* sv = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_StringView, n); + for (int i = 0; i < n; i++) { + sv[i].data = + upb_strdup2(protos[i].data, protos[i].size, _upb_DefBuilder_Arena(ctx)); + sv[i].size = protos[i].size; + } + return sv; +} + +static void create_msgdef(upb_DefBuilder* ctx, const char* prefix, + const google_protobuf_DescriptorProto* msg_proto, + const google_protobuf_FeatureSet* parent_features, + const upb_MessageDef* containing_type, + upb_MessageDef* m) { + const google_protobuf_OneofDescriptorProto* const* oneofs; + const google_protobuf_FieldDescriptorProto* const* fields; + const google_protobuf_DescriptorProto_ExtensionRange* const* ext_ranges; + const google_protobuf_DescriptorProto_ReservedRange* const* res_ranges; + const upb_StringView* res_names; + size_t n_oneof, n_field, n_enum, n_ext, n_msg; + size_t n_ext_range, n_res_range, n_res_name; + upb_StringView name; + + UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto); + m->resolved_features = _upb_DefBuilder_ResolveFeatures( + ctx, parent_features, google_protobuf_MessageOptions_features(m->opts)); + + // Must happen before _upb_DefBuilder_Add() + m->file = _upb_DefBuilder_File(ctx); + + m->containing_type = containing_type; + m->is_sorted = true; + + name = google_protobuf_DescriptorProto_name(msg_proto); + + m->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name); + _upb_DefBuilder_Add(ctx, m->full_name, _upb_DefType_Pack(m, UPB_DEFTYPE_MSG)); + + oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n_oneof); + fields = google_protobuf_DescriptorProto_field(msg_proto, &n_field); + ext_ranges = google_protobuf_DescriptorProto_extension_range(msg_proto, &n_ext_range); + res_ranges = google_protobuf_DescriptorProto_reserved_range(msg_proto, &n_res_range); + res_names = google_protobuf_DescriptorProto_reserved_name(msg_proto, &n_res_name); + + bool ok = upb_inttable_init(&m->itof, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + ok = upb_strtable_init(&m->ntof, n_oneof + n_field, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + ok = upb_strtable_init(&m->jtof, n_field, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + m->oneof_count = n_oneof; + m->oneofs = _upb_OneofDefs_New(ctx, n_oneof, oneofs, m->resolved_features, m); + + m->field_count = n_field; + m->fields = _upb_FieldDefs_New(ctx, n_field, fields, m->resolved_features, + m->full_name, m, &m->is_sorted); + + // Message Sets may not contain fields. + if (UPB_UNLIKELY(google_protobuf_MessageOptions_message_set_wire_format(m->opts))) { + if (UPB_UNLIKELY(n_field > 0)) { + _upb_DefBuilder_Errf(ctx, "invalid message set (%s)", m->full_name); + } + } + + m->ext_range_count = n_ext_range; + m->ext_ranges = _upb_ExtensionRanges_New(ctx, n_ext_range, ext_ranges, + m->resolved_features, m); + + m->res_range_count = n_res_range; + m->res_ranges = + _upb_MessageReservedRanges_New(ctx, n_res_range, res_ranges, m); + + m->res_name_count = n_res_name; + m->res_names = _upb_ReservedNames_New(ctx, n_res_name, res_names); + + const size_t synthetic_count = _upb_OneofDefs_Finalize(ctx, m); + m->real_oneof_count = m->oneof_count - synthetic_count; + + assign_msg_wellknowntype(m); + if (!upb_inttable_compact(&m->itof, ctx->arena)) _upb_DefBuilder_OomErr(ctx); + + const google_protobuf_EnumDescriptorProto* const* enums = + google_protobuf_DescriptorProto_enum_type(msg_proto, &n_enum); + m->nested_enum_count = n_enum; + m->nested_enums = + _upb_EnumDefs_New(ctx, n_enum, enums, m->resolved_features, m); + + const google_protobuf_FieldDescriptorProto* const* exts = + google_protobuf_DescriptorProto_extension(msg_proto, &n_ext); + m->nested_ext_count = n_ext; + m->nested_exts = _upb_Extensions_New(ctx, n_ext, exts, m->resolved_features, + m->full_name, m); + + const google_protobuf_DescriptorProto* const* msgs = + google_protobuf_DescriptorProto_nested_type(msg_proto, &n_msg); + m->nested_msg_count = n_msg; + m->nested_msgs = + _upb_MessageDefs_New(ctx, n_msg, msgs, m->resolved_features, m); + + m->visibility = google_protobuf_DescriptorProto_visibility(msg_proto); +} + +// Allocate and initialize an array of |n| message defs. +upb_MessageDef* _upb_MessageDefs_New( + upb_DefBuilder* ctx, int n, const google_protobuf_DescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, + const upb_MessageDef* containing_type) { + _upb_DefType_CheckPadding(sizeof(upb_MessageDef)); + + const char* name = containing_type ? containing_type->full_name + : _upb_FileDef_RawPackage(ctx->file); + + upb_MessageDef* m = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MessageDef, n); + for (int i = 0; i < n; i++) { + create_msgdef(ctx, name, protos[i], parent_features, containing_type, + &m[i]); + } + return m; +} + +#include + + +// Must be last. + +struct upb_MessageReservedRange { + int32_t start; + int32_t end; +}; + +upb_MessageReservedRange* _upb_MessageReservedRange_At( + const upb_MessageReservedRange* r, int i) { + return (upb_MessageReservedRange*)&r[i]; +} + +int32_t upb_MessageReservedRange_Start(const upb_MessageReservedRange* r) { + return r->start; +} +int32_t upb_MessageReservedRange_End(const upb_MessageReservedRange* r) { + return r->end; +} + +upb_MessageReservedRange* _upb_MessageReservedRanges_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_DescriptorProto_ReservedRange* const* protos, + const upb_MessageDef* m) { + upb_MessageReservedRange* r = + UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MessageReservedRange, n); + + for (int i = 0; i < n; i++) { + const int32_t start = google_protobuf_DescriptorProto_ReservedRange_start(protos[i]); + const int32_t end = google_protobuf_DescriptorProto_ReservedRange_end(protos[i]); + const int32_t max = kUpb_MaxFieldNumber + 1; + + // A full validation would also check that each range is disjoint, and that + // none of the fields overlap with the extension ranges, but we are just + // sanity checking here. + if (start < 1 || end <= start || end > max) { + _upb_DefBuilder_Errf(ctx, + "Reserved range (%d, %d) is invalid, message=%s\n", + (int)start, (int)end, upb_MessageDef_FullName(m)); + } + + r[i].start = start; + r[i].end = end; + } + + return r; +} + + + +// Must be last. + +struct upb_MethodDef { + const google_protobuf_MethodOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + upb_ServiceDef* service; + const char* full_name; + const upb_MessageDef* input_type; + const upb_MessageDef* output_type; + int index; + bool client_streaming; + bool server_streaming; +}; + +upb_MethodDef* _upb_MethodDef_At(const upb_MethodDef* m, int i) { + return (upb_MethodDef*)&m[i]; +} + +const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m) { + return m->service; +} + +const google_protobuf_MethodOptions* upb_MethodDef_Options(const upb_MethodDef* m) { + return m->opts; +} + +bool upb_MethodDef_HasOptions(const upb_MethodDef* m) { + return m->opts != (void*)kUpbDefOptDefault; +} + +const google_protobuf_FeatureSet* upb_MethodDef_ResolvedFeatures( + const upb_MethodDef* m) { + return m->resolved_features; +} + +const char* upb_MethodDef_FullName(const upb_MethodDef* m) { + return m->full_name; +} + +const char* upb_MethodDef_Name(const upb_MethodDef* m) { + return _upb_DefBuilder_FullToShort(m->full_name); +} + +int upb_MethodDef_Index(const upb_MethodDef* m) { return m->index; } + +const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m) { + return m->input_type; +} + +const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m) { + return m->output_type; +} + +bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m) { + return m->client_streaming; +} + +bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m) { + return m->server_streaming; +} + +static void create_method(upb_DefBuilder* ctx, + const google_protobuf_MethodDescriptorProto* method_proto, + const google_protobuf_FeatureSet* parent_features, + upb_ServiceDef* s, upb_MethodDef* m) { + UPB_DEF_SET_OPTIONS(m->opts, MethodDescriptorProto, MethodOptions, + method_proto); + m->resolved_features = _upb_DefBuilder_ResolveFeatures( + ctx, parent_features, google_protobuf_MethodOptions_features(m->opts)); + + upb_StringView name = google_protobuf_MethodDescriptorProto_name(method_proto); + + m->service = s; + m->full_name = + _upb_DefBuilder_MakeFullName(ctx, upb_ServiceDef_FullName(s), name); + m->client_streaming = + google_protobuf_MethodDescriptorProto_client_streaming(method_proto); + m->server_streaming = + google_protobuf_MethodDescriptorProto_server_streaming(method_proto); + m->input_type = _upb_DefBuilder_Resolve( + ctx, m->full_name, m->full_name, + google_protobuf_MethodDescriptorProto_input_type(method_proto), UPB_DEFTYPE_MSG); + m->output_type = _upb_DefBuilder_Resolve( + ctx, m->full_name, m->full_name, + google_protobuf_MethodDescriptorProto_output_type(method_proto), UPB_DEFTYPE_MSG); + _upb_ServiceDef_InsertMethod(ctx, s, m); +} + +// Allocate and initialize an array of |n| method defs belonging to |s|. +upb_MethodDef* _upb_MethodDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_MethodDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, upb_ServiceDef* s) { + upb_MethodDef* m = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MethodDef, n); + for (int i = 0; i < n; i++) { + create_method(ctx, protos[i], parent_features, s, &m[i]); + m[i].index = i; + } + return m; +} + + +#include +#include +#include + + +// Must be last. + +struct upb_OneofDef { + UPB_ALIGN_AS(8) const google_protobuf_OneofOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + const upb_MessageDef* parent; + const char* full_name; + int field_count; + bool synthetic; + const upb_FieldDef** fields; + upb_strtable ntof; // lookup a field by name + upb_inttable itof; // lookup a field by number (index) +}; + +upb_OneofDef* _upb_OneofDef_At(const upb_OneofDef* o, int i) { + return (upb_OneofDef*)&o[i]; +} + +const google_protobuf_OneofOptions* upb_OneofDef_Options(const upb_OneofDef* o) { + return o->opts; +} + +bool upb_OneofDef_HasOptions(const upb_OneofDef* o) { + return o->opts != (void*)kUpbDefOptDefault; +} + +const google_protobuf_FeatureSet* upb_OneofDef_ResolvedFeatures(const upb_OneofDef* o) { + return o->resolved_features; +} + +const char* upb_OneofDef_FullName(const upb_OneofDef* o) { + return o->full_name; +} + +const char* upb_OneofDef_Name(const upb_OneofDef* o) { + return _upb_DefBuilder_FullToShort(o->full_name); +} + +const upb_MessageDef* upb_OneofDef_ContainingType(const upb_OneofDef* o) { + return o->parent; +} + +int upb_OneofDef_FieldCount(const upb_OneofDef* o) { return o->field_count; } + +const upb_FieldDef* upb_OneofDef_Field(const upb_OneofDef* o, int i) { + UPB_ASSERT(i < o->field_count); + return o->fields[i]; +} + +int upb_OneofDef_numfields(const upb_OneofDef* o) { return o->field_count; } + +uint32_t upb_OneofDef_Index(const upb_OneofDef* o) { + // Compute index in our parent's array. + return o - upb_MessageDef_Oneof(o->parent, 0); +} + +bool upb_OneofDef_IsSynthetic(const upb_OneofDef* o) { return o->synthetic; } + +const upb_FieldDef* upb_OneofDef_LookupNameWithSize(const upb_OneofDef* o, + const char* name, + size_t size) { + upb_value val; + return upb_strtable_lookup2(&o->ntof, name, size, &val) + ? upb_value_getptr(val) + : NULL; +} + +const upb_FieldDef* upb_OneofDef_LookupName(const upb_OneofDef* o, + const char* name) { + return upb_OneofDef_LookupNameWithSize(o, name, strlen(name)); +} + +const upb_FieldDef* upb_OneofDef_LookupNumber(const upb_OneofDef* o, + uint32_t num) { + upb_value val; + return upb_inttable_lookup(&o->itof, num, &val) ? upb_value_getptr(val) + : NULL; +} + +void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o, + const upb_FieldDef* f, const char* name, + size_t size) { + o->field_count++; + if (_upb_FieldDef_IsProto3Optional(f)) o->synthetic = true; + + const int number = upb_FieldDef_Number(f); + const upb_value v = upb_value_constptr(f); + + // TODO: This lookup is unfortunate because we also perform it when + // inserting into the message's table. Unfortunately that step occurs after + // this one and moving things around could be tricky so let's leave it for + // a future refactoring. + const bool number_exists = upb_inttable_lookup(&o->itof, number, NULL); + if (UPB_UNLIKELY(number_exists)) { + _upb_DefBuilder_Errf(ctx, "oneof fields have the same number (%d)", number); + } + + // TODO: More redundant work happening here. + const bool name_exists = upb_strtable_lookup2(&o->ntof, name, size, NULL); + if (UPB_UNLIKELY(name_exists)) { + _upb_DefBuilder_Errf(ctx, "oneof fields have the same name (%.*s)", + (int)size, name); + } + + const bool ok = upb_inttable_insert(&o->itof, number, v, ctx->arena) && + upb_strtable_insert(&o->ntof, name, size, v, ctx->arena); + if (UPB_UNLIKELY(!ok)) { + _upb_DefBuilder_OomErr(ctx); + } +} + +// Returns the synthetic count. +size_t _upb_OneofDefs_Finalize(upb_DefBuilder* ctx, upb_MessageDef* m) { + int synthetic_count = 0; + + for (int i = 0; i < upb_MessageDef_OneofCount(m); i++) { + upb_OneofDef* o = (upb_OneofDef*)upb_MessageDef_Oneof(m, i); + + if (o->synthetic && o->field_count != 1) { + _upb_DefBuilder_Errf(ctx, + "Synthetic oneofs must have one field, not %d: %s", + o->field_count, upb_OneofDef_Name(o)); + } + + if (o->synthetic) { + synthetic_count++; + } else if (synthetic_count != 0) { + _upb_DefBuilder_Errf( + ctx, "Synthetic oneofs must be after all other oneofs: %s", + upb_OneofDef_Name(o)); + } + + o->fields = + UPB_DEFBUILDER_ALLOCARRAY(ctx, const upb_FieldDef*, o->field_count); + o->field_count = 0; + } + + for (int i = 0; i < upb_MessageDef_FieldCount(m); i++) { + const upb_FieldDef* f = upb_MessageDef_Field(m, i); + upb_OneofDef* o = (upb_OneofDef*)upb_FieldDef_ContainingOneof(f); + if (o) { + o->fields[o->field_count++] = f; + } + } + + return synthetic_count; +} + +static void create_oneofdef(upb_DefBuilder* ctx, upb_MessageDef* m, + const google_protobuf_OneofDescriptorProto* oneof_proto, + const google_protobuf_FeatureSet* parent_features, + const upb_OneofDef* _o) { + upb_OneofDef* o = (upb_OneofDef*)_o; + + UPB_DEF_SET_OPTIONS(o->opts, OneofDescriptorProto, OneofOptions, oneof_proto); + o->resolved_features = _upb_DefBuilder_ResolveFeatures( + ctx, parent_features, google_protobuf_OneofOptions_features(o->opts)); + + upb_StringView name = google_protobuf_OneofDescriptorProto_name(oneof_proto); + + o->parent = m; + o->full_name = + _upb_DefBuilder_MakeFullName(ctx, upb_MessageDef_FullName(m), name); + o->field_count = 0; + o->synthetic = false; + + if (upb_MessageDef_FindByNameWithSize(m, name.data, name.size, NULL, NULL)) { + _upb_DefBuilder_Errf(ctx, "duplicate oneof name (%s)", o->full_name); + } + + upb_value v = _upb_DefType_Pack(o, UPB_DEFTYPE_ONEOF); + bool ok = _upb_MessageDef_Insert(m, name.data, name.size, v, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + ok = upb_inttable_init(&o->itof, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + + ok = upb_strtable_init(&o->ntof, 4, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); +} + +// Allocate and initialize an array of |n| oneof defs. +upb_OneofDef* _upb_OneofDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_OneofDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, upb_MessageDef* m) { + _upb_DefType_CheckPadding(sizeof(upb_OneofDef)); + + upb_OneofDef* o = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_OneofDef, n); + for (int i = 0; i < n; i++) { + create_oneofdef(ctx, m, protos[i], parent_features, &o[i]); + } + return o; +} + + +#include +#include + + +// Must be last. + +struct upb_ServiceDef { + UPB_ALIGN_AS(8) const google_protobuf_ServiceOptions* opts; + const google_protobuf_FeatureSet* resolved_features; + const upb_FileDef* file; + const char* full_name; + upb_MethodDef* methods; + int method_count; + int index; + upb_strtable ntom; +}; + +upb_ServiceDef* _upb_ServiceDef_At(const upb_ServiceDef* s, int index) { + return (upb_ServiceDef*)&s[index]; +} + +const google_protobuf_ServiceOptions* upb_ServiceDef_Options(const upb_ServiceDef* s) { + return s->opts; +} + +bool upb_ServiceDef_HasOptions(const upb_ServiceDef* s) { + return s->opts != (void*)kUpbDefOptDefault; +} + +const google_protobuf_FeatureSet* upb_ServiceDef_ResolvedFeatures( + const upb_ServiceDef* s) { + return s->resolved_features; +} + +const char* upb_ServiceDef_FullName(const upb_ServiceDef* s) { + return s->full_name; +} + +const char* upb_ServiceDef_Name(const upb_ServiceDef* s) { + return _upb_DefBuilder_FullToShort(s->full_name); +} + +int upb_ServiceDef_Index(const upb_ServiceDef* s) { return s->index; } + +const upb_FileDef* upb_ServiceDef_File(const upb_ServiceDef* s) { + return s->file; +} + +int upb_ServiceDef_MethodCount(const upb_ServiceDef* s) { + return s->method_count; +} + +const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s, int i) { + return (i < 0 || i >= s->method_count) ? NULL + : _upb_MethodDef_At(s->methods, i); +} + +const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s, + const char* name) { + return upb_ServiceDef_FindMethodByNameWithSize(s, name, strlen(name)); +} + +const upb_MethodDef* upb_ServiceDef_FindMethodByNameWithSize( + const upb_ServiceDef* s, const char* name, size_t len) { + upb_value val; + + if (!upb_strtable_lookup2(&s->ntom, name, len, &val)) { + return NULL; + } + + return _upb_DefType_Unpack(val, UPB_DEFTYPE_METHOD); +} + +static void create_service(upb_DefBuilder* ctx, + const google_protobuf_ServiceDescriptorProto* svc_proto, + const google_protobuf_FeatureSet* parent_features, + upb_ServiceDef* s) { + UPB_DEF_SET_OPTIONS(s->opts, ServiceDescriptorProto, ServiceOptions, + svc_proto); + s->resolved_features = _upb_DefBuilder_ResolveFeatures( + ctx, parent_features, google_protobuf_ServiceOptions_features(s->opts)); + + // Must happen before _upb_DefBuilder_Add() + s->file = _upb_DefBuilder_File(ctx); + + upb_StringView name = google_protobuf_ServiceDescriptorProto_name(svc_proto); + const char* package = _upb_FileDef_RawPackage(s->file); + s->full_name = _upb_DefBuilder_MakeFullName(ctx, package, name); + _upb_DefBuilder_Add(ctx, s->full_name, + _upb_DefType_Pack(s, UPB_DEFTYPE_SERVICE)); + + size_t n; + const google_protobuf_MethodDescriptorProto* const* methods = + google_protobuf_ServiceDescriptorProto_method(svc_proto, &n); + s->method_count = n; + bool ok = upb_strtable_init(&s->ntom, n, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + s->methods = _upb_MethodDefs_New(ctx, n, methods, s->resolved_features, s); +} + +upb_ServiceDef* _upb_ServiceDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_ServiceDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features) { + _upb_DefType_CheckPadding(sizeof(upb_ServiceDef)); + + upb_ServiceDef* s = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_ServiceDef, n); + for (int i = 0; i < n; i++) { + create_service(ctx, protos[i], parent_features, &s[i]); + s[i].index = i; + } + return s; +} + +void _upb_ServiceDef_InsertMethod(upb_DefBuilder* ctx, upb_ServiceDef* s, + const upb_MethodDef* m) { + const char* shortname = upb_MethodDef_Name(m); + const size_t shortnamelen = strlen(shortname); + upb_value existing_v; + if (upb_strtable_lookup(&s->ntom, shortname, &existing_v)) { + _upb_DefBuilder_Errf(ctx, "duplicate method name (%s)", shortname); + } + const upb_value method_v = _upb_DefType_Pack(m, UPB_DEFTYPE_METHOD); + bool ok = upb_strtable_insert(&s->ntom, shortname, shortnamelen, method_v, + ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); +} + + +#include +#include +#include +#include +#include +#include + + +// Must be last. + +static void _upb_MessageDebugString(txtenc* e, const upb_Message* msg, + const upb_MiniTable* mt); + +static void _upb_FieldDebugString(txtenc* e, upb_MessageValue val, + const upb_MiniTableField* f, + const upb_MiniTable* mt, const char* label, + const upb_MiniTableExtension* ext) { + UPB_PRIVATE(_upb_TextEncode_Indent)(e); + const upb_CType ctype = upb_MiniTableField_CType(f); + const bool is_ext = upb_MiniTableField_IsExtension(f); + char number[10]; // A 32-bit integer can hold up to 10 digits. + snprintf(number, sizeof(number), "%" PRIu32, upb_MiniTableField_Number(f)); + // label is to pass down whether we're dealing with a "key" of a map or + // a "value" of a map. + if (!label) label = number; + + if (is_ext) { + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "[%s]", label); + } else { + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s", label); + } + + if (ctype == kUpb_CType_Message) { + UPB_PRIVATE(_upb_TextEncode_Printf)(e, " {"); + UPB_PRIVATE(_upb_TextEncode_EndField)(e); + e->indent_depth++; + const upb_MiniTable* subm = ext ? upb_MiniTableExtension_GetSubMessage(ext) + : upb_MiniTable_SubMessage(f); + _upb_MessageDebugString(e, val.msg_val, subm); + e->indent_depth--; + UPB_PRIVATE(_upb_TextEncode_Indent)(e); + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}"); + UPB_PRIVATE(_upb_TextEncode_EndField)(e); + return; + } + + UPB_PRIVATE(_upb_TextEncode_Printf)(e, ": "); + + if (ctype == + kUpb_CType_Enum) { // Enum has to be processed separately because of + // divergent behavior between encoders + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRId32, val.int32_val); + } else { + UPB_PRIVATE(_upb_TextEncode_Scalar)(e, val, ctype); + } + + UPB_PRIVATE(_upb_TextEncode_EndField)(e); +} + +/* + * Arrays print as simple repeated elements, eg. + * + * 5: 1 + * 5: 2 + * 5: 3 + */ +static void _upb_ArrayDebugString(txtenc* e, const upb_Array* arr, + const upb_MiniTableField* f, + const upb_MiniTable* mt, + const upb_MiniTableExtension* ext) { + for (size_t i = 0, n = upb_Array_Size(arr); i < n; i++) { + _upb_FieldDebugString(e, upb_Array_Get(arr, i), f, mt, NULL, ext); + } +} + +static void _upb_MapEntryDebugString(txtenc* e, upb_MessageValue key, + upb_MessageValue val, + const upb_MiniTableField* f, + const upb_MiniTable* mt) { + const upb_MiniTable* entry = upb_MiniTable_SubMessage(f); + const upb_MiniTableField* key_f = upb_MiniTable_MapKey(entry); + const upb_MiniTableField* val_f = upb_MiniTable_MapValue(entry); + + UPB_PRIVATE(_upb_TextEncode_Indent)(e); + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%u {", upb_MiniTableField_Number(f)); + UPB_PRIVATE(_upb_TextEncode_EndField)(e); + e->indent_depth++; + + _upb_FieldDebugString(e, key, key_f, entry, "key", NULL); + _upb_FieldDebugString(e, val, val_f, entry, "value", NULL); + + e->indent_depth--; + UPB_PRIVATE(_upb_TextEncode_Indent)(e); + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}"); + UPB_PRIVATE(_upb_TextEncode_EndField)(e); +} + +/* + * Maps print as messages of key/value, etc. + * + * 1 { + * key: "abc" + * value: 123 + * } + * 2 { + * key: "def" + * value: 456 + * } + */ +static void _upb_MapDebugString(txtenc* e, const upb_Map* map, + const upb_MiniTableField* f, + const upb_MiniTable* mt) { + if (e->options & UPB_TXTENC_NOSORT) { + size_t iter = kUpb_Map_Begin; + upb_MessageValue key, val; + while (upb_Map_Next(map, &key, &val, &iter)) { + _upb_MapEntryDebugString(e, key, val, f, mt); + } + } else { + if (upb_Map_Size(map) == 0) return; + + const upb_MiniTable* entry = upb_MiniTable_SubMessage(f); + const upb_MiniTableField* key_f = upb_MiniTable_GetFieldByIndex(entry, 0); + _upb_sortedmap sorted; + upb_MapEntry ent; + + _upb_mapsorter_pushmap(&e->sorter, upb_MiniTableField_Type(key_f), map, + &sorted); + while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) { + upb_MessageValue key, val; + memcpy(&key, &ent.k, sizeof(key)); + memcpy(&val, &ent.v, sizeof(val)); + _upb_MapEntryDebugString(e, key, val, f, mt); + } + _upb_mapsorter_popmap(&e->sorter, &sorted); + } +} + +static void _upb_MessageDebugString(txtenc* e, const upb_Message* msg, + const upb_MiniTable* mt) { + size_t iter = kUpb_BaseField_Begin; + const upb_MiniTableField* f; + upb_MessageValue val; + + // Base fields will be printed out first, followed by extension fields, and + // finally unknown fields. + + while (UPB_PRIVATE(_upb_Message_NextBaseField)(msg, mt, &f, &val, &iter)) { + if (upb_MiniTableField_IsMap(f)) { + _upb_MapDebugString(e, val.map_val, f, mt); + } else if (upb_MiniTableField_IsArray(f)) { + // ext set to NULL as we're not dealing with extensions yet + _upb_ArrayDebugString(e, val.array_val, f, mt, NULL); + } else { + // ext set to NULL as we're not dealing with extensions yet + // label set to NULL as we're not currently working with a MapEntry + _upb_FieldDebugString(e, val, f, mt, NULL, NULL); + } + } + + const upb_MiniTableExtension* ext; + upb_MessageValue val_ext; + iter = kUpb_Message_ExtensionBegin; + while (upb_Message_NextExtension(msg, &ext, &val_ext, &iter)) { + const upb_MiniTableField* f = &ext->UPB_PRIVATE(field); + // It is not sufficient to only pass |f| as we lose valuable information + // about sub-messages. It is required that we pass |ext|. + if (upb_MiniTableField_IsMap(f)) { + UPB_UNREACHABLE(); // Maps cannot be extensions. + break; + } else if (upb_MiniTableField_IsArray(f)) { + _upb_ArrayDebugString(e, val_ext.array_val, f, mt, ext); + } else { + // label set to NULL as we're not currently working with a MapEntry + _upb_FieldDebugString(e, val_ext, f, mt, NULL, ext); + } + } + + UPB_PRIVATE(_upb_TextEncode_ParseUnknown)(e, msg); +} + +size_t upb_DebugString(const upb_Message* msg, const upb_MiniTable* mt, + int options, char* buf, size_t size) { + txtenc e; + + e.buf = buf; + e.ptr = buf; + e.end = UPB_PTRADD(buf, size); + e.overflow = 0; + e.indent_depth = 0; + e.options = options; + e.ext_pool = NULL; + _upb_mapsorter_init(&e.sorter); + + _upb_MessageDebugString(&e, msg, mt); + _upb_mapsorter_destroy(&e.sorter); + return UPB_PRIVATE(_upb_TextEncode_Nullz)(&e, size); +} + + +#include +#include +#include + + +// Must be last. + +#define CHK(x) \ + do { \ + if (!(x)) { \ + return NULL; \ + } \ + } while (0) + +/* + * Unknown fields are printed by number. + * + * 1001: 123 + * 1002: "hello" + * 1006: 0xdeadbeef + * 1003: { + * 1: 111 + * } + */ +const char* UPB_PRIVATE(_upb_TextEncode_Unknown)(txtenc* e, const char* ptr, + upb_EpsCopyInputStream* stream, + int groupnum) { + // We are guaranteed that the unknown data is valid wire format, and will not + // contain tag zero. + uint32_t end_group = groupnum > 0 + ? ((groupnum << kUpb_WireReader_WireTypeBits) | + kUpb_WireType_EndGroup) + : 0; + + while (!upb_EpsCopyInputStream_IsDone(stream, &ptr)) { + uint32_t tag; + CHK(ptr = upb_WireReader_ReadTag(ptr, &tag, stream)); + if (tag == end_group) return ptr; + + UPB_PRIVATE(_upb_TextEncode_Indent)(e); + UPB_PRIVATE(_upb_TextEncode_Printf) + (e, "%d: ", (int)upb_WireReader_GetFieldNumber(tag)); + + switch (upb_WireReader_GetWireType(tag)) { + case kUpb_WireType_Varint: { + uint64_t val; + CHK(ptr = upb_WireReader_ReadVarint(ptr, &val, stream)); + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRIu64, val); + break; + } + case kUpb_WireType_32Bit: { + uint32_t val; + ptr = upb_WireReader_ReadFixed32(ptr, &val, stream); + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "0x%08" PRIu32, val); + break; + } + case kUpb_WireType_64Bit: { + uint64_t val; + ptr = upb_WireReader_ReadFixed64(ptr, &val, stream); + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "0x%016" PRIu64, val); + break; + } + case kUpb_WireType_Delimited: { + int size; + char* start = e->ptr; + size_t start_overflow = e->overflow; + upb_StringView sv; + CHK(ptr = upb_WireReader_ReadSize(ptr, &size, stream)); + CHK(ptr = upb_EpsCopyInputStream_ReadStringAlwaysAlias(stream, ptr, + size, &sv)); + + // Speculatively try to parse as message. + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "{"); + UPB_PRIVATE(_upb_TextEncode_EndField)(e); + + // EpsCopyInputStream can't back up, so create a sub-stream for the + // speculative parse. + upb_EpsCopyInputStream sub_stream; + const char* sub_ptr = sv.data; + upb_EpsCopyInputStream_Init(&sub_stream, &sub_ptr, size); + + e->indent_depth++; + if (UPB_PRIVATE(_upb_TextEncode_Unknown)(e, sub_ptr, &sub_stream, -1)) { + e->indent_depth--; + UPB_PRIVATE(_upb_TextEncode_Indent)(e); + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}"); + } else { + // Didn't work out, print as raw bytes. + e->indent_depth--; + e->ptr = start; + e->overflow = start_overflow; + UPB_PRIVATE(_upb_TextEncode_Bytes)(e, sv); + } + break; + } + case kUpb_WireType_StartGroup: + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "{"); + UPB_PRIVATE(_upb_TextEncode_EndField)(e); + e->indent_depth++; + CHK(ptr = UPB_PRIVATE(_upb_TextEncode_Unknown)( + e, ptr, stream, upb_WireReader_GetFieldNumber(tag))); + e->indent_depth--; + UPB_PRIVATE(_upb_TextEncode_Indent)(e); + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}"); + break; + default: + return NULL; + } + UPB_PRIVATE(_upb_TextEncode_EndField)(e); + } + + return end_group == 0 && !upb_EpsCopyInputStream_IsError(stream) ? ptr : NULL; +} + +#undef CHK + +void UPB_PRIVATE(_upb_TextEncode_ParseUnknown)(txtenc* e, + const upb_Message* msg) { + if ((e->options & UPB_TXTENC_SKIPUNKNOWN) != 0) return; + + uintptr_t iter = kUpb_Message_UnknownBegin; + upb_StringView view; + while (upb_Message_NextUnknown(msg, &view, &iter)) { + char* start = e->ptr; + upb_EpsCopyInputStream stream; + upb_EpsCopyInputStream_Init(&stream, &view.data, view.size); + if (!UPB_PRIVATE(_upb_TextEncode_Unknown)(e, view.data, &stream, -1)) { + /* Unknown failed to parse, back up and don't print it at all. */ + e->ptr = start; + } + } +} + +void UPB_PRIVATE(_upb_TextEncode_Scalar)(txtenc* e, upb_MessageValue val, + upb_CType ctype) { + switch (ctype) { + case kUpb_CType_Bool: + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, val.bool_val ? "true" : "false"); + break; + case kUpb_CType_Float: { + char buf[32]; + _upb_EncodeRoundTripFloat(val.float_val, buf, sizeof(buf)); + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, buf); + break; + } + case kUpb_CType_Double: { + char buf[32]; + _upb_EncodeRoundTripDouble(val.double_val, buf, sizeof(buf)); + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, buf); + break; + } + case kUpb_CType_Int32: + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRId32, val.int32_val); + break; + case kUpb_CType_UInt32: + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRIu32, val.uint32_val); + break; + case kUpb_CType_Int64: + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRId64, val.int64_val); + break; + case kUpb_CType_UInt64: + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRIu64, val.uint64_val); + break; + case kUpb_CType_String: + UPB_PRIVATE(_upb_HardenedPrintString) + (e, val.str_val.data, val.str_val.size); + break; + case kUpb_CType_Bytes: + UPB_PRIVATE(_upb_TextEncode_Bytes)(e, val.str_val); + break; + case kUpb_CType_Enum: + UPB_ASSERT(false); // handled separately in each encoder + break; + default: + UPB_UNREACHABLE(); + } +} + + +#include +#include +#include +#include +#include + + +// Our awkward dance for including fasttable only when it is enabled. +#if UPB_FASTTABLE +#define UPB_INCLUDE_FAST_DECODE +#endif + +#ifdef UPB_INCLUDE_FAST_DECODE +#endif + +#undef UPB_INCLUDE_FAST_DECODE + +// Must be last. + +// A few fake field types for our tables. +enum { + kUpb_FakeFieldType_FieldNotFound = 0, + kUpb_FakeFieldType_MessageSetItem = 19, +}; + +// DecodeOp: an action to be performed for a wire-type/field-type combination. +enum { + // Special ops: we don't write data to regular fields for these. + kUpb_DecodeOp_UnknownField = -1, + kUpb_DecodeOp_MessageSetItem = -2, + + // Scalar-only ops. + kUpb_DecodeOp_Scalar1Byte = 0, + kUpb_DecodeOp_Scalar4Byte = 2, + kUpb_DecodeOp_Scalar8Byte = 3, + + // Scalar/repeated ops. + kUpb_DecodeOp_String = 4, + kUpb_DecodeOp_Bytes = 5, + kUpb_DecodeOp_SubMessage = 6, + + // Repeated-only ops (also see macros below). + kUpb_DecodeOp_PackedEnum = 13, +}; + +// For packed fields it is helpful to be able to recover the lg2 of the data +// size from the op. +#define OP_FIXPCK_LG2(n) (n + 5) /* n in [2, 3] => op in [7, 8] */ +#define OP_VARPCK_LG2(n) (n + 9) /* n in [0, 2, 3] => op in [9, 11, 12] */ + +typedef union { + bool bool_val; + uint32_t uint32_val; + uint64_t uint64_val; + uint32_t size; +} wireval; + +static void _upb_Decoder_AssumeEpsHasErrorHandler(upb_Decoder* d) { + UPB_ASSUME(upb_EpsCopyInputStream_HasErrorHandler(&d->input)); +} + +#define EPS(d) (_upb_Decoder_AssumeEpsHasErrorHandler(d), &(d)->input) + +static bool _upb_Decoder_Reserve(upb_Decoder* d, upb_Array* arr, size_t elem) { + bool need_realloc = + arr->UPB_PRIVATE(capacity) - arr->UPB_PRIVATE(size) < elem; + if (need_realloc && !UPB_PRIVATE(_upb_Array_Realloc)( + arr, arr->UPB_PRIVATE(size) + elem, &d->arena)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } + return need_realloc; +} + +typedef struct { + const char* ptr; + uint64_t val; +} _upb_DecodeLongVarintReturn; + +// This is identical to _upb_Decoder_DecodeTag() except that the maximum value +// is INT32_MAX instead of UINT32_MAX. +UPB_FORCEINLINE +const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr, + uint32_t* size) { + int sz; + ptr = upb_WireReader_ReadSize(ptr, &sz, EPS(d)); + *size = sz; + return ptr; +} + +static void _upb_Decoder_MungeInt32(wireval* val) { + if (!upb_IsLittleEndian()) { + /* The next stage will memcpy(dst, &val, 4) */ + val->uint32_val = val->uint64_val; + } +} + +static void _upb_Decoder_Munge(const upb_MiniTableField* field, wireval* val) { + switch (field->UPB_PRIVATE(descriptortype)) { + case kUpb_FieldType_Bool: + val->bool_val = val->uint64_val != 0; + break; + case kUpb_FieldType_SInt32: { + uint32_t n = val->uint64_val; + val->uint32_val = (n >> 1) ^ -(int32_t)(n & 1); + break; + } + case kUpb_FieldType_SInt64: { + uint64_t n = val->uint64_val; + val->uint64_val = (n >> 1) ^ -(int64_t)(n & 1); + break; + } + case kUpb_FieldType_Int32: + case kUpb_FieldType_UInt32: + _upb_Decoder_MungeInt32(val); + break; + case kUpb_FieldType_Enum: + UPB_UNREACHABLE(); + } +} + +static upb_Message* _upb_Decoder_NewSubMessage2(upb_Decoder* d, + const upb_MiniTable* subl, + const upb_MiniTableField* field, + upb_Message** target) { + UPB_ASSERT(subl); + upb_Message* msg = _upb_Message_New(subl, &d->arena); + if (!msg) upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + + *target = msg; + return msg; +} + +static upb_Message* _upb_Decoder_NewSubMessage(upb_Decoder* d, + const upb_MiniTableField* field, + upb_Message** target) { + const upb_MiniTable* subl = upb_MiniTable_GetSubMessageTable(field); + return _upb_Decoder_NewSubMessage2(d, subl, field, target); +} + +static const char* _upb_Decoder_ReadString2(upb_Decoder* d, const char* ptr, + int size, upb_StringView* str, + bool validate_utf8) { + if (!_upb_Decoder_ReadString(d, &ptr, size, str, validate_utf8)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_RecurseSubMessage(upb_Decoder* d, const char* ptr, + upb_Message* submsg, + const upb_MiniTable* subl, + uint32_t expected_end_group) { + if (--d->depth < 0) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_MaxDepthExceeded); + } + ptr = _upb_Decoder_DecodeMessage(d, ptr, submsg, subl); + d->depth++; + if (d->end_group != expected_end_group) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); + } + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeSubMessage(upb_Decoder* d, const char* ptr, + upb_Message* submsg, + const upb_MiniTableField* field, + size_t size) { + ptrdiff_t delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, size); + const upb_MiniTable* subl = upb_MiniTable_GetSubMessageTable(field); + UPB_ASSERT(subl); + ptr = _upb_Decoder_RecurseSubMessage(d, ptr, submsg, subl, DECODE_NOGROUP); + upb_EpsCopyInputStream_PopLimit(&d->input, ptr, delta); + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeGroup(upb_Decoder* d, const char* ptr, + upb_Message* submsg, + const upb_MiniTable* subl, + uint32_t number) { + if (upb_EpsCopyInputStream_IsDone(EPS(d), &ptr)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); + } + ptr = _upb_Decoder_RecurseSubMessage(d, ptr, submsg, subl, number); + d->end_group = DECODE_NOGROUP; + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeKnownGroup(upb_Decoder* d, const char* ptr, + upb_Message* submsg, + const upb_MiniTableField* field) { + const upb_MiniTable* subl = upb_MiniTable_GetSubMessageTable(field); + UPB_ASSERT(subl); + return _upb_Decoder_DecodeGroup(d, ptr, submsg, subl, + field->UPB_PRIVATE(number)); +} + +#define kUpb_Decoder_EncodeVarint32MaxSize 5 +static char* upb_Decoder_EncodeVarint32(uint32_t val, char* ptr) { + do { + uint8_t byte = val & 0x7fU; + val >>= 7; + if (val) byte |= 0x80U; + *(ptr++) = byte; + } while (val); + return ptr; +} + +UPB_FORCEINLINE +void _upb_Decoder_AddEnumValueToUnknown(upb_Decoder* d, upb_Message* msg, + const upb_MiniTableField* field, + wireval* val) { + // Unrecognized enum goes into unknown fields. + // For packed fields the tag could be arbitrarily far in the past, + // so we just re-encode the tag and value here. + const uint32_t tag = + ((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint; + upb_Message* unknown_msg = + field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension ? d->original_msg + : msg; + char buf[2 * kUpb_Decoder_EncodeVarint32MaxSize]; + char* end = buf; + end = upb_Decoder_EncodeVarint32(tag, end); + end = upb_Decoder_EncodeVarint32(val->uint64_val, end); + + if (!UPB_PRIVATE(_upb_Message_AddUnknown)(unknown_msg, buf, end - buf, + &d->arena, kUpb_AddUnknown_Copy)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeFixedPacked(upb_Decoder* d, const char* ptr, + upb_Array* arr, wireval* val, + const upb_MiniTableField* field, + int lg2) { + upb_StringView sv; + ptr = upb_EpsCopyInputStream_ReadStringEphemeral(&d->input, ptr, val->size, + &sv); + if (!ptr) upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); + int mask = (1 << lg2) - 1; + if (UPB_UNLIKELY((val->size & mask) != 0 || ptr == NULL)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); + } + size_t count = val->size >> lg2; + if (count == 0) return ptr; + _upb_Decoder_Reserve(d, arr, count); + void* mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr), + arr->UPB_PRIVATE(size) << lg2, void); + arr->UPB_PRIVATE(size) += count; + if (upb_IsLittleEndian()) { + memcpy(mem, sv.data, sv.size); + } else { + const char* src = sv.data; + const char* src_end = src + sv.size; + char* dst = mem; + if (lg2 == 2) { + for (; src < src_end; src += 4, dst += 4) { + uint32_t x; + memcpy(&x, src, 4); + x = upb_BigEndian32(x); + memcpy(dst, &x, 4); + } + } else { + UPB_ASSERT(lg2 == 3); + for (; src < src_end; src += 8, dst += 8) { + uint64_t x; + memcpy(&x, src, 8); + x = upb_BigEndian64(x); + memcpy(dst, &x, 8); + } + } + } + + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeVarintPacked(upb_Decoder* d, const char* ptr, + upb_Array* arr, wireval* val, + const upb_MiniTableField* field, + int lg2) { + int scale = 1 << lg2; + ptrdiff_t delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size); + char* out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr), + arr->UPB_PRIVATE(size) << lg2, void); + while (!upb_EpsCopyInputStream_IsDone(EPS(d), &ptr)) { + wireval elem; + ptr = upb_WireReader_ReadVarint(ptr, &elem.uint64_val, EPS(d)); + _upb_Decoder_Munge(field, &elem); + if (_upb_Decoder_Reserve(d, arr, 1)) { + out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr), + arr->UPB_PRIVATE(size) << lg2, void); + } + arr->UPB_PRIVATE(size)++; + memcpy(out, &elem, scale); + out += scale; + } + upb_EpsCopyInputStream_PopLimit(&d->input, ptr, delta); + return ptr; +} + +UPB_NOINLINE +static const char* _upb_Decoder_DecodeEnumPacked( + upb_Decoder* d, const char* ptr, upb_Message* msg, upb_Array* arr, + const upb_MiniTableField* field, wireval* val) { + const upb_MiniTableEnum* e = upb_MiniTable_GetSubEnumTable(field); + ptrdiff_t delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size); + char* out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr), + arr->UPB_PRIVATE(size) * 4, void); + while (!upb_EpsCopyInputStream_IsDone(EPS(d), &ptr)) { + wireval elem; + ptr = upb_WireReader_ReadVarint(ptr, &elem.uint64_val, EPS(d)); + if (!upb_MiniTableEnum_CheckValue(e, elem.uint64_val)) { + _upb_Decoder_AddEnumValueToUnknown(d, msg, field, &elem); + continue; + } + if (_upb_Decoder_Reserve(d, arr, 1)) { + out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr), + arr->UPB_PRIVATE(size) * 4, void); + } + arr->UPB_PRIVATE(size)++; + memcpy(out, &elem, 4); + out += 4; + } + upb_EpsCopyInputStream_PopLimit(&d->input, ptr, delta); + return ptr; +} + +static upb_Array* _upb_Decoder_CreateArray(upb_Decoder* d, + const upb_MiniTableField* field) { + const upb_FieldType field_type = field->UPB_PRIVATE(descriptortype); + const size_t lg2 = UPB_PRIVATE(_upb_FieldType_SizeLg2)(field_type); + upb_Array* ret = UPB_PRIVATE(_upb_Array_New)(&d->arena, 4, lg2); + if (!ret) upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + return ret; +} + +static const char* _upb_Decoder_DecodeToArray(upb_Decoder* d, const char* ptr, + upb_Message* msg, + const upb_MiniTableField* field, + wireval* val, int op) { + upb_Array** arrp = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void); + upb_Array* arr = *arrp; + void* mem; + + if (arr) { + _upb_Decoder_Reserve(d, arr, 1); + } else { + arr = _upb_Decoder_CreateArray(d, field); + *arrp = arr; + } + + switch (op) { + case kUpb_DecodeOp_Scalar1Byte: + case kUpb_DecodeOp_Scalar4Byte: + case kUpb_DecodeOp_Scalar8Byte: + /* Append scalar value. */ + mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr), + arr->UPB_PRIVATE(size) << op, void); + arr->UPB_PRIVATE(size)++; + memcpy(mem, val, 1 << op); + return ptr; + case kUpb_DecodeOp_String: { + /* Append string. */ + upb_StringView* str = (upb_StringView*)upb_Array_MutableDataPtr(arr) + + arr->UPB_PRIVATE(size); + ptr = _upb_Decoder_ReadString2(d, ptr, val->size, str, + /*validate_utf8=*/true); + arr->UPB_PRIVATE(size)++; + return ptr; + } + case kUpb_DecodeOp_Bytes: { + /* Append bytes. */ + upb_StringView* str = (upb_StringView*)upb_Array_MutableDataPtr(arr) + + arr->UPB_PRIVATE(size); + ptr = _upb_Decoder_ReadString2(d, ptr, val->size, str, + /*validate_utf8=*/false); + arr->UPB_PRIVATE(size)++; + return ptr; + } + case kUpb_DecodeOp_SubMessage: { + /* Append submessage / group. */ + upb_Message** target = + UPB_PTR_AT(upb_Array_MutableDataPtr(arr), + arr->UPB_PRIVATE(size) * sizeof(void*), upb_Message*); + upb_Message* submsg = _upb_Decoder_NewSubMessage(d, field, target); + arr->UPB_PRIVATE(size)++; + if (UPB_UNLIKELY(field->UPB_PRIVATE(descriptortype) == + kUpb_FieldType_Group)) { + return _upb_Decoder_DecodeKnownGroup(d, ptr, submsg, field); + } else { + return _upb_Decoder_DecodeSubMessage(d, ptr, submsg, field, val->size); + } + } + case OP_FIXPCK_LG2(2): + case OP_FIXPCK_LG2(3): + return _upb_Decoder_DecodeFixedPacked(d, ptr, arr, val, field, + op - OP_FIXPCK_LG2(0)); + case OP_VARPCK_LG2(0): + case OP_VARPCK_LG2(2): + case OP_VARPCK_LG2(3): + return _upb_Decoder_DecodeVarintPacked(d, ptr, arr, val, field, + op - OP_VARPCK_LG2(0)); + case kUpb_DecodeOp_PackedEnum: + return _upb_Decoder_DecodeEnumPacked(d, ptr, msg, arr, field, val); + default: + UPB_UNREACHABLE(); + } +} + +static upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d, + const upb_MiniTable* entry) { + // Maps descriptor type -> upb map size + static const uint8_t kSizeInMap[] = { + [0] = -1, // invalid descriptor type + [kUpb_FieldType_Double] = 8, + [kUpb_FieldType_Float] = 4, + [kUpb_FieldType_Int64] = 8, + [kUpb_FieldType_UInt64] = 8, + [kUpb_FieldType_Int32] = 4, + [kUpb_FieldType_Fixed64] = 8, + [kUpb_FieldType_Fixed32] = 4, + [kUpb_FieldType_Bool] = 1, + [kUpb_FieldType_String] = UPB_MAPTYPE_STRING, + [kUpb_FieldType_Group] = sizeof(void*), + [kUpb_FieldType_Message] = sizeof(void*), + [kUpb_FieldType_Bytes] = UPB_MAPTYPE_STRING, + [kUpb_FieldType_UInt32] = 4, + [kUpb_FieldType_Enum] = 4, + [kUpb_FieldType_SFixed32] = 4, + [kUpb_FieldType_SFixed64] = 8, + [kUpb_FieldType_SInt32] = 4, + [kUpb_FieldType_SInt64] = 8, + }; + + const upb_MiniTableField* key_field = &entry->UPB_PRIVATE(fields)[0]; + const upb_MiniTableField* val_field = &entry->UPB_PRIVATE(fields)[1]; + char key_size = kSizeInMap[key_field->UPB_PRIVATE(descriptortype)]; + char val_size = kSizeInMap[val_field->UPB_PRIVATE(descriptortype)]; + UPB_ASSERT(key_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntry, k)); + UPB_ASSERT(val_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntry, v)); + upb_Map* ret = _upb_Map_New(&d->arena, key_size, val_size); + if (!ret) upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + return ret; +} + +UPB_NOINLINE static void _upb_Decoder_AddMapEntryUnknown( + upb_Decoder* d, upb_Message* msg, const upb_MiniTableField* field, + upb_Message* ent_msg, const upb_MiniTable* entry) { + char* buf; + size_t size; + upb_EncodeStatus status = + upb_Encode(ent_msg, entry, 0, &d->arena, &buf, &size); + if (status != kUpb_EncodeStatus_Ok) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } + char delim_buf[2 * kUpb_Decoder_EncodeVarint32MaxSize]; + char* delim_end = delim_buf; + uint32_t tag = + ((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Delimited; + delim_end = upb_Decoder_EncodeVarint32(tag, delim_end); + delim_end = upb_Decoder_EncodeVarint32(size, delim_end); + upb_StringView unknown[] = { + {delim_buf, delim_end - delim_buf}, + {buf, size}, + }; + + if (!UPB_PRIVATE(_upb_Message_AddUnknownV)(msg, &d->arena, unknown, 2)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } +} + +static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr, + upb_Message* msg, + const upb_MiniTableField* field, + wireval* val) { + upb_Map** map_p = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), upb_Map*); + upb_Map* map = *map_p; + upb_MapEntry ent; + UPB_ASSERT(upb_MiniTableField_Type(field) == kUpb_FieldType_Message); + const upb_MiniTable* entry = upb_MiniTable_GetSubMessageTable(field); + + UPB_ASSERT(entry); + UPB_ASSERT(entry->UPB_PRIVATE(field_count) == 2); + UPB_ASSERT(upb_MiniTableField_IsScalar(&entry->UPB_PRIVATE(fields)[0])); + UPB_ASSERT(upb_MiniTableField_IsScalar(&entry->UPB_PRIVATE(fields)[1])); + + if (!map) { + map = _upb_Decoder_CreateMap(d, entry); + *map_p = map; + } + + // Parse map entry. + memset(&ent, 0, sizeof(ent)); + + bool value_is_message = + entry->UPB_PRIVATE(fields)[1].UPB_PRIVATE(descriptortype) == + kUpb_FieldType_Message || + entry->UPB_PRIVATE(fields)[1].UPB_PRIVATE(descriptortype) == + kUpb_FieldType_Group; + const upb_MiniTable* sub_table = + value_is_message + ? upb_MiniTable_GetSubMessageTable(&entry->UPB_PRIVATE(fields)[1]) + : NULL; + upb_Message* sub_msg = NULL; + + if (sub_table) { + // Create proactively to handle the case where it doesn't appear. + _upb_Decoder_NewSubMessage(d, &entry->UPB_PRIVATE(fields)[1], &sub_msg); + ent.v.val = upb_value_ptr(sub_msg); + } + + ptr = _upb_Decoder_DecodeSubMessage(d, ptr, &ent.message, field, val->size); + + if (sub_msg && sub_table->UPB_PRIVATE(required_count)) { + // If the map entry did not contain a value on the wire, `sub_msg` is an + // empty message; we must check if it is missing any required fields. If the + // value was present, this check is redundant but harmless. + _upb_Decoder_CheckRequired(d, ptr, sub_msg, sub_table); + } + + if (upb_Message_HasUnknown(&ent.message)) { + _upb_Decoder_AddMapEntryUnknown(d, msg, field, &ent.message, entry); + } else { + if (_upb_Map_Insert(map, &ent.k, map->key_size, &ent.v, map->val_size, + &d->arena) == kUpb_MapInsertStatus_OutOfMemory) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } + } + return ptr; +} + +static const char* _upb_Decoder_DecodeToSubMessage( + upb_Decoder* d, const char* ptr, upb_Message* msg, + const upb_MiniTableField* field, wireval* val, int op) { + void* mem = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void); + int type = field->UPB_PRIVATE(descriptortype); + + // Set presence if necessary. + if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(field)) { + UPB_PRIVATE(_upb_Message_SetHasbit)(msg, field); + } else if (upb_MiniTableField_IsInOneof(field)) { + // Oneof case + uint32_t* oneof_case = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, field); + if (op == kUpb_DecodeOp_SubMessage && + *oneof_case != field->UPB_PRIVATE(number)) { + memset(mem, 0, sizeof(void*)); + } + *oneof_case = field->UPB_PRIVATE(number); + } + + // Store into message. + switch (op) { + case kUpb_DecodeOp_SubMessage: { + upb_Message** submsgp = mem; + upb_Message* submsg = *submsgp; + if (!submsg) submsg = _upb_Decoder_NewSubMessage(d, field, submsgp); + if (UPB_UNLIKELY(type == kUpb_FieldType_Group)) { + ptr = _upb_Decoder_DecodeKnownGroup(d, ptr, submsg, field); + } else { + ptr = _upb_Decoder_DecodeSubMessage(d, ptr, submsg, field, val->size); + } + break; + } + case kUpb_DecodeOp_String: + return _upb_Decoder_ReadString2(d, ptr, val->size, mem, + /*validate_utf8=*/true); + case kUpb_DecodeOp_Bytes: + return _upb_Decoder_ReadString2(d, ptr, val->size, mem, + /*validate_utf8=*/false); + case kUpb_DecodeOp_Scalar8Byte: + memcpy(mem, val, 8); + break; + case kUpb_DecodeOp_Scalar4Byte: + memcpy(mem, val, 4); + break; + case kUpb_DecodeOp_Scalar1Byte: + memcpy(mem, val, 1); + break; + default: + UPB_UNREACHABLE(); + } + + return ptr; +} + +enum { + kStartItemTag = ((kUpb_MsgSet_Item << 3) | kUpb_WireType_StartGroup), + kEndItemTag = ((kUpb_MsgSet_Item << 3) | kUpb_WireType_EndGroup), + kTypeIdTag = ((kUpb_MsgSet_TypeId << 3) | kUpb_WireType_Varint), + kMessageTag = ((kUpb_MsgSet_Message << 3) | kUpb_WireType_Delimited), +}; + +static void upb_Decoder_AddKnownMessageSetItem( + upb_Decoder* d, upb_Message* msg, const upb_MiniTableExtension* item_mt, + const char* data, uint32_t size) { + upb_Extension* ext = + UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, item_mt, &d->arena); + if (UPB_UNLIKELY(!ext)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } + upb_Message** submsgp = (upb_Message**)&ext->data.msg_val; + upb_Message* submsg = _upb_Decoder_NewSubMessage2( + d, ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg), + &ext->ext->UPB_PRIVATE(field), submsgp); + // upb_Decode_LimitDepth() takes uint32_t, d->depth - 1 can not be negative. + if (d->depth <= 1) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_MaxDepthExceeded); + } + upb_DecodeStatus status = upb_Decode( + data, size, submsg, upb_MiniTableExtension_GetSubMessage(item_mt), + d->extreg, upb_Decode_LimitDepth(d->options, d->depth - 1), &d->arena); + if (status != kUpb_DecodeStatus_Ok) { + upb_ErrorHandler_ThrowError(&d->err, status); + } +} + +static void upb_Decoder_AddUnknownMessageSetItem(upb_Decoder* d, + upb_Message* msg, + uint32_t type_id, + const char* message_data, + uint32_t message_size) { + char buf[6 * kUpb_Decoder_EncodeVarint32MaxSize]; + char* ptr = buf; + ptr = upb_Decoder_EncodeVarint32(kStartItemTag, ptr); + ptr = upb_Decoder_EncodeVarint32(kTypeIdTag, ptr); + ptr = upb_Decoder_EncodeVarint32(type_id, ptr); + ptr = upb_Decoder_EncodeVarint32(kMessageTag, ptr); + ptr = upb_Decoder_EncodeVarint32(message_size, ptr); + char* split = ptr; + + ptr = upb_Decoder_EncodeVarint32(kEndItemTag, ptr); + char* end = ptr; + upb_StringView unknown[] = { + {buf, split - buf}, + {message_data, message_size}, + {split, end - split}, + }; + if (!UPB_PRIVATE(_upb_Message_AddUnknownV)(msg, &d->arena, unknown, 3)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } +} + +static void upb_Decoder_AddMessageSetItem(upb_Decoder* d, upb_Message* msg, + const upb_MiniTable* t, + uint32_t type_id, const char* data, + uint32_t size) { + const upb_MiniTableExtension* item_mt = + upb_ExtensionRegistry_Lookup(d->extreg, t, type_id); + if (item_mt) { + upb_Decoder_AddKnownMessageSetItem(d, msg, item_mt, data, size); + } else { + upb_Decoder_AddUnknownMessageSetItem(d, msg, type_id, data, size); + } +} + +static const char* upb_Decoder_DecodeMessageSetItem( + upb_Decoder* d, const char* ptr, upb_Message* msg, + const upb_MiniTable* layout) { + uint32_t type_id = 0; + upb_StringView preserved = {NULL, 0}; + typedef enum { + kUpb_HaveId = 1 << 0, + kUpb_HavePayload = 1 << 1, + } StateMask; + StateMask state_mask = 0; + while (!upb_EpsCopyInputStream_IsDone(EPS(d), &ptr)) { + uint32_t tag; + ptr = upb_WireReader_ReadTag(ptr, &tag, EPS(d)); + switch (tag) { + case kEndItemTag: + return ptr; + case kTypeIdTag: { + uint64_t tmp; + ptr = upb_WireReader_ReadVarint(ptr, &tmp, EPS(d)); + if (state_mask & kUpb_HaveId) break; // Ignore dup. + state_mask |= kUpb_HaveId; + type_id = tmp; + if (state_mask & kUpb_HavePayload) { + upb_Decoder_AddMessageSetItem(d, msg, layout, type_id, preserved.data, + preserved.size); + } + break; + } + case kMessageTag: { + uint32_t size; + upb_StringView sv; + ptr = upb_Decoder_DecodeSize(d, ptr, &size); + ptr = upb_EpsCopyInputStream_ReadStringAlwaysAlias(&d->input, ptr, size, + &sv); + if (!ptr) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); + } + if (state_mask & kUpb_HavePayload) break; // Ignore dup. + state_mask |= kUpb_HavePayload; + if (state_mask & kUpb_HaveId) { + upb_Decoder_AddMessageSetItem(d, msg, layout, type_id, sv.data, + sv.size); + } else { + // Out of order, we must preserve the payload. + preserved = sv; + } + break; + } + default: + // We do not preserve unexpected fields inside a message set item. + ptr = _upb_WireReader_SkipValue(ptr, tag, d->depth, &d->input); + break; + } + } + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); +} + +static upb_MiniTableField upb_Decoder_FieldNotFoundField = { + 0, 0, 0, 0, kUpb_FakeFieldType_FieldNotFound, 0}; + +UPB_NOINLINE const upb_MiniTableField* _upb_Decoder_FindExtensionField( + upb_Decoder* d, const upb_MiniTable* t, uint32_t field_number, int ext_mode, + uint32_t wire_type) { + // Treat a message set as an extendable message if it is a delimited field. + // This provides compatibility with encoders that are unaware of message + // sets and serialize them as normal extensions. + if (ext_mode == kUpb_ExtMode_Extendable || + (ext_mode == kUpb_ExtMode_IsMessageSet && + wire_type == kUpb_WireType_Delimited)) { + const upb_MiniTableExtension* ext = + upb_ExtensionRegistry_Lookup(d->extreg, t, field_number); + if (ext) return &ext->UPB_PRIVATE(field); + } else if (ext_mode == kUpb_ExtMode_IsMessageSet) { + if (field_number == kUpb_MsgSet_Item) { + static upb_MiniTableField item = { + 0, 0, 0, 0, kUpb_FakeFieldType_MessageSetItem, 0}; + return &item; + } + } + return &upb_Decoder_FieldNotFoundField; +} + +static const upb_MiniTableField* _upb_Decoder_FindField(upb_Decoder* d, + const upb_MiniTable* t, + uint32_t field_number, + uint32_t wire_type) { + UPB_ASSERT(t); + const upb_MiniTableField* field = + upb_MiniTable_FindFieldByNumber(t, field_number); + if (field) return field; + + if (d->extreg && t->UPB_PRIVATE(ext)) { + return _upb_Decoder_FindExtensionField(d, t, field_number, + t->UPB_PRIVATE(ext), wire_type); + } + + return &upb_Decoder_FieldNotFoundField; // Unknown field. +} + +static int _upb_Decoder_GetVarintOp(const upb_MiniTableField* field) { + static const int8_t kVarintOps[] = { + [kUpb_FakeFieldType_FieldNotFound] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Double] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Float] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Int64] = kUpb_DecodeOp_Scalar8Byte, + [kUpb_FieldType_UInt64] = kUpb_DecodeOp_Scalar8Byte, + [kUpb_FieldType_Int32] = kUpb_DecodeOp_Scalar4Byte, + [kUpb_FieldType_Fixed64] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Fixed32] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Bool] = kUpb_DecodeOp_Scalar1Byte, + [kUpb_FieldType_String] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Group] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Message] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Bytes] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_UInt32] = kUpb_DecodeOp_Scalar4Byte, + [kUpb_FieldType_Enum] = kUpb_DecodeOp_Scalar4Byte, + [kUpb_FieldType_SFixed32] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_SFixed64] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_SInt32] = kUpb_DecodeOp_Scalar4Byte, + [kUpb_FieldType_SInt64] = kUpb_DecodeOp_Scalar8Byte, + [kUpb_FakeFieldType_MessageSetItem] = kUpb_DecodeOp_UnknownField, + }; + + return kVarintOps[field->UPB_PRIVATE(descriptortype)]; +} + +UPB_FORCEINLINE +void _upb_Decoder_CheckUnlinked(upb_Decoder* d, const upb_MiniTable* mt, + const upb_MiniTableField* field, int* op) { + // If sub-message is not linked, treat as unknown. + if (field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension) return; + const upb_MiniTable* mt_sub = upb_MiniTable_GetSubMessageTable(field); + if (mt_sub != NULL) return; // Normal case, sub-message is linked. +#ifndef NDEBUG + const upb_MiniTableField* oneof = upb_MiniTable_GetOneof(mt, field); + if (oneof) { + // All other members of the oneof must be message fields that are also + // unlinked. + do { + UPB_ASSERT(upb_MiniTableField_CType(oneof) == kUpb_CType_Message); + const upb_MiniTable* oneof_sub = upb_MiniTable_GetSubMessageTable(oneof); + UPB_ASSERT(!oneof_sub); + } while (upb_MiniTable_NextOneofField(mt, &oneof)); + } +#endif // NDEBUG + *op = kUpb_DecodeOp_UnknownField; +} + +UPB_FORCEINLINE +void _upb_Decoder_MaybeVerifyUtf8(upb_Decoder* d, + const upb_MiniTableField* field, int* op) { + UPB_ASSUME(field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bytes); + if (_upb_Decoder_FieldRequiresUtf8Validation(d, field)) { + *op = kUpb_DecodeOp_String; + } +} + +static int _upb_Decoder_GetDelimitedOp(upb_Decoder* d, const upb_MiniTable* mt, + const upb_MiniTableField* field) { + enum { kRepeatedBase = 19 }; + + static const int8_t kDelimitedOps[] = { + // For non-repeated field type. + [kUpb_FakeFieldType_FieldNotFound] = + kUpb_DecodeOp_UnknownField, // Field not found. + [kUpb_FieldType_Double] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Float] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Int64] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_UInt64] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Int32] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Fixed64] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Fixed32] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Bool] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_String] = kUpb_DecodeOp_String, + [kUpb_FieldType_Group] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Message] = kUpb_DecodeOp_SubMessage, + [kUpb_FieldType_Bytes] = kUpb_DecodeOp_Bytes, + [kUpb_FieldType_UInt32] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_Enum] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_SFixed32] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_SFixed64] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_SInt32] = kUpb_DecodeOp_UnknownField, + [kUpb_FieldType_SInt64] = kUpb_DecodeOp_UnknownField, + [kUpb_FakeFieldType_MessageSetItem] = kUpb_DecodeOp_SubMessage, + // For repeated field type. + [kRepeatedBase + kUpb_FieldType_Double] = OP_FIXPCK_LG2(3), + [kRepeatedBase + kUpb_FieldType_Float] = OP_FIXPCK_LG2(2), + [kRepeatedBase + kUpb_FieldType_Int64] = OP_VARPCK_LG2(3), + [kRepeatedBase + kUpb_FieldType_UInt64] = OP_VARPCK_LG2(3), + [kRepeatedBase + kUpb_FieldType_Int32] = OP_VARPCK_LG2(2), + [kRepeatedBase + kUpb_FieldType_Fixed64] = OP_FIXPCK_LG2(3), + [kRepeatedBase + kUpb_FieldType_Fixed32] = OP_FIXPCK_LG2(2), + [kRepeatedBase + kUpb_FieldType_Bool] = OP_VARPCK_LG2(0), + [kRepeatedBase + kUpb_FieldType_String] = kUpb_DecodeOp_String, + [kRepeatedBase + kUpb_FieldType_Group] = kUpb_DecodeOp_SubMessage, + [kRepeatedBase + kUpb_FieldType_Message] = kUpb_DecodeOp_SubMessage, + [kRepeatedBase + kUpb_FieldType_Bytes] = kUpb_DecodeOp_Bytes, + [kRepeatedBase + kUpb_FieldType_UInt32] = OP_VARPCK_LG2(2), + [kRepeatedBase + kUpb_FieldType_Enum] = kUpb_DecodeOp_PackedEnum, + [kRepeatedBase + kUpb_FieldType_SFixed32] = OP_FIXPCK_LG2(2), + [kRepeatedBase + kUpb_FieldType_SFixed64] = OP_FIXPCK_LG2(3), + [kRepeatedBase + kUpb_FieldType_SInt32] = OP_VARPCK_LG2(2), + [kRepeatedBase + kUpb_FieldType_SInt64] = OP_VARPCK_LG2(3), + // Omitting kUpb_FakeFieldType_MessageSetItem, because we never emit a + // repeated msgset type + }; + + int ndx = field->UPB_PRIVATE(descriptortype); + if (upb_MiniTableField_IsArray(field)) ndx += kRepeatedBase; + int op = kDelimitedOps[ndx]; + + if (op == kUpb_DecodeOp_SubMessage) { + _upb_Decoder_CheckUnlinked(d, mt, field, &op); + } else if (op == kUpb_DecodeOp_Bytes) { + _upb_Decoder_MaybeVerifyUtf8(d, field, &op); + } + + return op; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeWireValue(upb_Decoder* d, const char* ptr, + const upb_MiniTable* mt, + const upb_MiniTableField* field, + uint32_t wire_type, wireval* val, + int* op) { + static const unsigned kFixed32OkMask = (1 << kUpb_FieldType_Float) | + (1 << kUpb_FieldType_Fixed32) | + (1 << kUpb_FieldType_SFixed32); + + static const unsigned kFixed64OkMask = (1 << kUpb_FieldType_Double) | + (1 << kUpb_FieldType_Fixed64) | + (1 << kUpb_FieldType_SFixed64); + + switch (wire_type) { + case kUpb_WireType_Varint: + ptr = upb_WireReader_ReadVarint(ptr, &val->uint64_val, EPS(d)); + if (upb_MiniTableField_IsClosedEnum(field)) { + const upb_MiniTableEnum* e = upb_MiniTable_GetSubEnumTable(field); + if (!upb_MiniTableEnum_CheckValue(e, val->uint64_val)) { + *op = kUpb_DecodeOp_UnknownField; + return ptr; + } + _upb_Decoder_MungeInt32(val); + } else { + _upb_Decoder_Munge(field, val); + } + *op = _upb_Decoder_GetVarintOp(field); + return ptr; + case kUpb_WireType_32Bit: + *op = kUpb_DecodeOp_Scalar4Byte; + if (((1 << field->UPB_PRIVATE(descriptortype)) & kFixed32OkMask) == 0) { + *op = kUpb_DecodeOp_UnknownField; + } + return upb_WireReader_ReadFixed32(ptr, &val->uint32_val, &d->input); + case kUpb_WireType_64Bit: + *op = kUpb_DecodeOp_Scalar8Byte; + if (((1 << field->UPB_PRIVATE(descriptortype)) & kFixed64OkMask) == 0) { + *op = kUpb_DecodeOp_UnknownField; + } + return upb_WireReader_ReadFixed64(ptr, &val->uint64_val, &d->input); + case kUpb_WireType_Delimited: + ptr = upb_Decoder_DecodeSize(d, ptr, &val->size); + *op = _upb_Decoder_GetDelimitedOp(d, mt, field); + return ptr; + case kUpb_WireType_StartGroup: + val->uint32_val = field->UPB_PRIVATE(number); + if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group) { + *op = kUpb_DecodeOp_SubMessage; + _upb_Decoder_CheckUnlinked(d, mt, field, op); + } else if (field->UPB_PRIVATE(descriptortype) == + kUpb_FakeFieldType_MessageSetItem) { + *op = kUpb_DecodeOp_MessageSetItem; + } else { + *op = kUpb_DecodeOp_UnknownField; + } + return ptr; + default: + break; + } + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeKnownField(upb_Decoder* d, const char* ptr, + upb_Message* msg, + const upb_MiniTableField* field, + int op, wireval* val) { + uint8_t mode = field->UPB_PRIVATE(mode); + + if (UPB_UNLIKELY(mode & kUpb_LabelFlags_IsExtension)) { + const upb_MiniTableExtension* ext_layout = + (const upb_MiniTableExtension*)field; + upb_Extension* ext = UPB_PRIVATE(_upb_Message_GetOrCreateExtension)( + msg, ext_layout, &d->arena); + if (UPB_UNLIKELY(!ext)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } + d->original_msg = msg; + msg = &ext->data.UPB_PRIVATE(ext_msg_val); + } + + switch (mode & kUpb_FieldMode_Mask) { + case kUpb_FieldMode_Array: + return _upb_Decoder_DecodeToArray(d, ptr, msg, field, val, op); + case kUpb_FieldMode_Map: + return _upb_Decoder_DecodeToMap(d, ptr, msg, field, val); + case kUpb_FieldMode_Scalar: + return _upb_Decoder_DecodeToSubMessage(d, ptr, msg, field, val, op); + default: + UPB_UNREACHABLE(); + } +} + +static const char* _upb_Decoder_DecodeUnknownField( + upb_Decoder* d, const char* ptr, upb_Message* msg, uint32_t field_number, + uint32_t wire_type, wireval val, const char* start) { + if (field_number == 0) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); + } + + upb_EpsCopyInputStream_StartCapture(&d->input, start); + + if (wire_type == kUpb_WireType_Delimited) { + upb_StringView sv; + ptr = upb_EpsCopyInputStream_ReadStringEphemeral(&d->input, ptr, val.size, + &sv); + if (!ptr) upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_Malformed); + } else if (wire_type == kUpb_WireType_StartGroup) { + ptr = UPB_PRIVATE(_upb_WireReader_SkipGroup)(ptr, field_number << 3, + d->depth, &d->input); + } + + upb_StringView sv; + upb_EpsCopyInputStream_EndCapture(&d->input, ptr, &sv); + + upb_AddUnknownMode mode = kUpb_AddUnknown_Copy; + if (d->options & kUpb_DecodeOption_AliasString) { + if (sv.data != d->input.buffer_start) { + // If the data is not from the beginning of the input buffer, then we can + // safely attempt to coalesce this region with the previous one. + mode = kUpb_AddUnknown_AliasAllowMerge; + } else { + mode = kUpb_AddUnknown_Alias; + } + } + + if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, sv.data, sv.size, &d->arena, + mode)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_OutOfMemory); + } + + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeFieldTag(upb_Decoder* d, const char* ptr, + uint32_t* field_number, + uint32_t* wire_type) { + uint32_t tag; + UPB_ASSERT(ptr < d->input.limit_ptr); + ptr = upb_WireReader_ReadTag(ptr, &tag, EPS(d)); + *field_number = tag >> 3; + *wire_type = tag & 7; + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeFieldData( + upb_Decoder* d, const char* ptr, upb_Message* msg, const upb_MiniTable* mt, + uint32_t field_number, uint32_t wire_type, const char* start) { + int op; + wireval val; + + const upb_MiniTableField* field = + _upb_Decoder_FindField(d, mt, field_number, wire_type); + ptr = _upb_Decoder_DecodeWireValue(d, ptr, mt, field, wire_type, &val, &op); + + if (op >= 0) { + return _upb_Decoder_DecodeKnownField(d, ptr, msg, field, op, &val); + } else { + switch (op) { + case kUpb_DecodeOp_UnknownField: + return _upb_Decoder_DecodeUnknownField(d, ptr, msg, field_number, + wire_type, val, start); + case kUpb_DecodeOp_MessageSetItem: + return upb_Decoder_DecodeMessageSetItem(d, ptr, msg, mt); + default: + UPB_UNREACHABLE(); + } + } +} + +static const char* _upb_Decoder_EndMessage(upb_Decoder* d, const char* ptr) { + d->message_is_done = true; + return ptr; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeFieldNoFast(upb_Decoder* d, const char* ptr, + upb_Message* msg, + const upb_MiniTable* mt) { + uint32_t field_number; + uint32_t wire_type; + + const char* start = ptr; + ptr = _upb_Decoder_DecodeFieldTag(d, ptr, &field_number, &wire_type); + + if (wire_type == kUpb_WireType_EndGroup) { + d->end_group = field_number; + return _upb_Decoder_EndMessage(d, ptr); + } + + ptr = _upb_Decoder_DecodeFieldData(d, ptr, msg, mt, field_number, wire_type, + start); + _upb_Decoder_Trace(d, 'M'); + return ptr; +} + +UPB_FORCEINLINE +bool _upb_Decoder_TryDecodeMessageFast(upb_Decoder* d, const char** ptr, + upb_Message* msg, + const upb_MiniTable* mt, + uint64_t last_field_index, + uint64_t data) { +#ifdef UPB_ENABLE_FASTTABLE + if (mt->UPB_PRIVATE(table_mask) == (unsigned char)-1 || + (d->options & kUpb_DecodeOption_DisableFastTable)) { + // Fast table is unavailable or disabled. + return false; + } + + intptr_t table = decode_totable(mt); + const char* start = *ptr; + char* trace_next = _upb_Decoder_TraceNext(d); + + *ptr = upb_DecodeFast_Dispatch(d, *ptr, msg, table, 0, 0); + + if (d->message_is_done) { + // The entire message was successfully parsed fast. + return true; + } + + // *ptr now points to the beginning of a field that could not be parsed fast. + // It's possible that some fields were parsed fast, in which case *ptr will + // have been updated. However, it's also possible that the very first field + // encountered could not be parsed fast, in which case *ptr will be unchanged. + // + // If the fast decoder consumed any data, it must have emitted at least + // one 'F' event into the trace buffer (in addition to the 'D' event + // that is always emitted). + UPB_ASSERT(_upb_Decoder_TracePtr(d) != trace_next || *ptr == start); + _upb_Decoder_Trace(d, '<'); +#endif + return false; +} + +UPB_FORCEINLINE +const char* _upb_Decoder_DecodeField(upb_Decoder* d, const char* ptr, + upb_Message* msg, const upb_MiniTable* mt, + uint64_t last_field_index, uint64_t data) { + if (_upb_Decoder_TryDecodeMessageFast(d, &ptr, msg, mt, last_field_index, + data)) { + return ptr; + } else if (upb_EpsCopyInputStream_IsDone(EPS(d), &ptr)) { + return _upb_Decoder_EndMessage(d, ptr); + } + + return _upb_Decoder_DecodeFieldNoFast(d, ptr, msg, mt); +} + +UPB_NOINLINE +const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr, + upb_Message* msg, + const upb_MiniTable* mt) { + UPB_ASSERT(mt); + UPB_ASSERT(d->message_is_done == false); + + do { + ptr = _upb_Decoder_DecodeField(d, ptr, msg, mt, 0, 0); + } while (!d->message_is_done); + d->message_is_done = false; + + return UPB_UNLIKELY(mt && mt->UPB_PRIVATE(required_count)) + ? _upb_Decoder_CheckRequired(d, ptr, msg, mt) + : ptr; +} + +static upb_DecodeStatus _upb_Decoder_DecodeTop(struct upb_Decoder* d, + const char* buf, + upb_Message* msg, + const upb_MiniTable* m) { + _upb_Decoder_DecodeMessage(d, buf, msg, m); + if (d->end_group != DECODE_NOGROUP) return kUpb_DecodeStatus_Malformed; + if (d->missing_required) return kUpb_DecodeStatus_MissingRequired; + return kUpb_DecodeStatus_Ok; +} + +static upb_DecodeStatus upb_Decoder_Decode(upb_Decoder* const decoder, + const char* const buf, + upb_Message* const msg, + const upb_MiniTable* const m, + upb_Arena* const arena) { + if (UPB_SETJMP(decoder->err.buf) == 0) { + decoder->err.code = _upb_Decoder_DecodeTop(decoder, buf, msg, m); + } else { + UPB_ASSERT(decoder->err.code != kUpb_DecodeStatus_Ok); + } + + return upb_Decoder_Destroy(decoder, arena); +} + +static uint16_t upb_DecodeOptions_GetMaxDepth(uint32_t options) { + return options >> 16; +} + +uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options) { + uint16_t max_depth = upb_DecodeOptions_GetMaxDepth(options); + return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit; +} + +upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg, + const upb_MiniTable* mt, + const upb_ExtensionRegistry* extreg, int options, + upb_Arena* arena) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Decoder decoder; + buf = upb_Decoder_Init(&decoder, buf, size, extreg, options, arena, NULL, 0); + + return upb_Decoder_Decode(&decoder, buf, msg, mt, arena); +} + +upb_DecodeStatus upb_DecodeWithTrace(const char* buf, size_t size, + upb_Message* msg, const upb_MiniTable* mt, + const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena, + char* trace_buf, size_t trace_size) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Decoder decoder; + buf = upb_Decoder_Init(&decoder, buf, size, extreg, options, arena, trace_buf, + trace_size); + + return upb_Decoder_Decode(&decoder, buf, msg, mt, arena); +} + +upb_DecodeStatus upb_DecodeLengthPrefixed(const char* buf, size_t size, + upb_Message* msg, + size_t* num_bytes_read, + const upb_MiniTable* mt, + const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + // To avoid needing to make a Decoder just to decode the initial length, + // hand-decode the leading varint for the message length here. + uint64_t msg_len = 0; + for (size_t i = 0;; ++i) { + if (i >= size || i > 9) { + return kUpb_DecodeStatus_Malformed; + } + uint64_t b = *buf; + buf++; + msg_len += (b & 0x7f) << (i * 7); + if ((b & 0x80) == 0) { + *num_bytes_read = i + 1 + msg_len; + break; + } + } + + // If the total number of bytes we would read (= the bytes from the varint + // plus however many bytes that varint says we should read) is larger then the + // input buffer then error as malformed. + if (*num_bytes_read > size) { + return kUpb_DecodeStatus_Malformed; + } + if (msg_len > INT32_MAX) { + return kUpb_DecodeStatus_Malformed; + } + + return upb_Decode(buf, msg_len, msg, mt, extreg, options, arena); +} + +const char* upb_DecodeStatus_String(upb_DecodeStatus status) { + switch (status) { + case kUpb_DecodeStatus_Ok: + return "Ok"; + case kUpb_DecodeStatus_Malformed: + return "Wire format was corrupt"; + case kUpb_DecodeStatus_OutOfMemory: + return "Arena alloc failed"; + case kUpb_DecodeStatus_BadUtf8: + return "String field had bad UTF-8"; + case kUpb_DecodeStatus_MaxDepthExceeded: + return "Exceeded upb_DecodeOptions_MaxDepth"; + case kUpb_DecodeStatus_MissingRequired: + return "Missing required field"; + default: + return "Unknown decode status"; + } +} + +#undef OP_FIXPCK_LG2 +#undef OP_VARPCK_LG2 + +// We encode backwards, to avoid pre-computing lengths (one-pass encode). + + +#include +#include +#include +#include +#include + + +// Must be last. + +static uint32_t encode_zz32(int32_t n) { + return ((uint32_t)n << 1) ^ (n >> 31); +} +static uint64_t encode_zz64(int64_t n) { + return ((uint64_t)n << 1) ^ (n >> 63); +} + +typedef struct { + upb_EncodeStatus status; + jmp_buf err; + upb_Arena* arena; + // These should only be used for arithmetic and reallocation to allow full + // aliasing analysis on the ptr argument. + const char UPB_NODEREF *buf, *limit; + int options; + int depth; + _upb_mapsorter sorter; +} upb_encstate; + +UPB_NORETURN static void encode_err(upb_encstate* e, upb_EncodeStatus s) { + UPB_ASSERT(s != kUpb_EncodeStatus_Ok); + e->status = s; + UPB_LONGJMP(e->err, 1); +} + +// Subtraction is used for bounds checks, and the C standard says that pointer +// subtraction is UB if the pointers aren't part of the same array or one past +// the end, so we must avoid NULL - NULL. C++ defines it though. +static char initial_buf_sentinel; + +UPB_NOINLINE static char* encode_growbuffer(char* ptr, upb_encstate* e, + size_t bytes) { + size_t old_size = e->limit - e->buf; + size_t needed_size = bytes + (e->limit - ptr); + if (needed_size < bytes) encode_err(e, kUpb_EncodeStatus_OutOfMemory); + size_t new_size = upb_RoundUpToPowerOfTwo(UPB_MAX(128, needed_size)); + if (new_size == old_size) encode_err(e, kUpb_EncodeStatus_OutOfMemory); + void* old_buf = e->buf == &initial_buf_sentinel ? NULL : (void*)e->buf; + char* new_buf = upb_Arena_Realloc(e->arena, old_buf, old_size, new_size); + + if (!new_buf) encode_err(e, kUpb_EncodeStatus_OutOfMemory); + + // We want previous data at the end, realloc() put it at the beginning. + // TODO: This is somewhat inefficient since we are copying twice. + // Maybe create a realloc() that copies to the end of the new buffer? + if (old_size > 0) { + memmove(new_buf + new_size - old_size, new_buf, old_size); + } + + e->buf = new_buf; + e->limit = new_buf + new_size; + return new_buf + new_size - needed_size; +} + +/* Call to ensure that at least `bytes` bytes are available for writing at + * ptr. */ +UPB_FORCEINLINE +char* encode_reserve(char* ptr, upb_encstate* e, size_t bytes) { + if ((size_t)(ptr - e->buf) < bytes) { + return encode_growbuffer(ptr, e, bytes); + } + + return ptr - bytes; +} + +static char* encode_bytes_unchecked(char* ptr, upb_encstate* e, + const void* data, size_t len) { + if (len == 0) return ptr; /* memcpy() with zero size is UB */ + ptr -= len; + memcpy(ptr, data, len); + return ptr; +} + +/* Writes the given bytes to the buffer, handling reserve/advance. */ +static char* encode_bytes(char* ptr, upb_encstate* e, const void* data, + size_t len) { + if (len == 0) return ptr; /* memcpy() with zero size is UB */ + ptr = encode_reserve(ptr, e, len); + memcpy(ptr, data, len); + return ptr; +} + +static char* encode_fixed64_unchecked(char* ptr, upb_encstate* e, + uint64_t val) { + val = upb_BigEndian64(val); + return encode_bytes_unchecked(ptr, e, &val, sizeof(uint64_t)); +} + +static char* encode_fixed32_unchecked(char* ptr, upb_encstate* e, + uint32_t val) { + val = upb_BigEndian32(val); + return encode_bytes_unchecked(ptr, e, &val, sizeof(uint32_t)); +} + +#define UPB_PB_VARINT_MAX_LEN 10 +#define UPB_PB_VARINT32_MAX_LEN 5 + +#if UPB_ARM64_ASM +// Each arm64 instruction encodes to 4 bytes, and it takes two intructions +// to process each byte of output, so we branch ahead by (4 + 4) * skip to +// avoid the remaining bytes. When BTI is on, we need to use specific +// "landing pad" instructions, so we pad those with nop to make it a power +// of 2, skipping 16 bytes at each stage instead of 8. This carries some +// overhead especially on in-order cores so they're not included unless +// building with branch protection. +#if UPB_ARM64_BTI_DEFAULT +// BTI is used with jc targets here because we don't control which register will +// be used for addr; if it's x16 or x17 a `br` is treated like a call. +#define UPB_BTI_JC "bti jc\n" +#define UPB_BTI_NOP "nop\n" +#define UPB_BTI_SHIFT_IMM "4\n" +#else +#define UPB_BTI_JC +#define UPB_BTI_NOP +#define UPB_BTI_SHIFT_IMM "3\n" +#endif +UPB_NOINLINE static char* encode_longvarint(char* ptr, upb_encstate* e, + uint64_t val) { + ptr = encode_reserve(ptr, e, UPB_PB_VARINT_MAX_LEN); + uint64_t clz; + __asm__("clz %[cnt], %[val]\n" : [cnt] "=r"(clz) : [val] "r"(val)); + + uint32_t skip = + UPB_PRIVATE(upb_WireWriter_VarintUnusedSizeFromLeadingZeros64)(clz); + + ptr += skip; + uint64_t addr, mask; + __asm__ volatile( + // Formatter keeps merging short lines + // clang-format off + "adr %[addr], 0f\n" + "add %[addr], %[addr], %[cnt], lsl #" UPB_BTI_SHIFT_IMM + "mov %w[mask], #0x80\n" + "br %[addr]\n" + // Work around llvm/llvm-project#47432, where alignment directives inside + // functions crash the compiler when generating SEH info on windows. + #ifndef __SEH__ + ".p2align " UPB_BTI_SHIFT_IMM + #endif + "0:\n" + // We don't need addr any more, but we've got the register for our whole + // assembly block so we'll use it as scratch to store the shift+masked + // values before storing them. + // The following stores are unsigned offset stores: + // strb Wt, [Xn, #imm] + UPB_BTI_JC + "orr %[addr], %[mask], %[val], lsr #56\n" + "strb %w[addr], [%[ptr], #8]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %[addr], %[mask], %[val], lsr #49\n" + "strb %w[addr], [%[ptr], #7]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %[addr], %[mask], %[val], lsr #42\n" + "strb %w[addr], [%[ptr], #6]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %[addr], %[mask], %[val], lsr #35\n" + "strb %w[addr], [%[ptr], #5]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %[addr], %[mask], %[val], lsr #28\n" + "strb %w[addr], [%[ptr], #4]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %w[addr], %w[mask], %w[val], lsr #21\n" + "strb %w[addr], [%[ptr], #3]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %w[addr], %w[mask], %w[val], lsr #14\n" + "strb %w[addr], [%[ptr], #2]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %w[addr], %w[mask], %w[val], lsr #7\n" + "strb %w[addr], [%[ptr], #1]\n" + UPB_BTI_NOP + + UPB_BTI_JC + "orr %w[addr], %w[val], #0x80\n" + "strb %w[addr], [%[ptr]]\n" + UPB_BTI_NOP + + UPB_BTI_JC + // clang-format on + : [addr] "=&r"(addr), [mask] "=&r"(mask) + : [val] "r"(val), [ptr] "r"(ptr), [cnt] "r"((uint64_t)skip) + : "memory"); + uint32_t continuations = UPB_PB_VARINT_MAX_LEN - 1 - skip; + // msan can't instrument stores in inline assembly + UPB_PRIVATE(upb_Xsan_MarkInitialized)(ptr, continuations); + // Encode the final byte after the continuation bytes. + ptr[continuations] = val >> (7 * continuations); + return ptr; +} +#undef UPB_BTI_JC +#undef UPB_BTI_NOP +#undef UPB_BTI_SHIFT_IMM +#else +UPB_NOINLINE +static char* encode_longvarint(char* ptr, upb_encstate* e, uint64_t val) { + ptr = encode_reserve(ptr, e, UPB_PB_VARINT_MAX_LEN); + size_t len = 0; + do { + uint8_t byte = val & 0x7fU; + val >>= 7; + if (val) byte |= 0x80U; + ptr[len++] = byte; + } while (val); + char* start = ptr + UPB_PB_VARINT_MAX_LEN - len; + memmove(start, ptr, len); + return start; +} +#endif + +UPB_FORCEINLINE +char* encode_varint_unchecked(char* ptr, upb_encstate* e, uint64_t val) { + if (val < 128) { + --ptr; + *ptr = val; + return ptr; + } else { + return encode_longvarint(ptr, e, val); + } +} +UPB_FORCEINLINE +char* encode_varint(char* ptr, upb_encstate* e, uint64_t val) { + if (val < 128 && ptr != e->buf) { + --ptr; + *ptr = val; + return ptr; + } else { + return encode_longvarint(ptr, e, val); + } +} + +UPB_NOINLINE +char* encode_longlength(char* ptr, upb_encstate* e, uint64_t val) { + if (val > INT32_MAX) { + encode_err(e, kUpb_EncodeStatus_MaxSizeExceeded); + } + return encode_longvarint(ptr, e, val); +} + +UPB_FORCEINLINE +char* encode_length(char* ptr, upb_encstate* e, uint64_t val) { + if (val < 128 && ptr != e->buf) { + --ptr; + *ptr = val; + return ptr; + } else { + return encode_longlength(ptr, e, val); + } +} + +UPB_FORCEINLINE +char* encode_length_unchecked(char* ptr, upb_encstate* e, uint64_t val) { + if (val < 128) { + --ptr; + *ptr = val; + return ptr; + } else { + return encode_longlength(ptr, e, val); + } +} + +static char* encode_double_unchecked(char* ptr, upb_encstate* e, double d) { + uint64_t u64; + UPB_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t), "bad double size"); + memcpy(&u64, &d, sizeof(uint64_t)); + return encode_fixed64_unchecked(ptr, e, u64); +} + +static char* encode_float_unchecked(char* ptr, upb_encstate* e, float d) { + uint32_t u32; + UPB_STATIC_ASSERT(sizeof(float) == sizeof(uint32_t), "bad float size"); + memcpy(&u32, &d, sizeof(uint32_t)); + return encode_fixed32_unchecked(ptr, e, u32); +} + +static char* encode_tag_unchecked(char* ptr, upb_encstate* e, + uint32_t field_number, uint8_t wire_type) { + return encode_varint_unchecked(ptr, e, (field_number << 3) | wire_type); +} + +static char* encode_tag(char* ptr, upb_encstate* e, uint32_t field_number, + uint8_t wire_type) { + return encode_varint(ptr, e, (field_number << 3) | wire_type); +} + +static char* encode_fixedarray(char* ptr, upb_encstate* e, const upb_Array* arr, + size_t elem_size, uint32_t tag) { + size_t bytes = upb_Array_Size(arr) * elem_size; + const char* data = upb_Array_DataPtr(arr); + const char* arr_ptr = data + bytes - elem_size; + + if (tag || !upb_IsLittleEndian()) { + while (true) { + if (elem_size == 4) { + uint32_t val; + memcpy(&val, arr_ptr, sizeof(val)); + val = upb_BigEndian32(val); + ptr = encode_bytes(ptr, e, &val, elem_size); + } else { + UPB_ASSERT(elem_size == 8); + uint64_t val; + memcpy(&val, arr_ptr, sizeof(val)); + val = upb_BigEndian64(val); + ptr = encode_bytes(ptr, e, &val, elem_size); + } + + if (tag) { + ptr = encode_varint(ptr, e, tag); + } + if (arr_ptr == data) break; + arr_ptr -= elem_size; + } + return ptr; + } else { + return encode_bytes(ptr, e, data, bytes); + } +} + +static char* encode_message(char* ptr, upb_encstate* e, const upb_Message* msg, + const upb_MiniTable* m, size_t* size); + +static char* encode_scalar(char* ptr, upb_encstate* e, const void* field_mem, + const upb_MiniTableField* f) { + // Max size is tag + 10 bytes for max varint or 8 for largest fixed size +#define CASE(ctype, type, wtype, encodeval) \ + { \ + const size_t bytes = UPB_PB_VARINT32_MAX_LEN + UPB_PB_VARINT_MAX_LEN; \ + ptr = encode_reserve(ptr, e, bytes); \ + ptr += bytes; \ + const ctype val = *(const ctype*)field_mem; \ + ptr = encode_##type##_unchecked(ptr, e, encodeval); \ + return encode_tag_unchecked(ptr, e, upb_MiniTableField_Number(f), wtype); \ + } + + switch (f->UPB_PRIVATE(descriptortype)) { + case kUpb_FieldType_Double: + CASE(double, double, kUpb_WireType_64Bit, val); + case kUpb_FieldType_Float: + CASE(float, float, kUpb_WireType_32Bit, val); + case kUpb_FieldType_Int64: + case kUpb_FieldType_UInt64: + CASE(uint64_t, varint, kUpb_WireType_Varint, val); + case kUpb_FieldType_UInt32: + CASE(uint32_t, varint, kUpb_WireType_Varint, val); + case kUpb_FieldType_Int32: + case kUpb_FieldType_Enum: + CASE(int32_t, varint, kUpb_WireType_Varint, (int64_t)val); + case kUpb_FieldType_SFixed64: + case kUpb_FieldType_Fixed64: + CASE(uint64_t, fixed64, kUpb_WireType_64Bit, val); + case kUpb_FieldType_Fixed32: + case kUpb_FieldType_SFixed32: + CASE(uint32_t, fixed32, kUpb_WireType_32Bit, val); + case kUpb_FieldType_Bool: + CASE(bool, varint, kUpb_WireType_Varint, val); + case kUpb_FieldType_SInt32: + CASE(int32_t, varint, kUpb_WireType_Varint, encode_zz32(val)); + case kUpb_FieldType_SInt64: + CASE(int64_t, varint, kUpb_WireType_Varint, encode_zz64(val)); + case kUpb_FieldType_String: + case kUpb_FieldType_Bytes: { + upb_StringView view = *(upb_StringView*)field_mem; + const size_t max_size = + UPB_PB_VARINT32_MAX_LEN + UPB_PB_VARINT32_MAX_LEN + view.size; + ptr = encode_reserve(ptr, e, max_size); + ptr += max_size; + ptr = encode_bytes_unchecked(ptr, e, view.data, view.size); + ptr = encode_length_unchecked(ptr, e, view.size); + return encode_tag_unchecked(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_Delimited); + } + case kUpb_FieldType_Group: { + size_t size; + upb_Message* submsg = *(upb_Message**)field_mem; + const upb_MiniTable* subm = upb_MiniTable_GetSubMessageTable(f); + if (submsg == 0) { + return ptr; + } + if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded); + ptr = encode_tag(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_EndGroup); + ptr = encode_message(ptr, e, submsg, subm, &size); + e->depth++; + return encode_tag(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_StartGroup); + } + case kUpb_FieldType_Message: { + size_t size; + upb_Message* submsg = *(upb_Message**)field_mem; + const upb_MiniTable* subm = upb_MiniTable_GetSubMessageTable(f); + if (submsg == 0) { + return ptr; + } + if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded); + ptr = encode_message(ptr, e, submsg, subm, &size); + e->depth++; + size_t max_size = UPB_PB_VARINT32_MAX_LEN + UPB_PB_VARINT32_MAX_LEN; + ptr = encode_reserve(ptr, e, max_size); + ptr += max_size; + ptr = encode_length_unchecked(ptr, e, size); + return encode_tag_unchecked(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_Delimited); + } + default: + UPB_UNREACHABLE(); + } +#undef CASE +} + +static char* encode_array(char* ptr, upb_encstate* e, const upb_Message* msg, + const upb_MiniTableField* f) { + const upb_Array* arr = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), upb_Array*); + bool packed = upb_MiniTableField_IsPacked(f); + size_t pre_len = e->limit - ptr; + + if (arr == NULL || upb_Array_Size(arr) == 0) { + return ptr; + } + +#define VARINT_CASE(ctype, encode) \ + { \ + const ctype* start = upb_Array_DataPtr(arr); \ + const ctype* arr_ptr = start + upb_Array_Size(arr); \ + uint32_t tag = \ + packed ? 0 : (f->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint; \ + do { \ + arr_ptr--; \ + ptr = encode_varint(ptr, e, encode); \ + if (tag) { \ + ptr = encode_varint(ptr, e, tag); \ + } \ + } while (arr_ptr != start); \ + } \ + break; + +#define TAG(wire_type) (packed ? 0 : (f->UPB_PRIVATE(number) << 3 | wire_type)) + + switch (f->UPB_PRIVATE(descriptortype)) { + case kUpb_FieldType_Double: + ptr = encode_fixedarray(ptr, e, arr, sizeof(double), + TAG(kUpb_WireType_64Bit)); + break; + case kUpb_FieldType_Float: + ptr = encode_fixedarray(ptr, e, arr, sizeof(float), + TAG(kUpb_WireType_32Bit)); + break; + case kUpb_FieldType_SFixed64: + case kUpb_FieldType_Fixed64: + ptr = encode_fixedarray(ptr, e, arr, sizeof(uint64_t), + TAG(kUpb_WireType_64Bit)); + break; + case kUpb_FieldType_Fixed32: + case kUpb_FieldType_SFixed32: + ptr = encode_fixedarray(ptr, e, arr, sizeof(uint32_t), + TAG(kUpb_WireType_32Bit)); + break; + case kUpb_FieldType_Int64: + case kUpb_FieldType_UInt64: + VARINT_CASE(uint64_t, *arr_ptr); + case kUpb_FieldType_UInt32: + VARINT_CASE(uint32_t, *arr_ptr); + case kUpb_FieldType_Int32: + case kUpb_FieldType_Enum: + VARINT_CASE(int32_t, (int64_t)*arr_ptr); + case kUpb_FieldType_Bool: + VARINT_CASE(bool, *arr_ptr); + case kUpb_FieldType_SInt32: + VARINT_CASE(int32_t, encode_zz32(*arr_ptr)); + case kUpb_FieldType_SInt64: + VARINT_CASE(int64_t, encode_zz64(*arr_ptr)); + case kUpb_FieldType_String: + case kUpb_FieldType_Bytes: { + const upb_StringView* start = upb_Array_DataPtr(arr); + const upb_StringView* str_ptr = start + upb_Array_Size(arr); + do { + str_ptr--; + ptr = encode_bytes(ptr, e, str_ptr->data, str_ptr->size); + ptr = encode_length(ptr, e, str_ptr->size); + ptr = encode_tag(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_Delimited); + } while (str_ptr != start); + return ptr; + } + case kUpb_FieldType_Group: { + const upb_Message* const* start = upb_Array_DataPtr(arr); + const upb_Message* const* arr_ptr = start + upb_Array_Size(arr); + const upb_MiniTable* subm = upb_MiniTable_GetSubMessageTable(f); + if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded); + do { + size_t size; + arr_ptr--; + ptr = encode_tag(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_EndGroup); + ptr = encode_message(ptr, e, *arr_ptr, subm, &size); + ptr = encode_tag(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_StartGroup); + } while (arr_ptr != start); + e->depth++; + return ptr; + } + case kUpb_FieldType_Message: { + const upb_Message* const* start = upb_Array_DataPtr(arr); + const upb_Message* const* arr_ptr = start + upb_Array_Size(arr); + const upb_MiniTable* subm = upb_MiniTable_GetSubMessageTable(f); + if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded); + do { + size_t size; + arr_ptr--; + ptr = encode_message(ptr, e, *arr_ptr, subm, &size); + ptr = encode_length(ptr, e, size); + ptr = encode_tag(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_Delimited); + } while (arr_ptr != start); + e->depth++; + return ptr; + } + } +#undef VARINT_CASE + + if (packed) { + ptr = encode_length(ptr, e, e->limit - ptr - pre_len); + ptr = encode_tag(ptr, e, upb_MiniTableField_Number(f), + kUpb_WireType_Delimited); + } + return ptr; +} + +static char* encode_mapentry(char* ptr, upb_encstate* e, uint32_t number, + const upb_MiniTable* layout, + const upb_MapEntry* ent) { + const upb_MiniTableField* key_field = upb_MiniTable_MapKey(layout); + const upb_MiniTableField* val_field = upb_MiniTable_MapValue(layout); + size_t pre_len = e->limit - ptr; + size_t size; + ptr = encode_scalar(ptr, e, &ent->v, val_field); + ptr = encode_scalar(ptr, e, &ent->k, key_field); + size = (e->limit - ptr) - pre_len; + ptr = encode_length(ptr, e, size); + ptr = encode_tag(ptr, e, number, kUpb_WireType_Delimited); + return ptr; +} + +static char* encode_map(char* ptr, upb_encstate* e, const upb_Message* msg, + const upb_MiniTableField* f) { + const upb_Map* map = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), const upb_Map*); + const upb_MiniTable* layout = upb_MiniTable_MapEntrySubMessage(f); + UPB_ASSERT(upb_MiniTable_FieldCount(layout) == 2); + + if (!map || !upb_Map_Size(map)) return ptr; + + if (e->options & kUpb_EncodeOption_Deterministic) { + if (!map->UPB_PRIVATE(is_strtable)) { + // For inttable, first encode the array part, then sort the table entries. + intptr_t iter = UPB_INTTABLE_BEGIN; + while ((size_t)++iter < map->t.inttable.array_size) { + upb_value value = map->t.inttable.array[iter]; + if (upb_inttable_arrhas(&map->t.inttable, iter)) { + upb_MapEntry ent; + memcpy(&ent.k, &iter, sizeof(iter)); + _upb_map_fromvalue(value, &ent.v, map->val_size); + ptr = encode_mapentry(ptr, e, upb_MiniTableField_Number(f), layout, + &ent); + } + } + } + _upb_sortedmap sorted; + _upb_mapsorter_pushmap( + &e->sorter, layout->UPB_PRIVATE(fields)[0].UPB_PRIVATE(descriptortype), + map, &sorted); + upb_MapEntry ent; + while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) { + ptr = encode_mapentry(ptr, e, upb_MiniTableField_Number(f), layout, &ent); + } + _upb_mapsorter_popmap(&e->sorter, &sorted); + } else { + upb_value val; + if (map->UPB_PRIVATE(is_strtable)) { + intptr_t iter = UPB_STRTABLE_BEGIN; + upb_StringView strkey; + while (upb_strtable_next2(&map->t.strtable, &strkey, &val, &iter)) { + upb_MapEntry ent; + _upb_map_fromkey(strkey, &ent.k, map->key_size); + _upb_map_fromvalue(val, &ent.v, map->val_size); + ptr = + encode_mapentry(ptr, e, upb_MiniTableField_Number(f), layout, &ent); + } + } else { + intptr_t iter = UPB_INTTABLE_BEGIN; + uintptr_t intkey = 0; + while (upb_inttable_next(&map->t.inttable, &intkey, &val, &iter)) { + upb_MapEntry ent; + memcpy(&ent.k, &intkey, map->key_size); + _upb_map_fromvalue(val, &ent.v, map->val_size); + ptr = + encode_mapentry(ptr, e, upb_MiniTableField_Number(f), layout, &ent); + } + } + } + return ptr; +} + +static bool encode_shouldencode(const upb_Message* msg, + const upb_MiniTableField* f) { + if (f->presence == 0) { + // Proto3 presence or map/array. + const void* mem = UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), void); + switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) { + case kUpb_FieldRep_1Byte: { + char ch; + memcpy(&ch, mem, 1); + return ch != 0; + } + case kUpb_FieldRep_4Byte: { + uint32_t u32; + memcpy(&u32, mem, 4); + return u32 != 0; + } + case kUpb_FieldRep_8Byte: { + uint64_t u64; + memcpy(&u64, mem, 8); + return u64 != 0; + } + case kUpb_FieldRep_StringView: { + const upb_StringView* str = (const upb_StringView*)mem; + return str->size != 0; + } + default: + UPB_UNREACHABLE(); + } + } else if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) { + // Proto2 presence: hasbit. + return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, f); + } else { + // Field is in a oneof. + return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, f) == + upb_MiniTableField_Number(f); + } +} + +static char* encode_field(char* ptr, upb_encstate* e, const upb_Message* msg, + const upb_MiniTableField* field) { + switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(field)) { + case kUpb_FieldMode_Array: + return encode_array(ptr, e, msg, field); + case kUpb_FieldMode_Map: + return encode_map(ptr, e, msg, field); + case kUpb_FieldMode_Scalar: + return encode_scalar( + ptr, e, UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void), field); + default: + UPB_UNREACHABLE(); + } +} + +static char* encode_msgset_item(char* ptr, upb_encstate* e, + const upb_MiniTableExtension* ext, + const upb_MessageValue ext_val) { + size_t size; + ptr = encode_tag(ptr, e, kUpb_MsgSet_Item, kUpb_WireType_EndGroup); + ptr = encode_message(ptr, e, ext_val.msg_val, + upb_MiniTableExtension_GetSubMessage(ext), &size); + ptr = encode_varint(ptr, e, size); + ptr = encode_tag(ptr, e, kUpb_MsgSet_Message, kUpb_WireType_Delimited); + ptr = encode_varint(ptr, e, upb_MiniTableExtension_Number(ext)); + ptr = encode_tag(ptr, e, kUpb_MsgSet_TypeId, kUpb_WireType_Varint); + ptr = encode_tag(ptr, e, kUpb_MsgSet_Item, kUpb_WireType_StartGroup); + return ptr; +} + +static char* encode_ext(char* ptr, upb_encstate* e, + const upb_MiniTableExtension* ext, + upb_MessageValue ext_val, bool is_message_set) { + if (UPB_UNLIKELY(is_message_set)) { + ptr = encode_msgset_item(ptr, e, ext, ext_val); + } else { + ptr = encode_field(ptr, e, &ext_val.UPB_PRIVATE(ext_msg_val), + &ext->UPB_PRIVATE(field)); + } + return ptr; +} + +static char* encode_exts(char* ptr, upb_encstate* e, const upb_MiniTable* m, + const upb_Message* msg) { + if (m->UPB_PRIVATE(ext) == kUpb_ExtMode_NonExtendable) return ptr; + + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) return ptr; + + /* Encode all extensions together. Unlike C++, we do not attempt to keep + * these in field number order relative to normal fields or even to each + * other. */ + uintptr_t iter = kUpb_Message_ExtensionBegin; + const upb_MiniTableExtension* ext; + upb_MessageValue ext_val; + if (!UPB_PRIVATE(_upb_Message_NextExtensionReverse)(msg, &ext, &ext_val, + &iter)) { + // Message has no extensions. + return ptr; + } + + if (e->options & kUpb_EncodeOption_Deterministic) { + _upb_sortedmap sorted; + if (!_upb_mapsorter_pushexts(&e->sorter, in, &sorted)) { + // TODO: b/378744096 - handle alloc failure + } + const upb_Extension* ext; + while (_upb_sortedmap_nextext(&e->sorter, &sorted, &ext)) { + ptr = encode_ext(ptr, e, ext->ext, ext->data, + m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet); + } + _upb_mapsorter_popmap(&e->sorter, &sorted); + } else { + do { + ptr = encode_ext(ptr, e, ext, ext_val, + m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet); + } while (UPB_PRIVATE(_upb_Message_NextExtensionReverse)(msg, &ext, &ext_val, + &iter)); + } + return ptr; +} + +static char* encode_message(char* ptr, upb_encstate* e, const upb_Message* msg, + const upb_MiniTable* m, size_t* size) { + size_t pre_len = e->limit - ptr; + + if (e->options & kUpb_EncodeOption_CheckRequired) { + if (m->UPB_PRIVATE(required_count)) { + if (!UPB_PRIVATE(_upb_Message_IsInitializedShallow)(msg, m)) { + encode_err(e, kUpb_EncodeStatus_MissingRequired); + } + } + } + + if ((e->options & kUpb_EncodeOption_SkipUnknown) == 0) { + size_t unknown_size = 0; + uintptr_t iter = kUpb_Message_UnknownBegin; + upb_StringView unknown; + // Need to write in reverse order, but iteration is in-order; scan to + // reserve capacity up front, then write in-order + while (upb_Message_NextUnknown(msg, &unknown, &iter)) { + unknown_size += unknown.size; + } + if (unknown_size != 0) { + ptr = encode_reserve(ptr, e, unknown_size); + char* tmp_ptr = ptr; + iter = kUpb_Message_UnknownBegin; + while (upb_Message_NextUnknown(msg, &unknown, &iter)) { + memcpy(tmp_ptr, unknown.data, unknown.size); + tmp_ptr += unknown.size; + } + } + } + + ptr = encode_exts(ptr, e, m, msg); + + if (upb_MiniTable_FieldCount(m)) { + const upb_MiniTableField* f = + &m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)]; + const upb_MiniTableField* first = &m->UPB_PRIVATE(fields)[0]; + while (f != first) { + f--; + if (encode_shouldencode(msg, f)) { + ptr = encode_field(ptr, e, msg, f); + } + } + } + + *size = (e->limit - ptr) - pre_len; + return ptr; +} + +static upb_EncodeStatus upb_Encoder_Encode(char* ptr, + upb_encstate* const encoder, + const upb_Message* const msg, + const upb_MiniTable* const l, + char** const buf, size_t* const size, + bool prepend_len) { + // Unfortunately we must continue to perform hackery here because there are + // code paths which blindly copy the returned pointer without bothering to + // check for errors until much later (b/235839510). So we still set *buf to + // NULL on error and we still set it to non-NULL on a successful empty result. + if (UPB_SETJMP(encoder->err) == 0) { + size_t encoded_msg_size; + ptr = encode_message(ptr, encoder, msg, l, &encoded_msg_size); + if (prepend_len) { + ptr = encode_length(ptr, encoder, encoded_msg_size); + } + *size = encoder->limit - ptr; + if (*size == 0) { + static char ch; + *buf = &ch; + } else { + UPB_ASSERT(ptr); + *buf = ptr; + } + } else { + UPB_ASSERT(encoder->status != kUpb_EncodeStatus_Ok); + *buf = NULL; + *size = 0; + } + + _upb_mapsorter_destroy(&encoder->sorter); + return encoder->status; +} + +static uint16_t upb_EncodeOptions_GetMaxDepth(uint32_t options) { + return options >> 16; +} + +uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options) { + uint16_t max_depth = upb_EncodeOptions_GetMaxDepth(options); + return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit; +} + +static upb_EncodeStatus _upb_Encode(const upb_Message* msg, + const upb_MiniTable* l, int options, + upb_Arena* arena, char** buf, size_t* size, + bool prepend_len) { + upb_encstate e; + + e.status = kUpb_EncodeStatus_Ok; + e.arena = arena; + e.buf = &initial_buf_sentinel; + e.limit = &initial_buf_sentinel; + e.depth = upb_EncodeOptions_GetEffectiveMaxDepth(options); + e.options = options; + _upb_mapsorter_init(&e.sorter); + + return upb_Encoder_Encode(&initial_buf_sentinel, &e, msg, l, buf, size, + prepend_len); +} + +upb_EncodeStatus upb_Encode(const upb_Message* msg, const upb_MiniTable* l, + int options, upb_Arena* arena, char** buf, + size_t* size) { + return _upb_Encode(msg, l, options, arena, buf, size, false); +} + +upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg, + const upb_MiniTable* l, int options, + upb_Arena* arena, char** buf, + size_t* size) { + return _upb_Encode(msg, l, options, arena, buf, size, true); +} + +const char* upb_EncodeStatus_String(upb_EncodeStatus status) { + switch (status) { + case kUpb_EncodeStatus_Ok: + return "Ok"; + case kUpb_EncodeStatus_MissingRequired: + return "Missing required field"; + case kUpb_EncodeStatus_MaxDepthExceeded: + return "Max depth exceeded"; + case kUpb_EncodeStatus_OutOfMemory: + return "Arena alloc failed"; + default: + return "Unknown encode status"; + } +} + + + +// Must be last. + +UPB_NOINLINE +const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr, + const upb_Message* msg, + const upb_MiniTable* m) { + UPB_ASSERT(m->UPB_PRIVATE(required_count)); + if (UPB_UNLIKELY(d->options & kUpb_DecodeOption_CheckRequired)) { + d->missing_required = + d->missing_required || + !UPB_PRIVATE(_upb_Message_IsInitializedShallow)(msg, m); + } + return ptr; +} + + +#include +#include + + +// Must be last. + +const char* UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)( + upb_EpsCopyInputStream* e) { + e->error = true; + if (e->err) upb_ErrorHandler_ThrowError(e->err, kUpb_ErrorCode_Malformed); + return NULL; +} + +const char* UPB_PRIVATE(upb_EpsCopyInputStream_IsDoneFallback)( + struct upb_EpsCopyInputStream* e, const char* ptr, int overrun) { + if (overrun < e->limit) { + // Need to copy remaining data into patch buffer. + UPB_ASSERT(overrun < kUpb_EpsCopyInputStream_SlopBytes); + const char* old_end = ptr; + const char* new_start = &e->patch[overrun]; + memset(&e->patch[kUpb_EpsCopyInputStream_SlopBytes], 0, + kUpb_EpsCopyInputStream_SlopBytes); + memcpy(e->patch, e->end, kUpb_EpsCopyInputStream_SlopBytes); + ptr = new_start; + e->end = &e->patch[kUpb_EpsCopyInputStream_SlopBytes]; + e->limit -= kUpb_EpsCopyInputStream_SlopBytes; + e->limit_ptr = e->end + e->limit; + UPB_ASSERT(ptr < e->limit_ptr); + e->input_delta = (uintptr_t)old_end - (uintptr_t)new_start; + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e); + return new_start; + } else { + UPB_ASSERT(overrun > e->limit); + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(e); + } +} + + +#include +#include + + +// Must be last. + +UPB_NOINLINE UPB_PRIVATE(_upb_WireReader_LongVarint) + UPB_PRIVATE(_upb_WireReader_ReadLongVarint)( + const char* ptr, uint64_t val, upb_EpsCopyInputStream* stream) { + for (int i = 1; i < 10; i++) { + uint64_t byte = (uint8_t)ptr[i]; + val += (byte - 1) << (i * 7); + if (!(byte & 0x80)) { + return (UPB_PRIVATE(_upb_WireReader_LongVarint)){ptr + i + 1, val}; + } + } + return (UPB_PRIVATE(_upb_WireReader_LongVarint)){ + UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream), 0}; +} + +UPB_NOINLINE UPB_PRIVATE(_upb_WireReader_LongVarint) + UPB_PRIVATE(_upb_WireReader_ReadLongTag)(const char* ptr, uint64_t val, + upb_EpsCopyInputStream* stream) { + for (int i = 1; i < 5; i++) { + uint64_t byte = (uint8_t)ptr[i]; + val += (byte - 1) << (i * 7); + if (!(byte & 0x80)) { + if (val > UINT32_MAX) break; + return (UPB_PRIVATE(_upb_WireReader_LongVarint)){ptr + i + 1, val}; + } + } + return (UPB_PRIVATE(_upb_WireReader_LongVarint)){ + UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream), 0}; +} + +UPB_NOINLINE UPB_PRIVATE(_upb_WireReader_LongVarint) + UPB_PRIVATE(_upb_WireReader_ReadLongSize)(const char* ptr, uint64_t val, + upb_EpsCopyInputStream* stream) { + for (int i = 1; i < 5; i++) { + uint64_t byte = (uint8_t)ptr[i]; + val += (byte - 1) << (i * 7); + if (!(byte & 0x80)) { + if (val > INT32_MAX) break; + return (UPB_PRIVATE(_upb_WireReader_LongVarint)){ptr + i + 1, val}; + } + } + return (UPB_PRIVATE(_upb_WireReader_LongVarint)){ + UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream), 0}; +} + +const char* UPB_PRIVATE(_upb_WireReader_SkipGroup)( + const char* ptr, uint32_t tag, int depth_limit, + upb_EpsCopyInputStream* stream) { + if (--depth_limit < 0) { + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream); + } + uint32_t end_group_tag = (tag & ~7ULL) | kUpb_WireType_EndGroup; + while (!upb_EpsCopyInputStream_IsDone(stream, &ptr)) { + uint32_t tag; + ptr = upb_WireReader_ReadTag(ptr, &tag, stream); + if (!ptr) break; + if (tag == end_group_tag) return ptr; + ptr = _upb_WireReader_SkipValue(ptr, tag, depth_limit, stream); + if (!ptr) break; + } + // Encountered limit end before end group tag. + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream); +} diff --git a/third_party/upb/upb.h b/third_party/upb/upb.h new file mode 100644 index 000000000..51b41d4a2 --- /dev/null +++ b/third_party/upb/upb.h @@ -0,0 +1,18141 @@ +/* Amalgamated source file */ + +/* + * This is where we define internal portability macros used across upb. + * + * All of these macros are undef'd in undef.inc to avoid leaking them to users. + * + * The correct usage is: + * + * #include "upb/foobar.h" + * #include "upb/baz.h" + * + * // MUST be last included header. + * #include "upb/port/def.inc" + * + * // Code for this file. + * // <...> + * + * // Can be omitted for .c files, required for .h. + * #include "upb/port/undef.inc" + * + * This file is private and must not be included by users! + */ + +#if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ + (defined(__cplusplus) && __cplusplus >= 201703L) || \ + (defined(_MSC_VER) && _MSC_VER >= 1900)) +#error upb requires C99 or C++17 or MSVC >= 2015. +#endif + +// Portable check for GCC minimum version: +// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html +#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) +#define UPB_GNUC_MIN(x, y) \ + (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y)) +#else +#define UPB_GNUC_MIN(x, y) 0 +#endif + +// Macros for checking for compiler attributes, defined here to avoid the +// problem described in +// https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html. +#ifdef __has_attribute +#define UPB_HAS_ATTRIBUTE(x) __has_attribute(x) +#else +#define UPB_HAS_ATTRIBUTE(x) 0 +#endif + +#if defined(__cplusplus) && defined(__has_cpp_attribute) +// NOTE: requiring __cplusplus above should not be necessary, but +// works around https://bugs.llvm.org/show_bug.cgi?id=23435. +#define UPB_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) +#else +#define UPB_HAS_CPP_ATTRIBUTE(x) 0 +#endif + +// Once in a while we want to use this macro in a C++-only portion of an +// otherwise C-compatible header, so we copy and paste this from ABSL. +#if UPB_HAS_CPP_ATTRIBUTE(deprecated) && UPB_HAS_CPP_ATTRIBUTE(clang::annotate) +#define UPB_DEPRECATE_AND_INLINE() [[deprecated, clang::annotate("inline-me")]] +#elif UPB_HAS_CPP_ATTRIBUTE(deprecated) +#define UPB_DEPRECATE_AND_INLINE() [[deprecated]] +#else +#define UPB_DEPRECATE_AND_INLINE() +#endif + +#ifdef __has_builtin +#define UPB_HAS_BUILTIN(x) __has_builtin(x) +#else +#define UPB_HAS_BUILTIN(x) 0 +#endif + +#ifdef __has_extension +#define UPB_HAS_EXTENSION(x) __has_extension(x) +#else +#define UPB_HAS_EXTENSION(x) 0 +#endif + +#ifdef __has_feature +#define UPB_HAS_FEATURE(x) __has_feature(x) +#else +#define UPB_HAS_FEATURE(x) 0 +#endif + +#include +#include +#include +#include +#include +#include + +#ifndef UINTPTR_MAX +Error, UINTPTR_MAX is undefined +#endif + +#if UINTPTR_MAX == 0xffffffff +#define UPB_SIZE(size32, size64) size32 +#else +#define UPB_SIZE(size32, size64) size64 +#endif + +/* If we always read/write as a consistent type to each address, this shouldn't + * violate aliasing. + */ +#define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs))) + +// A flexible array member may have lower alignment requirements than the struct +// overall - in that case, it can overlap with the trailing padding of the rest +// of the struct, and a naive sizeof(base) + sizeof(flex) * count calculation +// will not take into account that overlap, and allocate more than is required. +#define UPB_SIZEOF_FLEX(type, member, count) \ + UPB_MAX(sizeof(type), offsetof(type, member[count])) + +#define UPB_SIZEOF_FLEX_WOULD_OVERFLOW(type, member, count) \ + (((SIZE_MAX - offsetof(type, member[0])) / \ + (offsetof(type, member[1]) - offsetof(type, member[0]))) < (size_t)count) + +#define UPB_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + +#define UPB_MAPTYPE_STRING 0 + +// UPB_EXPORT: always generate a public symbol. +#if defined(__GNUC__) || defined(__clang__) +#define UPB_EXPORT __attribute__((visibility("default"))) __attribute__((used)) +#else +#define UPB_EXPORT +#endif + +// UPB_INLINE: inline if possible, emit standalone code if required. +#ifdef __cplusplus +#define UPB_INLINE inline +#elif defined(__GNUC__) || defined(__clang__) +#define UPB_INLINE static __inline__ +#else +#define UPB_INLINE static +#endif + +// UPB_INLINE_IF_NOT_GCC: because gcc can be very noisy at times. +#if defined(__GNUC__) && !defined(__clang__) +#define UPB_INLINE_IF_NOT_GCC static +#else +#define UPB_INLINE_IF_NOT_GCC UPB_INLINE +#endif + +#ifdef UPB_BUILD_API +#define UPB_API UPB_EXPORT +#define UPB_API_INLINE UPB_EXPORT +#else +#define UPB_API +#define UPB_API_INLINE UPB_INLINE +#endif + +#ifdef EXPORT_UPBC +#define UPBC_API UPB_EXPORT +#else +#define UPBC_API +#endif + +#if UPB_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__) +#define UPB_ASAN 1 +#else +#define UPB_ASAN 0 +#endif + +#if UPB_HAS_FEATURE(hwaddress_sanitizer) +#define UPB_HWASAN 1 +#define UPB_HWASAN_POISON_TAG 17 +#define UPB_MALLOC_ALIGN 16 +#else +#define UPB_HWASAN 0 +#define UPB_MALLOC_ALIGN 8 +#endif + +#if UPB_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__) +#define UPB_TSAN 1 +#else +#define UPB_TSAN 0 +#endif + +#if UPB_HAS_FEATURE(memory_sanitizer) +#define UPB_MSAN 1 +#else +#define UPB_MSAN 0 +#endif + +// An unfortunate concession to C++17 and MSVC, which don't support zero-sized +// structs. +#if UPB_ASAN || UPB_HWASAN || UPB_TSAN +#define UPB_XSAN_MEMBER upb_Xsan xsan; +#define UPB_XSAN(st) (&(st)->xsan) +#define UPB_XSAN_STRUCT_SIZE 1 +#else +#define UPB_XSAN_MEMBER +#define UPB_XSAN(st) (NULL) +#define UPB_XSAN_STRUCT_SIZE 0 +#endif + +#define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align)) +#define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align)) +#define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, UPB_MALLOC_ALIGN) + +#if __STDC_VERSION__ >= 202311L || UPB_HAS_EXTENSION(cxx_alignof) || \ + defined(__cplusplus) +#define UPB_ALIGN_OF(type) alignof(type) +#elif __STDC_VERSION__ >= 201112L || UPB_HAS_EXTENSION(c_alignof) +#define UPB_ALIGN_OF(type) _Alignof(type) +#elif UPB_GNUC_MIN(2, 95) +#define UPB_ALIGN_OF(type) __alignof__(type) +#elif defined(_MSC_VER) +#define UPB_ALIGN_OF(type) __alignof(type) +#else +#define UPB_ALIGN_OF(type) \ + offsetof( \ + struct { \ + char c; \ + type member; \ + }, \ + member) +#endif + +#ifdef _MSC_VER +// Some versions of our Windows compiler don't support the C11 syntax. +#define UPB_ALIGN_AS(x) __declspec(align(x)) +#elif defined(__GNUC__) +#define UPB_ALIGN_AS(x) __attribute__((aligned(x))) +#else +#define UPB_ALIGN_AS(x) _Alignas(x) +#endif + +#if __STDC_VERSION__ >= 202311L || UPB_HAS_EXTENSION(cxx_static_assert) || \ + defined(__cplusplus) +#define UPB_STATIC_ASSERT(val, msg) static_assert((val), msg) +#elif __STDC_VERSION__ >= 201112L || UPB_HAS_EXTENSION(c_static_assert) || \ + UPB_GNUC_MIN(4, 6) +#define UPB_STATIC_ASSERT(val, msg) _Static_assert((val), msg) +#else +// Unfortunately this hack doesn't work inside struct declarations, but it works +// everywhere else +#define UPB_STATIC_ASSERT_CONCAT_IMPL(s1, s2) s1##s2 +#define UPB_STATIC_ASSERT_CONCAT(s1, s2) UPB_STATIC_ASSERT_CONCAT_IMPL(s1, s2) +#ifdef __COUNTER__ +#define UPB_STATIC_ASSERT(condition, message) \ + typedef char UPB_STATIC_ASSERT_CONCAT(static_assertion_failure_, \ + __COUNTER__)[(condition) ? 1 : -1] +#else +#define UPB_STATIC_ASSERT(condition, message) \ + typedef char UPB_STATIC_ASSERT_CONCAT(static_assertion_failure_, \ + __LINE__)[(condition) ? 1 : -1] +#endif +#endif + +// Hints to the compiler about likely/unlikely branches. +#if defined(__GNUC__) || defined(__clang__) +#define UPB_LIKELY(x) __builtin_expect((bool)(x), 1) +#define UPB_UNLIKELY(x) __builtin_expect((bool)(x), 0) +#else +#define UPB_LIKELY(x) (x) +#define UPB_UNLIKELY(x) (x) +#endif + +#if UPB_HAS_BUILTIN(__builtin_expect_with_probability) +#define UPB_UNPREDICTABLE(x) \ + __builtin_expect_with_probability((bool)(x), 1, 0.5) +#else +#define UPB_UNPREDICTABLE(x) (x) +#endif + +// Macros for function attributes on compilers that support them. +#if defined(__GNUC__) || defined(__clang__) +#define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) static +#define UPB_NOINLINE __attribute__((noinline)) +#define UPB_NORETURN __attribute__((__noreturn__)) +#define UPB_PRINTF(str, first_vararg) \ + __attribute__((format(printf, str, first_vararg))) +#elif defined(_MSC_VER) +#define UPB_NOINLINE +#define UPB_FORCEINLINE static +#define UPB_NORETURN __declspec(noreturn) +#define UPB_PRINTF(str, first_vararg) +#else /* !defined(__GNUC__) */ +#define UPB_FORCEINLINE static +#define UPB_NOINLINE +#define UPB_NORETURN +#define UPB_PRINTF(str, first_vararg) +#endif + +#if defined(__clang__) +#define UPB_NODEREF __attribute__((noderef)) +#else +#define UPB_NODEREF +#endif + +#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) +#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) + +#define UPB_UNUSED(var) (void)(var) + +// UPB_ASSUME(): in release mode, we tell the compiler to assume this is true. +#ifdef NDEBUG +#ifdef __GNUC__ +#define UPB_ASSUME(expr) \ + if (!(expr)) __builtin_unreachable() +#elif defined _MSC_VER +#define UPB_ASSUME(expr) \ + if (!(expr)) __assume(0) +#else +#define UPB_ASSUME(expr) \ + do { \ + } while (false && (expr)) +#endif +#else +#define UPB_ASSUME(expr) assert(expr) +#endif + +#if UPB_HAS_BUILTIN(__builtin_constant_p) && UPB_HAS_ATTRIBUTE(const) +#define UPB_MAYBE_ASSUME(pred, x) \ + if (__builtin_constant_p(pred) && pred) UPB_ASSUME(x) +#define UPB_ATTR_CONST __attribute__((const)) +#else +#define UPB_MAYBE_ASSUME(pred, x) +#define UPB_ATTR_CONST +#endif + +/* UPB_ASSERT(): in release mode, we use the expression without letting it be + * evaluated. This prevents "unused variable" warnings. */ +#ifdef NDEBUG +#define UPB_ASSERT(expr) \ + do { \ + } while (false && (expr)) +#else +#define UPB_ASSERT(expr) assert(expr) +#endif + +#if !defined(NDEBUG) && !defined(UPB_TSAN) +#define UPB_ENABLE_REF_CYCLE_CHECKS 1 +#else +#define UPB_ENABLE_REF_CYCLE_CHECKS 0 +#endif + +#if defined(__GNUC__) || defined(__clang__) +#define UPB_UNREACHABLE() \ + do { \ + assert(0); \ + __builtin_unreachable(); \ + } while (0) +#elif defined(_MSC_VER) +#define UPB_UNREACHABLE() \ + do { \ + assert(0); \ + __assume(0); \ + } while (0) +#else +#define UPB_UNREACHABLE() \ + do { \ + assert(0); \ + } while (0) +#endif + +#ifdef __ANDROID__ +#define UPB_DEFAULT_MAX_BLOCK_SIZE 8192 +#else +#define UPB_DEFAULT_MAX_BLOCK_SIZE 32768 +#endif + +/* UPB_SETJMP() / UPB_LONGJMP() */ +// Android uses a custom libc that does not implement all of posix, but it has +// had sigsetjmp/siglongjmp forever on arm and since API 12 on x86. Apple has +// sigsetjmp, but does not define the posix feature test macro. +#if defined(__APPLE__) || defined(_POSIX_C_SOURCE) || defined(__ANDROID__) +// avoid setting/restoring signal mask, which involves costly syscalls +#define UPB_SETJMP(buf) sigsetjmp(buf, 0) +#define UPB_LONGJMP(buf, val) siglongjmp(buf, val) +#elif defined(WASM_WAMR) +#define UPB_SETJMP(buf) 0 +#define UPB_LONGJMP(buf, val) abort() +#else +#define UPB_SETJMP(buf) setjmp(buf) +#define UPB_LONGJMP(buf, val) longjmp(buf, val) +#endif + +#if ((__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)) || \ + UPB_HAS_EXTENSION(c_atomic) || \ + defined(__GNUC__) // GCC supported atomics as an extension before it + // supported __has_extension +#define UPB_USE_C11_ATOMICS +#elif defined(_MSC_VER) +#define UPB_USE_MSC_ATOMICS +#endif + +#if defined(UPB_USE_C11_ATOMICS) +#define UPB_ATOMIC(T) _Atomic(T) +#elif defined(UPB_USE_MSC_ATOMICS) +#define UPB_ATOMIC(T) volatile T +#else +#define UPB_ATOMIC(T) T +#endif + +/* UPB_PTRADD(ptr, ofs): add pointer while avoiding "NULL + 0" UB */ +#define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr)) + +#define UPB_PRIVATE(x) x##_dont_copy_me__upb_internal_use_only + +#ifdef UPB_ALLOW_PRIVATE_ACCESS__FOR_BITS_ONLY +#define UPB_ONLYBITS(x) x +#else +#define UPB_ONLYBITS(x) UPB_PRIVATE(x) +#endif + +/* Configure whether fasttable is switched on or not. *************************/ + +#if UPB_HAS_ATTRIBUTE(musttail) +#define UPB_MUSTTAIL __attribute__((musttail)) +#else +#define UPB_MUSTTAIL +#endif + +#if UPB_HAS_ATTRIBUTE(preserve_most) && !defined(__i386__) +#define UPB_PRESERVE_MOST __attribute__((preserve_most)) +#else +#define UPB_PRESERVE_MOST +#endif + +#if UPB_HAS_ATTRIBUTE(preserve_none) +#define UPB_PRESERVE_NONE __attribute__((preserve_none)) +#else +#define UPB_PRESERVE_NONE +#endif + +#if defined(__aarch64__) && (defined(__GNUC__) || defined(__clang__)) +#define UPB_ARM64_ASM 1 +#else +#define UPB_ARM64_ASM 0 +#endif + +/* When compiling with branch protection, we need to ensure that all branch + * targets in assembly use the appropriate landing pad instruction. These + * instructions are backwards compatible with processors that don't have + * FEAT_BTI and are treated as nops. + */ +#if UPB_ARM64_ASM && defined(__ARM_FEATURE_BTI_DEFAULT) +#if __ARM_FEATURE_BTI_DEFAULT == 1 +#define UPB_ARM64_BTI_DEFAULT 1 +#else +#define UPB_ARM64_BTI_DEFAULT 0 +#endif +#else +#define UPB_ARM64_BTI_DEFAULT 0 +#endif + +/* This check is not fully robust: it does not require that we have "musttail" + * support available. We need tail calls to avoid consuming arbitrary amounts + * of stack space. + * + * GCC/Clang can mostly be trusted to generate tail calls as long as + * optimization is enabled, but, debug builds will not generate tail calls + * unless "musttail" is available. + * + * We should probably either: + * 1. require that the compiler supports musttail. + * 2. add some fallback code for when musttail isn't available (ie. return + * instead of tail calling). This is safe and portable, but this comes at + * a CPU cost. + */ +#if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__) +#define UPB_FASTTABLE_SUPPORTED 1 +#else +#define UPB_FASTTABLE_SUPPORTED 0 +#endif + +/* define UPB_ENABLE_FASTTABLE to force fast table support. + * This is useful when we want to ensure we are really getting fasttable, + * for example for testing or benchmarking. */ +#if defined(UPB_ENABLE_FASTTABLE) +#if !UPB_FASTTABLE_SUPPORTED +#error fasttable is x86-64/ARM64 only and requires GCC or Clang. +#endif +#define UPB_FASTTABLE 1 +/* Define UPB_TRY_ENABLE_FASTTABLE to use fasttable if possible. + * This is useful for releasing code that might be used on multiple platforms, + * for example the PHP or Ruby C extensions. */ +#elif defined(UPB_TRY_ENABLE_FASTTABLE) +#define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED +#else +#define UPB_FASTTABLE 0 +#endif + +/* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully + * 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 + +#if defined(__cplusplus) +#if defined(__clang__) || UPB_GNUC_MIN(6, 0) +// https://gcc.gnu.org/gcc-6/changes.html +#define UPB_DEPRECATED [[deprecated]] +#else +#define UPB_DEPRECATED +#endif +#else +#define UPB_DEPRECATED +#endif + +#if defined(UPB_IS_GOOGLE3) && \ + (!defined(UPB_BOOTSTRAP_STAGE) || UPB_BOOTSTRAP_STAGE != 0) +#define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init +#elif defined(UPB_IS_GOOGLE3) && defined(UPB_BOOTSTRAP_STAGE) && \ + UPB_BOOTSTRAP_STAGE == 0 +#define UPB_DESC_MINITABLE(sym) proto2__##sym##_msg_init() +#elif defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0 +#define UPB_DESC_MINITABLE(sym) google__protobuf__##sym##_msg_init() +#else +#define UPB_DESC_MINITABLE(sym) &google__protobuf__##sym##_msg_init +#endif + +#undef UPB_IS_GOOGLE3 + +#ifdef __clang__ +#define UPB_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("address"))) +#else +#define UPB_NO_SANITIZE_ADDRESS +#endif + +#if defined(__GNUC__) && (defined(__clang__) || UPB_GNUC_MIN(11, 0)) +#define UPB_RETAIN __attribute__((retain)) +#else +#define UPB_RETAIN +#endif + +// Linker arrays combine elements from multiple translation units into a single +// array that can be iterated over at runtime. +// +// It is an alternative to pre-main "registration" functions. +// +// Usage: +// +// // In N translation units. +// UPB_LINKARR_APPEND(foo_array) static int elems[3] = {1, 2, 3}; +// +// // At runtime: +// UPB_LINKARR_DECLARE(foo_array, int); +// +// void f() { +// const int* start = UPB_LINKARR_START(foo_array); +// const int* stop = UPB_LINKARR_STOP(foo_array); +// for (const int* p = start; p < stop; p++) { +// // Windows can introduce zero padding, so we have to skip zeroes. +// if (*p != 0) { +// vec.push_back(*p); +// } +// } +// } + +#if defined(__GNUC__) && !defined(__clang__) +// GCC can't handle mismatched retain attributes in the same section: +// https://github.com/protocolbuffers/protobuf/issues/26385 +// To work around this, we retain all linker array elements, even though this +// effectively disables tree-shaking of unused extensions when using GCC. +#define UPB_LINKARR_ATTR UPB_RETAIN +#else +#define UPB_LINKARR_ATTR +#endif + +#define UPB_LINKARR_SENTINEL UPB_RETAIN __attribute__((weak, used)) + +#if defined(__ELF__) || defined(__wasm__) + +#define UPB_LINKARR_APPEND(name) \ + __attribute__(( \ + section("linkarr_" #name))) UPB_LINKARR_ATTR UPB_NO_SANITIZE_ADDRESS +#define UPB_LINKARR_DECLARE(name, type) \ + extern type __start_linkarr_##name; \ + extern type __stop_linkarr_##name; \ + UPB_LINKARR_APPEND(name) \ + UPB_LINKARR_SENTINEL type UPB_linkarr_internal_empty_##name[1] +#define UPB_LINKARR_START(name) (&__start_linkarr_##name) +#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name) + +#elif defined(__MACH__) + +/* As described in: https://stackoverflow.com/a/22366882 */ +#define UPB_LINKARR_APPEND(name) \ + __attribute__(( \ + section("__DATA,__la_" #name))) UPB_LINKARR_ATTR UPB_NO_SANITIZE_ADDRESS +#define UPB_LINKARR_DECLARE(name, type) \ + extern type __start_linkarr_##name __asm( \ + "section$start$__DATA$__la_" #name); \ + extern type __stop_linkarr_##name __asm( \ + "section$end$__DATA$" \ + "__la_" #name); \ + UPB_LINKARR_APPEND(name) \ + UPB_LINKARR_SENTINEL type UPB_linkarr_internal_empty_##name[1] +#define UPB_LINKARR_START(name) (&__start_linkarr_##name) +#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name) + +#elif defined(_MSC_VER) + +/* See: + * https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155 + * https://devblogs.microsoft.com/oldnewthing/20181108-00/?p=100165 + * https://devblogs.microsoft.com/oldnewthing/20181109-00/?p=100175 */ +#define UPB_STRINGIFY_INTERNAL(x) #x +#define UPB_STRINGIFY(x) UPB_STRINGIFY_INTERNAL(x) +#define UPB_CONCAT(a, b, c) a##b##c +#define UPB_LINKARR_NAME(name, index) \ + UPB_STRINGIFY(UPB_CONCAT(la_, name, index)) +#define UPB_LINKARR_APPEND(name) \ + __pragma(section(UPB_LINKARR_NAME(name, $j), read)) \ + __declspec(allocate(UPB_LINKARR_NAME(name, $j))) +// clang-format off +#define UPB_LINKARR_DECLARE(name, type) \ + __pragma(message(UPB_LINKARR_NAME(name, $j))) \ + __pragma(section(UPB_LINKARR_NAME(name, $a), read)) \ + __pragma(section(UPB_LINKARR_NAME(name, $z), read)) \ + __declspec(allocate(UPB_LINKARR_NAME(name, $a)), selectany) \ + type __start_linkarr_##name; \ + __declspec(allocate(UPB_LINKARR_NAME(name, $z)), selectany) \ + type __stop_linkarr_##name; \ + UPB_LINKARR_APPEND(name) \ + __declspec(selectany) type UPB_linkarr_internal_empty_##name[1] = {0} +// clang-format on +#define UPB_LINKARR_START(name) (&__start_linkarr_##name) +#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name) + +#else + +// Linker arrays are not supported on this platform. Make macros no-ops. +#define UPB_LINKARR_APPEND(name) +#define UPB_LINKARR_DECLARE(name, type) +#define UPB_LINKARR_START(name) (NULL) +#define UPB_LINKARR_STOP(name) (NULL) + +#endif + +// Workaround for https://github.com/llvm/llvm-project/issues/167577 until it's +// fixed. Some function must exist for the constructor to work properly. +// TODO Remove this or gate it on a future version of clang. +#if defined(__clang__) && defined(__arm__) +#define _UPB_CONSTRUCTOR_PLACEHOLDER(unique_name) \ + __attribute__((used, visibility("hidden"))) void UPB_PRIVATE(unique_name)( \ + void) {} +#else +#define _UPB_CONSTRUCTOR_PLACEHOLDER(unique_name) +#endif + +#if defined(__ELF__) || defined(__wasm__) || defined(__MACH__) +#define UPB_CONSTRUCTOR(name, unique_name) \ + _UPB_CONSTRUCTOR_PLACEHOLDER(unique_name) \ + __attribute__((weak, visibility("hidden"), constructor)) void UPB_PRIVATE( \ + name)(void) +#elif defined(_MSC_VER) +/* + * See: https://stackoverflow.com/questions/1113409 + * + * The /include pragma suggested in the link above doesn't work in our case + * because it requires globally unique names. We need a different solution + * to prevent optimizers from removing the constructor. Our solution is to + * create a dummy exported weak symbol that prevent this stripping. + */ +#pragma section(".CRT$XCU", long, read) +#define UPB_CONSTRUCTOR(name, unique_name) \ + static void __cdecl UPB_PRIVATE(name)(void); \ + __declspec(allocate(".CRT$XCU"), selectany) void( \ + __cdecl * UPB_PRIVATE(name##_))(void) = UPB_PRIVATE(name); \ + __declspec(selectany, dllexport) void* UPB_PRIVATE(name##_force_linkage) = \ + &UPB_PRIVATE(name##_); \ + static void __cdecl UPB_PRIVATE(name)(void) + +#else +// No constructor support, nothing we can do except not break builds. +#define UPB_CONSTRUCTOR(name, unique_name) static void UPB_PRIVATE(name)(void) +#endif + +// +// Weak alias platform support. Theoretically this should be possible to do with +// only C using attributes like __attribute__((weak, alias("foo"))), but +// Clang doesn't support this properly on macOS. So we have to use assembly. +#if defined(__APPLE__) + +// TODO: once https://github.com/llvm/llvm-project/issues/167262 is fixed +// in the LLVM linker, we should have all weak variables point to a single +// "default" empty MiniTable instead of having each leaf define its own, like +// we do with ELF below. This will reduce binary size if many messages are tree +// shaken. +#define UPB_WEAK_SINGLETON_PLACEHOLDER_MINITABLE() +#define UPB_WEAK_PLACEHOLDER_MINITABLE(name) \ + __attribute__((weak)) const upb_MiniTable name = { \ + .UPB_PRIVATE(fields) = NULL, \ + .UPB_PRIVATE(size) = sizeof(struct upb_Message), \ + .UPB_PRIVATE(field_count) = 0, \ + .UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable, \ + .UPB_PRIVATE(dense_below) = 0, \ + .UPB_PRIVATE(table_mask) = -1, \ + .UPB_PRIVATE(required_count) = 0, \ + }; +#define UPB_WEAK_ALIAS(type, from, to) \ + extern type to; \ + __asm__(".globl _" #to); \ + __asm__(".private_extern _" #to); \ + __asm__(".set _" #to ", _" #from); \ + __asm__(".weak_definition _" #to); +#define UPB_STRONG_ALIAS(type, from, to) \ + __asm__(".globl _" #to); \ + __asm__(".private_extern _" #to); \ + __asm__(".set _" #to ", _" #from); + +#elif defined(__ELF__) + +// On ELF, weak aliases work properly, so we can have all weak MiniTables point +// to the same empty singleton MiniTable. This reduces code size if many +// MiniTables are tree shaken. +#define UPB_WEAK_SINGLETON_PLACEHOLDER_MINITABLE() \ + __attribute__((weak)) \ + const upb_MiniTable kUpb_WeakSingletonPlaceholderMiniTable = { \ + .UPB_PRIVATE(fields) = NULL, \ + .UPB_PRIVATE(size) = sizeof(struct upb_Message), \ + .UPB_PRIVATE(field_count) = 0, \ + .UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable, \ + .UPB_PRIVATE(dense_below) = 0, \ + .UPB_PRIVATE(table_mask) = -1, \ + .UPB_PRIVATE(required_count) = 0, \ + }; +#define UPB_WEAK_PLACEHOLDER_MINITABLE(name) +#define UPB_WEAK_ALIAS(type, from, to) \ + extern type to \ + __attribute__((weak, alias("kUpb_WeakSingletonPlaceholderMiniTable"))); +#define UPB_STRONG_ALIAS(type, from, to) \ + extern type to __attribute__((alias(#from))); + +#else +#define UPB_WEAK_SINGLETON_PLACEHOLDER_MINITABLE() +#define UPB_WEAK_PLACEHOLDER_MINITABLE(name) +#define UPB_WEAK_ALIAS(type, from, to) weak_alias_not_supported_on_this_platform +#define UPB_STRONG_ALIAS(type, from, to) \ + strong_alias_not_supported_on_this_platform +#endif + +// Future versions of upb will include breaking changes to some APIs. +// This macro can be set to enable these API changes ahead of time, so that +// user code can be updated before upgrading versions of protobuf. +#ifdef UPB_FUTURE_BREAKING_CHANGES + +#endif + +#ifndef UPB_GENERATED_CODE_SUPPORT_H_ +#define UPB_GENERATED_CODE_SUPPORT_H_ + +// This is a bit awkward; we want to conditionally include the fast decoder, +// but we generally don't let macros like UPB_FASTTABLE leak into user code. +// We can't #include "decode_fast.h" inside the port/def.inc, because the +// inc files strictly prohibit recursive inclusion, and decode_fast.h includes +// port/def.inc. So instead we use this two-part dance to conditionally include +// decode_fast.h. +#if UPB_FASTTABLE +#define UPB_INCLUDE_FAST_DECODE +#endif + +// IWYU pragma: begin_exports + +#ifndef UPB_BASE_UPCAST_H_ +#define UPB_BASE_UPCAST_H_ + +// Must be last. + +// This macro provides a way to upcast message pointers in a way that is +// somewhat more bulletproof than blindly casting a pointer. Example: +// +// typedef struct { +// upb_Message UPB_PRIVATE(base); +// } pkg_FooMessage; +// +// void f(pkg_FooMessage* msg) { +// upb_Decode(UPB_UPCAST(msg), ...); +// } + +#define UPB_UPCAST(x) (&(x)->base##_dont_copy_me__upb_internal_use_only) + + +#endif /* UPB_BASE_UPCAST_H_ */ + +#ifndef UPB_MESSAGE_ACCESSORS_H_ +#define UPB_MESSAGE_ACCESSORS_H_ + +#include + +#ifndef UPB_BASE_STRING_VIEW_H_ +#define UPB_BASE_STRING_VIEW_H_ + +#include + +// Must be last. + +#define UPB_STRINGVIEW_INIT(ptr, len) \ + { ptr, len } + +#define UPB_STRINGVIEW_FORMAT "%.*s" +#define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data + +// LINT.IfChange(struct_definition) +typedef struct { + const char* data; + size_t size; +} upb_StringView; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE upb_StringView upb_StringView_FromDataAndSize(const char* data, + size_t size) { + upb_StringView ret; + ret.data = data; + ret.size = size; + return ret; +} + +UPB_INLINE upb_StringView upb_StringView_FromString(const char* data) { + return upb_StringView_FromDataAndSize(data, strlen(data)); +} + +UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) { + return (a.size == b.size) && (!a.size || !memcmp(a.data, b.data, a.size)); +} + +// Compares StringViews following strcmp rules. +// Please note this comparison is neither unicode nor locale aware. +UPB_INLINE int upb_StringView_Compare(upb_StringView a, upb_StringView b) { + int result = memcmp(a.data, b.data, UPB_MIN(a.size, b.size)); + if (result != 0) return result; + if (a.size < b.size) { + return -1; + } else if (a.size > b.size) { + return 1; + } else { + return 0; + } +} + +// LINT.ThenChange( +// GoogleInternalName1, +// //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string, +// //depot/google3/third_party/upb/bits/typescript/string_view.ts +// ) + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_BASE_STRING_VIEW_H_ */ + +/* upb_Arena is a specific allocator implementation that uses arena allocation. + * The user provides an allocator that will be used to allocate the underlying + * arena blocks. Arenas by nature do not require the individual allocations + * to be freed. However the Arena does allow users to register cleanup + * functions that will run when the arena is destroyed. + * + * A upb_Arena is *not* thread-safe, although some functions related to its + * managing its lifetime are, and are documented as such. + * + * You could write a thread-safe arena allocator that satisfies the + * upb_alloc interface, but it would not be as efficient for the + * single-threaded case. */ + +#ifndef UPB_MEM_ARENA_H_ +#define UPB_MEM_ARENA_H_ + +#include +#include + + +#ifndef UPB_MEM_ALLOC_H_ +#define UPB_MEM_ALLOC_H_ + +#include + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct upb_alloc upb_alloc; + +/* A combined `malloc()`/`free()` function. + * If `size` is 0 then the function acts like `free()`, otherwise it acts like + * `realloc()`. Only `oldsize` bytes from a previous allocation are + * preserved. If `actual_size` is not null and the allocator supports it, the + * actual size of the resulting allocation is stored in `actual_size`. If + * `actual_size` is not null, you must zero out the memory pointed to by + * `actual_size` before calling. */ +typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize, + size_t size, size_t* actual_size); + +/* A upb_alloc is a possibly-stateful allocator object. + * + * It could either be an arena allocator (which doesn't require individual + * `free()` calls) or a regular `malloc()` (which does). The client must + * therefore free memory unless it knows that the allocator is an arena + * allocator. */ +struct upb_alloc { + upb_alloc_func* func; +}; + +UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) { + UPB_ASSERT(alloc); + return alloc->func(alloc, NULL, 0, size, NULL); +} + +typedef struct { + void* p; + size_t n; +} upb_SizedPtr; + +UPB_INLINE upb_SizedPtr upb_SizeReturningMalloc(upb_alloc* alloc, size_t size) { + UPB_ASSERT(alloc); + upb_SizedPtr result; + result.n = 0; + result.p = alloc->func(alloc, NULL, 0, size, &result.n); + result.n = result.p != NULL ? UPB_MAX(result.n, size) : 0; + return result; +} + +UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize, + size_t size) { + UPB_ASSERT(alloc); + return alloc->func(alloc, ptr, oldsize, size, NULL); +} + +UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) { + UPB_ASSERT(alloc); + alloc->func(alloc, ptr, 0, 0, NULL); +} + +UPB_INLINE void upb_free_sized(upb_alloc* alloc, void* ptr, size_t size) { + UPB_ASSERT(alloc); + alloc->func(alloc, ptr, size, 0, NULL); +} + +// The global allocator used by upb. Uses the standard malloc()/free(). + +extern upb_alloc upb_alloc_global; + +/* Functions that hard-code the global malloc. + * + * We still get benefit because we can put custom logic into our global + * allocator, like injecting out-of-memory faults in debug/testing builds. */ + +UPB_INLINE void* upb_gmalloc(size_t size) { + return upb_malloc(&upb_alloc_global, size); +} + +UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) { + return upb_realloc(&upb_alloc_global, ptr, oldsize, size); +} + +UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); } + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MEM_ALLOC_H_ */ + +#ifndef UPB_MEM_INTERNAL_ARENA_H_ +#define UPB_MEM_INTERNAL_ARENA_H_ + +#include +#include +#include + + +#ifndef UPB_PORT_SANITIZERS_H_ +#define UPB_PORT_SANITIZERS_H_ + +#include +#include +#include + +// Must be last. + +// Must be inside def.inc/undef.inc +#if UPB_HWASAN +#include +#endif + +#if UPB_MSAN +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// UPB_ARENA_SIZE_HACK depends on this struct having size 1. +typedef struct { + uint8_t state; +} upb_Xsan; + +UPB_INLINE uint8_t _upb_Xsan_NextTag(upb_Xsan *xsan) { +#if UPB_HWASAN + xsan->state++; + if (xsan->state <= UPB_HWASAN_POISON_TAG) { + xsan->state = UPB_HWASAN_POISON_TAG + 1; + } + return xsan->state; +#else + UPB_UNUSED(xsan); + return 0; +#endif +} + +enum { +#if UPB_ASAN + UPB_PRIVATE(kUpb_Asan_GuardSize) = 32, +#else + UPB_PRIVATE(kUpb_Asan_GuardSize) = 0, +#endif +}; + +UPB_INLINE uint8_t UPB_PRIVATE(_upb_Xsan_GetTag)(const void *addr) { +#if UPB_HWASAN + return __hwasan_get_tag_from_pointer(addr); +#else + UPB_UNUSED(addr); + return 0; +#endif +} + +UPB_INLINE void UPB_PRIVATE(upb_Xsan_Init)(upb_Xsan *xsan) { +#if UPB_HWASAN || UPB_TSAN + xsan->state = 0; +#else + UPB_UNUSED(xsan); +#endif +} + +UPB_INLINE void UPB_PRIVATE(upb_Xsan_MarkInitialized)(void* addr, size_t size) { +#if UPB_HAS_FEATURE(memory_sanitizer) + if (size) { + __msan_unpoison(addr, size); + } +#else + UPB_UNUSED(addr); + UPB_UNUSED(size); +#endif +} + +// Marks the given region as poisoned, meaning that it is not accessible until +// it is unpoisoned. +UPB_INLINE void UPB_PRIVATE(upb_Xsan_PoisonRegion)(const void *addr, + size_t size) { +#if UPB_ASAN + void __asan_poison_memory_region(void const volatile *addr, size_t size); + __asan_poison_memory_region(addr, size); +#elif UPB_HWASAN + __hwasan_tag_memory(addr, UPB_HWASAN_POISON_TAG, UPB_ALIGN_MALLOC(size)); +#else + UPB_UNUSED(addr); + UPB_UNUSED(size); +#endif +} + +UPB_INLINE void *UPB_PRIVATE(_upb_Xsan_UnpoisonRegion)(void *addr, size_t size, + uint8_t tag) { +#if UPB_ASAN + UPB_UNUSED(tag); + void __asan_unpoison_memory_region(void const volatile *addr, size_t size); + __asan_unpoison_memory_region(addr, size); + return addr; +#elif UPB_HWASAN + __hwasan_tag_memory(addr, tag, UPB_ALIGN_MALLOC(size)); + return __hwasan_tag_pointer(addr, tag); +#else + UPB_UNUSED(size); + UPB_UNUSED(tag); + + // `addr` is the pointer that will be returned from arena alloc/realloc + // functions. In this code-path we know it must be non-NULL, but the compiler + // doesn't know this unless we add a UPB_ASSUME() annotation. + // + // This will let the optimizer optimize away NULL-checks if it can see that + // this path was taken. + UPB_ASSUME(addr); + return addr; +#endif +} + +// Allows users to read and write to the given region, which will be considered +// distinct from other regions and may only be accessed through the returned +// pointer. +// +// `addr` must be aligned to the malloc alignment. Size may be unaligned, +// and with ASAN we can respect `size` precisely, but with HWASAN we must +// round `size` up to the next multiple of the malloc alignment, so the caller +// must guarantee that rounding up `size` will not cause overlap with other +// regions. +UPB_INLINE void *UPB_PRIVATE(upb_Xsan_NewUnpoisonedRegion)(upb_Xsan *xsan, + void *addr, + size_t size) { + return UPB_PRIVATE(_upb_Xsan_UnpoisonRegion)(addr, size, + _upb_Xsan_NextTag(xsan)); +} + +// Resizes the given region to a new size, *without* invalidating any existing +// pointers to the region. +// +// `tagged_addr` must be a pointer that was previously returned from +// `upb_Xsan_NewUnpoisonedRegion`. `old_size` must be the size that was +// originally passed to `upb_Xsan_NewUnpoisonedRegion`. +UPB_INLINE void *UPB_PRIVATE(upb_Xsan_ResizeUnpoisonedRegion)(void *tagged_addr, + size_t old_size, + size_t new_size) { + UPB_PRIVATE(upb_Xsan_PoisonRegion)(tagged_addr, old_size); + return UPB_PRIVATE(_upb_Xsan_UnpoisonRegion)( + tagged_addr, new_size, UPB_PRIVATE(_upb_Xsan_GetTag)(tagged_addr)); +} + +// Compares two pointers and returns true if they are equal. This returns the +// correct result even if one or both of the pointers are tagged. +UPB_INLINE bool UPB_PRIVATE(upb_Xsan_PtrEq)(const void *a, const void *b) { +#if UPB_HWASAN + return __hwasan_tag_pointer(a, 0) == __hwasan_tag_pointer(b, 0); +#else + return a == b; +#endif +} + +// These annotations improve TSAN's ability to detect data races. By +// proactively accessing a non-atomic variable at the point where it is +// "logically" accessed, we can trigger TSAN diagnostics that might have +// otherwise been masked by subsequent atomic operations. + +UPB_INLINE void UPB_PRIVATE(upb_Xsan_AccessReadOnly)(upb_Xsan *xsan) { +#if UPB_TSAN + // For performance we avoid using a volatile variable. + __asm__ volatile("" ::"r"(xsan->state)); +#else + UPB_UNUSED(xsan); +#endif +} + +UPB_INLINE void UPB_PRIVATE(upb_Xsan_AccessReadWrite)(upb_Xsan *xsan) { +#if UPB_TSAN + // For performance we avoid using a volatile variable. + __asm__ volatile("" : "+r"(xsan->state)); +#else + UPB_UNUSED(xsan); +#endif +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_PORT_SANITIZERS_H_ + +// Must be last. + +// This is QUITE an ugly hack, which specifies the number of pointers needed +// to equal (or exceed) the storage required for one upb_Arena. +// +// We need this because the decoder inlines a upb_Arena for performance but +// the full struct is not visible outside of arena.c. Yes, I know, it's awful. +#ifndef NDEBUG +#define UPB_ARENA_BASE_SIZE_HACK 10 +#else +#define UPB_ARENA_BASE_SIZE_HACK 9 +#endif + +#define UPB_ARENA_SIZE_HACK \ + (sizeof(void*) * (UPB_ARENA_BASE_SIZE_HACK + (UPB_XSAN_STRUCT_SIZE * 2))) + \ + (sizeof(uint32_t) * 2) + +// LINT.IfChange(upb_Arena) + +struct upb_Arena { + char* UPB_ONLYBITS(ptr); + const UPB_NODEREF char* UPB_ONLYBITS(end); + UPB_XSAN_MEMBER +}; + +// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/arena.ts:upb_Arena) + +#ifdef __cplusplus +extern "C" { +#endif + +void UPB_PRIVATE(_upb_Arena_SwapIn)(struct upb_Arena* des, + const struct upb_Arena* src); +void UPB_PRIVATE(_upb_Arena_SwapOut)(struct upb_Arena* des, + const struct upb_Arena* src); + +UPB_INLINE size_t UPB_PRIVATE(_upb_ArenaHas)(const struct upb_Arena* a) { + return (size_t)(a->UPB_ONLYBITS(end) - a->UPB_ONLYBITS(ptr)); +} + +UPB_INLINE size_t UPB_PRIVATE(_upb_Arena_AllocSpan)(size_t size) { + return UPB_ALIGN_MALLOC(size) + UPB_PRIVATE(kUpb_Asan_GuardSize); +} + +UPB_INLINE bool UPB_PRIVATE(_upb_Arena_WasLastAllocFromCurrentBlock)( + const struct upb_Arena* a, void* ptr, size_t size) { + return UPB_PRIVATE(upb_Xsan_PtrEq)( + (char*)ptr + UPB_PRIVATE(_upb_Arena_AllocSpan)(size), + a->UPB_ONLYBITS(ptr)); +} + +UPB_INLINE bool UPB_PRIVATE(_upb_Arena_IsAligned)(const void* ptr) { + return (uintptr_t)ptr % UPB_MALLOC_ALIGN == 0; +} + +UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size) { + UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(a)); + + size_t span = UPB_PRIVATE(_upb_Arena_AllocSpan)(size); + + if (UPB_UNLIKELY(UPB_PRIVATE(_upb_ArenaHas)(a) < span)) { + void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(struct upb_Arena * a, size_t size); + return UPB_PRIVATE(_upb_Arena_SlowMalloc)(a, span); + } + + // We have enough space to do a fast malloc. + void* ret = a->UPB_ONLYBITS(ptr); + a->UPB_ONLYBITS(ptr) += span; + UPB_ASSERT(UPB_PRIVATE(_upb_Arena_IsAligned)(ret)); + UPB_ASSERT(UPB_PRIVATE(_upb_Arena_IsAligned)(a->UPB_ONLYBITS(ptr))); + + return UPB_PRIVATE(upb_Xsan_NewUnpoisonedRegion)(UPB_XSAN(a), ret, size); +} + +UPB_API_INLINE void upb_Arena_ShrinkLast(struct upb_Arena* a, void* ptr, + size_t oldsize, size_t size) { + UPB_ASSERT(ptr); + UPB_ASSERT(size <= oldsize); + + UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(a)); + UPB_PRIVATE(upb_Xsan_ResizeUnpoisonedRegion)(ptr, oldsize, size); + + if (UPB_PRIVATE(_upb_Arena_WasLastAllocFromCurrentBlock)(a, ptr, oldsize)) { + // We can reclaim some memory. + a->UPB_ONLYBITS(ptr) -= UPB_ALIGN_MALLOC(oldsize) - UPB_ALIGN_MALLOC(size); + } else { + // We can't reclaim any memory, but we need to verify that `ptr` really + // does represent the most recent allocation. +#ifndef NDEBUG + bool _upb_Arena_WasLastAlloc(struct upb_Arena * a, void* ptr, + size_t oldsize); + UPB_ASSERT(_upb_Arena_WasLastAlloc(a, ptr, oldsize)); +#endif + } +} + +UPB_API_INLINE bool upb_Arena_TryExtend(struct upb_Arena* a, void* ptr, + size_t oldsize, size_t size) { + UPB_ASSERT(ptr); + UPB_ASSERT(size > oldsize); + + UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(a)); + size_t extend = UPB_ALIGN_MALLOC(size) - UPB_ALIGN_MALLOC(oldsize); + + if (UPB_PRIVATE(_upb_Arena_WasLastAllocFromCurrentBlock)(a, ptr, oldsize) && + UPB_PRIVATE(_upb_ArenaHas)(a) >= extend) { + a->UPB_ONLYBITS(ptr) += extend; + UPB_PRIVATE(upb_Xsan_ResizeUnpoisonedRegion)(ptr, oldsize, size); + return true; + } + + return false; +} + +UPB_API_INLINE void* upb_Arena_Realloc(struct upb_Arena* a, void* ptr, + size_t oldsize, size_t size) { + UPB_PRIVATE(upb_Xsan_AccessReadWrite)(UPB_XSAN(a)); + + void* ret; + + if (ptr && (size <= oldsize || upb_Arena_TryExtend(a, ptr, oldsize, size))) { + // We can extend or shrink in place. + if (size <= oldsize && + UPB_PRIVATE(_upb_Arena_WasLastAllocFromCurrentBlock)(a, ptr, oldsize)) { + upb_Arena_ShrinkLast(a, ptr, oldsize, size); + } + ret = ptr; + } else { + // We need to copy into a new allocation. + ret = upb_Arena_Malloc(a, size); + if (ret && oldsize > 0) { + memcpy(ret, ptr, UPB_MIN(oldsize, size)); + } + } + + if (ret) { + // We want to invalidate pointers to the old region if hwasan is enabled, so + // we poison and unpoison even if ptr == ret. However, if reallocation fails + // we do not want to poison the old memory, or attempt to poison null. + UPB_PRIVATE(upb_Xsan_PoisonRegion)(ptr, oldsize); + return UPB_PRIVATE(upb_Xsan_NewUnpoisonedRegion)(UPB_XSAN(a), ret, size); + } + return ret; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MEM_INTERNAL_ARENA_H_ */ + +// Must be last. + +typedef struct upb_Arena upb_Arena; + +typedef void upb_AllocCleanupFunc(upb_alloc* alloc); + +#ifdef __cplusplus +extern "C" { +#endif + +// Creates an arena from the given initial block (if any -- mem may be NULL). If +// an initial block is specified, the arena's lifetime cannot be extended by +// |upb_Arena_IncRefFor| or |upb_Arena_Fuse|. Additional blocks will be +// allocated from |alloc|. If |alloc| is NULL, this is a fixed-size arena and +// cannot grow. If an initial block is specified, |n| is its length; if there is +// no initial block, |n| is a hint of the size that should be allocated for the +// first block of the arena, such that `upb_Arena_Malloc(hint)` will not require +// another call to |alloc|. +UPB_API upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc); + +UPB_API void upb_Arena_Free(upb_Arena* a); +// Sets the cleanup function for the upb_alloc used by the arena. Only one +// cleanup function can be set, which will be called after all blocks are +// freed. +UPB_API void upb_Arena_SetAllocCleanup(upb_Arena* a, + upb_AllocCleanupFunc* func); + +// Fuses the lifetime of two arenas, such that no arenas that have been +// transitively fused together will be freed until all of them have reached a +// zero refcount. This operation is safe to use concurrently from multiple +// threads. +UPB_API bool upb_Arena_Fuse(const upb_Arena* a, const upb_Arena* b); + +// This operation is safe to use concurrently from multiple threads. +UPB_API bool upb_Arena_IsFused(const upb_Arena* a, const upb_Arena* b); + +// Returns the upb_alloc used by the arena. +UPB_API upb_alloc* upb_Arena_GetUpbAlloc(upb_Arena* a); + +// This operation is safe to use concurrently from multiple threads. +bool upb_Arena_IncRefFor(const upb_Arena* a, const void* owner); +// This operation is safe to use concurrently from multiple threads. +void upb_Arena_DecRefFor(const upb_Arena* a, const void* owner); + +// Creates a reference between the arenas `from` and `to`, guaranteeing that +// the latter will not be freed until `from` is freed. +// +// Users must avoid all of the following error conditions, which will be +// checked in debug mode but are UB in opt: +// +// - Creating reference cycles between arenas. +// - Creating a reference between two arenas that are fused, either now +// or in the future. +// +// Creating a reference multiple times between the same two arenas is not UB but +// is considered wasteful and may be disallowed in the future. +// +// Note that fuses can participate in reference cycles. The following set of +// calls creates a cycle A -> B -> C -> A +// Fuse(A, B); +// Ref(B, C); +// Ref(C, A); +// +// From this perspective, the second rule is just a special-case of the first. +// This set of calls is disallowed because it is effectively creating a +// cycle A -> B -> A +// Fuse(A, B); +// Ref(B, A); +// +// Fuse is special because it creates what is effectively a bidirectional +// ref, but it is not considered a cycle and will be collected correctly. +// +// Note that `from` is not `const`, so it may not be called concurrently +// with any other function on `from`. +// +// Returns whether the reference was created successfully. +bool upb_Arena_RefArena(upb_Arena* from, const upb_Arena* to); + +#ifndef NDEBUG +// Returns true if upb_Arena_RefArena(from, to) was previously called. +// Note that this does not take fuses into account, and it does not follow +// chains of references; it must have been these two arenas exactly that +// created a reference. +bool upb_Arena_HasRef(const upb_Arena* from, const upb_Arena* to); +#endif + +// This operation is safe to use concurrently from multiple threads. +uintptr_t upb_Arena_SpaceAllocated(const upb_Arena* a, size_t* fused_count); +// This operation is safe to use concurrently from multiple threads. +uint32_t upb_Arena_DebugRefCount(const upb_Arena* a); + +#if UPB_ENABLE_REF_CYCLE_CHECKS +// Returns true if there is a chain of arena refs that spans `from` -> `to`. +// Fused arenas are taken into account; for example, this series of calls +// will cause the function to return true: +// +// 1. upb_Arena_Fuse(a, b) +// 2. upb_Arena_RefArena(from, a) +// 3. upb_Arena_RefArena(b, to) +// +// However this function does not return true if `from` and `to` are directly +// fused. +bool upb_Arena_HasRefChain(const upb_Arena* from, const upb_Arena* to); +#endif + +UPB_API_INLINE upb_Arena* upb_Arena_New(void) { + return upb_Arena_Init(NULL, 0, &upb_alloc_global); +} + +UPB_API_INLINE upb_Arena* upb_Arena_NewSized(size_t size_hint) { + return upb_Arena_Init(NULL, size_hint, &upb_alloc_global); +} + +UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size); + +UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize, + size_t size); + +static const size_t UPB_PRIVATE(kUpbDefaultMaxBlockSize) = + UPB_DEFAULT_MAX_BLOCK_SIZE; + +// Sets the maximum block size for all arenas. This is a global configuration +// setting that will affect all existing and future arenas. If +// upb_Arena_Malloc() is called with a size larger than this, we will exceed +// this size and allocate a larger block. +// +// This API is meant for experimentation only. It will likely be removed in +// the future. +// This operation is safe to use concurrently from multiple threads. +void upb_Arena_SetMaxBlockSize(size_t max); + +// Shrinks the last alloc from arena. +// REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena. +// We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if +// this was not the last alloc. +UPB_API_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr, + size_t oldsize, size_t size); + +// Attempts to extend the given alloc from arena, in place. Is generally +// only likely to succeed for the most recent allocation from this arena. If it +// succeeds, returns true and `ptr`'s allocation is now `size` rather than +// `oldsize`. Returns false if the allocation cannot be extended; `ptr`'s +// allocation is unmodified. See also upb_Arena_Realloc. +// REQUIRES: `size > oldsize`; to shrink, use `upb_Arena_Realloc` or +// `upb_Arena_ShrinkLast`. +UPB_API_INLINE bool upb_Arena_TryExtend(upb_Arena* a, void* ptr, size_t oldsize, + size_t size); + +#ifdef UPB_TRACING_ENABLED +void upb_Arena_SetTraceHandler(void (*initArenaTraceHandler)(const upb_Arena*, + size_t size), + void (*fuseArenaTraceHandler)(const upb_Arena*, + const upb_Arena*), + void (*freeArenaTraceHandler)(const upb_Arena*)); +#endif + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MEM_ARENA_H_ */ + +#ifndef UPB_MESSAGE_ARRAY_H_ +#define UPB_MESSAGE_ARRAY_H_ + +#include + + +#ifndef UPB_BASE_DESCRIPTOR_CONSTANTS_H_ +#define UPB_BASE_DESCRIPTOR_CONSTANTS_H_ + +// Must be last. + +// The types a field can have. Note that this list is not identical to the +// types defined in descriptor.proto, which gives INT32 and SINT32 separate +// types (we distinguish the two with the "integer encoding" enum below). +// This enum is an internal convenience only and has no meaning outside of upb. +typedef enum { + kUpb_CType_Bool = 1, + kUpb_CType_Float = 2, + kUpb_CType_Int32 = 3, + kUpb_CType_UInt32 = 4, + kUpb_CType_Enum = 5, // Enum values are int32. TODO: rename + kUpb_CType_Message = 6, + kUpb_CType_Double = 7, + kUpb_CType_Int64 = 8, + kUpb_CType_UInt64 = 9, + kUpb_CType_String = 10, + kUpb_CType_Bytes = 11 +} upb_CType; + +// The repeated-ness of each field; this matches descriptor.proto. +typedef enum { + kUpb_Label_Optional = 1, + kUpb_Label_Required = 2, + kUpb_Label_Repeated = 3 +} upb_Label; + +// Descriptor types, as defined in descriptor.proto. +typedef enum { + kUpb_FieldType_Double = 1, + kUpb_FieldType_Float = 2, + kUpb_FieldType_Int64 = 3, + kUpb_FieldType_UInt64 = 4, + kUpb_FieldType_Int32 = 5, + kUpb_FieldType_Fixed64 = 6, + kUpb_FieldType_Fixed32 = 7, + kUpb_FieldType_Bool = 8, + kUpb_FieldType_String = 9, + kUpb_FieldType_Group = 10, + kUpb_FieldType_Message = 11, + kUpb_FieldType_Bytes = 12, + kUpb_FieldType_UInt32 = 13, + kUpb_FieldType_Enum = 14, + kUpb_FieldType_SFixed32 = 15, + kUpb_FieldType_SFixed64 = 16, + kUpb_FieldType_SInt32 = 17, + kUpb_FieldType_SInt64 = 18, +} upb_FieldType; + +#define kUpb_FieldType_SizeOf 19 + +#ifdef __cplusplus +extern "C" { +#endif + +// Convert from upb_FieldType to upb_CType +UPB_INLINE upb_CType upb_FieldType_CType(upb_FieldType field_type) { + static const upb_CType c_type[] = { + kUpb_CType_Double, // kUpb_FieldType_Double + kUpb_CType_Float, // kUpb_FieldType_Float + kUpb_CType_Int64, // kUpb_FieldType_Int64 + kUpb_CType_UInt64, // kUpb_FieldType_UInt64 + kUpb_CType_Int32, // kUpb_FieldType_Int32 + kUpb_CType_UInt64, // kUpb_FieldType_Fixed64 + kUpb_CType_UInt32, // kUpb_FieldType_Fixed32 + kUpb_CType_Bool, // kUpb_FieldType_Bool + kUpb_CType_String, // kUpb_FieldType_String + kUpb_CType_Message, // kUpb_FieldType_Group + kUpb_CType_Message, // kUpb_FieldType_Message + kUpb_CType_Bytes, // kUpb_FieldType_Bytes + kUpb_CType_UInt32, // kUpb_FieldType_UInt32 + kUpb_CType_Enum, // kUpb_FieldType_Enum + kUpb_CType_Int32, // kUpb_FieldType_SFixed32 + kUpb_CType_Int64, // kUpb_FieldType_SFixed64 + kUpb_CType_Int32, // kUpb_FieldType_SInt32 + kUpb_CType_Int64, // kUpb_FieldType_SInt64 + }; + + // -1 here because the enum is one-based but the table is zero-based. + return c_type[field_type - 1]; +} + +UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType field_type) { + // clang-format off + const unsigned kUnpackableTypes = + (1 << kUpb_FieldType_String) | + (1 << kUpb_FieldType_Bytes) | + (1 << kUpb_FieldType_Message) | + (1 << kUpb_FieldType_Group); + // clang-format on + return (1 << field_type) & ~kUnpackableTypes; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_BASE_DESCRIPTOR_CONSTANTS_H_ */ + +#ifndef UPB_MESSAGE_INTERNAL_ARRAY_H_ +#define UPB_MESSAGE_INTERNAL_ARRAY_H_ + +#include +#include + + +// Must be last. + +#define _UPB_ARRAY_MASK_IMM 0x4 // Frozen/immutable bit. +#define _UPB_ARRAY_MASK_LG2 0x3 // Encoded elem size. +#define _UPB_ARRAY_MASK_ALL (_UPB_ARRAY_MASK_IMM | _UPB_ARRAY_MASK_LG2) + +#ifdef __cplusplus +extern "C" { +#endif + +// LINT.IfChange(upb_Array) + +// Our internal representation for repeated fields. +struct upb_Array { + // This is a tagged pointer. Bits #0 and #1 encode the elem size as follows: + // 0 maps to elem size 1 + // 1 maps to elem size 4 + // 2 maps to elem size 8 + // 3 maps to elem size 16 + // + // Bit #2 contains the frozen/immutable flag. + uintptr_t UPB_ONLYBITS(data); + + size_t UPB_ONLYBITS(size); // The number of elements in the array. + size_t UPB_PRIVATE(capacity); // Allocated storage. Measured in elements. +}; + +UPB_INLINE void UPB_PRIVATE(_upb_Array_ShallowFreeze)(struct upb_Array* arr) { + arr->UPB_ONLYBITS(data) |= _UPB_ARRAY_MASK_IMM; +} + +UPB_API_INLINE bool upb_Array_IsFrozen(const struct upb_Array* arr) { + return (arr->UPB_ONLYBITS(data) & _UPB_ARRAY_MASK_IMM) != 0; +} + +UPB_INLINE void UPB_PRIVATE(_upb_Array_SetTaggedPtr)(struct upb_Array* array, + void* data, size_t lg2) { + UPB_ASSERT(lg2 != 1); + UPB_ASSERT(lg2 <= 4); + const size_t bits = lg2 - (lg2 != 0); + array->UPB_ONLYBITS(data) = (uintptr_t)data | bits; +} + +UPB_INLINE size_t +UPB_PRIVATE(_upb_Array_ElemSizeLg2)(const struct upb_Array* array) { + const size_t bits = array->UPB_ONLYBITS(data) & _UPB_ARRAY_MASK_LG2; + const size_t lg2 = bits + (bits != 0); + return lg2; +} + +UPB_API_INLINE const void* upb_Array_DataPtr(const struct upb_Array* array) { + UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array); // Check assertions. + return (void*)(array->UPB_ONLYBITS(data) & ~(uintptr_t)_UPB_ARRAY_MASK_ALL); +} + +UPB_API_INLINE void* upb_Array_MutableDataPtr(struct upb_Array* array) { + return (void*)upb_Array_DataPtr(array); +} + +UPB_INLINE struct upb_Array* UPB_PRIVATE(_upb_Array_NewMaybeAllowSlow)( + upb_Arena* arena, size_t init_capacity, int elem_size_lg2, + bool allow_slow) { + UPB_ASSERT(elem_size_lg2 != 1); + UPB_ASSERT(elem_size_lg2 <= 4); + const size_t array_size = + UPB_ALIGN_UP(sizeof(struct upb_Array), UPB_MALLOC_ALIGN); + const size_t bytes = array_size + (init_capacity << elem_size_lg2); + size_t span = UPB_PRIVATE(_upb_Arena_AllocSpan)(bytes); + if (!allow_slow && UPB_PRIVATE(_upb_ArenaHas)(arena) < span) return NULL; + struct upb_Array* array = (struct upb_Array*)upb_Arena_Malloc(arena, bytes); + if (!array) return NULL; + UPB_PRIVATE(_upb_Array_SetTaggedPtr) + (array, UPB_PTR_AT(array, array_size, void), (size_t)elem_size_lg2); + array->UPB_ONLYBITS(size) = 0; + array->UPB_PRIVATE(capacity) = init_capacity; + return array; +} + +UPB_INLINE struct upb_Array* UPB_PRIVATE(_upb_Array_New)(upb_Arena* arena, + size_t init_capacity, + int elem_size_lg2) { + return UPB_PRIVATE(_upb_Array_NewMaybeAllowSlow)(arena, init_capacity, + elem_size_lg2, true); +} + +UPB_INLINE struct upb_Array* UPB_PRIVATE(_upb_Array_TryFastNew)( + upb_Arena* arena, size_t init_capacity, int elem_size_lg2) { + return UPB_PRIVATE(_upb_Array_NewMaybeAllowSlow)(arena, init_capacity, + elem_size_lg2, false); +} + +// Resizes the capacity of the array to be at least min_size. +bool UPB_PRIVATE(_upb_Array_Realloc)(struct upb_Array* array, size_t min_size, + upb_Arena* arena); + +UPB_FORCEINLINE +bool UPB_PRIVATE(_upb_Array_TryFastRealloc)(struct upb_Array* array, + size_t capacity, int elem_size_lg2, + upb_Arena* arena) { + size_t old_bytes = array->UPB_PRIVATE(capacity) << elem_size_lg2; + size_t new_bytes = capacity << elem_size_lg2; + UPB_ASSUME(new_bytes > old_bytes); + if (!upb_Arena_TryExtend(arena, array, old_bytes, new_bytes)) return false; + array->UPB_PRIVATE(capacity) = capacity; + return true; +} + +UPB_API_INLINE bool upb_Array_Reserve(struct upb_Array* array, size_t size, + upb_Arena* arena) { + UPB_ASSERT(!upb_Array_IsFrozen(array)); + if (array->UPB_PRIVATE(capacity) < size) + return UPB_PRIVATE(_upb_Array_Realloc)(array, size, arena); + return true; +} + +// Resize without initializing new elements. +UPB_INLINE bool UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + struct upb_Array* array, size_t size, upb_Arena* arena) { + UPB_ASSERT(!upb_Array_IsFrozen(array)); + UPB_ASSERT(size <= array->UPB_ONLYBITS(size) || + arena); // Allow NULL arena when shrinking. + if (!upb_Array_Reserve(array, size, arena)) return false; + array->UPB_ONLYBITS(size) = size; + return true; +} + +// This function is intended for situations where elem_size is compile-time +// constant or a known expression of the form (1 << lg2), so that the expression +// i*elem_size does not result in an actual multiplication. +UPB_INLINE void UPB_PRIVATE(_upb_Array_Set)(struct upb_Array* array, size_t i, + const void* data, + size_t elem_size) { + UPB_ASSERT(!upb_Array_IsFrozen(array)); + UPB_ASSERT(i < array->UPB_ONLYBITS(size)); + UPB_ASSERT(elem_size == 1U << UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array)); + char* arr_data = (char*)upb_Array_MutableDataPtr(array); + memcpy(arr_data + (i * elem_size), data, elem_size); +} + +UPB_API_INLINE size_t upb_Array_Size(const struct upb_Array* arr) { + return arr->UPB_ONLYBITS(size); +} + +UPB_API_INLINE size_t upb_Array_Capacity(const struct upb_Array* arr) { + return arr->UPB_PRIVATE(capacity); +} + +// LINT.ThenChange(GoogleInternalName0) + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#undef _UPB_ARRAY_MASK_IMM +#undef _UPB_ARRAY_MASK_LG2 +#undef _UPB_ARRAY_MASK_ALL + + +#endif /* UPB_MESSAGE_INTERNAL_ARRAY_H_ */ + +#ifndef UPB_MESSAGE_INTERNAL_TYPES_H_ +#define UPB_MESSAGE_INTERNAL_TYPES_H_ + +#include + +// Must be last. + +#define UPB_OPAQUE(x) x##_opaque + +struct upb_Message { + union { + uintptr_t UPB_OPAQUE(internal); // tagged pointer, low bit == frozen + double d; // Forces same size for 32-bit/64-bit builds + }; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE void UPB_PRIVATE(_upb_Message_ShallowFreeze)( + struct upb_Message* msg) { + msg->UPB_OPAQUE(internal) |= 1ULL; +} + +UPB_API_INLINE bool upb_Message_IsFrozen(const struct upb_Message* msg) { + return (msg->UPB_OPAQUE(internal) & 1ULL) != 0; +} + +UPB_INLINE struct upb_Message_Internal* UPB_PRIVATE(_upb_Message_GetInternal)( + const struct upb_Message* msg) { + const uintptr_t tmp = msg->UPB_OPAQUE(internal) & ~1ULL; + return (struct upb_Message_Internal*)tmp; +} + +UPB_INLINE void UPB_PRIVATE(_upb_Message_SetInternal)( + struct upb_Message* msg, struct upb_Message_Internal* internal) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + msg->UPB_OPAQUE(internal) = (uintptr_t)internal; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#undef UPB_OPAQUE + + +#endif /* UPB_MESSAGE_INTERNAL_TYPES_H_ */ + +// Users should include array.h or map.h instead. +// IWYU pragma: private, include "upb/message/array.h" + +#ifndef UPB_MESSAGE_VALUE_H_ +#define UPB_MESSAGE_VALUE_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +typedef union { + bool bool_val; + float float_val; + double double_val; + int32_t int32_val; + int64_t int64_val; + uint32_t uint32_val; + uint64_t uint64_val; + const struct upb_Array* array_val; + const struct upb_Map* map_val; + const struct upb_Message* msg_val; + upb_StringView str_val; + + // For an extension field, we are essentially treating ext->data (a + // upb_MessageValue) as if it were a message with one field that lives at + // offset 0. This works because upb_MessageValue is precisely one value that + // can hold any type of data. Recall that an extension can be of any type + // (scalar, repeated, or message). For a message extension, that will be a + // single upb_Message* at offset 0 of the upb_MessageValue. + struct upb_Message UPB_PRIVATE(ext_msg_val); +} upb_MessageValue; + +UPB_API_INLINE upb_MessageValue upb_MessageValue_Zero(void) { + upb_MessageValue zero; + memset(&zero, 0, sizeof(zero)); + return zero; +} + +typedef union { + struct upb_Array* array; + struct upb_Map* map; + struct upb_Message* msg; +} upb_MutableMessageValue; + +UPB_API_INLINE upb_MutableMessageValue upb_MutableMessageValue_Zero(void) { + upb_MutableMessageValue zero; + memset(&zero, 0, sizeof(zero)); + return zero; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_VALUE_H_ */ + +#ifndef UPB_MINI_TABLE_MESSAGE_H_ +#define UPB_MINI_TABLE_MESSAGE_H_ + +#include + + +#ifndef UPB_MINI_TABLE_ENUM_H_ +#define UPB_MINI_TABLE_ENUM_H_ + +#include + + +#ifndef UPB_MINI_TABLE_INTERNAL_ENUM_H_ +#define UPB_MINI_TABLE_INTERNAL_ENUM_H_ + +#include + +// Must be last. + +struct upb_MiniTableEnum { + uint32_t UPB_PRIVATE(mask_limit); // Highest that can be tested with mask. + uint32_t UPB_PRIVATE(value_count); // Number of values after the bitfield. + uint32_t UPB_PRIVATE(data)[]; // Bitmask + enumerated values follow. +}; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE bool upb_MiniTableEnum_CheckValue( + const struct upb_MiniTableEnum* e, uint32_t val) { + if (UPB_LIKELY(val < 64)) { + const uint64_t mask = + e->UPB_PRIVATE(data)[0] | ((uint64_t)e->UPB_PRIVATE(data)[1] << 32); + const uint64_t bit = 1ULL << val; + return (mask & bit) != 0; + } + if (UPB_LIKELY(val < e->UPB_PRIVATE(mask_limit))) { + const uint32_t mask = e->UPB_PRIVATE(data)[val / 32]; + const uint32_t bit = 1U << (val % 32); + return (mask & bit) != 0; + } + + // OPT: binary search long lists? + const uint32_t* start = + &e->UPB_PRIVATE(data)[e->UPB_PRIVATE(mask_limit) / 32]; + const uint32_t* limit = &e->UPB_PRIVATE( + data)[e->UPB_PRIVATE(mask_limit) / 32 + e->UPB_PRIVATE(value_count)]; + for (const uint32_t* p = start; p < limit; p++) { + if (*p == val) return true; + } + return false; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_ENUM_H_ */ + +// Must be last + +typedef struct upb_MiniTableEnum upb_MiniTableEnum; + +#ifdef __cplusplus +extern "C" { +#endif + +// Validates enum value against range defined by enum mini table. +UPB_API_INLINE bool upb_MiniTableEnum_CheckValue(const upb_MiniTableEnum* e, + uint32_t val); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_ENUM_H_ */ + +#ifndef UPB_MINI_TABLE_FIELD_H_ +#define UPB_MINI_TABLE_FIELD_H_ + +#include + + +#ifndef UPB_MINI_TABLE_INTERNAL_FIELD_H_ +#define UPB_MINI_TABLE_INTERNAL_FIELD_H_ + +#include +#include + + +#ifndef UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_ +#define UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Return the log2 of the storage size in bytes for a upb_CType +UPB_INLINE int UPB_PRIVATE(_upb_CType_SizeLg2)(upb_CType c_type) { + static const int8_t size[] = { + 0, // kUpb_CType_Bool + 2, // kUpb_CType_Float + 2, // kUpb_CType_Int32 + 2, // kUpb_CType_UInt32 + 2, // kUpb_CType_Enum + UPB_SIZE(2, 3), // kUpb_CType_Message + 3, // kUpb_CType_Double + 3, // kUpb_CType_Int64 + 3, // kUpb_CType_UInt64 + UPB_SIZE(3, 4), // kUpb_CType_String + UPB_SIZE(3, 4), // kUpb_CType_Bytes + }; + + // -1 here because the enum is one-based but the table is zero-based. + return size[c_type - 1]; +} + +// Return the log2 of the storage size in bytes for a upb_FieldType +UPB_INLINE size_t +UPB_PRIVATE(_upb_FieldType_SizeLg2)(upb_FieldType field_type) { + static const int8_t size[] = { + 3, // kUpb_FieldType_Double + 2, // kUpb_FieldType_Float + 3, // kUpb_FieldType_Int64 + 3, // kUpb_FieldType_UInt64 + 2, // kUpb_FieldType_Int32 + 3, // kUpb_FieldType_Fixed64 + 2, // kUpb_FieldType_Fixed32 + 0, // kUpb_FieldType_Bool + UPB_SIZE(3, 4), // kUpb_FieldType_String + UPB_SIZE(2, 3), // kUpb_FieldType_Group + UPB_SIZE(2, 3), // kUpb_FieldType_Message + UPB_SIZE(3, 4), // kUpb_FieldType_Bytes + 2, // kUpb_FieldType_UInt32 + 2, // kUpb_FieldType_Enum + 2, // kUpb_FieldType_SFixed32 + 3, // kUpb_FieldType_SFixed64 + 2, // kUpb_FieldType_SInt32 + 3, // kUpb_FieldType_SInt64 + }; + + // -1 here because the enum is one-based but the table is zero-based. + return size[field_type - 1]; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_ */ + +// Must be last. + +// LINT.IfChange(struct_definition) +struct upb_MiniTableField { + uint32_t UPB_ONLYBITS(number); + uint16_t UPB_ONLYBITS(offset); + int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index + + // Offset from this upb_MiniTableField to the upb_MiniTableSubInternal + // for this field, in uint32_t units (so bytes/4). + // Will be set to `kUpb_NoSub` if `descriptortype` != MESSAGE/GROUP/ENUM + uint16_t UPB_PRIVATE(submsg_ofs); + + uint8_t UPB_PRIVATE(descriptortype); + + // upb_FieldMode | upb_LabelFlags | (upb_FieldRep << kUpb_FieldRep_Shift) + uint8_t UPB_ONLYBITS(mode); +}; + +#define kUpb_NoSub ((uint16_t)-1) +#define kUpb_SubmsgOffsetBytes 4 + +typedef enum { + kUpb_FieldMode_Map = 0, + kUpb_FieldMode_Array = 1, + kUpb_FieldMode_Scalar = 2, +} upb_FieldMode; + +// Mask to isolate the upb_FieldMode from field.mode. +#define kUpb_FieldMode_Mask 3 + +// Extra flags on the mode field. +typedef enum { + kUpb_LabelFlags_IsPacked = 4, + kUpb_LabelFlags_IsExtension = 8, + // Indicates that this descriptor type is an "alternate type": + // - for Int32, this indicates that the actual type is Enum (but was + // rewritten to Int32 because it is an open enum that requires no check). + // - for Bytes, this indicates that the actual type is String (but does + // not require any UTF-8 check). + kUpb_LabelFlags_IsAlternate = 16, +} upb_LabelFlags; + +// Note: we sort by this number when calculating layout order. +typedef enum { + kUpb_FieldRep_1Byte = 0, + kUpb_FieldRep_4Byte = 1, + kUpb_FieldRep_StringView = 2, + kUpb_FieldRep_8Byte = 3, + + kUpb_FieldRep_NativePointer = + UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte), + kUpb_FieldRep_Max = kUpb_FieldRep_8Byte, +} upb_FieldRep; + +#define kUpb_FieldRep_Shift 6 + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE upb_FieldMode +UPB_PRIVATE(_upb_MiniTableField_Mode)(const struct upb_MiniTableField* f) { + return (upb_FieldMode)(f->UPB_ONLYBITS(mode) & kUpb_FieldMode_Mask); +} + +UPB_INLINE upb_FieldRep +UPB_PRIVATE(_upb_MiniTableField_GetRep)(const struct upb_MiniTableField* f) { + return (upb_FieldRep)(f->UPB_ONLYBITS(mode) >> kUpb_FieldRep_Shift); +} + +UPB_API_INLINE bool upb_MiniTableField_IsArray( + const struct upb_MiniTableField* f) { + return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Array; +} + +UPB_API_INLINE bool upb_MiniTableField_IsMap( + const struct upb_MiniTableField* f) { + return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Map; +} + +UPB_API_INLINE bool upb_MiniTableField_IsScalar( + const struct upb_MiniTableField* f) { + return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Scalar; +} + +UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_IsAlternate)( + const struct upb_MiniTableField* f) { + return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsAlternate) != 0; +} + +UPB_API_INLINE bool upb_MiniTableField_IsExtension( + const struct upb_MiniTableField* f) { + return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsExtension) != 0; +} + +UPB_API_INLINE bool upb_MiniTableField_IsPacked( + const struct upb_MiniTableField* f) { + return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsPacked) != 0; +} + +UPB_API_INLINE upb_FieldType +upb_MiniTableField_Type(const struct upb_MiniTableField* f) { + const upb_FieldType type = (upb_FieldType)f->UPB_PRIVATE(descriptortype); + if (UPB_PRIVATE(_upb_MiniTableField_IsAlternate)(f)) { + if (type == kUpb_FieldType_Int32) return kUpb_FieldType_Enum; + if (type == kUpb_FieldType_Bytes) return kUpb_FieldType_String; + UPB_ASSERT(false); + } + return type; +} + +UPB_API_INLINE +upb_CType upb_MiniTableField_CType(const struct upb_MiniTableField* f) { + return upb_FieldType_CType(upb_MiniTableField_Type(f)); +} + +UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_HasHasbit)( + const struct upb_MiniTableField* f) { + return f->presence > 0; +} + +UPB_INLINE char UPB_PRIVATE(_upb_MiniTableField_HasbitMask)( + const struct upb_MiniTableField* f) { + UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)); + const uint16_t index = (uint16_t)f->presence; + return (char)(1 << (index % 8)); +} + +UPB_INLINE uint16_t UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)( + const struct upb_MiniTableField* f) { + UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)); + const uint16_t index = (uint16_t)f->presence; + return index / 8; +} + +UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum( + const struct upb_MiniTableField* f) { + return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum; +} + +UPB_API_INLINE bool upb_MiniTableField_IsInOneof( + const struct upb_MiniTableField* f) { + return f->presence < 0; +} + +UPB_API_INLINE bool upb_MiniTableField_IsSubMessage( + const struct upb_MiniTableField* f) { + return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message || + f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group; +} + +UPB_API_INLINE bool upb_MiniTableField_HasPresence( + const struct upb_MiniTableField* f) { + if (upb_MiniTableField_IsExtension(f)) { + return upb_MiniTableField_IsScalar(f); + } else { + return f->presence != 0; + } +} + +UPB_API_INLINE uint32_t +upb_MiniTableField_Number(const struct upb_MiniTableField* f) { + return f->UPB_ONLYBITS(number); +} + +UPB_INLINE uint16_t +UPB_PRIVATE(_upb_MiniTableField_Offset)(const struct upb_MiniTableField* f) { + return f->UPB_ONLYBITS(offset); +} + +UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_OneofOffset)( + const struct upb_MiniTableField* f) { + UPB_ASSERT(upb_MiniTableField_IsInOneof(f)); + return (size_t)(~(ptrdiff_t)f->presence); +} + +UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)( + const struct upb_MiniTableField* f) { + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == + kUpb_FieldRep_NativePointer); + UPB_ASSUME(upb_MiniTableField_IsArray(f)); + UPB_ASSUME(f->presence == 0); +} + +UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)( + const struct upb_MiniTableField* f) { + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == + kUpb_FieldRep_NativePointer); + UPB_ASSUME(upb_MiniTableField_IsMap(f)); + UPB_ASSUME(f->presence == 0); +} + +UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)( + const struct upb_MiniTableField* f) { + const upb_FieldType field_type = upb_MiniTableField_Type(f); + return UPB_PRIVATE(_upb_FieldType_SizeLg2)(field_type); +} + +// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table_field.ts) + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_FIELD_H_ */ + +// Must be last. + +typedef struct upb_MiniTableField upb_MiniTableField; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_HasPresence(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsArray(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum( + const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsExtension(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsInOneof(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsMap(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsPacked(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsScalar(const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_MiniTableField_IsSubMessage( + const upb_MiniTableField* f); + +UPB_API_INLINE uint32_t upb_MiniTableField_Number(const upb_MiniTableField* f); + +UPB_API_INLINE upb_FieldType +upb_MiniTableField_Type(const upb_MiniTableField* f); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_FIELD_H_ */ + +#ifndef UPB_MINI_TABLE_INTERNAL_MESSAGE_H_ +#define UPB_MINI_TABLE_INTERNAL_MESSAGE_H_ + +#include +#include + + +#ifndef UPB_MINI_TABLE_INTERNAL_SUB_H_ +#define UPB_MINI_TABLE_INTERNAL_SUB_H_ + +// Must be last. + +union upb_MiniTableSub { + const struct upb_MiniTable* UPB_PRIVATE(submsg); + const struct upb_MiniTableEnum* UPB_PRIVATE(subenum); +}; + +typedef union upb_MiniTableSub upb_MiniTableSubInternal; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE union upb_MiniTableSub upb_MiniTableSub_FromEnum( + const struct upb_MiniTableEnum* subenum) { + union upb_MiniTableSub out; + out.UPB_PRIVATE(subenum) = subenum; + return out; +} + +UPB_API_INLINE union upb_MiniTableSub upb_MiniTableSub_FromMessage( + const struct upb_MiniTable* submsg) { + union upb_MiniTableSub out; + out.UPB_PRIVATE(submsg) = submsg; + return out; +} + +UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTableSub_Enum( + const union upb_MiniTableSub sub) { + return sub.UPB_PRIVATE(subenum); +} + +UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableSub_Message( + const union upb_MiniTableSub sub) { + return sub.UPB_PRIVATE(submsg); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_SUB_H_ */ + +// Must be last. + +struct upb_Decoder; +struct upb_Message; + +typedef UPB_PRESERVE_NONE const char* _upb_FieldParser( + struct upb_Decoder* d, const char* ptr, struct upb_Message* msg, + intptr_t table, uint64_t hasbits, uint64_t data); + +typedef struct { + uint64_t field_data; + _upb_FieldParser* field_parser; +} _upb_FastTable_Entry; + +typedef enum { + kUpb_ExtMode_NonExtendable = 0, // Non-extendable message. + kUpb_ExtMode_Extendable = 1, // Normal extendable message. + kUpb_ExtMode_IsMessageSet = 2, // MessageSet message. + kUpb_ExtMode_IsMessageSet_ITEM = + 3, // MessageSet item (temporary only, see decode.c) + + // During table building we steal a bit to indicate that the message is a map + // entry. *Only* used during table building! + kUpb_ExtMode_IsMapEntry = 4, +} upb_ExtMode; + +enum { + kUpb_Message_Align = 8, +}; + +// upb_MiniTable represents the memory layout of a given upb_MessageDef. +// The members are public so generated code can initialize them, +// but users MUST NOT directly read or write any of its members. + +// LINT.IfChange(minitable_struct_definition) +struct upb_MiniTable { + const struct upb_MiniTableField* UPB_ONLYBITS(fields); + + // Must be aligned to kUpb_Message_Align. Doesn't include internal members + // like unknown fields, extension dict, pointer to msglayout, etc. + uint16_t UPB_PRIVATE(size); + + uint16_t UPB_ONLYBITS(field_count); + + uint8_t UPB_PRIVATE(ext); // upb_ExtMode, uint8_t here so sizeof(ext) == 1 + uint8_t UPB_PRIVATE(dense_below); + uint8_t UPB_PRIVATE(table_mask); + uint8_t UPB_PRIVATE(required_count); // Required fields have the low hasbits. + +#ifdef UPB_TRACING_ENABLED + const char* UPB_PRIVATE(full_name); +#endif + +#if UPB_FASTTABLE || !defined(__cplusplus) + // Flexible array member is not supported in C++, but it is an extension in + // every compiler that supports UPB_FASTTABLE. + _upb_FastTable_Entry UPB_PRIVATE(fasttable)[]; +#endif +}; +// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table.ts) + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE void UPB_PRIVATE(upb_MiniTable_CheckInvariants)( + const struct upb_MiniTable* mt) { + UPB_STATIC_ASSERT(UPB_MALLOC_ALIGN >= kUpb_Message_Align, "Under aligned"); + UPB_STATIC_ASSERT(kUpb_Message_Align >= UPB_ALIGN_OF(void*), "Under aligned"); + UPB_ASSERT(mt->UPB_PRIVATE(size) % kUpb_Message_Align == 0); +} + +UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE( + _upb_MiniTable_StrongReference)(const struct upb_MiniTable* mt) { +#if defined(__GNUC__) + __asm__("" : : "r"(mt)); +#else + const struct upb_MiniTable* volatile unused = mt; + (void)&unused; // Use address to avoid an extra load of "unused". +#endif + return mt; +} + +UPB_API_INLINE int upb_MiniTable_FieldCount(const struct upb_MiniTable* m) { + return m->UPB_ONLYBITS(field_count); +} + +UPB_API_INLINE bool upb_MiniTable_IsMessageSet(const struct upb_MiniTable* m) { + return m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet; +} + +UPB_API_INLINE +const struct upb_MiniTableField* upb_MiniTable_FindFieldByNumber( + const struct upb_MiniTable* m, uint32_t number) { + const uint32_t i = number - 1; // 0 wraps to UINT32_MAX + + // Ideal case: index into dense fields + if (i < m->UPB_PRIVATE(dense_below)) { + UPB_ASSERT(m->UPB_ONLYBITS(fields)[i].UPB_ONLYBITS(number) == number); + return &m->UPB_ONLYBITS(fields)[i]; + } + + // Early exit if the field number is out of range. + int32_t hi = m->UPB_ONLYBITS(field_count) - 1; + if (hi < 0 || number > m->UPB_ONLYBITS(fields)[hi].UPB_ONLYBITS(number)) { + return NULL; + } + + // Slow case: binary search + uint32_t lo = m->UPB_PRIVATE(dense_below); + const struct upb_MiniTableField* base = m->UPB_ONLYBITS(fields); + while (hi >= (int32_t)lo) { + uint32_t mid = ((uint32_t)hi + lo) / 2; + uint32_t num = base[mid].UPB_ONLYBITS(number); + // These comparison operations allow, on ARM machines, to fuse all these + // branches into one comparison followed by two CSELs to set the lo/hi + // values, followed by a BNE to continue or terminate the loop. Since binary + // search branches are generally unpredictable (50/50 in each direction), + // this is a good deal. We use signed for the high, as this decrement may + // underflow if mid is 0. + int32_t hi_mid = (int32_t)mid - 1; + uint32_t lo_mid = mid + 1; + if (num == number) { + return &base[mid]; + } + if (UPB_UNPREDICTABLE(num < number)) { + lo = lo_mid; + } else { + hi = hi_mid; + } + } + + return NULL; +} + +UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_GetFieldByIndex( + const struct upb_MiniTable* m, uint32_t i) { + UPB_ASSERT(i < m->UPB_ONLYBITS(field_count)); + return &m->UPB_ONLYBITS(fields)[i]; +} + +UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_GetSubMessageTable( + const struct upb_MiniTableField* f) { + UPB_ASSERT(upb_MiniTableField_CType(f) == kUpb_CType_Message); + upb_MiniTableSubInternal* sub = + UPB_PTR_AT(f, f->UPB_PRIVATE(submsg_ofs) * kUpb_SubmsgOffsetBytes, + upb_MiniTableSubInternal); + return sub->UPB_PRIVATE(submsg); +} + +UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_SubMessage( + const struct upb_MiniTableField* f) { + if (upb_MiniTableField_CType(f) != kUpb_CType_Message) { + return NULL; + } + return upb_MiniTable_GetSubMessageTable(f); +} + +UPB_API_INLINE bool upb_MiniTable_FieldIsLinked( + const struct upb_MiniTableField* f) { + return upb_MiniTable_GetSubMessageTable(f) != NULL; +} + +UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_MapEntrySubMessage( + const struct upb_MiniTableField* f) { + UPB_ASSERT(upb_MiniTable_FieldIsLinked(f)); // Map entries must be linked. + UPB_ASSERT(upb_MiniTableField_IsMap(f)); // Function precondition. + return upb_MiniTable_GetSubMessageTable(f); +} + +UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable( + const struct upb_MiniTableField* f) { + UPB_ASSERT(upb_MiniTableField_CType(f) == kUpb_CType_Enum); + upb_MiniTableSubInternal* sub = + UPB_PTR_AT(f, f->UPB_PRIVATE(submsg_ofs) * kUpb_SubmsgOffsetBytes, + upb_MiniTableSubInternal); + return sub->UPB_PRIVATE(subenum); +} + +UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapKey( + const struct upb_MiniTable* m) { + UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2); + const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 0); + UPB_ASSERT(upb_MiniTableField_Number(f) == 1); + return f; +} + +UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapValue( + const struct upb_MiniTable* m) { + UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2); + const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 1); + UPB_ASSERT(upb_MiniTableField_Number(f) == 2); + return f; +} + +// Computes a bitmask in which the |m->required_count| lowest bits are set. +// +// Sample output: +// RequiredMask(1) => 0b1 (0x1) +// RequiredMask(5) => 0b11111 (0x1f) +UPB_INLINE uint64_t +UPB_PRIVATE(_upb_MiniTable_RequiredMask)(const struct upb_MiniTable* m) { + int n = m->UPB_PRIVATE(required_count); + UPB_ASSERT(0 < n && n <= 64); + return (1ULL << n) - 1; +} + +#ifdef UPB_TRACING_ENABLED +UPB_INLINE const char* upb_MiniTable_FullName( + const struct upb_MiniTable* mini_table) { + return mini_table->UPB_PRIVATE(full_name); +} +// Initializes tracing proto name from language runtimes that construct +// mini tables dynamically at runtime. The runtime is responsible for passing +// controlling lifetime of name such as storing in same arena as mini_table. +UPB_INLINE void upb_MiniTable_SetFullName(struct upb_MiniTable* mini_table, + const char* full_name) { + mini_table->UPB_PRIVATE(full_name) = full_name; +} +#endif + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_MESSAGE_H_ */ + +// Must be last. + +typedef struct upb_MiniTable upb_MiniTable; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_FindFieldByNumber( + const upb_MiniTable* m, uint32_t number); + +UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_GetFieldByIndex( + const upb_MiniTable* m, uint32_t index); + +UPB_API_INLINE int upb_MiniTable_FieldCount(const upb_MiniTable* m); + +UPB_API_INLINE bool upb_MiniTable_IsMessageSet(const upb_MiniTable* m); + +// DEPRECATED: use upb_MiniTable_SubMessage() instead +// Returns the MiniTable for a message field, NULL if the field is unlinked. +UPB_API_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable( + const upb_MiniTableField* f); + +// Returns the MiniTable for a message field if it is a submessage and the field +// is linked, otherwise returns NULL. +UPB_API_INLINE const upb_MiniTable* upb_MiniTable_SubMessage( + const upb_MiniTableField* f); + +// Returns the MiniTable for a map field. The given field must refer to a map. +UPB_API_INLINE const upb_MiniTable* upb_MiniTable_MapEntrySubMessage( + const upb_MiniTableField* f); + +// Returns the MiniTableEnum for a message field, NULL if the field is unlinked. +UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable( + const upb_MiniTableField* f); + +// Returns the MiniTableField for the key of a map. +UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_MapKey( + const upb_MiniTable* m); + +// Returns the MiniTableField for the value of a map. +UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_MapValue( + const upb_MiniTable* m); + +// Returns true if this MiniTable field is linked to a MiniTable for the +// sub-message. +UPB_API_INLINE bool upb_MiniTable_FieldIsLinked(const upb_MiniTableField* f); + +// If this field is in a oneof, returns the first field in the oneof. +// +// Otherwise returns NULL. +// +// Usage: +// const upb_MiniTableField* field = upb_MiniTable_GetOneof(m, f); +// do { +// .. +// } while (upb_MiniTable_NextOneofField(m, &field); +// +const upb_MiniTableField* upb_MiniTable_GetOneof(const upb_MiniTable* m, + const upb_MiniTableField* f); + +// Iterates to the next field in the oneof. If this is the last field in the +// oneof, returns false. The ordering of fields in the oneof is not +// guaranteed. +// REQUIRES: |f| is the field initialized by upb_MiniTable_GetOneof and updated +// by prior upb_MiniTable_NextOneofField calls. +bool upb_MiniTable_NextOneofField(const upb_MiniTable* m, + const upb_MiniTableField** f); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#ifdef __cplusplus +// Temporary overloads for functions whose signature has recently changed. +UPB_DEPRECATE_AND_INLINE() +inline const upb_MiniTable* upb_MiniTable_SubMessage( + const upb_MiniTable* m, const upb_MiniTableField* f) { + return upb_MiniTable_SubMessage(f); +} + +UPB_DEPRECATE_AND_INLINE() +inline const upb_MiniTable* upb_MiniTable_GetSubMessageTable( + const upb_MiniTable* m, const upb_MiniTableField* f) { + return upb_MiniTable_GetSubMessageTable(f); +} + +UPB_DEPRECATE_AND_INLINE() +inline const upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable( + const upb_MiniTable* m, const upb_MiniTableField* f) { + return upb_MiniTable_GetSubEnumTable(f); +} + +UPB_DEPRECATE_AND_INLINE() +inline bool upb_MiniTable_FieldIsLinked(const upb_MiniTable* m, + const upb_MiniTableField* f) { + return upb_MiniTable_FieldIsLinked(f); +} +#endif + + +#endif /* UPB_MINI_TABLE_MESSAGE_H_ */ + +// Must be last. + +typedef struct upb_Array upb_Array; + +#ifdef __cplusplus +extern "C" { +#endif + +// Creates a new array on the given arena that holds elements of this type. +UPB_API upb_Array* upb_Array_New(upb_Arena* a, upb_CType type); + +// Returns the number of elements in the array. +UPB_API_INLINE size_t upb_Array_Size(const upb_Array* arr); + +// Returns the number of elements in the array. +UPB_API_INLINE size_t upb_Array_Capacity(const upb_Array* arr); + +// Returns the given element, which must be within the array's current size. +UPB_API upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i); + +// Returns a non-null mutating pointer to the given element. `arr` must be an +// array of a message type, and `i` must be within the array's current size. +UPB_API struct upb_Message* upb_Array_GetMutable(upb_Array* arr, size_t i); + +// Sets the given element, which must be within the array's current size. +UPB_API void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val); + +// Appends an element to the array. Returns false on allocation failure. +UPB_API bool upb_Array_Append(upb_Array* array, upb_MessageValue val, + upb_Arena* arena); + +// Copies elements from |src| to |dst|, resizing |dst| to match |src| size. +// Returns false on allocation failure. +UPB_API bool upb_Array_Copy(upb_Array* dst, const upb_Array* src, + upb_Arena* arena); + +// Appends all elements from |src| to the end of |dst|. +// Returns false on allocation failure. +UPB_API bool upb_Array_AppendAll(upb_Array* dst, const upb_Array* src, + upb_Arena* arena); + +// Moves elements within the array using memmove(). +// Like memmove(), the source and destination elements may be overlapping. +UPB_API void upb_Array_Move(upb_Array* array, size_t dst_idx, size_t src_idx, + size_t count); + +// Inserts one or more empty elements into the array. +// Existing elements are shifted right. +// The new elements have undefined state and must be set with `upb_Array_Set()`. +// REQUIRES: `i <= upb_Array_Size(arr)` +UPB_API bool upb_Array_Insert(upb_Array* array, size_t i, size_t count, + upb_Arena* arena); + +// Deletes one or more elements from the array. +// Existing elements are shifted left. +// REQUIRES: `i + count <= upb_Array_Size(arr)` +UPB_API void upb_Array_Delete(upb_Array* array, size_t i, size_t count); + +// Reserves |size| elements of storage for the array. +UPB_API_INLINE bool upb_Array_Reserve(struct upb_Array* array, size_t size, + upb_Arena* arena); + +// Changes the size of a vector. New elements are initialized to NULL/0. +// Returns false on allocation failure. +UPB_API bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena); + +// Returns pointer to array data. +UPB_API_INLINE const void* upb_Array_DataPtr(const upb_Array* arr); + +// Returns mutable pointer to array data. +UPB_API_INLINE void* upb_Array_MutableDataPtr(upb_Array* arr); + +// Mark an array and all of its descendents as frozen/immutable. +// If the array elements are messages then |m| must point to the minitable for +// those messages. Otherwise |m| must be NULL. +UPB_API void upb_Array_Freeze(upb_Array* arr, const upb_MiniTable* m); + +// Returns whether an array has been frozen. +UPB_API_INLINE bool upb_Array_IsFrozen(const upb_Array* arr); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_ARRAY_H_ */ + +#ifndef UPB_MESSAGE_INTERNAL_ACCESSORS_H_ +#define UPB_MESSAGE_INTERNAL_ACCESSORS_H_ + +#include +#include +#include + + +#ifndef UPB_BASE_INTERNAL_ENDIAN_H_ +#define UPB_BASE_INTERNAL_ENDIAN_H_ + +#include + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE bool upb_IsLittleEndian(void) { + const int x = 1; + return *(char*)&x == 1; +} + +UPB_INLINE uint32_t upb_BigEndian32(uint32_t val) { + if (upb_IsLittleEndian()) return val; + + return ((val & 0xff) << 24) | ((val & 0xff00) << 8) | + ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24); +} + +UPB_INLINE uint64_t upb_BigEndian64(uint64_t val) { + if (upb_IsLittleEndian()) return val; + + const uint64_t hi = ((uint64_t)upb_BigEndian32((uint32_t)val)) << 32; + const uint64_t lo = upb_BigEndian32((uint32_t)(val >> 32)); + return hi | lo; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_BASE_INTERNAL_ENDIAN_H_ */ + +#ifndef UPB_MESSAGE_INTERNAL_EXTENSION_H_ +#define UPB_MESSAGE_INTERNAL_EXTENSION_H_ + +#include + + +#ifndef UPB_MESSAGE_INTERNAL_MAP_H_ +#define UPB_MESSAGE_INTERNAL_MAP_H_ + +#include +#include +#include + + +/* + * upb_table + * + * This header is INTERNAL-ONLY! Its interfaces are not public or stable! + * This file defines very fast int->upb_value (inttable) and string->upb_value + * (strtable) hash tables. + * + * The table uses chained scatter with Brent's variation (inspired by the Lua + * implementation of hash tables). The hash function for strings is Austin + * Appleby's "MurmurHash." + * + * The inttable uses uintptr_t as its key, which guarantees it can be used to + * store pointers or integers of at least 32 bits (upb isn't really useful on + * systems where sizeof(void*) < 4). + * + * The table must be homogeneous (all values of the same type). In debug + * mode, we check this on insert and lookup. + */ + +#ifndef UPB_HASH_COMMON_H_ +#define UPB_HASH_COMMON_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +/* upb_value ******************************************************************/ + +typedef struct { + uint64_t val; +} upb_value; + +/* For each value ctype, define the following set of functions: + * + * // Get/set an int32 from a upb_value. + * int32_t upb_value_getint32(upb_value val); + * void upb_value_setint32(upb_value *val, int32_t cval); + * + * // Construct a new upb_value from an int32. + * upb_value upb_value_int32(int32_t val); */ +#define FUNCS(name, membername, type_t, converter) \ + UPB_INLINE void upb_value_set##name(upb_value* val, type_t cval) { \ + val->val = (uint64_t)cval; \ + } \ + UPB_INLINE upb_value upb_value_##name(type_t val) { \ + upb_value ret; \ + upb_value_set##name(&ret, val); \ + return ret; \ + } \ + UPB_INLINE type_t upb_value_get##name(upb_value val) { \ + return (type_t)(converter)val.val; \ + } + +FUNCS(int32, int32, int32_t, int32_t) +FUNCS(int64, int64, int64_t, int64_t) +FUNCS(uint32, uint32, uint32_t, uint32_t) +FUNCS(uint64, uint64, uint64_t, uint64_t) +FUNCS(bool, _bool, bool, bool) +FUNCS(cstr, cstr, char*, uintptr_t) +FUNCS(uintptr, uptr, uintptr_t, uintptr_t) +FUNCS(ptr, ptr, void*, uintptr_t) +FUNCS(constptr, constptr, const void*, uintptr_t) + +#undef FUNCS + +UPB_INLINE void upb_value_setfloat(upb_value* val, float cval) { + memcpy(&val->val, &cval, sizeof(cval)); +} + +UPB_INLINE void upb_value_setdouble(upb_value* val, double cval) { + memcpy(&val->val, &cval, sizeof(cval)); +} + +UPB_INLINE upb_value upb_value_float(float cval) { + upb_value ret; + upb_value_setfloat(&ret, cval); + return ret; +} + +UPB_INLINE upb_value upb_value_double(double cval) { + upb_value ret; + upb_value_setdouble(&ret, cval); + return ret; +} + +/* upb_key *****************************************************************/ + +// A uint32 size followed by that number of bytes stored contiguously. +typedef struct { + uint32_t size; + const char data[]; +} upb_SizePrefixString; + +/* Either: + * 1. an actual integer key + * 2. a SizePrefixString*, owned by us. + * + * ...depending on whether this is a string table or an int table. */ +typedef union { + uintptr_t num; + const upb_SizePrefixString* str; +} upb_key; + +UPB_INLINE upb_StringView upb_key_strview(upb_key key) { + return upb_StringView_FromDataAndSize(key.str->data, key.str->size); +} + +/* upb_table ******************************************************************/ + +typedef struct _upb_tabent { + upb_value val; + upb_key key; + + /* Internal chaining. This is const so we can create static initializers for + * tables. We cast away const sometimes, but *only* when the containing + * upb_table is known to be non-const. This requires a bit of care, but + * the subtlety is confined to table.c. */ + const struct _upb_tabent* next; +} upb_tabent; + +typedef struct { + upb_tabent* entries; + /* Number of entries in the hash part. */ + uint32_t count; + + /* Mask to turn hash value -> bucket. The map's allocated size is mask + 1.*/ + uint32_t mask; +} upb_table; + +UPB_INLINE size_t upb_table_size(const upb_table* t) { return t->mask + 1; } + +// Internal-only functions, in .h file only out of necessity. + +UPB_INLINE upb_key upb_key_empty(void) { + upb_key ret; + memset(&ret, 0, sizeof(upb_key)); + return ret; +} + +UPB_INLINE bool upb_tabent_isempty(const upb_tabent* e) { + upb_key key = e->key; + UPB_STATIC_ASSERT(sizeof(key.num) == sizeof(key.str), "Sizes don't match"); + uintptr_t val; + memcpy(&val, &key, sizeof(val)); + // Note: for upb_inttables a tab_key is a true integer key value, but the + // inttable maintains the invariant that 0 value is always stored in the + // compact table and never as a upb_tabent* so we can always use the 0 + // key value to identify an empty tabent. + return val == 0; +} + +uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_HASH_COMMON_H_ */ + +#ifndef UPB_HASH_INT_TABLE_H_ +#define UPB_HASH_INT_TABLE_H_ + +#include +#include + + +// Must be last. + +typedef struct { + upb_table t; // For entries that don't fit in the array part. + // Array part of the table. + // Pointers on this table are const so we can create static initializers for + // tables. We cast away const sometimes, but *only* when the containing + // upb_table is known to be non-const. This requires a bit of care, but + // the subtlety is confined to table.c. + const upb_value* array; + // Track presence in the array part. Each bit at index (key % 8) at the + // presence_mask[key/8] indicates if the element is present in the array part. + const uint8_t* presence_mask; + uint32_t array_size; // Array part size. + uint32_t array_count; // Array part number of elements. +} upb_inttable; + +#ifdef __cplusplus +extern "C" { +#endif + +// Initialize a table. If memory allocation failed, false is returned and +// the table is uninitialized. +bool upb_inttable_init(upb_inttable* table, upb_Arena* a); + +// Returns the number of values in the table. +size_t upb_inttable_count(const upb_inttable* t); + +// Inserts the given key into the hashtable with the given value. +// The key must not already exist in the hash table. +// The value must not be UINTPTR_MAX. +// +// If a table resize was required but memory allocation failed, false is +// returned and the table is unchanged. +bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val, + upb_Arena* a); + +// Looks up key in this table, returning "true" if the key was found. +// If v is non-NULL, copies the value for this key into *v. +bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v); + +// Removes an item from the table. Returns true if the remove was successful, +// and stores the removed item in *val if non-NULL. +bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val); + +// Updates an existing entry in an inttable. +// If the entry does not exist, returns false and does nothing. +// Unlike insert/remove, this does not invalidate iterators. +bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val); + +// Optimizes the table for the current set of entries, for both memory use and +// lookup time. Client should call this after all entries have been inserted; +// inserting more entries is legal, but will likely require a table resize. +// Returns false if reallocation fails. +bool upb_inttable_compact(upb_inttable* t, upb_Arena* a); + +// Clears the table. +void upb_inttable_clear(upb_inttable* t); + +// Iteration over inttable: +// +// intptr_t iter = UPB_INTTABLE_BEGIN; +// uintptr_t key; +// upb_value val; +// while (upb_inttable_next(t, &key, &val, &iter)) { +// // ... +// } + +#define UPB_INTTABLE_BEGIN -1 + +bool upb_inttable_next(const upb_inttable* t, uintptr_t* key, upb_value* val, + intptr_t* iter); +void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter); +void upb_inttable_setentryvalue(upb_inttable* t, intptr_t iter, upb_value v); +bool upb_inttable_done(const upb_inttable* t, intptr_t i); +uintptr_t upb_inttable_iter_key(const upb_inttable* t, intptr_t iter); +upb_value upb_inttable_iter_value(const upb_inttable* t, intptr_t iter); + +UPB_INLINE bool upb_inttable_arrhas(const upb_inttable* t, uintptr_t key) { + return (t->presence_mask[key / 8] & (1 << (key % 8))) != 0; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_HASH_INT_TABLE_H_ */ + +#ifndef UPB_HASH_STR_TABLE_H_ +#define UPB_HASH_STR_TABLE_H_ + +#include +#include +#include + + +// Must be last. + +typedef struct { + upb_table t; +} upb_strtable; + +#ifdef __cplusplus +extern "C" { +#endif + +// Initialize a table. If memory allocation failed, false is returned and +// the table is uninitialized. +bool upb_strtable_init(upb_strtable* table, size_t expected_size, upb_Arena* a); + +// Returns the number of values in the table. +UPB_INLINE size_t upb_strtable_count(const upb_strtable* t) { + return t->t.count; +} + +void upb_strtable_clear(upb_strtable* t); + +// Inserts the given key into the hashtable with the given value. +// The key must not already exist in the hash table. The key is not required +// to be NULL-terminated, and the table will make an internal copy of the key. +// +// If a table resize was required but memory allocation failed, false is +// returned and the table is unchanged. */ +bool upb_strtable_insert(upb_strtable* t, const char* key, size_t len, + upb_value val, upb_Arena* a); + +// Looks up key in this table, returning "true" if the key was found. +// If v is non-NULL, copies the value for this key into *v. +bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len, + upb_value* v); + +// For NULL-terminated strings. +UPB_INLINE bool upb_strtable_lookup(const upb_strtable* t, const char* key, + upb_value* v) { + return upb_strtable_lookup2(t, key, strlen(key), v); +} + +// Removes an item from the table. Returns true if the remove was successful, +// and stores the removed item in *val if non-NULL. +bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len, + upb_value* val); + +UPB_INLINE bool upb_strtable_remove(upb_strtable* t, const char* key, + upb_value* v) { + return upb_strtable_remove2(t, key, strlen(key), v); +} + +// Exposed for testing only. +bool upb_strtable_resize(upb_strtable* t, size_t size_lg2, upb_Arena* a); + +/* Iteration over strtable: + * + * intptr_t iter = UPB_STRTABLE_BEGIN; + * upb_StringView key; + * upb_value val; + * while (upb_strtable_next2(t, &key, &val, &iter)) { + * // ... + * } + */ + +#define UPB_STRTABLE_BEGIN -1 + +bool upb_strtable_next2(const upb_strtable* t, upb_StringView* key, + upb_value* val, intptr_t* iter); +void upb_strtable_removeiter(upb_strtable* t, intptr_t* iter); +void upb_strtable_setentryvalue(upb_strtable* t, intptr_t iter, upb_value v); + +/* DEPRECATED iterators, slated for removal. + * + * Iterators for string tables. We are subject to some kind of unusual + * design constraints: + * + * For high-level languages: + * - we must be able to guarantee that we don't crash or corrupt memory even if + * the program accesses an invalidated iterator. + * + * For C++11 range-based for: + * - iterators must be copyable + * - iterators must be comparable + * - it must be possible to construct an "end" value. + * + * Iteration order is undefined. + * + * Modifying the table invalidates iterators. upb_{str,int}table_done() is + * guaranteed to work even on an invalidated iterator, as long as the table it + * is iterating over has not been freed. Calling next() or accessing data from + * an invalidated iterator yields unspecified elements from the table, but it is + * guaranteed not to crash and to return real table elements (except when done() + * is true). */ +/* upb_strtable_iter **********************************************************/ + +/* upb_strtable_iter i; + * upb_strtable_begin(&i, t); + * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) { + * const char *key = upb_strtable_iter_key(&i); + * const upb_value val = upb_strtable_iter_value(&i); + * // ... + * } + */ + +typedef struct { + const upb_strtable* t; + size_t index; +} upb_strtable_iter; + +UPB_INLINE const upb_tabent* str_tabent(const upb_strtable_iter* i) { + return &i->t->t.entries[i->index]; +} + +void upb_strtable_begin(upb_strtable_iter* i, const upb_strtable* t); +void upb_strtable_next(upb_strtable_iter* i); +bool upb_strtable_done(const upb_strtable_iter* i); +upb_StringView upb_strtable_iter_key(const upb_strtable_iter* i); +upb_value upb_strtable_iter_value(const upb_strtable_iter* i); +void upb_strtable_iter_setdone(upb_strtable_iter* i); +bool upb_strtable_iter_isequal(const upb_strtable_iter* i1, + const upb_strtable_iter* i2); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_HASH_STR_TABLE_H_ */ + +// Must be last. + +typedef enum { + kUpb_MapInsertStatus_Inserted = 0, + kUpb_MapInsertStatus_Replaced = 1, + kUpb_MapInsertStatus_OutOfMemory = 2, +} upb_MapInsertStatus; + +// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE ///////////////////////// + +union upb_Map_Table { + upb_strtable strtable; + upb_inttable inttable; +}; + +struct upb_Map { + // Size of key and val, based on the map type. + // Strings are represented as '0' because they must be handled specially. + char key_size; + char val_size; + bool UPB_PRIVATE(is_frozen); + bool UPB_PRIVATE(is_strtable); + + union upb_Map_Table t; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE void UPB_PRIVATE(_upb_Map_ShallowFreeze)(struct upb_Map* map) { + map->UPB_PRIVATE(is_frozen) = true; +} + +UPB_API_INLINE bool upb_Map_IsFrozen(const struct upb_Map* map) { + return map->UPB_PRIVATE(is_frozen); +} + +// Converting between internal table representation and user values. +// +// _upb_map_tokey() and _upb_map_fromkey() are inverses. +// _upb_map_tovalue() and _upb_map_fromvalue() are inverses. +// +// These functions account for the fact that strings are treated differently +// from other types when stored in a map. + +UPB_INLINE upb_StringView _upb_map_tokey(const void* key, size_t size) { + if (size == UPB_MAPTYPE_STRING) { + return *(upb_StringView*)key; + } else { + return upb_StringView_FromDataAndSize((const char*)key, size); + } +} + +UPB_INLINE uintptr_t _upb_map_tointkey(const void* key, size_t key_size) { + uintptr_t intkey = 0; + memcpy(&intkey, key, key_size); + return intkey; +} + +UPB_INLINE void _upb_map_fromkey(upb_StringView key, void* out, size_t size) { + if (size == UPB_MAPTYPE_STRING) { + memcpy(out, &key, sizeof(key)); + } else { + memcpy(out, key.data, size); + } +} + +UPB_INLINE bool _upb_map_tovalue(const void* val, size_t size, + upb_value* msgval, upb_Arena* a) { + if (size == UPB_MAPTYPE_STRING) { + upb_StringView* strp = (upb_StringView*)upb_Arena_Malloc(a, sizeof(*strp)); + if (!strp) return false; + *strp = *(upb_StringView*)val; + *msgval = upb_value_ptr(strp); + } else { + memcpy(msgval, val, size); + } + return true; +} + +UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) { + if (size == UPB_MAPTYPE_STRING) { + const upb_StringView* strp = (const upb_StringView*)upb_value_getptr(val); + memcpy(out, strp, sizeof(upb_StringView)); + } else { + memcpy(out, &val, size); + } +} + +UPB_INLINE bool _upb_map_next(const struct upb_Map* map, size_t* iter) { + if (map->UPB_PRIVATE(is_strtable)) { + upb_strtable_iter it; + it.t = &map->t.strtable; + it.index = *iter; + upb_strtable_next(&it); + *iter = it.index; + return !upb_strtable_done(&it); + } else { + uintptr_t key; + upb_value val; + intptr_t int_iter = 0; + memcpy(&int_iter, iter, sizeof(intptr_t)); + upb_inttable_next(&map->t.inttable, &key, &val, &int_iter); + memcpy(iter, &int_iter, sizeof(size_t)); + return !upb_inttable_done(&map->t.inttable, int_iter); + } +} + +UPB_INLINE void _upb_Map_Clear(struct upb_Map* map) { + UPB_ASSERT(!upb_Map_IsFrozen(map)); + + if (map->UPB_PRIVATE(is_strtable)) { + upb_strtable_clear(&map->t.strtable); + } else { + upb_inttable_clear(&map->t.inttable); + } +} + +UPB_INLINE bool _upb_Map_Delete(struct upb_Map* map, const void* key, + size_t key_size, upb_value* val) { + UPB_ASSERT(!upb_Map_IsFrozen(map)); + + if (map->UPB_PRIVATE(is_strtable)) { + upb_StringView k = _upb_map_tokey(key, key_size); + return upb_strtable_remove2(&map->t.strtable, k.data, k.size, val); + } else { + uintptr_t intkey = _upb_map_tointkey(key, key_size); + return upb_inttable_remove(&map->t.inttable, intkey, val); + } +} + +UPB_INLINE bool _upb_Map_Get(const struct upb_Map* map, const void* key, + size_t key_size, void* val, size_t val_size) { + upb_value tabval = {0}; + bool ret; + if (map->UPB_PRIVATE(is_strtable)) { + upb_StringView k = _upb_map_tokey(key, key_size); + ret = upb_strtable_lookup2(&map->t.strtable, k.data, k.size, &tabval); + } else { + uintptr_t intkey = _upb_map_tointkey(key, key_size); + ret = upb_inttable_lookup(&map->t.inttable, intkey, &tabval); + } + if (ret && val) { + _upb_map_fromvalue(tabval, val, val_size); + } + return ret; +} + +UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map, + const void* key, size_t key_size, + void* val, size_t val_size, + upb_Arena* a) { + UPB_ASSERT(!upb_Map_IsFrozen(map)); + + // Prep the value. + upb_value tabval = {0}; + if (!_upb_map_tovalue(val, val_size, &tabval, a)) { + return kUpb_MapInsertStatus_OutOfMemory; + } + + bool removed; + if (map->UPB_PRIVATE(is_strtable)) { + upb_StringView strkey = _upb_map_tokey(key, key_size); + // TODO: add overwrite operation to minimize number of lookups. + removed = + upb_strtable_remove2(&map->t.strtable, strkey.data, strkey.size, NULL); + if (!upb_strtable_insert(&map->t.strtable, strkey.data, strkey.size, tabval, + a)) { + return kUpb_MapInsertStatus_OutOfMemory; + } + } else { + uintptr_t intkey = _upb_map_tointkey(key, key_size); + removed = upb_inttable_remove(&map->t.inttable, intkey, NULL); + if (!upb_inttable_insert(&map->t.inttable, intkey, tabval, a)) { + return kUpb_MapInsertStatus_OutOfMemory; + } + } + return removed ? kUpb_MapInsertStatus_Replaced + : kUpb_MapInsertStatus_Inserted; +} + +UPB_INLINE size_t _upb_Map_Size(const struct upb_Map* map) { + if (map->UPB_PRIVATE(is_strtable)) { + return map->t.strtable.t.count; + } else { + return upb_inttable_count(&map->t.inttable); + } +} + +// Strings/bytes are special-cased in maps. +extern char _upb_Map_CTypeSizeTable[12]; + +UPB_INLINE size_t _upb_Map_CTypeSize(upb_CType ctype) { + return (size_t)_upb_Map_CTypeSizeTable[ctype]; +} + +// Creates a new map on the given arena with this key/value type. +struct upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_INTERNAL_MAP_H_ */ + +#ifndef UPB_MINI_TABLE_EXTENSION_H_ +#define UPB_MINI_TABLE_EXTENSION_H_ + +#include + + +#ifndef UPB_MINI_TABLE_INTERNAL_EXTENSION_H_ +#define UPB_MINI_TABLE_INTERNAL_EXTENSION_H_ + +#include +#include + + +// Must be last. + +struct upb_MiniTableExtension { + // Do not move this field. We need to be able to alias pointers. + struct upb_MiniTableField UPB_PRIVATE(field); + + union upb_MiniTableSub UPB_PRIVATE(sub); // NULL unless submsg or proto2 enum + const struct upb_MiniTable* UPB_PRIVATE(extendee); +}; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE upb_CType +upb_MiniTableExtension_CType(const struct upb_MiniTableExtension* e) { + return upb_MiniTableField_CType(&e->UPB_PRIVATE(field)); +} + +UPB_API_INLINE uint32_t +upb_MiniTableExtension_Number(const struct upb_MiniTableExtension* e) { + return e->UPB_PRIVATE(field).UPB_ONLYBITS(number); +} + +UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableExtension_Extendee( + const struct upb_MiniTableExtension* e) { + return e->UPB_PRIVATE(extendee); +} + +UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableExtension_GetSubMessage( + const struct upb_MiniTableExtension* e) { + if (upb_MiniTableExtension_CType(e) != kUpb_CType_Message) { + return NULL; + } + return upb_MiniTableSub_Message(e->UPB_PRIVATE(sub)); +} + +UPB_API_INLINE const struct upb_MiniTableEnum* +upb_MiniTableExtension_GetSubEnum(const struct upb_MiniTableExtension* e) { + if (upb_MiniTableExtension_CType(e) != kUpb_CType_Enum) { + return NULL; + } + return upb_MiniTableSub_Enum(e->UPB_PRIVATE(sub)); +} + +UPB_API_INLINE bool upb_MiniTableExtension_SetSubMessage( + struct upb_MiniTableExtension* e, const struct upb_MiniTable* m) { + if (e->UPB_PRIVATE(field).UPB_PRIVATE(descriptortype) != + kUpb_FieldType_Message && + e->UPB_PRIVATE(field).UPB_PRIVATE(descriptortype) != + kUpb_FieldType_Group) { + return false; + } + e->UPB_PRIVATE(sub).UPB_PRIVATE(submsg) = m; + return true; +} + +UPB_API_INLINE bool upb_MiniTableExtension_SetSubEnum( + struct upb_MiniTableExtension* e, const struct upb_MiniTableEnum* en) { + if (e->UPB_PRIVATE(field).UPB_PRIVATE(descriptortype) != + kUpb_FieldType_Enum) { + return false; + } + e->UPB_PRIVATE(sub).UPB_PRIVATE(subenum) = en; + return true; +} + +UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTableExtension_ToField( + const struct upb_MiniTableExtension* e) { + return &e->UPB_PRIVATE(field); +} + +UPB_INLINE upb_FieldRep UPB_PRIVATE(_upb_MiniTableExtension_GetRep)( + const struct upb_MiniTableExtension* e) { + return UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field)); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_EXTENSION_H_ */ + +// Must be last. + +typedef struct upb_MiniTableExtension upb_MiniTableExtension; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE upb_CType +upb_MiniTableExtension_CType(const upb_MiniTableExtension* e); + +UPB_API_INLINE uint32_t +upb_MiniTableExtension_Number(const upb_MiniTableExtension* e); + +UPB_API_INLINE const upb_MiniTable* upb_MiniTableExtension_Extendee( + const upb_MiniTableExtension* e); + +UPB_API_INLINE const upb_MiniTable* upb_MiniTableExtension_GetSubMessage( + const upb_MiniTableExtension* e); + +UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTableExtension_GetSubEnum( + const upb_MiniTableExtension* e); + +UPB_API_INLINE bool upb_MiniTableExtension_SetSubMessage( + upb_MiniTableExtension* e, const upb_MiniTable* m); + +UPB_API_INLINE bool upb_MiniTableExtension_SetSubEnum( + upb_MiniTableExtension* e, const upb_MiniTableEnum* m); + +UPB_API_INLINE const upb_MiniTableField* upb_MiniTableExtension_ToField( + const upb_MiniTableExtension* e); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_EXTENSION_H_ */ + +// Must be last. + +// The internal representation of an extension is self-describing: it contains +// enough information that we can serialize it to binary format without needing +// to look it up in a upb_ExtensionRegistry. +// +// This representation allocates 16 bytes to data on 64-bit platforms. +// This is rather wasteful for scalars (in the extreme case of bool, +// it wastes 15 bytes). We accept this because we expect messages to be +// the most common extension type. +typedef struct { + const upb_MiniTableExtension* ext; + upb_MessageValue data; +} upb_Extension; + +#ifdef __cplusplus +extern "C" { +#endif + +// Adds the given extension data to the given message. +// |ext| is copied into the message instance. +// This logically replaces any previously-added extension with this number. +upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)( + struct upb_Message* msg, const upb_MiniTableExtension* ext, + upb_Arena* arena); + +// Returns an extension for a message with a given mini table, +// or NULL if no extension exists with this mini table. +const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)( + const struct upb_Message* msg, const upb_MiniTableExtension* ext); + +UPB_INLINE bool UPB_PRIVATE(_upb_Extension_IsEmpty)(const upb_Extension* ext) { + switch ( + UPB_PRIVATE(_upb_MiniTableField_Mode)(&ext->ext->UPB_PRIVATE(field))) { + case kUpb_FieldMode_Scalar: + return false; + case kUpb_FieldMode_Array: + return upb_Array_Size(ext->data.array_val) == 0; + case kUpb_FieldMode_Map: + return _upb_Map_Size(ext->data.map_val) == 0; + } + UPB_UNREACHABLE(); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_INTERNAL_EXTENSION_H_ */ + +/* +** Our memory representation for parsing tables and messages themselves. +** Functions in this file are used by generated code and possibly reflection. +** +** The definitions in this file are internal to upb. +**/ + +#ifndef UPB_MESSAGE_INTERNAL_MESSAGE_H_ +#define UPB_MESSAGE_INTERNAL_MESSAGE_H_ + +#include +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +extern const float kUpb_FltInfinity; +extern const double kUpb_Infinity; +extern const double kUpb_NaN; + +// Internal members of a upb_Message that track unknown fields and/or +// extensions. We can change this without breaking binary compatibility. + +typedef struct upb_TaggedAuxPtr { + // Two lowest bits form a tag: + // 00 - non-aliased unknown data + // 10 - aliased unknown data + // 01 - extension + // + // The main semantic difference between aliased and non-aliased unknown data + // is that non-aliased unknown data can be assumed to have the following + // layout: + // + // [upb_StringView] [data] + // + // where the StringView points to the data buffer and the data buffer is + // immediately following the StringView. + // + // The string view does not necessarily point to the start of the data buffer; + // if the initial part of the buffer is removed from the message, the string + // view will point to the beginning of the remaining buffer. + // + // For aliased unknown data, this layout is _not_ guaranteed, since the + // pointer to the StringView can be anywhere in the allocation, and the + // StringView may point to non-data memory. + uintptr_t ptr; +} upb_TaggedAuxPtr; + +UPB_INLINE bool upb_TaggedAuxPtr_IsNull(upb_TaggedAuxPtr ptr) { + return ptr.ptr == 0; +} + +UPB_INLINE bool upb_TaggedAuxPtr_IsExtension(upb_TaggedAuxPtr ptr) { + return ptr.ptr & 1; +} + +UPB_INLINE bool upb_TaggedAuxPtr_IsUnknown(upb_TaggedAuxPtr ptr) { + return (ptr.ptr != 0) && ((ptr.ptr & 1) == 0); +} + +UPB_INLINE bool upb_TaggedAuxPtr_IsUnknownAliased(upb_TaggedAuxPtr ptr) { + return (ptr.ptr != 0) && ((ptr.ptr & 2) == 2); +} + +UPB_INLINE upb_Extension* upb_TaggedAuxPtr_Extension(upb_TaggedAuxPtr ptr) { + UPB_ASSERT(upb_TaggedAuxPtr_IsExtension(ptr)); + return (upb_Extension*)(ptr.ptr & ~3ULL); +} + +UPB_INLINE upb_StringView* upb_TaggedAuxPtr_UnknownData(upb_TaggedAuxPtr ptr) { + UPB_ASSERT(!upb_TaggedAuxPtr_IsExtension(ptr)); + return (upb_StringView*)(ptr.ptr & ~3ULL); +} + +UPB_INLINE upb_TaggedAuxPtr upb_TaggedAuxPtr_Null(void) { + upb_TaggedAuxPtr ptr; + ptr.ptr = 0; + return ptr; +} + +UPB_INLINE upb_TaggedAuxPtr +upb_TaggedAuxPtr_MakeExtension(const upb_Extension* e) { + upb_TaggedAuxPtr ptr; + ptr.ptr = (uintptr_t)e | 1; + return ptr; +} + +// This tag means that the original allocation for this field starts with the +// string view and ends with the end of the content referenced by the string +// view. +UPB_INLINE upb_TaggedAuxPtr +upb_TaggedAuxPtr_MakeUnknownData(const upb_StringView* sv) { + upb_TaggedAuxPtr ptr; + ptr.ptr = (uintptr_t)sv; + return ptr; +} + +// This tag implies no guarantee between the relationship of the string view and +// the data it points to. +UPB_INLINE upb_TaggedAuxPtr +upb_TaggedAuxPtr_MakeUnknownDataAliased(const upb_StringView* sv) { + upb_TaggedAuxPtr ptr; + ptr.ptr = (uintptr_t)sv | 2; + return ptr; +} + +typedef struct upb_Message_Internal { + // Total number of entries set in aux_data + uint32_t size; + uint32_t capacity; + // Tagged pointers to upb_StringView or upb_Extension + upb_TaggedAuxPtr aux_data[]; +} upb_Message_Internal; + +#ifdef UPB_TRACING_ENABLED +UPB_API void upb_Message_LogNewMessage(const upb_MiniTable* m, + const upb_Arena* arena); +UPB_API void upb_Message_SetNewMessageTraceHandler( + void (*handler)(const upb_MiniTable*, const upb_Arena*)); +#endif // UPB_TRACING_ENABLED + +// Inline version upb_Message_New(), for internal use. +UPB_INLINE struct upb_Message* _upb_Message_New(const upb_MiniTable* m, + upb_Arena* a) { + UPB_PRIVATE(upb_MiniTable_CheckInvariants)(m); +#ifdef UPB_TRACING_ENABLED + upb_Message_LogNewMessage(m, a); +#endif // UPB_TRACING_ENABLED + + const size_t size = m->UPB_PRIVATE(size); + // Message sizes are aligned up when constructing minitables; telling the + // compiler this avoids redoing alignment on the malloc fast path + UPB_ASSUME(size % kUpb_Message_Align == 0); + struct upb_Message* msg = (struct upb_Message*)upb_Arena_Malloc(a, size); + if (UPB_UNLIKELY(!msg)) return NULL; + memset(msg, 0, size); + return msg; +} + +// Discards the unknown fields for this message only. +void _upb_Message_DiscardUnknown_shallow(struct upb_Message* msg); + +UPB_NOINLINE bool UPB_PRIVATE(_upb_Message_AddUnknownSlowPath)( + struct upb_Message* msg, const char* data, size_t len, upb_Arena* arena, + bool alias); + +typedef enum { + // Provided buffer is copied into the message. + kUpb_AddUnknown_Copy = 0, + + // The message will alias the provided buffer. + kUpb_AddUnknown_Alias = 1, + + // The message will alias the provided buffer, and we may merge the data with + // the immediately preceding unknown field if possible. + kUpb_AddUnknown_AliasAllowMerge = 2, +} upb_AddUnknownMode; + +// Adds unknown data (serialized protobuf data) to the given message. The data +// must represent one or more complete and well formed proto fields. +// +// If `alias_base` is NULL, the bytes from `data` will be copied into the +// destination arena. Otherwise it must be a pointer to the beginning of the +// buffer that `data` points into, which signals that the message must alias +// the bytes instead of copying them. The value of `alias_base` is also used +// to mark the boundary of the buffer, so that we do not inappropriately +// coalesce two buffers that are separate objects but happen to be contiguous +// in memory. +UPB_INLINE bool UPB_PRIVATE(_upb_Message_AddUnknown)(struct upb_Message* msg, + const char* data, + size_t len, + upb_Arena* arena, + upb_AddUnknownMode mode) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + if (mode == kUpb_AddUnknown_AliasAllowMerge) { + // Aliasing parse of a message with sequential unknown fields is a simple + // pointer bump, so inline it. + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (in && in->size) { + upb_TaggedAuxPtr ptr = in->aux_data[in->size - 1]; + if (upb_TaggedAuxPtr_IsUnknown(ptr)) { + upb_StringView* existing = upb_TaggedAuxPtr_UnknownData(ptr); + // Fast path if the field we're adding is immediately after the last + // added unknown field. + // + // The caller has guaranteed to us, by passing + // kUpb_AddUnknown_AliasAllowMerge, that there is no risk that these two + // regions of memory are from different objects that are contiguous in + // memory by coincidence. + if (existing->data + existing->size == data) { + existing->size += len; + return true; + } + } + } + } + return UPB_PRIVATE(_upb_Message_AddUnknownSlowPath)( + msg, data, len, arena, mode != kUpb_AddUnknown_Copy); +} + +// Adds unknown data (serialized protobuf data) to the given message. +// The data is copied into the message instance. Data when concatenated together +// must represent one or more complete and well formed proto fields, but the +// individual spans may point only to partial fields. +bool UPB_PRIVATE(_upb_Message_AddUnknownV)(struct upb_Message* msg, + upb_Arena* arena, + upb_StringView data[], size_t count); + +// Ensures at least one slot is available in the aux_data of this message. +// Returns false if a reallocation is needed to satisfy the request, and fails. +bool UPB_PRIVATE(_upb_Message_ReserveSlot)(struct upb_Message* msg, + upb_Arena* arena); + +#define kUpb_Message_UnknownBegin 0 +#define kUpb_Message_ExtensionBegin 0 + +UPB_INLINE bool upb_Message_NextUnknown(const struct upb_Message* msg, + upb_StringView* data, uintptr_t* iter) { + const upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + size_t i = *iter; + if (in) { + while (i < in->size) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i++]; + if (upb_TaggedAuxPtr_IsUnknown(tagged_ptr)) { + *data = *upb_TaggedAuxPtr_UnknownData(tagged_ptr); + *iter = i; + return true; + } + } + } + data->size = 0; + data->data = NULL; + *iter = i; + return false; +} + +UPB_INLINE bool upb_Message_HasUnknown(const struct upb_Message* msg) { + upb_StringView data; + uintptr_t iter = kUpb_Message_UnknownBegin; + return upb_Message_NextUnknown(msg, &data, &iter); +} + +UPB_INLINE bool upb_Message_NextExtension(const struct upb_Message* msg, + const upb_MiniTableExtension** out_e, + upb_MessageValue* out_v, + uintptr_t* iter) { + const upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + uintptr_t i = *iter; + if (in) { + while (i < in->size) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i++]; + if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr); + + // Empty repeated fields or maps semantically don't exist. + if (UPB_PRIVATE(_upb_Extension_IsEmpty)(ext)) continue; + + *out_e = ext->ext; + *out_v = ext->data; + *iter = i; + return true; + } + } + } + *iter = i; + + return false; +} + +UPB_INLINE bool UPB_PRIVATE(_upb_Message_NextExtensionReverse)( + const struct upb_Message* msg, const upb_MiniTableExtension** out_e, + upb_MessageValue* out_v, uintptr_t* iter) { + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) return false; + uintptr_t i = *iter; + uint32_t size = in->size; + while (i < size) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[size - 1 - i]; + i++; + if (!upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + continue; + } + const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr); + + // Empty repeated fields or maps semantically don't exist. + if (UPB_PRIVATE(_upb_Extension_IsEmpty)(ext)) continue; + + *out_e = ext->ext; + *out_v = ext->data; + *iter = i; + return true; + } + *iter = i; + return false; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_INTERNAL_MESSAGE_H_ */ + +// Must be last. + +#if defined(__GNUC__) && !defined(__clang__) +// GCC raises incorrect warnings in these functions. It thinks that we are +// overrunning buffers, but we carefully write the functions in this file to +// guarantee that this is impossible. GCC gets this wrong due it its failure +// to perform constant propagation as we expect: +// - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108217 +// - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108226 +// +// Unfortunately this also indicates that GCC is not optimizing away the +// switch() in cases where it should be, compromising the performance. +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" +#pragma GCC diagnostic ignored "-Wstringop-overflow" +#if __GNUC__ >= 11 +#pragma GCC diagnostic ignored "-Wstringop-overread" +#endif +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// LINT.IfChange(presence_logic) + +// Hasbit access /////////////////////////////////////////////////////////////// + +UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbit)( + const struct upb_Message* msg, const upb_MiniTableField* f) { + const uint16_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f); + const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f); + + return (*UPB_PTR_AT(msg, offset, const char) & mask) != 0; +} + +UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)( + const struct upb_Message* msg, const upb_MiniTableField* f) { + const uint16_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f); + const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f); + + (*UPB_PTR_AT(msg, offset, char)) |= mask; +} + +UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)( + const struct upb_Message* msg, const upb_MiniTableField* f) { + const uint16_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f); + const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f); + + (*UPB_PTR_AT(msg, offset, char)) &= ~mask; +} + +// Oneof case access /////////////////////////////////////////////////////////// + +UPB_INLINE uint32_t* UPB_PRIVATE(_upb_Message_OneofCasePtr)( + struct upb_Message* msg, const upb_MiniTableField* f) { + return UPB_PTR_AT(msg, UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(f), + uint32_t); +} + +UPB_INLINE uint32_t UPB_PRIVATE(_upb_Message_GetOneofCase)( + const struct upb_Message* msg, const upb_MiniTableField* f) { + const uint32_t* ptr = + UPB_PRIVATE(_upb_Message_OneofCasePtr)((struct upb_Message*)msg, f); + + return *ptr; +} + +UPB_INLINE void UPB_PRIVATE(_upb_Message_SetOneofCase)( + struct upb_Message* msg, const upb_MiniTableField* f) { + uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f); + + *ptr = upb_MiniTableField_Number(f); +} + +// Returns true if the given field is the current oneof case. +// Does nothing if it is not the current oneof case. +UPB_INLINE bool UPB_PRIVATE(_upb_Message_ClearOneofCase)( + struct upb_Message* msg, const upb_MiniTableField* f) { + uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f); + + if (*ptr != upb_MiniTableField_Number(f)) return false; + *ptr = 0; + return true; +} + +UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber( + const struct upb_Message* message, const upb_MiniTableField* oneof_field) { + UPB_ASSUME(upb_MiniTableField_IsInOneof(oneof_field)); + return UPB_PRIVATE(_upb_Message_GetOneofCase)(message, oneof_field); +} + +UPB_API_INLINE const upb_MiniTableField* upb_Message_WhichOneof( + const struct upb_Message* msg, const upb_MiniTable* m, + const upb_MiniTableField* f) { + uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f); + if (field_number == 0) { + // No field in the oneof is set. + return NULL; + } + return upb_MiniTable_FindFieldByNumber(m, field_number); +} + +// LINT.ThenChange(GoogleInternalName2) + +// Returns false if the message is missing any of its required fields. +UPB_INLINE bool UPB_PRIVATE(_upb_Message_IsInitializedShallow)( + const struct upb_Message* msg, const upb_MiniTable* m) { + uint64_t bits; + memcpy(&bits, msg + 1, sizeof(bits)); + bits = upb_BigEndian64(bits); + return (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~bits) == 0; +} + +UPB_INLINE void* UPB_PRIVATE(_upb_Message_MutableDataPtr)( + struct upb_Message* msg, const upb_MiniTableField* f) { + return (char*)msg + f->UPB_ONLYBITS(offset); +} + +UPB_INLINE const void* UPB_PRIVATE(_upb_Message_DataPtr)( + const struct upb_Message* msg, const upb_MiniTableField* f) { + return (const char*)msg + f->UPB_ONLYBITS(offset); +} + +UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)( + struct upb_Message* msg, const upb_MiniTableField* f) { + if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) { + UPB_PRIVATE(_upb_Message_SetHasbit)(msg, f); + } else if (upb_MiniTableField_IsInOneof(f)) { + UPB_PRIVATE(_upb_Message_SetOneofCase)(msg, f); + } +} + +UPB_INLINE_IF_NOT_GCC void UPB_PRIVATE(_upb_MiniTableField_DataCopy)( + const upb_MiniTableField* f, void* to, const void* from) { + switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) { + case kUpb_FieldRep_1Byte: + memcpy(to, from, 1); + return; + case kUpb_FieldRep_4Byte: + memcpy(to, from, 4); + return; + case kUpb_FieldRep_8Byte: + memcpy(to, from, 8); + return; + case kUpb_FieldRep_StringView: { + memcpy(to, from, sizeof(upb_StringView)); + return; + } + } + UPB_UNREACHABLE(); +} + +UPB_INLINE_IF_NOT_GCC bool UPB_PRIVATE(_upb_MiniTableField_DataEquals)( + const upb_MiniTableField* f, const void* a, const void* b) { + switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) { + case kUpb_FieldRep_1Byte: + return memcmp(a, b, 1) == 0; + case kUpb_FieldRep_4Byte: + return memcmp(a, b, 4) == 0; + case kUpb_FieldRep_8Byte: + return memcmp(a, b, 8) == 0; + case kUpb_FieldRep_StringView: { + const upb_StringView sa = *(const upb_StringView*)a; + const upb_StringView sb = *(const upb_StringView*)b; + return upb_StringView_IsEqual(sa, sb); + } + } + UPB_UNREACHABLE(); +} + +UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataClear)( + const upb_MiniTableField* f, void* val) { + const char zero[16] = {0}; + UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, zero); +} + +UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataIsZero)( + const upb_MiniTableField* f, const void* val) { + const char zero[16] = {0}; + return UPB_PRIVATE(_upb_MiniTableField_DataEquals)(f, val, zero); +} + +// Here we define universal getter/setter functions for message fields. +// These look very branchy and inefficient, but as long as the MiniTableField +// values are known at compile time, all the branches are optimized away and +// we are left with ideal code. This can happen either through through +// literals or UPB_ASSUME(): +// +// // Via struct literals. +// bool FooMessage_set_bool_field(const upb_Message* msg, bool val) { +// const upb_MiniTableField field = {1, 0, 0, /* etc... */}; +// // All value in "field" are compile-time known. +// upb_Message_SetBaseField(msg, &field, &value); +// } +// +// // Via UPB_ASSUME(). +// UPB_INLINE bool upb_Message_SetBool(upb_Message* msg, +// const upb_MiniTableField* field, +// bool value, upb_Arena* a) { +// UPB_ASSUME(field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bool); +// UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) == +// kUpb_FieldRep_1Byte); +// upb_Message_SetField(msg, field, &value, a); +// } +// +// As a result, we can use these universal getters/setters for *all* message +// accessors: generated code, MiniTable accessors, and reflection. The only +// exception is the binary encoder/decoder, which need to be a bit more clever +// about how they read/write the message data, for efficiency. +// +// These functions work on both extensions and non-extensions. If the field +// of a setter is known to be a non-extension, the arena may be NULL and the +// returned bool value may be ignored since it will always succeed. + +UPB_API_INLINE bool upb_Message_HasBaseField(const struct upb_Message* msg, + const upb_MiniTableField* field) { + UPB_ASSERT(upb_MiniTableField_HasPresence(field)); + UPB_ASSUME(!upb_MiniTableField_IsExtension(field)); + if (upb_MiniTableField_IsInOneof(field)) { + return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, field) == + upb_MiniTableField_Number(field); + } else { + return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, field); + } +} + +UPB_API_INLINE bool upb_Message_HasExtension(const struct upb_Message* msg, + const upb_MiniTableExtension* e) { + UPB_ASSERT(upb_MiniTableField_HasPresence(&e->UPB_PRIVATE(field))); + return UPB_PRIVATE(_upb_Message_Getext)(msg, e) != NULL; +} + +UPB_FORCEINLINE void _upb_Message_GetNonExtensionField( + const struct upb_Message* msg, const upb_MiniTableField* field, + const void* default_val, void* val) { + UPB_ASSUME(!upb_MiniTableField_IsExtension(field)); + if ((upb_MiniTableField_IsInOneof(field) || + !UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(field, default_val)) && + !upb_Message_HasBaseField(msg, field)) { + UPB_PRIVATE(_upb_MiniTableField_DataCopy)(field, val, default_val); + return; + } + UPB_PRIVATE(_upb_MiniTableField_DataCopy) + (field, val, UPB_PRIVATE(_upb_Message_DataPtr)(msg, field)); +} + +UPB_INLINE void _upb_Message_GetExtensionField( + const struct upb_Message* msg, const upb_MiniTableExtension* mt_ext, + const void* default_val, void* val) { + const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, mt_ext); + const upb_MiniTableField* f = &mt_ext->UPB_PRIVATE(field); + UPB_ASSUME(upb_MiniTableField_IsExtension(f)); + + if (ext) { + UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, &ext->data); + } else { + UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, default_val); + } +} + +// NOTE: The default_val is only used for fields that support presence. +// For repeated/map fields, the resulting upb_Array*/upb_Map* can be NULL if a +// upb_Array/upb_Map has not been allocated yet. Array/map fields do not have +// presence, so this is semantically identical to a pointer to an empty +// array/map, and must be treated the same for all semantic purposes. +UPB_API_INLINE upb_MessageValue upb_Message_GetField( + const struct upb_Message* msg, const upb_MiniTableField* field, + upb_MessageValue default_val) { + upb_MessageValue ret; + if (upb_MiniTableField_IsExtension(field)) { + _upb_Message_GetExtensionField(msg, (upb_MiniTableExtension*)field, + &default_val, &ret); + } else { + _upb_Message_GetNonExtensionField(msg, field, &default_val, &ret); + } + return ret; +} + +UPB_API_INLINE void upb_Message_SetBaseField(struct upb_Message* msg, + const upb_MiniTableField* f, + const void* val) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + UPB_ASSUME(!upb_MiniTableField_IsExtension(f)); + UPB_PRIVATE(_upb_Message_SetPresence)(msg, f); + UPB_PRIVATE(_upb_MiniTableField_DataCopy) + (f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), val); +} + +UPB_API_INLINE bool upb_Message_SetExtension(struct upb_Message* msg, + const upb_MiniTableExtension* e, + const void* val, upb_Arena* a) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + UPB_ASSERT(a); + upb_Extension* ext = + UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, e, a); + if (!ext) return false; + UPB_PRIVATE(_upb_MiniTableField_DataCopy) + (&e->UPB_PRIVATE(field), &ext->data, val); + return true; +} + +// Sets the value of the given field in the given msg. The return value is true +// if the operation completed successfully, or false if memory allocation +// failed. +UPB_INLINE bool UPB_PRIVATE(_upb_Message_SetField)(struct upb_Message* msg, + const upb_MiniTableField* f, + upb_MessageValue val, + upb_Arena* a) { + if (upb_MiniTableField_IsExtension(f)) { + const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)f; + return upb_Message_SetExtension(msg, ext, &val, a); + } else { + upb_Message_SetBaseField(msg, f, &val); + return true; + } +} + +UPB_API_INLINE const upb_Array* upb_Message_GetArray( + const struct upb_Message* msg, const upb_MiniTableField* f) { + UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f); + upb_Array* ret; + const upb_Array* default_val = NULL; + _upb_Message_GetNonExtensionField(msg, f, &default_val, &ret); + return ret; +} + +UPB_API_INLINE bool upb_Message_GetBool(const struct upb_Message* msg, + const upb_MiniTableField* f, + bool default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte); + upb_MessageValue def; + def.bool_val = default_val; + return upb_Message_GetField(msg, f, def).bool_val; +} + +UPB_API_INLINE double upb_Message_GetDouble(const struct upb_Message* msg, + const upb_MiniTableField* f, + double default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte); + + upb_MessageValue def; + def.double_val = default_val; + return upb_Message_GetField(msg, f, def).double_val; +} + +UPB_API_INLINE float upb_Message_GetFloat(const struct upb_Message* msg, + const upb_MiniTableField* f, + float default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte); + + upb_MessageValue def; + def.float_val = default_val; + return upb_Message_GetField(msg, f, def).float_val; +} + +UPB_API_INLINE int32_t upb_Message_GetInt32(const struct upb_Message* msg, + const upb_MiniTableField* f, + int32_t default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 || + upb_MiniTableField_CType(f) == kUpb_CType_Enum); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte); + + upb_MessageValue def; + def.int32_val = default_val; + return upb_Message_GetField(msg, f, def).int32_val; +} + +UPB_API_INLINE int64_t upb_Message_GetInt64(const struct upb_Message* msg, + const upb_MiniTableField* f, + int64_t default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte); + + upb_MessageValue def; + def.int64_val = default_val; + return upb_Message_GetField(msg, f, def).int64_val; +} + +UPB_API_INLINE const struct upb_Map* upb_Message_GetMap( + const struct upb_Message* msg, const upb_MiniTableField* f) { + UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(f); + struct upb_Map* ret; + const struct upb_Map* default_val = NULL; + _upb_Message_GetNonExtensionField(msg, f, &default_val, &ret); + return ret; +} + +UPB_API_INLINE const struct upb_Message* upb_Message_GetMessage( + const struct upb_Message* msg, const upb_MiniTableField* f) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == + UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + upb_MessageValue def; + def.msg_val = NULL; + return upb_Message_GetField(msg, f, def).msg_val; +} + +UPB_API_INLINE upb_Array* upb_Message_GetMutableArray( + struct upb_Message* msg, const upb_MiniTableField* f) { + UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f); + return (upb_Array*)upb_Message_GetArray(msg, f); +} + +UPB_API_INLINE struct upb_Map* upb_Message_GetMutableMap( + struct upb_Message* msg, const upb_MiniTableField* f) { + return (struct upb_Map*)upb_Message_GetMap(msg, f); +} + +UPB_API_INLINE struct upb_Message* upb_Message_GetMutableMessage( + struct upb_Message* msg, const upb_MiniTableField* f) { + return (struct upb_Message*)upb_Message_GetMessage(msg, f); +} + +UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray( + struct upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena) { + UPB_ASSERT(arena); + UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f); + upb_Array* array = upb_Message_GetMutableArray(msg, f); + if (!array) { + array = UPB_PRIVATE(_upb_Array_New)( + arena, 4, UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(f)); + // Check again due to: https://godbolt.org/z/7WfaoKG1r + UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f); + upb_MessageValue val; + val.array_val = array; + UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, arena); + } + return array; +} + +UPB_INLINE struct upb_Map* _upb_Message_GetOrCreateMutableMap( + struct upb_Message* msg, const upb_MiniTableField* field, size_t key_size, + size_t val_size, upb_Arena* arena) { + UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field); + struct upb_Map* map = NULL; + struct upb_Map* default_map_value = NULL; + _upb_Message_GetNonExtensionField(msg, field, &default_map_value, &map); + if (!map) { + map = _upb_Map_New(arena, key_size, val_size); + // Check again due to: https://godbolt.org/z/7WfaoKG1r + UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field); + upb_Message_SetBaseField(msg, field, &map); + } + return map; +} + +UPB_API_INLINE struct upb_Map* upb_Message_GetOrCreateMutableMap( + struct upb_Message* msg, const upb_MiniTable* map_entry_mini_table, + const upb_MiniTableField* f, upb_Arena* arena) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message); + const upb_MiniTableField* map_entry_key_field = + &map_entry_mini_table->UPB_ONLYBITS(fields)[0]; + const upb_MiniTableField* map_entry_value_field = + &map_entry_mini_table->UPB_ONLYBITS(fields)[1]; + return _upb_Message_GetOrCreateMutableMap( + msg, f, _upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)), + _upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)), + arena); +} + +UPB_API_INLINE struct upb_Message* upb_Message_GetOrCreateMutableMessage( + struct upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena) { + UPB_ASSERT(arena); + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message); + UPB_ASSUME(!upb_MiniTableField_IsExtension(f)); + struct upb_Message* sub_message = + *UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*); + if (!sub_message) { + const upb_MiniTable* sub_mini_table = upb_MiniTable_SubMessage(f); + UPB_ASSERT(sub_mini_table); + sub_message = _upb_Message_New(sub_mini_table, arena); + *UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*) = + sub_message; + UPB_PRIVATE(_upb_Message_SetPresence)(msg, f); + } + return sub_message; +} + +UPB_API_INLINE upb_StringView +upb_Message_GetString(const struct upb_Message* msg, + const upb_MiniTableField* f, upb_StringView default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String || + upb_MiniTableField_CType(f) == kUpb_CType_Bytes); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == + kUpb_FieldRep_StringView); + + upb_MessageValue def; + def.str_val = default_val; + return upb_Message_GetField(msg, f, def).str_val; +} + +UPB_API_INLINE uint32_t upb_Message_GetUInt32(const struct upb_Message* msg, + const upb_MiniTableField* f, + uint32_t default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte); + + upb_MessageValue def; + def.uint32_val = default_val; + return upb_Message_GetField(msg, f, def).uint32_val; +} + +UPB_API_INLINE uint64_t upb_Message_GetUInt64(const struct upb_Message* msg, + const upb_MiniTableField* f, + uint64_t default_val) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte); + + upb_MessageValue def; + def.uint64_val = default_val; + return upb_Message_GetField(msg, f, def).uint64_val; +} + +// BaseField Setters /////////////////////////////////////////////////////////// + +UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg, + const upb_MiniTableField* f, + bool value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg, + const upb_MiniTableField* f, + double value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg, + const upb_MiniTableField* f, + float value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg, + const upb_MiniTableField* f, + int32_t value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 || + upb_MiniTableField_CType(f) == kUpb_CType_Enum); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg, + const upb_MiniTableField* f, + int64_t value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldMessage(struct upb_Message* msg, + const upb_MiniTableField* f, + struct upb_Message* value) { + // TODO - Re-enable this assertion. + // UPB_ASSERT(value); + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == + UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg, + const upb_MiniTableField* f, + upb_StringView value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String || + upb_MiniTableField_CType(f) == kUpb_CType_Bytes); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == + kUpb_FieldRep_StringView); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg, + const upb_MiniTableField* f, + uint32_t value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg, + const upb_MiniTableField* f, + uint64_t value) { + UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64); + UPB_ASSUME(upb_MiniTableField_IsScalar(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte); + upb_Message_SetBaseField(msg, f, &value); +} + +UPB_API_INLINE void upb_Message_SetClosedEnum(struct upb_Message* msg, + const upb_MiniTableField* f, + int32_t value) { + UPB_ASSERT(upb_MiniTableField_IsClosedEnum(f)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte); + UPB_ASSERT(upb_MiniTableEnum_CheckValue(upb_MiniTable_GetSubEnumTable(f), + (uint32_t)value)); + upb_Message_SetBaseField(msg, f, &value); +} + +// Extension Setters /////////////////////////////////////////////////////////// + +UPB_API_INLINE bool upb_Message_SetExtensionMessage( + struct upb_Message* msg, const upb_MiniTableExtension* e, + struct upb_Message* value, upb_Arena* a) { + UPB_ASSERT(value); + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Message); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionBool( + struct upb_Message* msg, const upb_MiniTableExtension* e, bool value, + upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_1Byte); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionDouble( + struct upb_Message* msg, const upb_MiniTableExtension* e, double value, + upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_8Byte); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionFloat( + struct upb_Message* msg, const upb_MiniTableExtension* e, float value, + upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_4Byte); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionInt32( + struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value, + upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 || + upb_MiniTableExtension_CType(e) == kUpb_CType_Enum); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_4Byte); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionInt64( + struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value, + upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_8Byte); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionString( + struct upb_Message* msg, const upb_MiniTableExtension* e, + upb_StringView value, upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String || + upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_StringView); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionUInt32( + struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value, + upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_4Byte); + return upb_Message_SetExtension(msg, e, &value, a); +} + +UPB_API_INLINE bool upb_Message_SetExtensionUInt64( + struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value, + upb_Arena* a) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_8Byte); + return upb_Message_SetExtension(msg, e, &value, a); +} + +// Universal Setters /////////////////////////////////////////////////////////// + +UPB_API_INLINE bool upb_Message_SetBool(struct upb_Message* msg, + const upb_MiniTableField* f, bool value, + upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionBool( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldBool(msg, f, value), true); +} + +UPB_API_INLINE bool upb_Message_SetDouble(struct upb_Message* msg, + const upb_MiniTableField* f, + double value, upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionDouble( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldDouble(msg, f, value), true); +} + +UPB_API_INLINE bool upb_Message_SetFloat(struct upb_Message* msg, + const upb_MiniTableField* f, + float value, upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionFloat( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldFloat(msg, f, value), true); +} + +UPB_API_INLINE bool upb_Message_SetInt32(struct upb_Message* msg, + const upb_MiniTableField* f, + int32_t value, upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionInt32( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldInt32(msg, f, value), true); +} + +UPB_API_INLINE bool upb_Message_SetInt64(struct upb_Message* msg, + const upb_MiniTableField* f, + int64_t value, upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionInt64( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldInt64(msg, f, value), true); +} + +// Sets the value of a message-typed field. The mini_tables of `msg` and +// `value` must have been linked for this to work correctly. +UPB_API_INLINE void upb_Message_SetMessage(struct upb_Message* msg, + const upb_MiniTableField* f, + struct upb_Message* value) { + UPB_ASSERT(!upb_MiniTableField_IsExtension(f)); + upb_Message_SetBaseFieldMessage(msg, f, value); +} + +// Sets the value of a `string` or `bytes` field. The bytes of the value are not +// copied, so it is the caller's responsibility to ensure that they remain valid +// for the lifetime of `msg`. That might be done by copying them into the given +// arena, or by fusing that arena with the arena the bytes live in, for example. +UPB_API_INLINE bool upb_Message_SetString(struct upb_Message* msg, + const upb_MiniTableField* f, + upb_StringView value, upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionString( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldString(msg, f, value), true); +} + +UPB_API_INLINE bool upb_Message_SetUInt32(struct upb_Message* msg, + const upb_MiniTableField* f, + uint32_t value, upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionUInt32( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldUInt32(msg, f, value), true); +} + +UPB_API_INLINE bool upb_Message_SetUInt64(struct upb_Message* msg, + const upb_MiniTableField* f, + uint64_t value, upb_Arena* a) { + return upb_MiniTableField_IsExtension(f) + ? upb_Message_SetExtensionUInt64( + msg, (const upb_MiniTableExtension*)f, value, a) + : (upb_Message_SetBaseFieldUInt64(msg, f, value), true); +} + +UPB_API_INLINE void upb_Message_Clear(struct upb_Message* msg, + const upb_MiniTable* m) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + memset(msg, 0, m->UPB_PRIVATE(size)); + if (in) { + // Reset the internal buffer to empty. + in->size = 0; + } +} + +UPB_API_INLINE void upb_Message_ClearBaseField(struct upb_Message* msg, + const upb_MiniTableField* f) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) { + UPB_PRIVATE(_upb_Message_ClearHasbit)(msg, f); + } else if (upb_MiniTableField_IsInOneof(f)) { + uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f); + if (*ptr != upb_MiniTableField_Number(f)) return; + *ptr = 0; + } + const char zeros[16] = {0}; + UPB_PRIVATE(_upb_MiniTableField_DataCopy) + (f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), zeros); +} + +UPB_API_INLINE void upb_Message_ClearExtension( + struct upb_Message* msg, const upb_MiniTableExtension* e) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg); + if (!in) return; + for (size_t i = 0; i < in->size; i++) { + upb_TaggedAuxPtr tagged_ptr = in->aux_data[i]; + if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) { + const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr); + if (ext->ext == e) { + in->aux_data[i] = upb_TaggedAuxPtr_Null(); + return; + } + } + } +} + +UPB_API_INLINE void upb_Message_ClearOneof(struct upb_Message* msg, + const upb_MiniTable* m, + const upb_MiniTableField* f) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f); + if (field_number == 0) { + // No field in the oneof is set. + return; + } + + const upb_MiniTableField* field = + upb_MiniTable_FindFieldByNumber(m, field_number); + upb_Message_ClearBaseField(msg, field); +} + +UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized( + struct upb_Message* msg, const upb_MiniTableField* f, size_t size, + upb_Arena* arena) { + UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, f, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena)) { + return NULL; + } + return upb_Array_MutableDataPtr(arr); +} + +UPB_API_INLINE bool upb_Message_GetExtensionBool( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + bool default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_1Byte); + bool ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE double upb_Message_GetExtensionDouble( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + double default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_8Byte); + double ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE float upb_Message_GetExtensionFloat( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + float default_val) { + float ret; + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_4Byte); + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE int32_t upb_Message_GetExtensionInt32( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + int32_t default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 || + upb_MiniTableExtension_CType(e) == kUpb_CType_Enum); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_4Byte); + int32_t ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE int64_t upb_Message_GetExtensionInt64( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + int64_t default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_8Byte); + int64_t ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE uint32_t upb_Message_GetExtensionUInt32( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + uint32_t default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_4Byte); + uint32_t ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE uint64_t upb_Message_GetExtensionUInt64( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + uint64_t default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_8Byte); + uint64_t ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE upb_StringView upb_Message_GetExtensionString( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + upb_StringView default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String || + upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + kUpb_FieldRep_StringView); + upb_StringView ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE struct upb_Message* upb_Message_GetExtensionMessage( + const struct upb_Message* msg, const upb_MiniTableExtension* e, + struct upb_Message* default_val) { + UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Message); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) == + UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); + struct upb_Message* ret; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +// Repeated +UPB_API_INLINE const upb_Array* upb_Message_GetExtensionArray( + const struct upb_Message* msg, const upb_MiniTableExtension* e) { + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field)) == + kUpb_FieldRep_NativePointer); + UPB_ASSUME(upb_MiniTableField_IsArray(&e->UPB_PRIVATE(field))); + UPB_ASSUME(e->UPB_PRIVATE(field).presence == 0); + upb_Array* ret; + const upb_Array* default_val = NULL; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +UPB_API_INLINE upb_Array* upb_Message_GetExtensionMutableArray( + struct upb_Message* msg, const upb_MiniTableExtension* e) { + UPB_ASSERT(!upb_Message_IsFrozen(msg)); + UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field)) == + kUpb_FieldRep_NativePointer); + UPB_ASSUME(upb_MiniTableField_IsArray(&e->UPB_PRIVATE(field))); + UPB_ASSUME(e->UPB_PRIVATE(field).presence == 0); + upb_Array* ret; + upb_Array* default_val = NULL; + _upb_Message_GetExtensionField(msg, e, &default_val, &ret); + return ret; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif + + +#endif // UPB_MESSAGE_INTERNAL_ACCESSORS_H_ + +#ifndef UPB_MESSAGE_MAP_H_ +#define UPB_MESSAGE_MAP_H_ + +#include + + +// Must be last. + +typedef struct upb_Map upb_Map; + +#ifdef __cplusplus +extern "C" { +#endif + +// Creates a new map on the given arena with the given key/value size. +UPB_API upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, + upb_CType value_type); + +// Returns the number of entries in the map. +UPB_API size_t upb_Map_Size(const upb_Map* map); + +// Stores a value for the given key into |*val| (or the zero value if the key is +// not present). Returns whether the key was present. The |val| pointer may be +// NULL, in which case the function tests whether the given key is present. +UPB_API bool upb_Map_Get(const upb_Map* map, upb_MessageValue key, + upb_MessageValue* val); + +// Returns a mutable pointer to the value for the given key. Returns NULL if the +// key is not present. +// This function is only legal to call for maps that contain messages. +UPB_API struct upb_Message* upb_Map_GetMutable(upb_Map* map, + upb_MessageValue key); + +// Removes all entries in the map. +UPB_API void upb_Map_Clear(upb_Map* map); + +// Sets the given key to the given value, returning whether the key was inserted +// or replaced. If the key was inserted, then any existing iterators will be +// invalidated. +UPB_API upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key, + upb_MessageValue val, + upb_Arena* arena); + +// Sets the given key to the given value. Returns false if memory allocation +// failed. If the key is newly inserted, then any existing iterators will be +// invalidated. +UPB_API_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key, + upb_MessageValue val, upb_Arena* arena) { + return upb_Map_Insert(map, key, val, arena) != + kUpb_MapInsertStatus_OutOfMemory; +} + +// Deletes this key from the table. Returns true if the key was present. +// If present and |val| is non-NULL, stores the deleted value. +UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key, + upb_MessageValue* val); + +// Map iteration: +// +// size_t iter = kUpb_Map_Begin; +// upb_MessageValue key, val; +// while (upb_Map_Next(map, &key, &val, &iter)) { +// ... +// } + +#define kUpb_Map_Begin ((size_t)-1) + +// Advances to the next entry. Returns false if no more entries are present. +// Otherwise returns true and populates both *key and *value. +UPB_API bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key, + upb_MessageValue* val, size_t* iter); + +// Sets the value for the entry pointed to by iter. +// WARNING: this does not currently work for string values! +UPB_API void upb_Map_SetEntryValue(upb_Map* map, size_t iter, + upb_MessageValue val); + +// DEPRECATED iterator, slated for removal. + +/* Map iteration: + * + * size_t iter = kUpb_Map_Begin; + * while (upb_MapIterator_Next(map, &iter)) { + * upb_MessageValue key = upb_MapIterator_Key(map, iter); + * upb_MessageValue val = upb_MapIterator_Value(map, iter); + * } + */ + +// Advances to the next entry. Returns false if no more entries are present. +UPB_API bool upb_MapIterator_Next(const upb_Map* map, size_t* iter); + +// Returns true if the iterator still points to a valid entry, or false if the +// iterator is past the last element. It is an error to call this function with +// kUpb_Map_Begin (you must call next() at least once first). +UPB_API bool upb_MapIterator_Done(const upb_Map* map, size_t iter); + +// Returns the key and value for this entry of the map. +UPB_API upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter); +UPB_API upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter); + +// Mark a map and all of its descendents as frozen/immutable. +// If the map values are messages then |m| must point to the minitable for +// those messages. Otherwise |m| must be NULL. +UPB_API void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m); + +// Returns whether a map has been frozen. +UPB_API_INLINE bool upb_Map_IsFrozen(const upb_Map* map); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_MAP_H_ */ + +// Public APIs for message operations that do not depend on the schema. +// +// MiniTable-based accessors live in accessors.h. + +#ifndef UPB_MESSAGE_MESSAGE_H_ +#define UPB_MESSAGE_MESSAGE_H_ + +#include +#include + + +// Must be last. + +typedef struct upb_Message upb_Message; + +#ifdef __cplusplus +extern "C" { +#endif + +// Creates a new message with the given mini_table on the given arena. +UPB_API upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* arena); + +// +// Unknown data may be stored non-contiguously. Each segment stores a block of +// unknown fields. To iterate over segments: +// +// uintptr_t iter = kUpb_Message_UnknownBegin; +// upb_StringView data; +// while (upb_Message_NextUnknown(msg, &data, &iter)) { +// // Use data +// } +// Iterates in the order unknown fields were parsed. + +#define kUpb_Message_UnknownBegin 0 +#define kUpb_Message_ExtensionBegin 0 + +UPB_INLINE bool upb_Message_NextUnknown(const upb_Message* msg, + upb_StringView* data, uintptr_t* iter); + +UPB_INLINE bool upb_Message_HasUnknown(const upb_Message* msg); + +// Removes a segment of unknown data from the message, advancing to the next +// segment. Returns false if the removed segment was at the end of the last +// chunk. +// +// This must be done while iterating: +// +// uintptr_t iter = kUpb_Message_UnknownBegin; +// upb_StringView data; +// // Iterate chunks +// while (upb_Message_NextUnknown(msg, &data, &iter)) { +// // Iterate within a chunk, deleting ranges +// while (ShouldDeleteSubSegment(&data)) { +// // Data now points to the region to be deleted +// switch (upb_Message_DeleteUnknown(msg, &data, &iter)) { +// case kUpb_Message_DeleteUnknown_DeletedLast: return ok; +// case kUpb_Message_DeleteUnknown_IterUpdated: break; +// // If DeleteUnknown returned kUpb_Message_DeleteUnknown_IterUpdated, +// // then data now points to the remaining unknown fields after the +// // region that was just deleted. +// case kUpb_Message_DeleteUnknown_AllocFail: return err; +// } +// } +// } +// +// The range given in `data` must be contained inside the most recently +// returned region. +typedef enum upb_Message_DeleteUnknownStatus { + kUpb_DeleteUnknown_DeletedLast, + kUpb_DeleteUnknown_IterUpdated, + kUpb_DeleteUnknown_AllocFail, +} upb_Message_DeleteUnknownStatus; +upb_Message_DeleteUnknownStatus upb_Message_DeleteUnknown(upb_Message* msg, + upb_StringView* data, + uintptr_t* iter, + upb_Arena* arena); + +// Returns the number of extensions present in this message. +size_t upb_Message_ExtensionCount(const upb_Message* msg); + +// Iterates extensions in wire order +UPB_INLINE bool upb_Message_NextExtension(const upb_Message* msg, + const upb_MiniTableExtension** out_e, + upb_MessageValue* out_v, + uintptr_t* iter); + +// Iterates extensions in reverse wire order +UPB_INLINE bool UPB_PRIVATE(_upb_Message_NextExtensionReverse)( + const struct upb_Message* msg, const upb_MiniTableExtension** out_e, + upb_MessageValue* out_v, uintptr_t* iter); + +// Mark a message and all of its descendents as frozen/immutable. +UPB_API void upb_Message_Freeze(upb_Message* msg, const upb_MiniTable* m); + +// Returns whether a message has been frozen. +UPB_API_INLINE bool upb_Message_IsFrozen(const upb_Message* msg); + +#ifdef UPB_TRACING_ENABLED +UPB_API void upb_Message_LogNewMessage(const upb_MiniTable* m, + const upb_Arena* arena); + +UPB_API void upb_Message_SetNewMessageTraceHandler( + void (*handler)(const upb_MiniTable* m, const upb_Arena* arena)); +#endif // UPB_TRACING_ENABLED + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_MESSAGE_H_ */ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Functions ending in BaseField() take a (upb_MiniTableField*) argument +// and work only on non-extension fields. +// +// Functions ending in Extension() take a (upb_MiniTableExtension*) argument +// and work only on extensions. + +UPB_API_INLINE void upb_Message_Clear(upb_Message* msg, const upb_MiniTable* m); + +UPB_API_INLINE void upb_Message_ClearBaseField(upb_Message* msg, + const upb_MiniTableField* f); + +UPB_API_INLINE void upb_Message_ClearExtension(upb_Message* msg, + const upb_MiniTableExtension* e); + +UPB_API_INLINE void upb_Message_ClearOneof(upb_Message* msg, + const upb_MiniTable* m, + const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_Message_HasBaseField(const upb_Message* msg, + const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_Message_HasExtension(const upb_Message* msg, + const upb_MiniTableExtension* e); + +UPB_API_INLINE upb_MessageValue +upb_Message_GetField(const upb_Message* msg, const upb_MiniTableField* f, + upb_MessageValue default_val); + +UPB_API_INLINE const upb_Array* upb_Message_GetArray( + const upb_Message* msg, const upb_MiniTableField* f); + +UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg, + const upb_MiniTableField* f, + bool default_val); + +UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg, + const upb_MiniTableField* field, + double default_val); + +UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg, + const upb_MiniTableField* f, + float default_val); + +UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg, + const upb_MiniTableField* f, + int32_t default_val); + +UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg, + const upb_MiniTableField* f, + int64_t default_val); + +UPB_API_INLINE const upb_Map* upb_Message_GetMap(const upb_Message* msg, + const upb_MiniTableField* f); + +UPB_API_INLINE const upb_Message* upb_Message_GetMessage( + const upb_Message* msg, const upb_MiniTableField* f); + +UPB_API_INLINE upb_Array* upb_Message_GetMutableArray( + upb_Message* msg, const upb_MiniTableField* f); + +UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(upb_Message* msg, + const upb_MiniTableField* f); + +UPB_API_INLINE upb_Message* upb_Message_GetMutableMessage( + upb_Message* msg, const upb_MiniTableField* f); + +UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray( + upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena); + +UPB_API_INLINE upb_Map* upb_Message_GetOrCreateMutableMap( + upb_Message* msg, const upb_MiniTable* map_entry_mini_table, + const upb_MiniTableField* f, upb_Arena* arena); + +UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage( + upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena); + +UPB_API_INLINE upb_StringView +upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field, + upb_StringView default_val); + +UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg, + const upb_MiniTableField* f, + uint32_t default_val); + +UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg, + const upb_MiniTableField* f, + uint64_t default_val); + +UPB_API_INLINE void upb_Message_SetClosedEnum(upb_Message* msg, + const upb_MiniTableField* f, + int32_t value); + +// BaseField Setters /////////////////////////////////////////////////////////// + +UPB_API_INLINE void upb_Message_SetBaseField(upb_Message* msg, + const upb_MiniTableField* f, + const void* val); + +UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg, + const upb_MiniTableField* f, + bool value); + +UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg, + const upb_MiniTableField* f, + double value); + +UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg, + const upb_MiniTableField* f, + float value); + +UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg, + const upb_MiniTableField* f, + int32_t value); + +UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg, + const upb_MiniTableField* f, + int64_t value); + +UPB_API_INLINE void upb_Message_SetBaseFieldMessage(struct upb_Message* msg, + const upb_MiniTableField* f, + upb_Message* value); + +UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg, + const upb_MiniTableField* f, + upb_StringView value); + +UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg, + const upb_MiniTableField* f, + uint32_t value); + +UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg, + const upb_MiniTableField* f, + uint64_t value); + +// Extension Getters /////////////////////////////////////////////////////////// +UPB_API_INLINE bool upb_Message_GetExtensionBool( + const upb_Message* msg, const upb_MiniTableExtension* f, bool default_val); + +UPB_API_INLINE double upb_Message_GetExtensionDouble( + const upb_Message* msg, const upb_MiniTableExtension* f, + double default_val); + +UPB_API_INLINE float upb_Message_GetExtensionFloat( + const upb_Message* msg, const upb_MiniTableExtension* f, float default_val); + +UPB_API_INLINE int32_t upb_Message_GetExtensionInt32( + const upb_Message* msg, const upb_MiniTableExtension* f, + int32_t default_val); + +UPB_API_INLINE int64_t upb_Message_GetExtensionInt64( + const upb_Message* msg, const upb_MiniTableExtension* f, + int64_t default_val); + +UPB_API_INLINE uint32_t upb_Message_GetExtensionUInt32( + const upb_Message* msg, const upb_MiniTableExtension* f, + uint32_t default_val); + +UPB_API_INLINE uint64_t upb_Message_GetExtensionUInt64( + const upb_Message* msg, const upb_MiniTableExtension* f, + uint64_t default_val); + +UPB_API_INLINE upb_StringView upb_Message_GetExtensionString( + const upb_Message* msg, const upb_MiniTableExtension* f, + upb_StringView default_val); + +UPB_API_INLINE upb_Message* upb_Message_GetExtensionMessage( + const upb_Message* msg, const upb_MiniTableExtension* f, + struct upb_Message* default_val); + +UPB_API_INLINE const upb_Array* upb_Message_GetExtensionArray( + const upb_Message* msg, const upb_MiniTableExtension* f); + +UPB_API_INLINE upb_Array* upb_Message_GetExtensionMutableArray( + upb_Message* msg, const upb_MiniTableExtension* f); + +// Extension Setters /////////////////////////////////////////////////////////// + +UPB_API_INLINE bool upb_Message_SetExtension(upb_Message* msg, + const upb_MiniTableExtension* e, + const void* value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionMessage( + struct upb_Message* msg, const upb_MiniTableExtension* e, + struct upb_Message* value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionBool( + struct upb_Message* msg, const upb_MiniTableExtension* e, bool value, + upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionDouble( + struct upb_Message* msg, const upb_MiniTableExtension* e, double value, + upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionFloat( + struct upb_Message* msg, const upb_MiniTableExtension* e, float value, + upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionInt32( + struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value, + upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionInt64( + struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value, + upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionString( + struct upb_Message* msg, const upb_MiniTableExtension* e, + upb_StringView value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionUInt32( + struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value, + upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetExtensionUInt64( + struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value, + upb_Arena* a); + +// Universal Setters /////////////////////////////////////////////////////////// + +UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg, + const upb_MiniTableField* f, bool value, + upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg, + const upb_MiniTableField* f, + double value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg, + const upb_MiniTableField* f, + float value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg, + const upb_MiniTableField* f, + int32_t value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg, + const upb_MiniTableField* f, + int64_t value, upb_Arena* a); + +// Unlike the other similarly-named setters, this function can only be +// called on base fields. Prefer upb_Message_SetBaseFieldMessage(). +UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg, + const upb_MiniTableField* f, + upb_Message* value); + +UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg, + const upb_MiniTableField* f, + upb_StringView value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg, + const upb_MiniTableField* f, + uint32_t value, upb_Arena* a); + +UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg, + const upb_MiniTableField* f, + uint64_t value, upb_Arena* a); + +//////////////////////////////////////////////////////////////////////////////// + +UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized( + upb_Message* msg, const upb_MiniTableField* f, size_t size, + upb_Arena* arena); + +UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber( + const upb_Message* message, const upb_MiniTableField* oneof_field); + +// For a field `f` which is in a oneof, return the field of that +// oneof that is actually set (or NULL if none). +UPB_API_INLINE const upb_MiniTableField* upb_Message_WhichOneof( + const upb_Message* msg, const upb_MiniTable* m, + const upb_MiniTableField* f); + +// Updates a map entry given an entry message. +bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTableField* field, + upb_Message* map_entry_message, upb_Arena* arena); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#if defined(__cplusplus) +// Temporary overloads for functions whose signature has recently changed. +UPB_DEPRECATE_AND_INLINE() +inline upb_Message* upb_Message_GetOrCreateMutableMessage( + upb_Message* msg, const upb_MiniTable* mini_table, + const upb_MiniTableField* f, upb_Arena* arena) { + return upb_Message_GetOrCreateMutableMessage(msg, f, arena); +} + +UPB_DEPRECATE_AND_INLINE() +inline void upb_Message_SetClosedEnum(upb_Message* msg, + const upb_MiniTable* msg_mini_table, + const upb_MiniTableField* f, + int32_t value) { + upb_Message_SetClosedEnum(msg, f, value); +} + +UPB_DEPRECATE_AND_INLINE() +inline bool upb_Message_SetMapEntry(upb_Map* map, + const upb_MiniTable* mini_table, + const upb_MiniTableField* field, + upb_Message* map_entry_message, + upb_Arena* arena) { + return upb_Message_SetMapEntry(map, field, map_entry_message, arena); +} +#endif + + +#endif // UPB_MESSAGE_ACCESSORS_H_ + +// These functions are only used by generated code. + +#ifndef UPB_MESSAGE_MAP_GENCODE_UTIL_H_ +#define UPB_MESSAGE_MAP_GENCODE_UTIL_H_ + +// This header file is referenced by multiple files. Leave it empty. +// TODO: b/399481227 - Remove this header file, after all the references are +// cleaned up. +#endif /* UPB_MESSAGE_MAP_GENCODE_UTIL_H_ */ + +#ifndef UPB_MINI_TABLE_DECODE_H_ +#define UPB_MINI_TABLE_DECODE_H_ + +#include + + +#ifndef UPB_BASE_STATUS_H_ +#define UPB_BASE_STATUS_H_ + +#include + +// Must be last. + +#define _kUpb_Status_MaxMessage 511 + +typedef struct { + bool ok; + char msg[_kUpb_Status_MaxMessage]; // Error message; NULL-terminated. +} upb_Status; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API const char* upb_Status_ErrorMessage(const upb_Status* status); +UPB_API bool upb_Status_IsOk(const upb_Status* status); + +// These are no-op if |status| is NULL. +UPB_API void upb_Status_Clear(upb_Status* status); +void upb_Status_SetErrorMessage(upb_Status* status, const char* msg); +void upb_Status_SetErrorFormat(upb_Status* status, const char* fmt, ...) + UPB_PRINTF(2, 3); +void upb_Status_VSetErrorFormat(upb_Status* status, const char* fmt, + va_list args) UPB_PRINTF(2, 0); +void upb_Status_VAppendErrorFormat(upb_Status* status, const char* fmt, + va_list args) UPB_PRINTF(2, 0); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_BASE_STATUS_H_ */ + +#ifndef UPB_MINI_TABLE_SUB_H_ +#define UPB_MINI_TABLE_SUB_H_ + + +// Must be last. + +typedef union upb_MiniTableSub upb_MiniTableSub; + +#ifdef __cplusplus +extern "C" { +#endif + +// Constructors + +UPB_API_INLINE upb_MiniTableSub +upb_MiniTableSub_FromEnum(const upb_MiniTableEnum* subenum); + +UPB_API_INLINE upb_MiniTableSub +upb_MiniTableSub_FromMessage(const upb_MiniTable* submsg); + +// Getters + +UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTableSub_Enum( + upb_MiniTableSub sub); + +UPB_API_INLINE const upb_MiniTable* upb_MiniTableSub_Message( + upb_MiniTableSub sub); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_SUB_H_ */ + +// Export the newer headers, for legacy users. New users should include the +// more specific headers directly. +// IWYU pragma: begin_exports + +#ifndef UPB_MINI_DESCRIPTOR_BUILD_ENUM_H_ +#define UPB_MINI_DESCRIPTOR_BUILD_ENUM_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Builds a upb_MiniTableEnum from an enum mini descriptor. +// The mini descriptor must be for an enum, not a message. +UPB_API upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data, size_t len, + upb_Arena* arena, + upb_Status* status); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_MINI_DESCRIPTOR_BUILD_ENUM_H_ + +// Functions for linking MiniTables together once they are built from a +// MiniDescriptor. +// +// These functions have names like upb_MiniTable_Link() because they operate on +// MiniTables. We put them here, rather than in the mini_table/ directory, +// because they are only needed when building MiniTables from MiniDescriptors. +// The interfaces in mini_table/ assume that MiniTables are immutable. + +#ifndef UPB_MINI_DESCRIPTOR_LINK_H_ +#define UPB_MINI_DESCRIPTOR_LINK_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Links a sub-message field to a MiniTable for that sub-message. If a +// sub-message field is not linked, it will be treated as an unknown field +// during parsing, and setting the field will not be allowed. It is possible +// to link the message field later, at which point it will no longer be treated +// as unknown. However there is no synchronization for this operation, which +// means parallel mutation requires external synchronization. +// Returns success/failure. +UPB_API bool upb_MiniTable_SetSubMessage(upb_MiniTable* table, + upb_MiniTableField* field, + const upb_MiniTable* sub); + +// Links an enum field to a MiniTable for that enum. +// All enum fields must be linked prior to parsing. +// Returns success/failure. +UPB_API bool upb_MiniTable_SetSubEnum(upb_MiniTable* table, + upb_MiniTableField* field, + const upb_MiniTableEnum* sub); + +// Returns a list of fields that require linking at runtime, to connect the +// MiniTable to its sub-messages and sub-enums. The list of fields will be +// written to the `subs` array, which must have been allocated by the caller +// and must be large enough to hold a list of all fields in the message. +// +// The order of the fields returned by this function is significant: it matches +// the order expected by upb_MiniTable_Link() below. +// +// The return value packs the sub-message count and sub-enum count into a single +// integer like so: +// return (msg_count << 16) | enum_count; +UPB_API uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt, + const upb_MiniTableField** subs); + +// Links a message to its sub-messages and sub-enums. The caller must pass +// arrays of sub-tables and sub-enums, in the same length and order as is +// returned by upb_MiniTable_GetSubList() above. However, individual elements +// of the sub_tables may be NULL if those sub-messages were tree shaken. +// +// Returns false if either array is too short, or if any of the tables fails +// to link. +UPB_API bool upb_MiniTable_Link(upb_MiniTable* mt, + const upb_MiniTable** sub_tables, + size_t sub_table_count, + const upb_MiniTableEnum** sub_enums, + size_t sub_enum_count); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_MINI_DESCRIPTOR_LINK_H_ +// IWYU pragma: end_exports + +// Must be last. + +typedef enum { + kUpb_MiniTablePlatform_32Bit, + kUpb_MiniTablePlatform_64Bit, + kUpb_MiniTablePlatform_Native = + UPB_SIZE(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit), +} upb_MiniTablePlatform; + +#ifdef __cplusplus +extern "C" { +#endif + +// Builds a mini table from the data encoded in the buffer [data, len]. If any +// errors occur, returns NULL and sets a status message. In the success case, +// the caller must call upb_MiniTable_SetSub*() for all message or proto2 enum +// fields to link the table to the appropriate sub-tables. +upb_MiniTable* _upb_MiniTable_Build(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, upb_Status* status); + +UPB_API_INLINE upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len, + upb_Arena* arena, + upb_Status* status) { + return _upb_MiniTable_Build(data, len, kUpb_MiniTablePlatform_Native, arena, + status); +} + +// Initializes a MiniTableExtension buffer that has already been allocated. +// This is needed by upb_FileDef and upb_MessageDef, which allocate all of the +// extensions together in a single contiguous array. +const char* _upb_MiniTableExtension_Init(const char* data, size_t len, + upb_MiniTableExtension* ext, + const upb_MiniTable* extendee, + upb_MiniTableSub sub, + upb_MiniTablePlatform platform, + upb_Status* status); + +UPB_API_INLINE const char* upb_MiniTableExtension_Init( + const char* data, size_t len, upb_MiniTableExtension* ext, + const upb_MiniTable* extendee, upb_MiniTableSub sub, upb_Status* status) { + return _upb_MiniTableExtension_Init(data, len, ext, extendee, sub, + kUpb_MiniTablePlatform_Native, status); +} + +UPB_API upb_MiniTableExtension* _upb_MiniTableExtension_Build( + const char* data, size_t len, const upb_MiniTable* extendee, + upb_MiniTableSub sub, upb_MiniTablePlatform platform, upb_Arena* arena, + upb_Status* status); + +UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_Build( + const char* data, size_t len, const upb_MiniTable* extendee, + upb_Arena* arena, upb_Status* status) { + upb_MiniTableSub sub = upb_MiniTableSub_FromMessage(NULL); + return _upb_MiniTableExtension_Build( + data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status); +} + +UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildMessage( + const char* data, size_t len, const upb_MiniTable* extendee, + const upb_MiniTable* submsg, upb_Arena* arena, upb_Status* status) { + upb_MiniTableSub sub = upb_MiniTableSub_FromMessage(submsg); + return _upb_MiniTableExtension_Build( + data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status); +} + +UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildEnum( + const char* data, size_t len, const upb_MiniTable* extendee, + const upb_MiniTableEnum* subenum, upb_Arena* arena, upb_Status* status) { + upb_MiniTableSub sub = upb_MiniTableSub_FromEnum(subenum); + return _upb_MiniTableExtension_Build( + data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status); +} + +// Like upb_MiniTable_Build(), but the user provides a buffer of layout data so +// it can be reused from call to call, avoiding repeated +// upb_grealloc()/upb_gfree(). +// +// The caller owns `*buf` both before and after the call, and must upb_gfree() +// it when it is no longer in use. The function will upb_grealloc() `*buf` as +// necessary, updating `*size` accordingly. +upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len, + upb_MiniTablePlatform platform, + upb_Arena* arena, void** buf, + size_t* buf_size, upb_Status* status); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_DECODE_H_ */ + +#ifndef UPB_MINI_TABLE_EXTENSION_REGISTRY_H_ +#define UPB_MINI_TABLE_EXTENSION_REGISTRY_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +/* Extension registry: a dynamic data structure that stores a map of: + * (upb_MiniTable, number) -> extension info + * + * upb_decode() uses upb_ExtensionRegistry to look up extensions while parsing + * binary format. + * + * upb_ExtensionRegistry is part of the mini-table (msglayout) family of + * objects. Like all mini-table objects, it is suitable for reflection-less + * builds that do not want to expose names into the binary. + * + * Unlike most mini-table types, upb_ExtensionRegistry requires dynamic memory + * allocation and dynamic initialization: + * * If reflection is being used, then upb_DefPool will construct an appropriate + * upb_ExtensionRegistry automatically. + * * For a mini-table only build, the user must manually construct the + * upb_ExtensionRegistry and populate it with all of the extensions the user + * cares about. + * * A third alternative is to manually unpack relevant extensions after the + * main parse is complete, similar to how Any works. This is perhaps the + * nicest solution from the perspective of reducing dependencies, avoiding + * dynamic memory allocation, and avoiding the need to parse uninteresting + * extensions. The downsides are: + * (1) parse errors are not caught during the main parse + * (2) the CPU hit of parsing comes during access, which could cause an + * undesirable stutter in application performance. + * + * Users cannot directly get or put into this map. Users can only add the + * extensions from a generated module and pass the extension registry to the + * binary decoder. + * + * A upb_DefPool provides a upb_ExtensionRegistry, so any users who use + * reflection do not need to populate a upb_ExtensionRegistry directly. + */ + +typedef struct upb_ExtensionRegistry upb_ExtensionRegistry; + +// LINT.IfChange +typedef enum { + kUpb_ExtensionRegistryStatus_Ok = 0, + kUpb_ExtensionRegistryStatus_DuplicateEntry = 1, + kUpb_ExtensionRegistryStatus_OutOfMemory = 2, + kUpb_ExtensionRegistryStatus_InvalidExtension = 3, +} upb_ExtensionRegistryStatus; +// LINT.ThenChange(//depot/google3/third_party/upb/rust/sys/mini_table/extension_registry.rs) + +// Creates a upb_ExtensionRegistry in the given arena. +// The arena must outlive any use of the extreg. +UPB_API upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena); + +UPB_API upb_ExtensionRegistryStatus upb_ExtensionRegistry_Add( + upb_ExtensionRegistry* r, const upb_MiniTableExtension* e); + +// Adds the given extension info for the array |e| of size |count| into the +// registry. If there are any errors, the entire array is backed out. +// The extensions must outlive the registry. +// Possible errors include OOM or an extension number that already exists. +upb_ExtensionRegistryStatus upb_ExtensionRegistry_AddArray( + upb_ExtensionRegistry* r, const upb_MiniTableExtension** e, size_t count); + +// Looks up the extension (if any) defined for message type |t| and field +// number |num|. Returns the extension if found, otherwise NULL. +UPB_API const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup( + const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num); + +// Returns the number of extensions in the registry. For testing/debugging only. +UPB_API size_t upb_ExtensionRegistry_Size(const upb_ExtensionRegistry* r); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_EXTENSION_REGISTRY_H_ */ + +#ifndef UPB_MINI_TABLE_FILE_H_ +#define UPB_MINI_TABLE_FILE_H_ + + +#ifndef UPB_MINI_TABLE_INTERNAL_FILE_H_ +#define UPB_MINI_TABLE_INTERNAL_FILE_H_ + +// Must be last. + +struct upb_MiniTableFile { + const struct upb_MiniTable** UPB_PRIVATE(msgs); + const struct upb_MiniTableEnum** UPB_PRIVATE(enums); + const struct upb_MiniTableExtension** UPB_PRIVATE(exts); + int UPB_PRIVATE(msg_count); + int UPB_PRIVATE(enum_count); + int UPB_PRIVATE(ext_count); +}; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE int upb_MiniTableFile_EnumCount( + const struct upb_MiniTableFile* f) { + return f->UPB_PRIVATE(enum_count); +} + +UPB_API_INLINE int upb_MiniTableFile_ExtensionCount( + const struct upb_MiniTableFile* f) { + return f->UPB_PRIVATE(ext_count); +} + +UPB_API_INLINE int upb_MiniTableFile_MessageCount( + const struct upb_MiniTableFile* f) { + return f->UPB_PRIVATE(msg_count); +} + +UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTableFile_Enum( + const struct upb_MiniTableFile* f, int i) { + UPB_ASSERT(i < f->UPB_PRIVATE(enum_count)); + return f->UPB_PRIVATE(enums)[i]; +} + +UPB_API_INLINE const struct upb_MiniTableExtension* upb_MiniTableFile_Extension( + const struct upb_MiniTableFile* f, int i) { + UPB_ASSERT(i < f->UPB_PRIVATE(ext_count)); + return f->UPB_PRIVATE(exts)[i]; +} + +UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableFile_Message( + const struct upb_MiniTableFile* f, int i) { + UPB_ASSERT(i < f->UPB_PRIVATE(msg_count)); + return f->UPB_PRIVATE(msgs)[i]; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_FILE_H_ */ + +// Must be last. + +typedef struct upb_MiniTableFile upb_MiniTableFile; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTableFile_Enum( + const upb_MiniTableFile* f, int i); + +UPB_API_INLINE int upb_MiniTableFile_EnumCount(const upb_MiniTableFile* f); + +UPB_API_INLINE const upb_MiniTableExtension* upb_MiniTableFile_Extension( + const upb_MiniTableFile* f, int i); + +UPB_API_INLINE int upb_MiniTableFile_ExtensionCount(const upb_MiniTableFile* f); + +UPB_API_INLINE const upb_MiniTable* upb_MiniTableFile_Message( + const upb_MiniTableFile* f, int i); + +UPB_API_INLINE int upb_MiniTableFile_MessageCount(const upb_MiniTableFile* f); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_FILE_H_ */ + +#ifndef UPB_MINI_TABLE_INTERNAL_GENERATED_EXTENSION_REGISTRY_H_ +#define UPB_MINI_TABLE_INTERNAL_GENERATED_EXTENSION_REGISTRY_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct UPB_PRIVATE(upb_GeneratedExtensionListEntry) { + const struct upb_MiniTableExtension** start; + const struct upb_MiniTableExtension** stop; + const struct UPB_PRIVATE(upb_GeneratedExtensionListEntry) * next; +} UPB_PRIVATE(upb_GeneratedExtensionListEntry); + +struct upb_GeneratedRegistryRef { + struct upb_Arena* UPB_PRIVATE(arena); + const struct upb_ExtensionRegistry* UPB_PRIVATE(registry); +}; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_TABLE_INTERNAL_GENERATED_EXTENSION_REGISTRY_H_ */ + +// upb_decode: parsing into a upb_Message using a upb_MiniTable. + +#ifndef UPB_WIRE_DECODE_H_ +#define UPB_WIRE_DECODE_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// LINT.IfChange +enum { + /* If set, strings and unknown fields will alias the input buffer instead of + * copying into the arena. */ + kUpb_DecodeOption_AliasString = 1, + + /* If set, the parse will return failure if any message is missing any + * required fields when the message data ends. The parse will still continue, + * and the failure will only be reported at the end. + * + * IMPORTANT CAVEATS: + * + * 1. This can throw a false positive failure if an incomplete message is seen + * on the wire but is later completed when the sub-message occurs again. + * For this reason, a second pass is required to verify a failure, to be + * truly robust. + * + * 2. This can return a false success if you are decoding into a message that + * already has some sub-message fields present. If the sub-message does + * not occur in the binary payload, we will never visit it and discover the + * incomplete sub-message. For this reason, this check is only useful for + * implementing ParseFromString() semantics. For MergeFromString(), a + * post-parse validation step will always be necessary. */ + kUpb_DecodeOption_CheckRequired = 2, + + /* EXPERIMENTAL: + * + * If set, decoding will enforce UTF-8 validation for string fields, even for + * proto2 or fields with `features.utf8_validation = NONE`. Normally, only + * proto3 string fields will be validated for UTF-8. Decoding will return + * kUpb_DecodeStatus_BadUtf8 for non-UTF-8 strings, which is the same behavior + * as non-UTF-8 proto3 string fields. + */ + kUpb_DecodeOption_AlwaysValidateUtf8 = 8, + + /* EXPERIMENTAL: + * + * If set, the fasttable decoder will not be used. */ + kUpb_DecodeOption_DisableFastTable = 16, +}; +// LINT.ThenChange(//depot/google3/third_party/upb/rust/wire.rs:decode_status) + +UPB_INLINE uint32_t upb_DecodeOptions_MaxDepth(uint16_t depth) { + return (uint32_t)depth << 16; +} + +uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options); + +// Enforce an upper bound on recursion depth. +UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) { + uint32_t max_depth = upb_DecodeOptions_GetEffectiveMaxDepth(decode_options); + if (max_depth > limit) max_depth = limit; + return (int)(upb_DecodeOptions_MaxDepth(max_depth) | + (decode_options & 0xffff)); +} + +// LINT.IfChange +typedef enum { + kUpb_DecodeStatus_Ok = 0, + kUpb_DecodeStatus_OutOfMemory = 1, // Arena alloc failed + kUpb_DecodeStatus_Malformed = 2, // Wire format was corrupt + kUpb_DecodeStatus_BadUtf8 = 3, // String field had bad UTF-8 + kUpb_DecodeStatus_MaxDepthExceeded = + 4, // Exceeded upb_DecodeOptions_MaxDepth + + // kUpb_DecodeOption_CheckRequired failed (see above), but the parse otherwise + // succeeded. + kUpb_DecodeStatus_MissingRequired = 5, +} upb_DecodeStatus; +// LINT.ThenChange(//depot/google3/third_party/upb/rust/sys/wire/wire.rs:decode_status) + +UPB_API upb_DecodeStatus upb_Decode(const char* buf, size_t size, + upb_Message* msg, const upb_MiniTable* mt, + const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena); + +// Same as upb_Decode but with a varint-encoded length prepended. +// On success 'num_bytes_read' will be set to the how many bytes were read, +// on failure the contents of num_bytes_read is undefined. +UPB_API upb_DecodeStatus upb_DecodeLengthPrefixed( + const char* buf, size_t size, upb_Message* msg, size_t* num_bytes_read, + const upb_MiniTable* mt, const upb_ExtensionRegistry* extreg, int options, + upb_Arena* arena); + +// For testing: decode with tracing. +UPB_API upb_DecodeStatus upb_DecodeWithTrace( + const char* buf, size_t size, upb_Message* msg, const upb_MiniTable* mt, + const upb_ExtensionRegistry* extreg, int options, upb_Arena* arena, + char* trace_buf, size_t trace_size); + +// Utility function for wrapper languages to get an error string from a +// upb_DecodeStatus. +UPB_API const char* upb_DecodeStatus_String(upb_DecodeStatus status); +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_WIRE_DECODE_H_ */ + +// upb_Encode: parsing from a upb_Message using a upb_MiniTable. + +#ifndef UPB_WIRE_ENCODE_H_ +#define UPB_WIRE_ENCODE_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + /* If set, the results of serializing will be deterministic across all + * instances of this binary. There are no guarantees across different + * binary builds. + * + * If your proto contains maps, the encoder will need to malloc()/free() + * memory during encode. */ + kUpb_EncodeOption_Deterministic = 1, + + // When set, unknown fields are not encoded. + kUpb_EncodeOption_SkipUnknown = 2, + + // When set, the encode will fail if any required fields are missing. + kUpb_EncodeOption_CheckRequired = 4, +}; + +// LINT.IfChange +typedef enum { + kUpb_EncodeStatus_Ok = 0, + kUpb_EncodeStatus_OutOfMemory = 1, // Arena alloc failed + kUpb_EncodeStatus_MaxDepthExceeded = 2, + + // One or more required fields are missing. Only returned if + // kUpb_EncodeOption_CheckRequired is set. + kUpb_EncodeStatus_MissingRequired = 3, + + // The message is larger than protobuf's 2GB size limit. + kUpb_EncodeStatus_MaxSizeExceeded = 4, +} upb_EncodeStatus; +// LINT.ThenChange(//depot/google3/third_party/upb/rust/sys/wire/wire.rs:encode_status) + +UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth) { + return (uint32_t)depth << 16; +} + +uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options); + +// Enforce an upper bound on recursion depth. +UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) { + uint32_t max_depth = upb_EncodeOptions_GetEffectiveMaxDepth(encode_options); + if (max_depth > limit) max_depth = limit; + return (int)(upb_EncodeOptions_MaxDepth(max_depth) | + (encode_options & 0xffff)); +} + +UPB_API upb_EncodeStatus upb_Encode(const upb_Message* msg, + const upb_MiniTable* l, int options, + upb_Arena* arena, char** buf, size_t* size); + +// Encodes the message prepended by a varint of the serialized length. +UPB_API upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg, + const upb_MiniTable* l, + int options, upb_Arena* arena, + char** buf, size_t* size); +// Utility function for wrapper languages to get an error string from a +// upb_EncodeStatus. +UPB_API const char* upb_EncodeStatus_String(upb_EncodeStatus status); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_WIRE_ENCODE_H_ */ +#ifdef UPB_INCLUDE_FAST_DECODE +#endif +// IWYU pragma: end_exports + +#undef UPB_INCLUDE_FAST_DECODE + +#endif // UPB_GENERATED_CODE_SUPPORT_H_ +/* This file was generated by upb_generator from the input file: + * + * google/protobuf/descriptor.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_MINITABLE_H_ +#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_MINITABLE_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +extern const upb_MiniTable google__protobuf__FileDescriptorSet_msg_init; +extern const upb_MiniTable google__protobuf__FileDescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__DescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__DescriptorProto__ExtensionRange_msg_init; +extern const upb_MiniTable google__protobuf__DescriptorProto__ReservedRange_msg_init; +extern const upb_MiniTable google__protobuf__ExtensionRangeOptions_msg_init; +extern const upb_MiniTable google__protobuf__ExtensionRangeOptions__Declaration_msg_init; +extern const upb_MiniTable google__protobuf__FieldDescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__OneofDescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__EnumDescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init; +extern const upb_MiniTable google__protobuf__EnumValueDescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__ServiceDescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__MethodDescriptorProto_msg_init; +extern const upb_MiniTable google__protobuf__FileOptions_msg_init; +extern const upb_MiniTable google__protobuf__MessageOptions_msg_init; +extern const upb_MiniTable google__protobuf__FieldOptions_msg_init; +extern const upb_MiniTable google__protobuf__FieldOptions__EditionDefault_msg_init; +extern const upb_MiniTable google__protobuf__FieldOptions__FeatureSupport_msg_init; +extern const upb_MiniTable google__protobuf__OneofOptions_msg_init; +extern const upb_MiniTable google__protobuf__EnumOptions_msg_init; +extern const upb_MiniTable google__protobuf__EnumValueOptions_msg_init; +extern const upb_MiniTable google__protobuf__ServiceOptions_msg_init; +extern const upb_MiniTable google__protobuf__MethodOptions_msg_init; +extern const upb_MiniTable google__protobuf__UninterpretedOption_msg_init; +extern const upb_MiniTable google__protobuf__UninterpretedOption__NamePart_msg_init; +extern const upb_MiniTable google__protobuf__FeatureSet_msg_init; +extern const upb_MiniTable google__protobuf__FeatureSet__VisibilityFeature_msg_init; +extern const upb_MiniTable google__protobuf__FeatureSetDefaults_msg_init; +extern const upb_MiniTable google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init; +extern const upb_MiniTable google__protobuf__SourceCodeInfo_msg_init; +extern const upb_MiniTable google__protobuf__SourceCodeInfo__Location_msg_init; +extern const upb_MiniTable google__protobuf__GeneratedCodeInfo_msg_init; +extern const upb_MiniTable google__protobuf__GeneratedCodeInfo__Annotation_msg_init; + +extern const upb_MiniTableEnum google__protobuf__Edition_enum_init; +extern const upb_MiniTableEnum google__protobuf__ExtensionRangeOptions__VerificationState_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__EnforceNamingStyle_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__EnumType_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__FieldPresence_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__JsonFormat_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__MessageEncoding_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__Utf8Validation_enum_init; +extern const upb_MiniTableEnum google__protobuf__FeatureSet__VisibilityFeature__DefaultSymbolVisibility_enum_init; +extern const upb_MiniTableEnum google__protobuf__FieldDescriptorProto__Label_enum_init; +extern const upb_MiniTableEnum google__protobuf__FieldDescriptorProto__Type_enum_init; +extern const upb_MiniTableEnum google__protobuf__FieldOptions__CType_enum_init; +extern const upb_MiniTableEnum google__protobuf__FieldOptions__JSType_enum_init; +extern const upb_MiniTableEnum google__protobuf__FieldOptions__OptionRetention_enum_init; +extern const upb_MiniTableEnum google__protobuf__FieldOptions__OptionTargetType_enum_init; +extern const upb_MiniTableEnum google__protobuf__FileOptions__OptimizeMode_enum_init; +extern const upb_MiniTableEnum google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init; +extern const upb_MiniTableEnum google__protobuf__MethodOptions__IdempotencyLevel_enum_init; +extern const upb_MiniTableEnum google__protobuf__SymbolVisibility_enum_init; +extern const upb_MiniTableFile google_protobuf_descriptor_proto_upb_file_layout; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_MINITABLE_H_ */ + +#ifndef UPB_BASE_INTERNAL_LOG2_H_ +#define UPB_BASE_INTERNAL_LOG2_H_ + +#include +#include +#include + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE int upb_Log2Ceiling(size_t x) { + if (x <= 1) return 0; +#if SIZE_MAX == ULLONG_MAX && UPB_HAS_BUILTIN(__builtin_clzll) + return (sizeof(size_t) * CHAR_BIT) - __builtin_clzll(x - 1); +#elif SIZE_MAX == ULONG_MAX && UPB_HAS_BUILTIN(__builtin_clzl) + return (sizeof(size_t) * CHAR_BIT) - __builtin_clzl(x - 1); +#elif SIZE_MAX == UINT_MAX && UPB_HAS_BUILTIN(__builtin_clz) + return (sizeof(size_t) * CHAR_BIT) - __builtin_clz(x - 1); +#else + if (x > SIZE_MAX / 2) return sizeof(size_t) * CHAR_BIT; + int lg2 = 0; + while ((1 << lg2) < x) lg2++; + return lg2; +#endif +} + +// Returns the smallest power of two that is greater than or equal to x. Returns +// SIZE_MAX if the computation would overflow. +UPB_INLINE size_t upb_RoundUpToPowerOfTwo(size_t x) { + int lg2 = upb_Log2Ceiling(x); + UPB_ASSERT(lg2 >= 0 && lg2 <= (int)sizeof(size_t) * CHAR_BIT); + if (lg2 == sizeof(size_t) * CHAR_BIT) { + return SIZE_MAX; + } + return ((size_t)1) << lg2; +} + +UPB_INLINE bool upb_ShlOverflow(size_t* a, unsigned int b) { + if (*a > (SIZE_MAX >> b)) { + return true; + } + *a <<= b; + return false; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_BASE_INTERNAL_LOG2_H_ */ + +#ifndef UPB_HASH_EXT_TABLE_H_ +#define UPB_HASH_EXT_TABLE_H_ + +#include +#include + + +// Must be last. + +typedef struct { + upb_table t; +} upb_exttable; + +#ifdef __cplusplus +extern "C" { +#endif + +// Initialize a table. If memory allocation failed, false is returned and +// the table is uninitialized. +bool upb_exttable_init(upb_exttable* table, size_t expected_size, upb_Arena* a); + +// Returns the number of values in the table. +UPB_INLINE size_t upb_exttable_count(const upb_exttable* t) { + return t->t.count; +} + +void upb_exttable_clear(upb_exttable* t); + +// Inserts the given key and value into the hashtable. +// The key must not already exist in the hash table, and must not be NULL. +// +// If a table resize was required but memory allocation failed, false is +// returned and the table is unchanged. +bool upb_exttable_insert(upb_exttable* t, const void* k, const uint32_t* v, + upb_Arena* a); + +// Looks up key and ext_number in this table, returning the value if the key was +// found, or NULL otherwise. +const uint32_t* upb_exttable_lookup(const upb_exttable* t, const void* k, + uint32_t ext_number); + +// Removes an item from the table. Returns the removed item if the remove was +// successful, or NULL if the key was not found. +const uint32_t* upb_exttable_remove(upb_exttable* t, const void* k, + uint32_t ext_number); + +size_t upb_exttable_size(const upb_exttable* t); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_HASH_EXT_TABLE_H_ */ + +#ifndef UPB_LEX_ATOI_H_ +#define UPB_LEX_ATOI_H_ + +#include + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// We use these hand-written routines instead of strto[u]l() because the "long +// long" variants aren't in c89. Also our version allows setting a ptr limit. +// Return the new position of the pointer after parsing the int, or NULL on +// integer overflow. + +const char* upb_BufToUint64(const char* ptr, const char* end, uint64_t* val); +const char* upb_BufToInt64(const char* ptr, const char* end, int64_t* val, + bool* is_neg); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_LEX_ATOI_H_ */ + +#ifndef UPB_LEX_ROUND_TRIP_H_ +#define UPB_LEX_ROUND_TRIP_H_ + +// Must be last. + +// Encodes a float or double that is round-trippable, but as short as possible. +// These routines are not fully optimal (not guaranteed to be shortest), but are +// short-ish and match the implementation that has been used in protobuf since +// the beginning. + +// The given buffer size must be at least kUpb_RoundTripBufferSize. +enum { kUpb_RoundTripBufferSize = 32 }; + +#ifdef __cplusplus +extern "C" { +#endif + +void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size); +void _upb_EncodeRoundTripFloat(float val, char* buf, size_t size); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_LEX_ROUND_TRIP_H_ */ + +#ifndef UPB_LEX_STRTOD_H_ +#define UPB_LEX_STRTOD_H_ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +double _upb_NoLocaleStrtod(const char *str, char **endptr); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_LEX_STRTOD_H_ */ + +#ifndef UPB_LEX_UNICODE_H_ +#define UPB_LEX_UNICODE_H_ + +#include + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Returns true iff a codepoint is the value for a high surrogate. +UPB_INLINE bool upb_Unicode_IsHigh(uint32_t cp) { + return (cp >= 0xd800 && cp <= 0xdbff); +} + +// Returns true iff a codepoint is the value for a low surrogate. +UPB_INLINE bool upb_Unicode_IsLow(uint32_t cp) { + return (cp >= 0xdc00 && cp <= 0xdfff); +} + +// Returns the high 16-bit surrogate value for a supplementary codepoint. +// Does not sanity-check the input. +UPB_INLINE uint16_t upb_Unicode_ToHigh(uint32_t cp) { + return (cp >> 10) + 0xd7c0; +} + +// Returns the low 16-bit surrogate value for a supplementary codepoint. +// Does not sanity-check the input. +UPB_INLINE uint16_t upb_Unicode_ToLow(uint32_t cp) { + return (cp & 0x3ff) | 0xdc00; +} + +// Returns the 32-bit value corresponding to a pair of 16-bit surrogates. +// Does not sanity-check the input. +UPB_INLINE uint32_t upb_Unicode_FromPair(uint32_t high, uint32_t low) { + return ((high & 0x3ff) << 10) + (low & 0x3ff) + 0x10000; +} + +// Outputs a codepoint as UTF8. +// Returns the number of bytes written (1-4 on success, 0 on error). +// Does not sanity-check the input. Specifically does not check for surrogates. +int upb_Unicode_ToUTF8(uint32_t cp, char* out); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_LEX_UNICODE_H_ */ + +#ifndef UPB_PORT_ATOMIC_H_ +#define UPB_PORT_ATOMIC_H_ + + +#ifdef UPB_USE_C11_ATOMICS + +// IWYU pragma: begin_exports +#include +#include +// IWYU pragma: end_exports + +#define upb_Atomic_Init(addr, val) atomic_init(addr, val) +#define upb_Atomic_Load(addr, order) atomic_load_explicit(addr, order) +#define upb_Atomic_Store(addr, val, order) \ + atomic_store_explicit(addr, val, order) +#define upb_Atomic_Exchange(addr, val, order) \ + atomic_exchange_explicit(addr, val, order) +#define upb_Atomic_CompareExchangeStrong(addr, expected, desired, \ + success_order, failure_order) \ + atomic_compare_exchange_strong_explicit(addr, expected, desired, \ + success_order, failure_order) +#define upb_Atomic_CompareExchangeWeak(addr, expected, desired, success_order, \ + failure_order) \ + atomic_compare_exchange_weak_explicit(addr, expected, desired, \ + success_order, failure_order) +#define upb_Atomic_Add(addr, val, order) \ + atomic_fetch_add_explicit(addr, val, order) +#define upb_Atomic_Sub(addr, val, order) \ + atomic_fetch_sub_explicit(addr, val, order) + +#elif defined(UPB_USE_MSC_ATOMICS) +#include +#include +#include + +#define upb_Atomic_Init(addr, val) (*(addr) = val) + +#pragma intrinsic(_InterlockedExchange) +static int32_t upb_Atomic_LoadMsc32(int32_t volatile* addr) { + // Compare exchange with an unlikely value reduces the risk of a spurious + // (but harmless) store + return _InterlockedCompareExchange(addr, 0xDEADC0DE, 0xDEADC0DE); +} + +#pragma intrinsic(_InterlockedCompareExchange) +static bool upb_Atomic_CompareExchangeMscP32(int32_t volatile* addr, + int32_t* expected, + int32_t desired) { + int32_t expect_val = *expected; + int32_t actual_val = _InterlockedCompareExchange(addr, desired, expect_val); + if (expect_val != actual_val) { + *expected = actual_val; + return false; + } + return true; +} + +#if defined(_WIN64) +// MSVC, without C11 atomics, does not have any way in pure C to force +// load-acquire store-release behavior, so we hack it with exchanges. +#pragma intrinsic(_InterlockedCompareExchange64) +static uintptr_t upb_Atomic_LoadMsc64(uint64_t volatile* addr) { + // Compare exchange with an unlikely value reduces the risk of a spurious + // (but harmless) store + return _InterlockedCompareExchange64(addr, 0xDEADC0DEBAADF00D, + 0xDEADC0DEBAADF00D); +} + +#pragma intrinsic(_InterlockedCompareExchange64) +static bool upb_Atomic_CompareExchangeMscP64(uint64_t volatile* addr, + uint64_t* expected, + uint64_t desired) { + uint64_t expect_val = *expected; + uint64_t actual_val = + _InterlockedCompareExchange64(addr, desired, expect_val); + if (expect_val != actual_val) { + *expected = actual_val; + return false; + } + return true; +} + +#pragma intrinsic(_InterlockedExchange64) +// If _Generic is available, use it to avoid emitting a "'uintptr_t' differs in +// levels of indirection from 'void *'" or -Wint-conversion compiler warning. +#if __STDC_VERSION__ >= 201112L +#define upb_Atomic_Store(addr, val, order) \ + _Generic(addr, \ + UPB_ATOMIC(uintptr_t)*: (void)_InterlockedExchange64( \ + (uint64_t volatile*)(addr), (uint64_t)val), \ + UPB_ATOMIC(int32_t)*: (void)_InterlockedExchange( \ + (int32_t volatile*)(addr), (int32_t)val), \ + default: (void)_InterlockedExchange64((uint64_t volatile*)addr, \ + (uint64_t)val)) + +#define upb_Atomic_Load(addr, order) \ + _Generic(addr, \ + UPB_ATOMIC(uintptr_t)*: upb_Atomic_LoadMsc64( \ + (uint64_t volatile*)(addr)), \ + UPB_ATOMIC(int32_t)*: upb_Atomic_LoadMsc32((int32_t volatile*)(addr)), \ + default: (void*)upb_Atomic_LoadMsc64((uint64_t volatile*)(addr))) + +#define upb_Atomic_Exchange(addr, val, order) \ + _Generic(addr, \ + UPB_ATOMIC(uintptr_t)*: _InterlockedExchange64( \ + (uint64_t volatile*)(addr), (uint64_t)val), \ + UPB_ATOMIC(int32_t)*: _InterlockedExchange((int32_t volatile*)(addr), \ + (int32_t)val), \ + default: (void*)_InterlockedExchange64((uint64_t volatile*)addr, \ + (uint64_t)val)) + +#define upb_Atomic_CompareExchangeStrong(addr, expected, desired, \ + success_order, failure_order) \ + _Generic(addr, \ + UPB_ATOMIC(int32_t)*: upb_Atomic_CompareExchangeMscP32( \ + (int32_t volatile*)(addr), (int32_t*)expected, \ + (int32_t)desired), \ + default: upb_Atomic_CompareExchangeMscP64((uint64_t volatile*)(addr), \ + (uint64_t*)expected, \ + (uint64_t)desired)) + +#define upb_Atomic_CompareExchangeWeak(addr, expected, desired, success_order, \ + failure_order) \ + _Generic(addr, \ + UPB_ATOMIC(int32_t)*: upb_Atomic_CompareExchangeMscP32( \ + (int32_t volatile*)(addr), (int32_t*)expected, \ + (int32_t)desired), \ + default: upb_Atomic_CompareExchangeMscP64((uint64_t volatile*)(addr), \ + (uint64_t*)expected, \ + (uint64_t)desired)) + +#else + +UPB_INLINE void _upb_Atomic_StoreP(void volatile* addr, uint64_t val, + size_t size) { + if (size == sizeof(int32_t)) { + (void)_InterlockedExchange((int32_t volatile*)addr, (int32_t)val); + } else { + (void)_InterlockedExchange64((uint64_t volatile*)addr, val); + } +} + +#define upb_Atomic_Store(addr, val, order) \ + _upb_Atomic_StoreP(addr, val, sizeof(*addr)) + +UPB_INLINE int64_t _upb_Atomic_LoadP(void volatile* addr, size_t size) { + if (size == sizeof(int32_t)) { + return (int64_t)upb_Atomic_LoadMsc32((int32_t volatile*)addr); + } else { + return upb_Atomic_LoadMsc64((uint64_t volatile*)addr); + } +} + +#define upb_Atomic_Load(addr, order) \ + (void*)_upb_Atomic_LoadP((void volatile*)addr, sizeof(*addr)) + +UPB_INLINE int64_t _upb_Atomic_ExchangeP(void volatile* addr, uint64_t val, + size_t size) { + if (size == sizeof(int32_t)) { + return (int64_t)_InterlockedExchange((int32_t volatile*)addr, (int32_t)val); + } else { + return (int64_t)_InterlockedExchange64((uint64_t volatile*)addr, val); + } +} + +#define upb_Atomic_Exchange(addr, val, order) \ + (void*)_upb_Atomic_ExchangeP((void volatile*)addr, (uint64_t)val, \ + sizeof(*addr)) + +UPB_INLINE bool _upb_Atomic_CompareExchangeMscP(void volatile* addr, + void* expected, + uint64_t desired, size_t size) { + if (size == sizeof(int32_t)) { + return upb_Atomic_CompareExchangeMscP32( + (int32_t volatile*)addr, (int32_t*)expected, (int32_t)desired); + } else { + return upb_Atomic_CompareExchangeMscP64((uint64_t volatile*)addr, + (uint64_t*)expected, desired); + } +} + +#define upb_Atomic_CompareExchangeStrong(addr, expected, desired, \ + success_order, failure_order) \ + _upb_Atomic_CompareExchangeMscP(addr, expected, (uint64_t)desired, \ + sizeof(*addr)) + +#define upb_Atomic_CompareExchangeWeak(addr, expected, desired, success_order, \ + failure_order) \ + _upb_Atomic_CompareExchangeMscP(addr, expected, (uint64_t)desired, \ + sizeof(*addr)) + +#endif + +#else // 32 bit pointers +#pragma intrinsic(_InterlockedExchange) +#define upb_Atomic_Store(addr, val, order) \ + (void)_InterlockedExchange((uint32_t volatile*)addr, (uint32_t)val) + +// If _Generic is available, use it to avoid emitting 'uintptr_t' differs in +// levels of indirection from 'void *' +#if __STDC_VERSION__ >= 201112L +#define upb_Atomic_Load(addr, order) \ + _Generic(addr, \ + UPB_ATOMIC(uintptr_t)*: (uintptr_t)upb_Atomic_LoadMsc32( \ + (uint32_t volatile*)(addr)), \ + UPB_ATOMIC(int32_t)*: upb_Atomic_LoadMsc32((int32_t volatile*)(addr)), \ + default: (void*)upb_Atomic_LoadMsc32((uint32_t volatile*)(addr))) + +#define upb_Atomic_Exchange(addr, val, order) \ + _Generic(addr, \ + UPB_ATOMIC(uintptr_t)*: _InterlockedExchange((uint32_t volatile*)(addr), \ + (uint32_t)val), \ + default: (void*)_InterlockedExchange((uint32_t volatile*)addr, \ + (uint32_t)val)) +#else +#define upb_Atomic_Load(addr, order) \ + (void*)upb_Atomic_LoadMsc32((uint32_t volatile*)(addr)) + +#define upb_Atomic_Exchange(addr, val, order) \ + (void*)_InterlockedExchange((uint32_t volatile*)addr, (uint32_t)val) +#endif + +#define upb_Atomic_CompareExchangeStrong(addr, expected, desired, \ + success_order, failure_order) \ + upb_Atomic_CompareExchangeMscP32((uint32_t volatile*)addr, \ + (uint32_t*)expected, (uint32_t)desired) + +#define upb_Atomic_CompareExchangeWeak(addr, expected, desired, success_order, \ + failure_order) \ + upb_Atomic_CompareExchangeMscP32((uint32_t volatile*)addr, \ + (uint32_t*)expected, (uint32_t)desired) +#endif + +#pragma intrinsic(_InterlockedExchangeAdd) +#pragma intrinsic(_InterlockedExchangeAdd64) + +// If _Generic is available, use it to switch between 32 and 64 bit types. +#if __STDC_VERSION__ >= 201112L +#define upb_Atomic_Add(addr, val, order) \ + _Generic(addr, \ + UPB_ATOMIC(int64_t)*: _InterlockedExchangeAdd64(addr, (int64_t)val), \ + UPB_ATOMIC(int32_t)*: _InterlockedExchangeAdd(addr, (int32_t)val)) +#define upb_Atomic_Sub(addr, val, order) \ + _Generic(addr, \ + UPB_ATOMIC(int64_t)*: _InterlockedExchangeAdd64(addr, -(int64_t)val), \ + UPB_ATOMIC(int32_t)*: _InterlockedExchangeAdd(addr, -(int32_t)val)) +#else +#define upb_Atomic_Add(addr, val, order) \ + sizeof(*addr) == sizeof(int32_t) \ + ? _InterlockedExchangeAdd((uint32_t volatile*)addr, (int32_t)val) \ + : _InterlockedExchangeAdd64((uint64_t volatile*)addr, (int64_t)val) +#define upb_Atomic_Sub(addr, val, order) \ + sizeof(*addr) == sizeof(int32_t) \ + ? _InterlockedExchangeAdd((uint32_t volatile*)addr, -(int32_t)val) \ + : _InterlockedExchangeAdd64((uint64_t volatile*)addr, -(int64_t)val) +#endif + +#else // No atomics + +#if !defined(UPB_SUPPRESS_MISSING_ATOMICS) +// NOLINTNEXTLINE +#error Your compiler does not support atomic instructions, which UPB uses. If you do not use UPB on multiple threads, you can suppress this error by defining UPB_SUPPRESS_MISSING_ATOMICS. +#endif + +#include + +#define upb_Atomic_Init(addr, val) (*addr = val) +#define upb_Atomic_Load(addr, order) (*addr) +#define upb_Atomic_Store(addr, val, order) (*(addr) = val) + +UPB_INLINE void* _upb_NonAtomic_Exchange(void* addr, void* value) { + void* old; + memcpy(&old, addr, sizeof(value)); + memcpy(addr, &value, sizeof(value)); + return old; +} + +#define upb_Atomic_Exchange(addr, val, order) _upb_NonAtomic_Exchange(addr, val) + +// `addr` and `expected` are logically double pointers. +UPB_INLINE bool _upb_NonAtomic_CompareExchangeStrongP(void* addr, + void* expected, + void* desired) { + if (memcmp(addr, expected, sizeof(desired)) == 0) { + memcpy(addr, &desired, sizeof(desired)); + return true; + } else { + memcpy(expected, addr, sizeof(desired)); + return false; + } +} + +#define upb_Atomic_CompareExchangeStrong(addr, expected, desired, \ + success_order, failure_order) \ + _upb_NonAtomic_CompareExchangeStrongP((void*)addr, (void*)expected, \ + (void*)desired) +#define upb_Atomic_CompareExchangeWeak(addr, expected, desired, success_order, \ + failure_order) \ + upb_Atomic_CompareExchangeStrong(addr, expected, desired, 0, 0) + +#define upb_Atomic_Add(addr, val, order) (*addr += val) +#define upb_Atomic_Sub(addr, val, order) (*addr -= val) + +#endif + + +#endif // UPB_PORT_ATOMIC_H_ + +#ifndef UPB_MESSAGE_COMPAT_H_ +#define UPB_MESSAGE_COMPAT_H_ + +#include + + +// Must be last. + +// upb does not support mixing minitables from different sources but these +// functions are still used by some existing users so for now we make them +// available here. This may or may not change in the future so do not add +// them to new code. + +#ifdef __cplusplus +extern "C" { +#endif + +// Same as upb_Message_NextExtension but iterates in reverse wire order +bool upb_Message_NextExtensionReverse(const upb_Message* msg, + const upb_MiniTableExtension** result, + uintptr_t* iter); +// Returns the minitable with the given field number, or NULL on failure. +const upb_MiniTableExtension* upb_Message_FindExtensionByNumber( + const upb_Message* msg, uint32_t field_number); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_COMPAT_H_ */ + +// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE ///////////////////////// + +#ifndef UPB_MESSAGE_INTERNAL_MAP_SORTER_H_ +#define UPB_MESSAGE_INTERNAL_MAP_SORTER_H_ + +#include +#include +#include + + +#ifndef UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_ +#define UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_ + +#include + + +// Map entries aren't actually stored for map fields, they are only used during +// parsing. (It helps a lot if all map entry messages have the same layout.) +// The mini_table layout code will ensure that all map entries have this layout. +// +// Note that users can and do create map entries directly, which will also use +// this layout. + +typedef struct { + struct upb_Message message; + // We only need 2 hasbits max, but due to alignment we'll use 8 bytes here, + // and the uint64_t helps make this clear. + uint64_t hasbits; + union { + upb_StringView str; // For str/bytes. + upb_value val; // For all other types. + double d[2]; // Padding for 32-bit builds. + } k; + union { + upb_StringView str; // For str/bytes. + upb_value val; // For all other types. + double d[2]; // Padding for 32-bit builds. + } v; +} upb_MapEntry; + +#endif // UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// _upb_mapsorter sorts maps and provides ordered iteration over the entries. +// Since maps can be recursive (map values can be messages which contain other +// maps), _upb_mapsorter can contain a stack of maps. + +typedef struct { + void const** entries; + int size; + int cap; +} _upb_mapsorter; + +typedef struct { + int start; + int pos; + int end; +} _upb_sortedmap; + +UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter* s) { + s->entries = NULL; + s->size = 0; + s->cap = 0; +} + +UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter* s) { + if (s->entries) upb_gfree(s->entries); +} + +UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, + const struct upb_Map* map, + _upb_sortedmap* sorted, upb_MapEntry* ent) { + if (sorted->pos == sorted->end) return false; + const upb_tabent* tabent = (const upb_tabent*)s->entries[sorted->pos++]; + if (map->UPB_PRIVATE(is_strtable)) { + upb_StringView key = upb_key_strview(tabent->key); + _upb_map_fromkey(key, &ent->k, map->key_size); + } else { + uintptr_t key = tabent->key.num; + memcpy(&ent->k, &key, map->key_size); + } + upb_value val = {tabent->val.val}; + _upb_map_fromvalue(val, &ent->v, map->val_size); + return true; +} + +UPB_INLINE bool _upb_sortedmap_nextext(_upb_mapsorter* s, + _upb_sortedmap* sorted, + const upb_Extension** ext) { + if (sorted->pos == sorted->end) return false; + *ext = (const upb_Extension*)s->entries[sorted->pos++]; + return true; +} + +UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter* s, + _upb_sortedmap* sorted) { + s->size = sorted->start; +} + +bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type, + const struct upb_Map* map, _upb_sortedmap* sorted); + +bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Message_Internal* in, + _upb_sortedmap* sorted); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_INTERNAL_MAP_SORTER_H_ */ + +#ifndef UPB_MESSAGE_COMPARE_H_ +#define UPB_MESSAGE_COMPARE_H_ + +#include + + +// Must be last. + +enum { + // If set, upb_Message_IsEqual() will attempt to compare unknown fields. + // By its very nature this comparison is inexact. + kUpb_CompareOption_IncludeUnknownFields = (1 << 0) +}; + +#ifdef __cplusplus +extern "C" { +#endif + +// Returns true if no known fields or extensions are set in the message. +UPB_API bool upb_Message_IsEmpty(const upb_Message* msg, + const upb_MiniTable* m); + +UPB_API bool upb_Message_IsEqual(const upb_Message* msg1, + const upb_Message* msg2, + const upb_MiniTable* m, int options); + +// If |ctype| is a message then |m| must point to its minitable. +UPB_API_INLINE bool upb_MessageValue_IsEqual(upb_MessageValue val1, + upb_MessageValue val2, + upb_CType ctype, + const upb_MiniTable* m, + int options) { + switch (ctype) { + case kUpb_CType_Bool: + return val1.bool_val == val2.bool_val; + + case kUpb_CType_Float: + case kUpb_CType_Int32: + case kUpb_CType_UInt32: + case kUpb_CType_Enum: + return val1.int32_val == val2.int32_val; + + case kUpb_CType_Double: + case kUpb_CType_Int64: + case kUpb_CType_UInt64: + return val1.int64_val == val2.int64_val; + + case kUpb_CType_String: + case kUpb_CType_Bytes: + return upb_StringView_IsEqual(val1.str_val, val2.str_val); + + case kUpb_CType_Message: + return upb_Message_IsEqual(val1.msg_val, val2.msg_val, m, options); + + default: + UPB_UNREACHABLE(); + return false; + } +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_MESSAGE_COMPARE_H_ + +#ifndef UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_ +#define UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_ + +#include + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Returns true if unknown fields from the two messages are equal when sorted +// and varints are made canonical. +// +// This function is discouraged, as the comparison is inherently lossy without +// schema data: +// +// 1. We don't know whether delimited fields are sub-messages. Unknown +// sub-messages will therefore not have their fields sorted and varints +// canonicalized. +// 2. We don't know about oneof/non-repeated fields, which should semantically +// discard every value except the last. + +typedef enum { + kUpb_UnknownCompareResult_Equal = 0, + kUpb_UnknownCompareResult_NotEqual = 1, + kUpb_UnknownCompareResult_OutOfMemory = 2, + kUpb_UnknownCompareResult_MaxDepthExceeded = 3, +} upb_UnknownCompareResult; + +upb_UnknownCompareResult UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)( + const upb_Message* msg1, const upb_Message* msg2, int max_depth); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_ */ + +#ifndef GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__ +#define GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__ + +#include +#include + + +// Must be last. + +#define kUpb_BaseField_Begin ((size_t)-1) +bool UPB_PRIVATE(_upb_Message_NextBaseField)(const upb_Message* msg, + const upb_MiniTable* m, + const upb_MiniTableField** out_f, + upb_MessageValue* out_v, + uintptr_t* iter); + +#endif // GOOGLE_UPB_UPB_MESSAGE_INTERNAL_ITERATOR_H__ + +#ifndef UPB_WIRE_EPS_COPY_INPUT_STREAM_H_ +#define UPB_WIRE_EPS_COPY_INPUT_STREAM_H_ + +#include +#include + + +#ifndef GOOGLE_UPB_UPB_BASE_ERROR_HANDLER_H__ +#define GOOGLE_UPB_UPB_BASE_ERROR_HANDLER_H__ + +#include + +// Must be last. + +// upb_ErrorHandler is a standard longjmp()-based exception handler for UPB. +// It is used for efficient error handling in cases where longjmp() is safe to +// use, such as in highly performance-sensitive C parsing code. +// +// This structure contains both a jmp_buf and an error code; the error code is +// stored in the structure prior to calling longjmp(). This is necessary because +// per the C standard, it is not possible to store the result of setjmp(), so +// the error code must be passed out-of-band. +// +// upb_ErrorHandler is generally not C++-compatible, because longjmp() does not +// run C++ destructors. So any library that supports upb_ErrorHandler should +// also support a regular return-based error handling mechanism. (Note: we +// could conceivably extend this to take a callback, which could either call +// longjmp() or throw a C++ exception. But since C++ exceptions are forbidden +// by the C++ style guide, there's not likely to be a demand for this.) +// +// To support both cases (longjmp() or return-based status) efficiently, code +// can be written like this: +// +// UPB_ATTR_CONST bool upb_Arena_HasErrHandler(const upb_Arena* a); +// +// INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) { +// if (UPB_UNLIKELY(a->end - a->ptr < size)) { +// void* ret = upb_Arena_MallocFallback(a, size); +// UPB_MAYBE_ASSUME(upb_Arena_HasErrHandler(a), ret != NULL); +// return ret; +// } +// void* ret = a->ptr; +// a->ptr += size; +// UPB_ASSUME(ret != NULL); +// return ret; +// } +// +// If the optimizer can prove that an error handler is present, it can assume +// that upb_Arena_Malloc() will not return NULL. + +// We need to standardize on any error code that might be thrown by an error +// handler. + +typedef enum { + kUpb_ErrorCode_Ok = 0, + kUpb_ErrorCode_OutOfMemory = 1, + kUpb_ErrorCode_Malformed = 2, +} upb_ErrorCode; + +typedef struct { + int code; + jmp_buf buf; +} upb_ErrorHandler; + +UPB_INLINE void upb_ErrorHandler_Init(upb_ErrorHandler* e) { + e->code = kUpb_ErrorCode_Ok; +} + +UPB_INLINE UPB_NORETURN void upb_ErrorHandler_ThrowError(upb_ErrorHandler* e, + int code) { + UPB_ASSERT(code != kUpb_ErrorCode_Ok); + e->code = code; + UPB_LONGJMP(e->buf, 1); +} + + +#endif // GOOGLE_UPB_UPB_BASE_ERROR_HANDLER_H__ + +#ifndef UPB_WIRE_INTERNAL_EPS_COPY_INPUT_STREAM_H_ +#define UPB_WIRE_INTERNAL_EPS_COPY_INPUT_STREAM_H_ + +#include +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// The maximum number of bytes a single protobuf field can take up in the +// wire format. We only want to do one bounds check per field, so the input +// stream guarantees that after upb_EpsCopyInputStream_IsDone() is called, +// the decoder can read this many bytes without performing another bounds +// check. The stream will copy into a patch buffer as necessary to guarantee +// this invariant. Since tags can only be up to 5 bytes, and a max-length scalar +// field can be 10 bytes, only 15 is required; but sizing up to 16 permits more +// efficient fixed size copies. +#define kUpb_EpsCopyInputStream_SlopBytes 16 + +struct upb_EpsCopyInputStream { + const char* end; // Can read up to SlopBytes bytes beyond this. + const char* limit_ptr; // For bounds checks, = end + UPB_MIN(limit, 0) + uintptr_t input_delta; // Diff between the original input pointer and patch + const char* buffer_start; // Pointer to the original input buffer + const char* capture_start; // If non-NULL, the start of the captured region. + ptrdiff_t limit; // Submessage limit relative to end + upb_ErrorHandler* err; // Error handler to use when things go wrong. + bool error; // To distinguish between EOF and error. +#ifndef NDEBUG + int guaranteed_bytes; +#endif + // Allocate double the size of what's required; this permits a fixed-size copy + // from the input buffer, regardless of how many bytes actually remain in the + // input buffer. + char patch[kUpb_EpsCopyInputStream_SlopBytes * 2]; +}; + +UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)( + struct upb_EpsCopyInputStream* e); + +UPB_INLINE bool upb_EpsCopyInputStream_IsError( + struct upb_EpsCopyInputStream* e) { + return e->error; +} + +UPB_INLINE void upb_EpsCopyInputStream_InitWithErrorHandler( + struct upb_EpsCopyInputStream* e, const char** ptr, size_t size, + upb_ErrorHandler* err) { + e->buffer_start = *ptr; + e->capture_start = NULL; + e->err = err; + if (size <= kUpb_EpsCopyInputStream_SlopBytes) { + memset(&e->patch, 0, 32); + if (size) memcpy(&e->patch, *ptr, size); + e->input_delta = (uintptr_t)*ptr - (uintptr_t)e->patch; + *ptr = e->patch; + e->end = *ptr + size; + e->limit = 0; + } else { + e->end = *ptr + size - kUpb_EpsCopyInputStream_SlopBytes; + e->limit = kUpb_EpsCopyInputStream_SlopBytes; + e->input_delta = 0; + } + e->limit_ptr = e->end; + e->error = false; + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e); +} + +UPB_INLINE void upb_EpsCopyInputStream_Init(struct upb_EpsCopyInputStream* e, + const char** ptr, size_t size) { + upb_EpsCopyInputStream_InitWithErrorHandler(e, ptr, size, NULL); +} + +UPB_ATTR_CONST +UPB_INLINE bool upb_EpsCopyInputStream_HasErrorHandler( + const struct upb_EpsCopyInputStream* e) { + return e && e->err != NULL; +} + +// Call this function to signal an error. If an error handler is set, it will be +// called and the function will never return. Otherwise, returns NULL to +// indicate an error. +const char* UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)( + struct upb_EpsCopyInputStream* e); + +UPB_INLINE const char* UPB_PRIVATE(upb_EpsCopyInputStream_AssumeResult)( + struct upb_EpsCopyInputStream* e, const char* ptr) { + UPB_MAYBE_ASSUME(upb_EpsCopyInputStream_HasErrorHandler(e), ptr != NULL); + return ptr; +} + +//////////////////////////////////////////////////////////////////////////////// + +// Debug checks that attempt to ensure that no code paths will overrun the slop +// bytes even in the worst case. Since we are frequently parsing varints, it's +// possible that the user is trying to parse too many varints before calling +// upb_EpsCopyInputStream_IsDone(), but this error case is not detected because +// the varints are short. These checks ensure that will not overrun the slop +// bytes, even if each varint is its maximum possible length. + +UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)( + struct upb_EpsCopyInputStream* e) { +#ifndef NDEBUG + e->guaranteed_bytes = kUpb_EpsCopyInputStream_SlopBytes; +#endif +} + +UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)( + struct upb_EpsCopyInputStream* e) { +#ifndef NDEBUG + e->guaranteed_bytes = 0; +#endif +} + +// Signals the maximum number that the operation about to be performed may +// consume. +UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)( + struct upb_EpsCopyInputStream* e, int n) { +#ifndef NDEBUG + if (e) { + UPB_ASSERT(e->guaranteed_bytes >= n); + e->guaranteed_bytes -= n; + } +#endif +} + +//////////////////////////////////////////////////////////////////////////////// + +typedef enum { + // The current stream position is at a limit. + kUpb_IsDoneStatus_Done, + + // The current stream position is not at a limit. + kUpb_IsDoneStatus_NotDone, + + // The current stream position is not at a limit, and the stream needs to + // be flipped to a new buffer before more data can be read. + kUpb_IsDoneStatus_NeedFallback, +} upb_IsDoneStatus; + +// Returns the status of the current stream position. This is a low-level +// function, it is simpler to call upb_EpsCopyInputStream_IsDone() if possible. +UPB_INLINE upb_IsDoneStatus UPB_PRIVATE(upb_EpsCopyInputStream_IsDoneStatus)( + struct upb_EpsCopyInputStream* e, const char* ptr, int* overrun) { + *overrun = ptr - e->end; + if (UPB_LIKELY(ptr < e->limit_ptr)) { + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e); + return kUpb_IsDoneStatus_NotDone; + } else if (UPB_LIKELY(*overrun == e->limit)) { + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)(e); + return kUpb_IsDoneStatus_Done; + } else { + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)(e); + return kUpb_IsDoneStatus_NeedFallback; + } +} + +const char* UPB_PRIVATE(upb_EpsCopyInputStream_IsDoneFallback)( + struct upb_EpsCopyInputStream* e, const char* ptr, int overrun); + +UPB_INLINE bool upb_EpsCopyInputStream_IsDone(struct upb_EpsCopyInputStream* e, + const char** ptr) { + int overrun; + switch (UPB_PRIVATE(upb_EpsCopyInputStream_IsDoneStatus)(e, *ptr, &overrun)) { + case kUpb_IsDoneStatus_Done: + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)(e); + return true; + case kUpb_IsDoneStatus_NotDone: + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e); + return false; + case kUpb_IsDoneStatus_NeedFallback: + *ptr = + UPB_PRIVATE(upb_EpsCopyInputStream_IsDoneFallback)(e, *ptr, overrun); + if (*ptr) { + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsChecked)(e); + } else { + UPB_PRIVATE(upb_EpsCopyInputStream_BoundsHit)(e); + } + return *ptr == NULL; + } + UPB_UNREACHABLE(); +} + +UPB_INLINE bool upb_EpsCopyInputStream_CheckSize( + const struct upb_EpsCopyInputStream* e, const char* ptr, int size) { + UPB_ASSERT(size >= 0); + return size <= e->limit - (ptr - e->end); +} + +// Returns a pointer into an input buffer that corresponds to the parsing +// pointer `ptr`. The returned pointer may be the same as `ptr`, but also may +// be different if we are currently parsing out of the patch buffer. +UPB_INLINE const char* UPB_PRIVATE(upb_EpsCopyInputStream_GetInputPtr)( + struct upb_EpsCopyInputStream* e, const char* ptr) { + // This somewhat silly looking add-and-subtract behavior provides provenance + // from the original input buffer's pointer. After optimization it produces + // the same assembly as just casting `(uintptr_t)ptr+input_delta` + // https://godbolt.org/z/zosG88oPn + size_t position = + (uintptr_t)ptr + e->input_delta - (uintptr_t)e->buffer_start; + return e->buffer_start + position; +} + +UPB_INLINE void upb_EpsCopyInputStream_StartCapture( + struct upb_EpsCopyInputStream* e, const char* ptr) { + UPB_ASSERT(e->capture_start == NULL); + e->capture_start = UPB_PRIVATE(upb_EpsCopyInputStream_GetInputPtr)(e, ptr); +} + +UPB_INLINE bool upb_EpsCopyInputStream_EndCapture( + struct upb_EpsCopyInputStream* e, const char* ptr, upb_StringView* sv) { + UPB_ASSERT(e->capture_start != NULL); + if (ptr - e->end > e->limit) { + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(e); + } + const char* end = UPB_PRIVATE(upb_EpsCopyInputStream_GetInputPtr)(e, ptr); + sv->data = e->capture_start; + sv->size = end - sv->data; + e->capture_start = NULL; + return true; +} + +UPB_INLINE const char* upb_EpsCopyInputStream_ReadStringAlwaysAlias( + struct upb_EpsCopyInputStream* e, const char* ptr, size_t size, + upb_StringView* sv) { + UPB_ASSERT(size <= PTRDIFF_MAX); + // The `size` must be within the input buffer. If `ptr` is in the input + // buffer, then using the slop bytes is fine (because they are real bytes from + // the tail of the input buffer). If `ptr` is in the patch buffer, then slop + // bytes represent bytes that do not actually exist in the original input + // buffer, so we must fail if the size extends into the slop bytes. + const char* limit = + e->end + (e->input_delta == 0) * kUpb_EpsCopyInputStream_SlopBytes; + if ((ptrdiff_t)size > limit - ptr) { + // For the moment, we consider this an error. In a multi-buffer world, + // it could be that the requested string extends into the next buffer, which + // is not an error and should be recoverable. + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(e); + } + const char* input = UPB_PRIVATE(upb_EpsCopyInputStream_GetInputPtr)(e, ptr); + *sv = upb_StringView_FromDataAndSize(input, size); + return ptr + size; +} + +UPB_INLINE const char* upb_EpsCopyInputStream_ReadStringEphemeral( + struct upb_EpsCopyInputStream* e, const char* ptr, size_t size, + upb_StringView* sv) { + UPB_ASSERT(size <= PTRDIFF_MAX); + // Size must be within the current buffer (including slop bytes). + const char* limit = e->end + kUpb_EpsCopyInputStream_SlopBytes; + if ((ptrdiff_t)size > limit - ptr) { + // For the moment, we consider this an error. In a multi-buffer world, + // it could be that the requested string extends into the next buffer, which + // is not an error and should be recoverable. + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(e); + } + *sv = upb_StringView_FromDataAndSize(ptr, size); + return ptr + size; +} + +UPB_INLINE void UPB_PRIVATE(upb_EpsCopyInputStream_CheckLimit)( + struct upb_EpsCopyInputStream* e) { + UPB_ASSERT(e->limit_ptr == e->end + UPB_MIN(0, e->limit)); +} + +UPB_INLINE ptrdiff_t upb_EpsCopyInputStream_PushLimit( + struct upb_EpsCopyInputStream* e, const char* ptr, size_t size) { + UPB_ASSERT(size <= PTRDIFF_MAX); + ptrdiff_t limit = (ptrdiff_t)size + (ptr - e->end); + ptrdiff_t delta = e->limit - limit; + UPB_PRIVATE(upb_EpsCopyInputStream_CheckLimit)(e); + e->limit = limit; + e->limit_ptr = e->end + UPB_MIN(0, limit); + UPB_PRIVATE(upb_EpsCopyInputStream_CheckLimit)(e); + if (UPB_UNLIKELY(delta < 0)) { + UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(e); + } + return delta; +} + +// Pops the last limit that was pushed on this stream. This may only be called +// once IsDone() returns true. The user must pass the delta that was returned +// from PushLimit(). +UPB_INLINE void upb_EpsCopyInputStream_PopLimit( + struct upb_EpsCopyInputStream* e, const char* ptr, ptrdiff_t saved_delta) { + UPB_ASSERT(ptr - e->end == e->limit); + UPB_PRIVATE(upb_EpsCopyInputStream_CheckLimit)(e); + e->limit += saved_delta; + e->limit_ptr = e->end + UPB_MIN(0, e->limit); + UPB_PRIVATE(upb_EpsCopyInputStream_CheckLimit)(e); +} + +typedef const char* upb_EpsCopyInputStream_ParseDelimitedFunc( + struct upb_EpsCopyInputStream* e, const char* ptr, int size, void* ctx); + +UPB_FORCEINLINE bool upb_EpsCopyInputStream_TryParseDelimitedFast( + struct upb_EpsCopyInputStream* e, const char** ptr, size_t size, + upb_EpsCopyInputStream_ParseDelimitedFunc* func, void* ctx) { + UPB_ASSERT(size <= PTRDIFF_MAX); + if ((ptrdiff_t)size > e->limit_ptr - *ptr) { + return false; + } + + // Fast case: Sub-message is <128 bytes and fits in the current buffer. + // This means we can preserve limit/limit_ptr verbatim. + const char* saved_limit_ptr = e->limit_ptr; + int saved_limit = e->limit; + e->limit_ptr = *ptr + size; + e->limit = e->limit_ptr - e->end; + UPB_ASSERT(e->limit_ptr == e->end + UPB_MIN(0, e->limit)); + *ptr = func(e, *ptr, size, ctx); + e->limit_ptr = saved_limit_ptr; + e->limit = saved_limit; + UPB_ASSERT(e->limit_ptr == e->end + UPB_MIN(0, e->limit)); + return true; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_WIRE_INTERNAL_EPS_COPY_INPUT_STREAM_H_ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct upb_EpsCopyInputStream upb_EpsCopyInputStream; + +// Initializes a upb_EpsCopyInputStream using the contents of the buffer +// [*ptr, size]. Updates `*ptr` as necessary to guarantee that at least +// kUpb_EpsCopyInputStream_SlopBytes are available to read. +UPB_INLINE void upb_EpsCopyInputStream_Init(upb_EpsCopyInputStream* e, + const char** ptr, size_t size); + +// Like the previous function, but registers an error handler that will be +// called for any errors encountered. +UPB_INLINE void upb_EpsCopyInputStream_InitWithErrorHandler( + upb_EpsCopyInputStream* e, const char** ptr, size_t size, + upb_ErrorHandler* err); + +// Returns true if the stream has an error handler. +// +// This function is marked const, which indicates to the compiler that the +// return value is solely a function of the pointer value. This is not +// entirely true if the stream is reinitialized with +// upb_EpsCopyInputStream_Init*(), so users must not call this function in +// any context where the stream may be reinitialized between calls to this +// function, and the presence of an error handler changes when reinitialized. +UPB_ATTR_CONST +UPB_INLINE bool upb_EpsCopyInputStream_HasErrorHandler( + const upb_EpsCopyInputStream* e); + +// Returns true if the stream is in the error state. A stream enters the error +// state when the user reads past a limit (caught in IsDone()) or the +// ZeroCopyInputStream returns an error. +UPB_INLINE bool upb_EpsCopyInputStream_IsError(upb_EpsCopyInputStream* e); + +// Returns true if the stream has hit a limit, either the current delimited +// limit or the overall end-of-stream. As a side effect, this function may flip +// the pointer to a new buffer if there are less than +// kUpb_EpsCopyInputStream_SlopBytes of data to be read in the current buffer. +// +// Postcondition: if the function returns false, there are at least +// kUpb_EpsCopyInputStream_SlopBytes of data available to read at *ptr. +// +// If this returns true, the user must call upb_EpsCopyInputStream_IsError() +// to distinguish between EOF and error. +UPB_INLINE bool upb_EpsCopyInputStream_IsDone(upb_EpsCopyInputStream* e, + const char** ptr); + +// Returns true if the given delimited field size is valid (it does not extend +// beyond any previously-pushed limits). `ptr` should point to the beginning +// of the field data, after the delimited size. +// +// Note that this does *not* guarantee that all of the data for this field is in +// the current buffer. +UPB_INLINE bool upb_EpsCopyInputStream_CheckSize( + const upb_EpsCopyInputStream* e, const char* ptr, int size); + +// Marks the start of a capture operation. Only one capture operation may be +// active at a time. The capture operation will be finalized by a call to +// upb_EpsCopyInputStream_EndCapture(). The captured string will be returned in +// sv, and will point to the original input buffer if possible. +UPB_INLINE void upb_EpsCopyInputStream_StartCapture(upb_EpsCopyInputStream* e, + const char* ptr); + +// Ends a capture operation and returns the captured string. This may only be +// called once per capture operation. Returns false if the capture operation +// was invalid (the parsing pointer extends beyond the end of the stream). +UPB_INLINE bool upb_EpsCopyInputStream_EndCapture(upb_EpsCopyInputStream* e, + const char* ptr, + upb_StringView* sv); + +// Reads a string from the stream and advances the pointer accordingly. The +// returned string view will always alias the input buffer. +// +// Returns NULL if size extends beyond the end of the current input buffer. +// Currently, we only support a single input buffer, so this function will only +// fail if `size` overflows the end of the stream. +// +// If/when we support multiple input buffers, there may be cases where this +// function returns failure, even if the requested region is valid, because the +// requested region spans multiple buffers. In this case, the caller must +// attempt to read the string using other string reading functions before +// signaling an error. +UPB_INLINE const char* upb_EpsCopyInputStream_ReadStringAlwaysAlias( + upb_EpsCopyInputStream* e, const char* ptr, size_t size, + upb_StringView* sv); + +// Reads a string from the stream and advances the pointer accordingly. The +// returned string view is ephemeral, only valid until the next call to +// upb_EpsCopyInputStream. It may point to the patch buffer. +// +// Returns NULL if size extends beyond the end of the current buffer (which may +// be the patch buffer). +// +// IMPORTANT NOTE: If `size` extends beyond the end of the stream, the returned +// data may contain garbage bytes from the patch buffer. For efficiency, this +// function does not check that `size` is within the current limit or even the +// end of the stream. +// +// The bytes are guaranteed to be safe to read ephemerally, but they may contain +// garbage data that does not correspond to anything in the input. This error +// will be detected later, when calling upb_EpsCopyInputStream_IsDone() (because +// we will not end at the proper limit), but it may result in nonsense bytes +// ending up in the output. +UPB_INLINE const char* upb_EpsCopyInputStream_ReadStringEphemeral( + upb_EpsCopyInputStream* e, const char* ptr, size_t size, + upb_StringView* sv); + +// Pushes a limit onto the stack of limits for the current stream. The limit +// will extend for `size` bytes beyond the position in `ptr`. Future calls to +// upb_EpsCopyInputStream_IsDone() will return `true` when the stream position +// reaches this limit. +// +// Returns a delta that the caller must store and supply to PopLimit() below. +// +// A return value of <0 indicates that `size` is too large, and exceeds a +// previous limit. If this occurs, the stream is in an error state and must no +// longer be used. +UPB_INLINE ptrdiff_t upb_EpsCopyInputStream_PushLimit(upb_EpsCopyInputStream* e, + const char* ptr, + size_t size); + +// Pops the last limit that was pushed on this stream. This may only be called +// once IsDone() returns true. The user must pass the delta that was returned +// from PushLimit(). +UPB_INLINE void upb_EpsCopyInputStream_PopLimit(upb_EpsCopyInputStream* e, + const char* ptr, + ptrdiff_t saved_delta); + +// Tries to perform a fast-path handling of the given delimited message data. +// If the sub-message beginning at `*ptr` and extending for `len` is short and +// fits within this buffer, calls `func` with `ctx` as a parameter, where the +// pushing and popping of limits is handled automatically and with lower cost +// than the normal PushLimit()/PopLimit() sequence. +UPB_FORCEINLINE bool upb_EpsCopyInputStream_TryParseDelimitedFast( + upb_EpsCopyInputStream* e, const char** ptr, size_t size, + upb_EpsCopyInputStream_ParseDelimitedFunc* func, void* ctx); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#ifdef __cplusplus +// Temporary overloads for functions whose signature has recently changed. +UPB_DEPRECATE_AND_INLINE() +UPB_INLINE void upb_EpsCopyInputStream_Init(upb_EpsCopyInputStream* e, + const char** ptr, size_t size, + bool enable_aliasing) { + upb_EpsCopyInputStream_Init(e, ptr, size); +} +#endif + + +#endif // UPB_WIRE_EPS_COPY_INPUT_STREAM_H_ + +#ifndef UPB_WIRE_READER_H_ +#define UPB_WIRE_READER_H_ + +#include +#include +#include + + +#ifndef UPB_WIRE_INTERNAL_READER_H_ +#define UPB_WIRE_INTERNAL_READER_H_ + +#include +#include + + +// Must be last. + +#define kUpb_WireReader_WireTypeBits 3 +#define kUpb_WireReader_WireTypeMask 7 + +typedef struct { + const char* ptr; + uint64_t val; +} UPB_PRIVATE(_upb_WireReader_LongVarint); + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_PRIVATE(_upb_WireReader_LongVarint) +UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(const char* ptr, uint64_t val, + upb_EpsCopyInputStream* stream); +UPB_PRIVATE(_upb_WireReader_LongVarint) +UPB_PRIVATE(_upb_WireReader_ReadLongTag)(const char* ptr, uint64_t val, + upb_EpsCopyInputStream* stream); +UPB_PRIVATE(_upb_WireReader_LongVarint) +UPB_PRIVATE(_upb_WireReader_ReadLongSize)(const char* ptr, uint64_t val, + upb_EpsCopyInputStream* stream); + +UPB_FORCEINLINE const char* upb_WireReader_ReadVarint( + const char* ptr, uint64_t* val, upb_EpsCopyInputStream* stream) { + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(stream, 10); + uint8_t byte = *ptr; + if (UPB_LIKELY((byte & 0x80) == 0)) { + *val = byte; + return ptr + 1; + } + UPB_PRIVATE(_upb_WireReader_LongVarint) res; + res = UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(ptr, byte, stream); + *val = res.val; + return UPB_PRIVATE(upb_EpsCopyInputStream_AssumeResult)(stream, res.ptr); +} + +UPB_FORCEINLINE const char* upb_WireReader_ReadTag( + const char* ptr, uint32_t* val, upb_EpsCopyInputStream* stream) { + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(stream, 5); + uint8_t byte = *ptr; + if (UPB_LIKELY((byte & 0x80) == 0)) { + *val = byte; + return ptr + 1; + } + UPB_PRIVATE(_upb_WireReader_LongVarint) res; + res = UPB_PRIVATE(_upb_WireReader_ReadLongTag)(ptr, byte, stream); + *val = res.val; + return UPB_PRIVATE(upb_EpsCopyInputStream_AssumeResult)(stream, res.ptr); +} + +UPB_FORCEINLINE const char* upb_WireReader_ReadSize( + const char* ptr, int* val, upb_EpsCopyInputStream* stream) { + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(stream, 5); + uint8_t byte = *ptr; + if (UPB_LIKELY((byte & 0x80) == 0)) { + *val = byte; + return ptr + 1; + } + UPB_PRIVATE(_upb_WireReader_LongVarint) res; + res = UPB_PRIVATE(_upb_WireReader_ReadLongSize)(ptr, byte, stream); + *val = res.val; + return UPB_PRIVATE(upb_EpsCopyInputStream_AssumeResult)(stream, res.ptr); +} + +UPB_API_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag) { + return tag >> kUpb_WireReader_WireTypeBits; +} + +UPB_API_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag) { + return tag & kUpb_WireReader_WireTypeMask; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_WIRE_INTERNAL_READER_H_ + +#ifndef UPB_WIRE_TYPES_H_ +#define UPB_WIRE_TYPES_H_ + +// A list of types as they are encoded on the wire. +typedef enum { + kUpb_WireType_Varint = 0, + kUpb_WireType_64Bit = 1, + kUpb_WireType_Delimited = 2, + kUpb_WireType_StartGroup = 3, + kUpb_WireType_EndGroup = 4, + kUpb_WireType_32Bit = 5 +} upb_WireType; + +#endif /* UPB_WIRE_TYPES_H_ */ + +// Must be last. + +// The upb_WireReader interface is suitable for general-purpose parsing of +// protobuf binary wire format. It is designed to be used along with +// upb_EpsCopyInputStream for buffering, and all parsing routines in this file +// assume that at least kUpb_EpsCopyInputStream_SlopBytes worth of data is +// available to read without any bounds checks. + +#ifdef __cplusplus +extern "C" { +#endif + +// Parses a tag into `tag`, and returns a pointer past the end of the tag, or +// NULL if there was an error in the tag data. +// +// REQUIRES: there must be at least 10 bytes of data available at `ptr`. +// Bounds checks must be performed before calling this function, preferably +// by calling upb_EpsCopyInputStream_IsDone(). +UPB_FORCEINLINE const char* upb_WireReader_ReadTag( + const char* ptr, uint32_t* tag, upb_EpsCopyInputStream* stream); + +// Given a tag, returns the field number. +UPB_API_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag); + +// Given a tag, returns the wire type. +UPB_API_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag); + +UPB_FORCEINLINE const char* upb_WireReader_ReadVarint( + const char* ptr, uint64_t* val, upb_EpsCopyInputStream* stream); + +// Skips data for a varint, returning a pointer past the end of the varint, or +// NULL if there was an error in the varint data. +// +// REQUIRES: there must be at least 10 bytes of data available at `ptr`. +// Bounds checks must be performed before calling this function, preferably +// by calling upb_EpsCopyInputStream_IsDone(). +UPB_INLINE const char* upb_WireReader_SkipVarint( + const char* ptr, upb_EpsCopyInputStream* stream) { + uint64_t val; + return upb_WireReader_ReadVarint(ptr, &val, stream); +} + +// Reads a varint indicating the size of a delimited field into `size`, or +// NULL if there was an error in the varint data. +// +// REQUIRES: there must be at least 10 bytes of data available at `ptr`. +// Bounds checks must be performed before calling this function, preferably +// by calling upb_EpsCopyInputStream_IsDone(). +UPB_INLINE const char* upb_WireReader_ReadSize(const char* ptr, int* size, + upb_EpsCopyInputStream* stream); + +// Reads a fixed32 field, performing byte swapping if necessary. +// +// REQUIRES: there must be at least 4 bytes of data available at `ptr`. +// Bounds checks must be performed before calling this function, preferably +// by calling upb_EpsCopyInputStream_IsDone(). +UPB_INLINE const char* upb_WireReader_ReadFixed32( + const char* ptr, void* val, upb_EpsCopyInputStream* stream) { + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(stream, 4); + uint32_t uval; + memcpy(&uval, ptr, 4); + uval = upb_BigEndian32(uval); + memcpy(val, &uval, 4); + return ptr + 4; +} + +// Reads a fixed64 field, performing byte swapping if necessary. +// +// REQUIRES: there must be at least 4 bytes of data available at `ptr`. +// Bounds checks must be performed before calling this function, preferably +// by calling upb_EpsCopyInputStream_IsDone(). +UPB_INLINE const char* upb_WireReader_ReadFixed64( + const char* ptr, void* val, upb_EpsCopyInputStream* stream) { + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(stream, 8); + uint64_t uval; + memcpy(&uval, ptr, 8); + uval = upb_BigEndian64(uval); + memcpy(val, &uval, 8); + return ptr + 8; +} + +const char* UPB_PRIVATE(_upb_WireReader_SkipGroup)( + const char* ptr, uint32_t tag, int depth_limit, + upb_EpsCopyInputStream* stream); + +// Skips data for a group, returning a pointer past the end of the group, or +// NULL if there was an error parsing the group. The `tag` argument should be +// the start group tag that begins the group. The `depth_limit` argument +// indicates how many levels of recursion the group is allowed to have before +// reporting a parse error (this limit exists to protect against stack +// overflow). +// +// TODO: evaluate how the depth_limit should be specified. Do users need +// control over this? +UPB_INLINE const char* upb_WireReader_SkipGroup( + const char* ptr, uint32_t tag, upb_EpsCopyInputStream* stream) { + const char* ret = + UPB_PRIVATE(_upb_WireReader_SkipGroup)(ptr, tag, 100, stream); + return UPB_PRIVATE(upb_EpsCopyInputStream_AssumeResult)(stream, ret); +} + +UPB_INLINE const char* _upb_WireReader_SkipValue( + const char* ptr, uint32_t tag, int depth_limit, + upb_EpsCopyInputStream* stream) { + switch (upb_WireReader_GetWireType(tag)) { + case kUpb_WireType_Varint: + return upb_WireReader_SkipVarint(ptr, stream); + case kUpb_WireType_32Bit: + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(stream, 4); + return ptr + 4; + case kUpb_WireType_64Bit: + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(stream, 8); + return ptr + 8; + case kUpb_WireType_Delimited: { + int size; + ptr = upb_WireReader_ReadSize(ptr, &size, stream); + if (!ptr || !upb_EpsCopyInputStream_CheckSize(stream, ptr, size)) { + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream); + } + ptr += size; + return ptr; + } + case kUpb_WireType_StartGroup: + return UPB_PRIVATE(_upb_WireReader_SkipGroup)(ptr, tag, depth_limit, + stream); + case kUpb_WireType_EndGroup: + // Should be handled before now. + default: + // Unknown wire type. + return UPB_PRIVATE(upb_EpsCopyInputStream_ReturnError)(stream); + } +} + +// Skips data for a wire value of any type, returning a pointer past the end of +// the data, or NULL if there was an error parsing the group. The `tag` argument +// should be the tag that was just parsed. The `depth_limit` argument indicates +// how many levels of recursion a group is allowed to have before reporting a +// parse error (this limit exists to protect against stack overflow). +// +// REQUIRES: there must be at least 10 bytes of data available at `ptr`. +// Bounds checks must be performed before calling this function, preferably +// by calling upb_EpsCopyInputStream_IsDone(). +// +// TODO: evaluate how the depth_limit should be specified. Do users need +// control over this? +UPB_INLINE const char* upb_WireReader_SkipValue( + const char* ptr, uint32_t tag, upb_EpsCopyInputStream* stream) { + return _upb_WireReader_SkipValue(ptr, tag, 100, stream); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_WIRE_READER_H_ + +#ifndef UPB_MESSAGE_COPY_H_ +#define UPB_MESSAGE_COPY_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Deep clones a message using the provided target arena. +upb_Message* upb_Message_DeepClone(const upb_Message* msg, + const upb_MiniTable* m, upb_Arena* arena); + +// Shallow clones a message using the provided target arena. +upb_Message* upb_Message_ShallowClone(const upb_Message* msg, + const upb_MiniTable* m, upb_Arena* arena); + +// Deep clones array contents. +upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type, + const upb_MiniTable* sub, upb_Arena* arena); + +// Deep clones map contents. +upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type, + upb_CType value_type, + const upb_MiniTable* map_entry_table, + upb_Arena* arena); + +// Deep copies the message from src to dst. +bool upb_Message_DeepCopy(upb_Message* dst, const upb_Message* src, + const upb_MiniTable* m, upb_Arena* arena); + +// Shallow copies the message from src to dst. +void upb_Message_ShallowCopy(upb_Message* dst, const upb_Message* src, + const upb_MiniTable* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_MESSAGE_COPY_H_ +#ifndef GOOGLE_UPB_UPB_MESSAGE_MERGE_H__ +#define GOOGLE_UPB_UPB_MESSAGE_MERGE_H__ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API bool upb_Message_MergeFrom(upb_Message* dst, const upb_Message* src, + const upb_MiniTable* mt, + const upb_ExtensionRegistry* extreg, + upb_Arena* arena); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif // GOOGLE_UPB_UPB_MESSAGE_MERGE_H__ + +#ifndef UPB_MINI_DESCRIPTOR_INTERNAL_BASE92_H_ +#define UPB_MINI_DESCRIPTOR_INTERNAL_BASE92_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE char _upb_ToBase92(int8_t ch) { + extern const char _kUpb_ToBase92[]; + UPB_ASSERT(0 <= ch && ch < 92); + return _kUpb_ToBase92[ch]; +} + +UPB_INLINE char _upb_FromBase92(uint8_t ch) { + extern const int8_t _kUpb_FromBase92[]; + if (' ' > ch || ch > '~') return -1; + return _kUpb_FromBase92[ch - ' ']; +} + +UPB_INLINE const char* _upb_Base92_DecodeVarint(const char* ptr, + const char* end, char first_ch, + uint8_t min, uint8_t max, + uint32_t* out_val) { + uint32_t val = 0; + uint32_t shift = 0; + const int bits_per_char = + upb_Log2Ceiling(_upb_FromBase92(max) - _upb_FromBase92(min)); + char ch = first_ch; + while (1) { + uint32_t bits = _upb_FromBase92(ch) - _upb_FromBase92(min); + val |= bits << shift; + if (ptr == end || *ptr < min || max < *ptr) { + *out_val = val; + UPB_ASSUME(ptr != NULL); + return ptr; + } + ch = *ptr++; + shift += bits_per_char; + if (shift >= 32) return NULL; + } +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_MINI_DESCRIPTOR_INTERNAL_BASE92_H_ + +#ifndef UPB_MINI_DESCRIPTOR_INTERNAL_DECODER_H_ +#define UPB_MINI_DESCRIPTOR_INTERNAL_DECODER_H_ + + +// Must be last. + +// upb_MdDecoder: used internally for decoding MiniDescriptors for messages, +// extensions, and enums. +typedef struct { + const char* end; + upb_Status* status; + jmp_buf err; +} upb_MdDecoder; + +UPB_PRINTF(2, 3) +UPB_NORETURN UPB_INLINE void upb_MdDecoder_ErrorJmp(upb_MdDecoder* d, + const char* fmt, ...) { + if (d->status) { + va_list argp; + upb_Status_SetErrorMessage(d->status, "Error building mini table: "); + va_start(argp, fmt); + upb_Status_VAppendErrorFormat(d->status, fmt, argp); + va_end(argp); + } + UPB_LONGJMP(d->err, 1); +} + +UPB_INLINE void upb_MdDecoder_CheckOutOfMemory(upb_MdDecoder* d, + const void* ptr) { + if (!ptr) upb_MdDecoder_ErrorJmp(d, "Out of memory"); +} + +UPB_INLINE const char* upb_MdDecoder_DecodeBase92Varint( + upb_MdDecoder* d, const char* ptr, char first_ch, uint8_t min, uint8_t max, + uint32_t* out_val) { + ptr = _upb_Base92_DecodeVarint(ptr, d->end, first_ch, min, max, out_val); + if (!ptr) upb_MdDecoder_ErrorJmp(d, "Overlong varint"); + return ptr; +} + + +#endif // UPB_MINI_DESCRIPTOR_INTERNAL_DECODER_H_ + +#ifndef UPB_MINI_DESCRIPTOR_INTERNAL_WIRE_CONSTANTS_H_ +#define UPB_MINI_DESCRIPTOR_INTERNAL_WIRE_CONSTANTS_H_ + + +// Must be last. + +typedef enum { + kUpb_EncodedType_Double = 0, + kUpb_EncodedType_Float = 1, + kUpb_EncodedType_Fixed32 = 2, + kUpb_EncodedType_Fixed64 = 3, + kUpb_EncodedType_SFixed32 = 4, + kUpb_EncodedType_SFixed64 = 5, + kUpb_EncodedType_Int32 = 6, + kUpb_EncodedType_UInt32 = 7, + kUpb_EncodedType_SInt32 = 8, + kUpb_EncodedType_Int64 = 9, + kUpb_EncodedType_UInt64 = 10, + kUpb_EncodedType_SInt64 = 11, + kUpb_EncodedType_OpenEnum = 12, + kUpb_EncodedType_Bool = 13, + kUpb_EncodedType_Bytes = 14, + kUpb_EncodedType_String = 15, + kUpb_EncodedType_Group = 16, + kUpb_EncodedType_Message = 17, + kUpb_EncodedType_ClosedEnum = 18, + + kUpb_EncodedType_RepeatedBase = 20, +} upb_EncodedType; + +typedef enum { + kUpb_EncodedFieldModifier_FlipPacked = 1 << 0, + kUpb_EncodedFieldModifier_IsRequired = 1 << 1, + kUpb_EncodedFieldModifier_IsProto3Singular = 1 << 2, + kUpb_EncodedFieldModifier_FlipValidateUtf8 = 1 << 3, +} upb_EncodedFieldModifier; + +enum { + kUpb_EncodedValue_MinField = ' ', + kUpb_EncodedValue_MaxField = 'I', + kUpb_EncodedValue_MinModifier = 'L', + kUpb_EncodedValue_MaxModifier = '[', + kUpb_EncodedValue_End = '^', + kUpb_EncodedValue_MinSkip = '_', + kUpb_EncodedValue_MaxSkip = '~', + kUpb_EncodedValue_OneofSeparator = '~', + kUpb_EncodedValue_FieldSeparator = '|', + kUpb_EncodedValue_MinOneofField = ' ', + kUpb_EncodedValue_MaxOneofField = 'b', + kUpb_EncodedValue_MaxEnumMask = 'A', +}; + +enum { + kUpb_EncodedVersion_EnumV1 = '!', + kUpb_EncodedVersion_ExtensionV1 = '#', + kUpb_EncodedVersion_MapV1 = '%', + kUpb_EncodedVersion_MessageV1 = '$', + kUpb_EncodedVersion_MessageSetV1 = '&', +}; + + +#endif // UPB_MINI_DESCRIPTOR_INTERNAL_WIRE_CONSTANTS_H_ + +#ifndef UPB_MINI_DESCRIPTOR_INTERNAL_MODIFIERS_H_ +#define UPB_MINI_DESCRIPTOR_INTERNAL_MODIFIERS_H_ + +// Must be last. + +typedef enum { + kUpb_FieldModifier_IsRepeated = 1 << 0, + kUpb_FieldModifier_IsPacked = 1 << 1, + kUpb_FieldModifier_IsClosedEnum = 1 << 2, + kUpb_FieldModifier_IsProto3Singular = 1 << 3, + kUpb_FieldModifier_IsRequired = 1 << 4, + kUpb_FieldModifier_ValidateUtf8 = 1 << 5, +} kUpb_FieldModifier; + +// These modifiers are also used on the wire. +typedef enum { + kUpb_MessageModifier_ValidateUtf8 = 1 << 0, + kUpb_MessageModifier_DefaultIsPacked = 1 << 1, + kUpb_MessageModifier_IsExtendable = 1 << 2, +} kUpb_MessageModifier; + + +#endif // UPB_MINI_DESCRIPTOR_INTERNAL_MODIFIERS_H_ + +#ifndef UPB_MINI_DESCRIPTOR_INTERNAL_ENCODE_H_ +#define UPB_MINI_DESCRIPTOR_INTERNAL_ENCODE_H_ + +#include + + +// Must be last. + +// If the input buffer has at least this many bytes available, the encoder call +// is guaranteed to succeed (as long as field number order is maintained). +#define kUpb_MtDataEncoder_MinSize 16 + +typedef struct { + char* end; // Limit of the buffer passed as a parameter. + // Aliased to internal-only members in .cc. + char internal[32]; +} upb_MtDataEncoder; + +#ifdef __cplusplus +extern "C" { +#endif + +// Encodes field/oneof information for a given message. The sequence of calls +// should look like: +// +// upb_MtDataEncoder e; +// char buf[256]; +// char* ptr = buf; +// e.end = ptr + sizeof(buf); +// unit64_t msg_mod = ...; // bitwise & of kUpb_MessageModifiers or zero +// ptr = upb_MtDataEncoder_StartMessage(&e, ptr, msg_mod); +// // Fields *must* be in field number order. +// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...); +// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...); +// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...); +// +// // If oneofs are present. Oneofs must be encoded after regular fields. +// ptr = upb_MiniTable_StartOneof(&e, ptr) +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// +// ptr = upb_MiniTable_StartOneof(&e, ptr); +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...); +// +// Oneofs must be encoded after all regular fields. +char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr, + uint64_t msg_mod); +char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr, + upb_FieldType type, uint32_t field_num, + uint64_t field_mod); +char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr); +char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr, + uint32_t field_num); + +// Encodes the set of values for a given enum. The values must be given in +// order (after casting to uint32_t), and repeats are not allowed. +char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr); +char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr, + uint32_t val); +char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr); + +// Encodes an entire mini descriptor for an extension. +char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr, + upb_FieldType type, uint32_t field_num, + uint64_t field_mod); + +// Encodes an entire mini descriptor for a map. +char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr, + upb_FieldType key_type, + upb_FieldType value_type, uint64_t key_mod, + uint64_t value_mod); + +// Encodes an entire mini descriptor for a message set. +char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_MINI_DESCRIPTOR_INTERNAL_ENCODE_H_ */ + +#ifndef UPB_MINI_TABLE_GENERATED_REGISTRY_H_ +#define UPB_MINI_TABLE_GENERATED_REGISTRY_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +/* Generated registry: a global singleton that gathers all extensions linked + * into the binary. + * + * This singleton is thread-safe and lock-free, implemented using atomics. The + * registry is lazily initialized the first time it is loaded. When all + * references are released, the registry will be destroyed. New loads + * afterwards will simply reload the same registry as needed. + * + * The extension minitables are registered in gencode using linker arrays. Each + * .proto file produces a weak, hidden, constructor function that adds all + * visible extensions from the array into the registry. In each binary, only + * one copy of the constructor will actually be preserved by the linker, and + * that copy will add all of the extensions for the entire binary. All of these + * are added to a global linked list of minitables pre-main, which are then used + * to construct this singleton as needed. + */ + +typedef struct upb_GeneratedRegistryRef upb_GeneratedRegistryRef; + +// Loads the generated registry, returning a reference to it. The reference +// must be held for the lifetime of any ExtensionRegistry obtained from it. +// +// Returns NULL on failure. +UPB_API const upb_GeneratedRegistryRef* upb_GeneratedRegistry_Load(void); + +// Releases a reference to the generated registry. This may destroy the +// registry if there are no other references to it. +// +// NULL is a valid argument and is simply ignored for easier error handling in +// callers. +UPB_API void upb_GeneratedRegistry_Release(const upb_GeneratedRegistryRef* r); + +// Returns the extension registry contained by a reference to the generated +// registry. +// +// The reference must be held for the lifetime of the registry. +UPB_API const upb_ExtensionRegistry* upb_GeneratedRegistry_Get( + const upb_GeneratedRegistryRef* r); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif // UPB_MINI_TABLE_GENERATED_REGISTRY_H_ + +#ifndef UPB_REFLECTION_DEF_POOL_INTERNAL_H_ +#define UPB_REFLECTION_DEF_POOL_INTERNAL_H_ + + +#ifndef UPB_REFLECTION_DEF_H_ +#define UPB_REFLECTION_DEF_H_ + +// IWYU pragma: begin_exports + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_DEF_POOL_H_ +#define UPB_REFLECTION_DEF_POOL_H_ + + +// IWYU pragma: private, include "upb/reflection/def.h" + +// Declarations common to all public def types. + +#ifndef UPB_REFLECTION_COMMON_H_ +#define UPB_REFLECTION_COMMON_H_ + +#ifndef GOOGLE_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H__ +#define GOOGLE_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H__ + +// IWYU pragma: begin_exports + +#if defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0 +// This header is checked in. +#elif defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 1 +// This header is generated at build time by the bootstrapping process. +#else +// This is the normal header, generated by upb_c_proto_library(). +/* This file was generated by upb_generator from the input file: + * + * google/protobuf/descriptor.proto + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. + * NO CHECKED-IN PROTOBUF GENCODE */ + +#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_H_ +#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif +typedef struct google_protobuf_FileDescriptorSet { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FileDescriptorSet; + +typedef struct google_protobuf_FileDescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FileDescriptorProto; + +typedef struct google_protobuf_DescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_DescriptorProto; + +typedef struct google_protobuf_DescriptorProto_ExtensionRange { + upb_Message UPB_PRIVATE(base); +} google_protobuf_DescriptorProto_ExtensionRange; + +typedef struct google_protobuf_DescriptorProto_ReservedRange { + upb_Message UPB_PRIVATE(base); +} google_protobuf_DescriptorProto_ReservedRange; + +typedef struct google_protobuf_ExtensionRangeOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_ExtensionRangeOptions; + +typedef struct google_protobuf_ExtensionRangeOptions_Declaration { + upb_Message UPB_PRIVATE(base); +} google_protobuf_ExtensionRangeOptions_Declaration; + +typedef struct google_protobuf_FieldDescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FieldDescriptorProto; + +typedef struct google_protobuf_OneofDescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_OneofDescriptorProto; + +typedef struct google_protobuf_EnumDescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_EnumDescriptorProto; + +typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange { + upb_Message UPB_PRIVATE(base); +} google_protobuf_EnumDescriptorProto_EnumReservedRange; + +typedef struct google_protobuf_EnumValueDescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_EnumValueDescriptorProto; + +typedef struct google_protobuf_ServiceDescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_ServiceDescriptorProto; + +typedef struct google_protobuf_MethodDescriptorProto { + upb_Message UPB_PRIVATE(base); +} google_protobuf_MethodDescriptorProto; + +typedef struct google_protobuf_FileOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FileOptions; + +typedef struct google_protobuf_MessageOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_MessageOptions; + +typedef struct google_protobuf_FieldOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FieldOptions; + +typedef struct google_protobuf_FieldOptions_EditionDefault { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FieldOptions_EditionDefault; + +typedef struct google_protobuf_FieldOptions_FeatureSupport { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FieldOptions_FeatureSupport; + +typedef struct google_protobuf_OneofOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_OneofOptions; + +typedef struct google_protobuf_EnumOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_EnumOptions; + +typedef struct google_protobuf_EnumValueOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_EnumValueOptions; + +typedef struct google_protobuf_ServiceOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_ServiceOptions; + +typedef struct google_protobuf_MethodOptions { + upb_Message UPB_PRIVATE(base); +} google_protobuf_MethodOptions; + +typedef struct google_protobuf_UninterpretedOption { + upb_Message UPB_PRIVATE(base); +} google_protobuf_UninterpretedOption; + +typedef struct google_protobuf_UninterpretedOption_NamePart { + upb_Message UPB_PRIVATE(base); +} google_protobuf_UninterpretedOption_NamePart; + +typedef struct google_protobuf_FeatureSet { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FeatureSet; + +typedef struct google_protobuf_FeatureSet_VisibilityFeature { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FeatureSet_VisibilityFeature; + +typedef struct google_protobuf_FeatureSetDefaults { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FeatureSetDefaults; + +typedef struct google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault { + upb_Message UPB_PRIVATE(base); +} google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault; + +typedef struct google_protobuf_SourceCodeInfo { + upb_Message UPB_PRIVATE(base); +} google_protobuf_SourceCodeInfo; + +typedef struct google_protobuf_SourceCodeInfo_Location { + upb_Message UPB_PRIVATE(base); +} google_protobuf_SourceCodeInfo_Location; + +typedef struct google_protobuf_GeneratedCodeInfo { + upb_Message UPB_PRIVATE(base); +} google_protobuf_GeneratedCodeInfo; + +typedef struct google_protobuf_GeneratedCodeInfo_Annotation { + upb_Message UPB_PRIVATE(base); +} google_protobuf_GeneratedCodeInfo_Annotation; + + +typedef enum { + google_protobuf_EDITION_UNKNOWN = 0, + google_protobuf_EDITION_1_TEST_ONLY = 1, + google_protobuf_EDITION_2_TEST_ONLY = 2, + google_protobuf_EDITION_LEGACY = 900, + google_protobuf_EDITION_PROTO2 = 998, + google_protobuf_EDITION_PROTO3 = 999, + google_protobuf_EDITION_2023 = 1000, + google_protobuf_EDITION_2024 = 1001, + google_protobuf_EDITION_2026 = 1002, + google_protobuf_EDITION_UNSTABLE = 9999, + google_protobuf_EDITION_99997_TEST_ONLY = 99997, + google_protobuf_EDITION_99998_TEST_ONLY = 99998, + google_protobuf_EDITION_99999_TEST_ONLY = 99999, + google_protobuf_EDITION_MAX = 2147483647 +} google_protobuf_Edition; + +typedef enum { + google_protobuf_ExtensionRangeOptions_DECLARATION = 0, + google_protobuf_ExtensionRangeOptions_UNVERIFIED = 1 +} google_protobuf_ExtensionRangeOptions_VerificationState; + +typedef enum { + google_protobuf_FeatureSet_ENFORCE_NAMING_STYLE_UNKNOWN = 0, + google_protobuf_FeatureSet_STYLE2024 = 1, + google_protobuf_FeatureSet_STYLE_LEGACY = 2, + google_protobuf_FeatureSet_STYLE2026 = 3 +} google_protobuf_FeatureSet_EnforceNamingStyle; + +typedef enum { + google_protobuf_FeatureSet_ENUM_TYPE_UNKNOWN = 0, + google_protobuf_FeatureSet_OPEN = 1, + google_protobuf_FeatureSet_CLOSED = 2 +} google_protobuf_FeatureSet_EnumType; + +typedef enum { + google_protobuf_FeatureSet_FIELD_PRESENCE_UNKNOWN = 0, + google_protobuf_FeatureSet_EXPLICIT = 1, + google_protobuf_FeatureSet_IMPLICIT = 2, + google_protobuf_FeatureSet_LEGACY_REQUIRED = 3 +} google_protobuf_FeatureSet_FieldPresence; + +typedef enum { + google_protobuf_FeatureSet_JSON_FORMAT_UNKNOWN = 0, + google_protobuf_FeatureSet_ALLOW = 1, + google_protobuf_FeatureSet_LEGACY_BEST_EFFORT = 2 +} google_protobuf_FeatureSet_JsonFormat; + +typedef enum { + google_protobuf_FeatureSet_MESSAGE_ENCODING_UNKNOWN = 0, + google_protobuf_FeatureSet_LENGTH_PREFIXED = 1, + google_protobuf_FeatureSet_DELIMITED = 2 +} google_protobuf_FeatureSet_MessageEncoding; + +typedef enum { + google_protobuf_FeatureSet_REPEATED_FIELD_ENCODING_UNKNOWN = 0, + google_protobuf_FeatureSet_PACKED = 1, + google_protobuf_FeatureSet_EXPANDED = 2 +} google_protobuf_FeatureSet_RepeatedFieldEncoding; + +typedef enum { + google_protobuf_FeatureSet_UTF8_VALIDATION_UNKNOWN = 0, + google_protobuf_FeatureSet_VERIFY = 2, + google_protobuf_FeatureSet_NONE = 3 +} google_protobuf_FeatureSet_Utf8Validation; + +typedef enum { + google_protobuf_FeatureSet_VisibilityFeature_DEFAULT_SYMBOL_VISIBILITY_UNKNOWN = 0, + google_protobuf_FeatureSet_VisibilityFeature_EXPORT_ALL = 1, + google_protobuf_FeatureSet_VisibilityFeature_EXPORT_TOP_LEVEL = 2, + google_protobuf_FeatureSet_VisibilityFeature_LOCAL_ALL = 3, + google_protobuf_FeatureSet_VisibilityFeature_STRICT = 4 +} google_protobuf_FeatureSet_VisibilityFeature_DefaultSymbolVisibility; + +typedef enum { + google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1, + google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2, + google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3 +} google_protobuf_FieldDescriptorProto_Label; + +typedef enum { + google_protobuf_FieldDescriptorProto_TYPE_DOUBLE = 1, + google_protobuf_FieldDescriptorProto_TYPE_FLOAT = 2, + google_protobuf_FieldDescriptorProto_TYPE_INT64 = 3, + google_protobuf_FieldDescriptorProto_TYPE_UINT64 = 4, + google_protobuf_FieldDescriptorProto_TYPE_INT32 = 5, + google_protobuf_FieldDescriptorProto_TYPE_FIXED64 = 6, + google_protobuf_FieldDescriptorProto_TYPE_FIXED32 = 7, + google_protobuf_FieldDescriptorProto_TYPE_BOOL = 8, + google_protobuf_FieldDescriptorProto_TYPE_STRING = 9, + google_protobuf_FieldDescriptorProto_TYPE_GROUP = 10, + google_protobuf_FieldDescriptorProto_TYPE_MESSAGE = 11, + google_protobuf_FieldDescriptorProto_TYPE_BYTES = 12, + google_protobuf_FieldDescriptorProto_TYPE_UINT32 = 13, + google_protobuf_FieldDescriptorProto_TYPE_ENUM = 14, + google_protobuf_FieldDescriptorProto_TYPE_SFIXED32 = 15, + google_protobuf_FieldDescriptorProto_TYPE_SFIXED64 = 16, + google_protobuf_FieldDescriptorProto_TYPE_SINT32 = 17, + google_protobuf_FieldDescriptorProto_TYPE_SINT64 = 18 +} google_protobuf_FieldDescriptorProto_Type; + +typedef enum { + google_protobuf_FieldOptions_STRING = 0, + google_protobuf_FieldOptions_CORD = 1, + google_protobuf_FieldOptions_STRING_PIECE = 2 +} google_protobuf_FieldOptions_CType; + +typedef enum { + google_protobuf_FieldOptions_JS_NORMAL = 0, + google_protobuf_FieldOptions_JS_STRING = 1, + google_protobuf_FieldOptions_JS_NUMBER = 2 +} google_protobuf_FieldOptions_JSType; + +typedef enum { + google_protobuf_FieldOptions_RETENTION_UNKNOWN = 0, + google_protobuf_FieldOptions_RETENTION_RUNTIME = 1, + google_protobuf_FieldOptions_RETENTION_SOURCE = 2 +} google_protobuf_FieldOptions_OptionRetention; + +typedef enum { + google_protobuf_FieldOptions_TARGET_TYPE_UNKNOWN = 0, + google_protobuf_FieldOptions_TARGET_TYPE_FILE = 1, + google_protobuf_FieldOptions_TARGET_TYPE_EXTENSION_RANGE = 2, + google_protobuf_FieldOptions_TARGET_TYPE_MESSAGE = 3, + google_protobuf_FieldOptions_TARGET_TYPE_FIELD = 4, + google_protobuf_FieldOptions_TARGET_TYPE_ONEOF = 5, + google_protobuf_FieldOptions_TARGET_TYPE_ENUM = 6, + google_protobuf_FieldOptions_TARGET_TYPE_ENUM_ENTRY = 7, + google_protobuf_FieldOptions_TARGET_TYPE_SERVICE = 8, + google_protobuf_FieldOptions_TARGET_TYPE_METHOD = 9 +} google_protobuf_FieldOptions_OptionTargetType; + +typedef enum { + google_protobuf_FileOptions_SPEED = 1, + google_protobuf_FileOptions_CODE_SIZE = 2, + google_protobuf_FileOptions_LITE_RUNTIME = 3 +} google_protobuf_FileOptions_OptimizeMode; + +typedef enum { + google_protobuf_GeneratedCodeInfo_Annotation_NONE = 0, + google_protobuf_GeneratedCodeInfo_Annotation_SET = 1, + google_protobuf_GeneratedCodeInfo_Annotation_ALIAS = 2 +} google_protobuf_GeneratedCodeInfo_Annotation_Semantic; + +typedef enum { + google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0, + google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1, + google_protobuf_MethodOptions_IDEMPOTENT = 2 +} google_protobuf_MethodOptions_IdempotencyLevel; + +typedef enum { + google_protobuf_VISIBILITY_UNSET = 0, + google_protobuf_VISIBILITY_LOCAL = 1, + google_protobuf_VISIBILITY_EXPORT = 2 +} google_protobuf_SymbolVisibility; + + + +/* google.protobuf.FileDescriptorSet */ +UPB_INLINE google_protobuf_FileDescriptorSet* google_protobuf_FileDescriptorSet_new(upb_Arena* arena) { + return (google_protobuf_FileDescriptorSet*)_upb_Message_New(&google__protobuf__FileDescriptorSet_msg_init, arena); +} +UPB_INLINE google_protobuf_FileDescriptorSet* google_protobuf_FileDescriptorSet_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FileDescriptorSet* ret = google_protobuf_FileDescriptorSet_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FileDescriptorSet_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FileDescriptorSet* google_protobuf_FileDescriptorSet_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FileDescriptorSet* ret = google_protobuf_FileDescriptorSet_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FileDescriptorSet_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorSet_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FileDescriptorSet_serialize_ex(const google_protobuf_FileDescriptorSet* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorSet_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FileDescriptorSet_clear_file(google_protobuf_FileDescriptorSet* msg) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FileDescriptorProto* const* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet* msg, + size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_FileDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorSet_file_upb_array( + const google_protobuf_FileDescriptorSet* msg, size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorSet_file_mutable_upb_array( + google_protobuf_FileDescriptorSet* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_mutable_file(google_protobuf_FileDescriptorSet* msg, + size_t* size) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_FileDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_resize_file(google_protobuf_FileDescriptorSet* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileDescriptorProto_msg_init); + return (google_protobuf_FileDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorSet_add_file( + google_protobuf_FileDescriptorSet* msg, upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(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_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.FileDescriptorProto */ +UPB_INLINE google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_FileDescriptorProto*)_upb_Message_New(&google__protobuf__FileDescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FileDescriptorProto* ret = google_protobuf_FileDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FileDescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FileDescriptorProto* ret = google_protobuf_FileDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FileDescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FileDescriptorProto_serialize_ex(const google_protobuf_FileDescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileDescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_name(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(56, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(56, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(56, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_package(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(64, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, UPB_SIZE(64, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(64, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_dependency(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_dependency_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_dependency_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_message_type(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_DescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_message_type_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_message_type_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_enum_type(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_EnumDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_enum_type_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_enum_type_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_service(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_ServiceDescriptorProto* const* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_ServiceDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_service_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_service_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_extension(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_FieldDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_extension_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_extension_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_options(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {8, UPB_SIZE(32, 104), 66, UPB_SIZE(25, 29), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto* msg) { + const google_protobuf_FileOptions* default_val = NULL; + const google_protobuf_FileOptions* ret; + const upb_MiniTableField field = {8, UPB_SIZE(32, 104), 66, UPB_SIZE(25, 29), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {8, UPB_SIZE(32, 104), 66, UPB_SIZE(25, 29), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_source_code_info(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {9, UPB_SIZE(36, 112), 67, UPB_SIZE(23, 28), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto* msg) { + const google_protobuf_SourceCodeInfo* default_val = NULL; + const google_protobuf_SourceCodeInfo* ret; + const upb_MiniTableField field = {9, UPB_SIZE(36, 112), 67, UPB_SIZE(23, 28), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {9, UPB_SIZE(36, 112), 67, UPB_SIZE(23, 28), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_public_dependency(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_public_dependency_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_public_dependency_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_weak_dependency(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_weak_dependency_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_weak_dependency_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_syntax(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {12, UPB_SIZE(72, 48), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {12, UPB_SIZE(72, 48), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {12, UPB_SIZE(72, 48), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_edition(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {14, UPB_SIZE(48, 12), 69, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FileDescriptorProto_edition(const google_protobuf_FileDescriptorProto* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {14, UPB_SIZE(48, 12), 69, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_edition(const google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {14, UPB_SIZE(48, 12), 69, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_clear_option_dependency(google_protobuf_FileDescriptorProto* msg) { + const upb_MiniTableField field = {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* google_protobuf_FileDescriptorProto_option_dependency(const google_protobuf_FileDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileDescriptorProto_option_dependency_upb_array( + const google_protobuf_FileDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileDescriptorProto_option_dependency_mutable_upb_array( + google_protobuf_FileDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(56, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {2, UPB_SIZE(64, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE upb_StringView* google_protobuf_FileDescriptorProto_mutable_dependency(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* google_protobuf_FileDescriptorProto_resize_dependency(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_dependency(google_protobuf_FileDescriptorProto* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_mutable_message_type(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_DescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_resize_message_type(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + return (google_protobuf_DescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_FileDescriptorProto_add_message_type( + google_protobuf_FileDescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {4, UPB_SIZE(16, 72), 0, 33, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_DescriptorProto* sub = + (struct google_protobuf_DescriptorProto*)_upb_Message_New(&google__protobuf__DescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_mutable_enum_type(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_EnumDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_resize_enum_type(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + return (google_protobuf_EnumDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_FileDescriptorProto_add_enum_type( + google_protobuf_FileDescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(20, 80), 0, UPB_SIZE(31, 32), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_EnumDescriptorProto* sub = + (struct google_protobuf_EnumDescriptorProto*)_upb_Message_New(&google__protobuf__EnumDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_mutable_service(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_ServiceDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_resize_service(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceDescriptorProto_msg_init); + return (google_protobuf_ServiceDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_ServiceDescriptorProto* google_protobuf_FileDescriptorProto_add_service( + google_protobuf_FileDescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {6, UPB_SIZE(24, 88), 0, UPB_SIZE(29, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_ServiceDescriptorProto* sub = + (struct google_protobuf_ServiceDescriptorProto*)_upb_Message_New(&google__protobuf__ServiceDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_mutable_extension(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_FieldDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_resize_extension(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + return (google_protobuf_FieldDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_FileDescriptorProto_add_extension( + google_protobuf_FileDescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {7, UPB_SIZE(28, 96), 0, UPB_SIZE(27, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_FieldDescriptorProto* sub = + (struct google_protobuf_FieldDescriptorProto*)_upb_Message_New(&google__protobuf__FieldDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto* msg, google_protobuf_FileOptions* value) { + const upb_MiniTableField field = {8, UPB_SIZE(32, 104), 66, UPB_SIZE(25, 29), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FileOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_mutable_options( + google_protobuf_FileDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_FileOptions* sub = (struct google_protobuf_FileOptions*)google_protobuf_FileDescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FileOptions*)_upb_Message_New(&google__protobuf__FileOptions_msg_init, arena); + if (sub) google_protobuf_FileDescriptorProto_set_options(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto* msg, google_protobuf_SourceCodeInfo* value) { + const upb_MiniTableField field = {9, UPB_SIZE(36, 112), 67, UPB_SIZE(23, 28), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_mutable_source_code_info( + google_protobuf_FileDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_SourceCodeInfo* sub = (struct google_protobuf_SourceCodeInfo*)google_protobuf_FileDescriptorProto_source_code_info(msg); + if (sub == NULL) { + sub = (struct google_protobuf_SourceCodeInfo*)_upb_Message_New(&google__protobuf__SourceCodeInfo_msg_init, arena); + if (sub) google_protobuf_FileDescriptorProto_set_source_code_info(msg, sub); + } + return sub; +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_public_dependency(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_public_dependency(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (int32_t*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_public_dependency(google_protobuf_FileDescriptorProto* msg, int32_t val, + upb_Arena* arena) { + upb_MiniTableField field = {10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_weak_dependency(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_weak_dependency(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (int32_t*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_weak_dependency(google_protobuf_FileDescriptorProto* msg, int32_t val, + upb_Arena* arena) { + upb_MiniTableField field = {11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {12, UPB_SIZE(72, 48), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileDescriptorProto_set_edition(google_protobuf_FileDescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {14, UPB_SIZE(48, 12), 69, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE upb_StringView* google_protobuf_FileDescriptorProto_mutable_option_dependency(google_protobuf_FileDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* google_protobuf_FileDescriptorProto_resize_option_dependency(google_protobuf_FileDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_FileDescriptorProto_add_option_dependency(google_protobuf_FileDescriptorProto* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} + +/* google.protobuf.DescriptorProto */ +UPB_INLINE google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_DescriptorProto*)_upb_Message_New(&google__protobuf__DescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_DescriptorProto* ret = google_protobuf_DescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__DescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_DescriptorProto* ret = google_protobuf_DescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__DescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_DescriptorProto_serialize_ex(const google_protobuf_DescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_name(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(52, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(52, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(52, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_field(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_FieldDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_field_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_field_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_nested_type(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_DescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_nested_type_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_nested_type_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_enum_type(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_EnumDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_enum_type_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_enum_type_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_extension_range(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_DescriptorProto_ExtensionRange* const* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ExtensionRange_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_DescriptorProto_ExtensionRange* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_extension_range_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ExtensionRange_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_extension_range_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ExtensionRange_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_extension(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_FieldDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_extension_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_extension_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_options(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(32, 72), 65, UPB_SIZE(20, 26), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto* msg) { + const google_protobuf_MessageOptions* default_val = NULL; + const google_protobuf_MessageOptions* ret; + const upb_MiniTableField field = {7, UPB_SIZE(32, 72), 65, UPB_SIZE(20, 26), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MessageOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(32, 72), 65, UPB_SIZE(20, 26), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_oneof_decl(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_OneofDescriptorProto* const* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_OneofDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_oneof_decl_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_oneof_decl_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_reserved_range(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_DescriptorProto_ReservedRange* const* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ReservedRange_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_DescriptorProto_ReservedRange* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_reserved_range_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ReservedRange_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_reserved_range_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ReservedRange_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_reserved_name(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_DescriptorProto_reserved_name_upb_array( + const google_protobuf_DescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_DescriptorProto_reserved_name_mutable_upb_array( + google_protobuf_DescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_DescriptorProto_clear_visibility(google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {11, UPB_SIZE(48, 12), 66, UPB_SIZE(11, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_DescriptorProto_visibility(const google_protobuf_DescriptorProto* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {11, UPB_SIZE(48, 12), 66, UPB_SIZE(11, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_has_visibility(const google_protobuf_DescriptorProto* msg) { + const upb_MiniTableField field = {11, UPB_SIZE(48, 12), 66, UPB_SIZE(11, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(52, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_field(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_FieldDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_field(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + return (google_protobuf_FieldDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_field( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(30, 31), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_FieldDescriptorProto* sub = + (struct google_protobuf_FieldDescriptorProto*)_upb_Message_New(&google__protobuf__FieldDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_mutable_nested_type(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_DescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_resize_nested_type(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + return (google_protobuf_DescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_add_nested_type( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {3, UPB_SIZE(16, 40), 0, UPB_SIZE(28, 30), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_DescriptorProto* sub = + (struct google_protobuf_DescriptorProto*)_upb_Message_New(&google__protobuf__DescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_mutable_enum_type(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_EnumDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_resize_enum_type(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + return (google_protobuf_EnumDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_DescriptorProto_add_enum_type( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(26, 29), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_EnumDescriptorProto* sub = + (struct google_protobuf_EnumDescriptorProto*)_upb_Message_New(&google__protobuf__EnumDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_mutable_extension_range(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ExtensionRange_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_DescriptorProto_ExtensionRange**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_resize_extension_range(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ExtensionRange_msg_init); + return (google_protobuf_DescriptorProto_ExtensionRange**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_add_extension_range( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, UPB_SIZE(24, 28), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ExtensionRange_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_DescriptorProto_ExtensionRange* sub = + (struct google_protobuf_DescriptorProto_ExtensionRange*)_upb_Message_New(&google__protobuf__DescriptorProto__ExtensionRange_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_extension(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_FieldDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_extension(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + return (google_protobuf_FieldDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_extension( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {6, UPB_SIZE(28, 64), 0, UPB_SIZE(22, 27), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_FieldDescriptorProto* sub = + (struct google_protobuf_FieldDescriptorProto*)_upb_Message_New(&google__protobuf__FieldDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto* msg, google_protobuf_MessageOptions* value) { + const upb_MiniTableField field = {7, UPB_SIZE(32, 72), 65, UPB_SIZE(20, 26), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MessageOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_MessageOptions* google_protobuf_DescriptorProto_mutable_options( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_MessageOptions* sub = (struct google_protobuf_MessageOptions*)google_protobuf_DescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_MessageOptions*)_upb_Message_New(&google__protobuf__MessageOptions_msg_init, arena); + if (sub) google_protobuf_DescriptorProto_set_options(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_mutable_oneof_decl(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_OneofDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_resize_oneof_decl(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofDescriptorProto_msg_init); + return (google_protobuf_OneofDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_OneofDescriptorProto* google_protobuf_DescriptorProto_add_oneof_decl( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {8, UPB_SIZE(36, 80), 0, UPB_SIZE(18, 25), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_OneofDescriptorProto* sub = + (struct google_protobuf_OneofDescriptorProto*)_upb_Message_New(&google__protobuf__OneofDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_mutable_reserved_range(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ReservedRange_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_DescriptorProto_ReservedRange**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_resize_reserved_range(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ReservedRange_msg_init); + return (google_protobuf_DescriptorProto_ReservedRange**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_add_reserved_range( + google_protobuf_DescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {9, UPB_SIZE(40, 88), 0, UPB_SIZE(16, 24), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__DescriptorProto__ReservedRange_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_DescriptorProto_ReservedRange* sub = + (struct google_protobuf_DescriptorProto_ReservedRange*)_upb_Message_New(&google__protobuf__DescriptorProto__ReservedRange_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE upb_StringView* google_protobuf_DescriptorProto_mutable_reserved_name(google_protobuf_DescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* google_protobuf_DescriptorProto_resize_reserved_name(google_protobuf_DescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_DescriptorProto_add_reserved_name(google_protobuf_DescriptorProto* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void google_protobuf_DescriptorProto_set_visibility(google_protobuf_DescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {11, UPB_SIZE(48, 12), 66, UPB_SIZE(11, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.DescriptorProto.ExtensionRange */ +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_ExtensionRange_new(upb_Arena* arena) { + return (google_protobuf_DescriptorProto_ExtensionRange*)_upb_Message_New(&google__protobuf__DescriptorProto__ExtensionRange_msg_init, arena); +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_ExtensionRange_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_DescriptorProto_ExtensionRange* ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__DescriptorProto__ExtensionRange_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_ExtensionRange_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_DescriptorProto_ExtensionRange* ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__DescriptorProto__ExtensionRange_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ExtensionRange_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_DescriptorProto_ExtensionRange_serialize_ex(const google_protobuf_DescriptorProto_ExtensionRange* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ExtensionRange_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_clear_start(google_protobuf_DescriptorProto_ExtensionRange* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_clear_end(google_protobuf_DescriptorProto_ExtensionRange* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_clear_options(google_protobuf_DescriptorProto_ExtensionRange* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(20, 24), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange* msg) { + const google_protobuf_ExtensionRangeOptions* default_val = NULL; + const google_protobuf_ExtensionRangeOptions* ret; + const upb_MiniTableField field = {3, UPB_SIZE(20, 24), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(20, 24), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange* msg, int32_t value) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange* msg, int32_t value) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange* msg, google_protobuf_ExtensionRangeOptions* value) { + const upb_MiniTableField field = {3, UPB_SIZE(20, 24), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_mutable_options( + google_protobuf_DescriptorProto_ExtensionRange* msg, upb_Arena* arena) { + struct google_protobuf_ExtensionRangeOptions* sub = (struct google_protobuf_ExtensionRangeOptions*)google_protobuf_DescriptorProto_ExtensionRange_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_ExtensionRangeOptions*)_upb_Message_New(&google__protobuf__ExtensionRangeOptions_msg_init, arena); + if (sub) google_protobuf_DescriptorProto_ExtensionRange_set_options(msg, sub); + } + return sub; +} + +/* google.protobuf.DescriptorProto.ReservedRange */ +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_ReservedRange_new(upb_Arena* arena) { + return (google_protobuf_DescriptorProto_ReservedRange*)_upb_Message_New(&google__protobuf__DescriptorProto__ReservedRange_msg_init, arena); +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_ReservedRange_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_DescriptorProto_ReservedRange* ret = google_protobuf_DescriptorProto_ReservedRange_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__DescriptorProto__ReservedRange_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_ReservedRange_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_DescriptorProto_ReservedRange* ret = google_protobuf_DescriptorProto_ReservedRange_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__DescriptorProto__ReservedRange_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ReservedRange_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_DescriptorProto_ReservedRange_serialize_ex(const google_protobuf_DescriptorProto_ReservedRange* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__DescriptorProto__ReservedRange_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_clear_start(google_protobuf_DescriptorProto_ReservedRange* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_clear_end(google_protobuf_DescriptorProto_ReservedRange* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange* msg, int32_t value) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange* msg, int32_t value) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.ExtensionRangeOptions */ +UPB_INLINE google_protobuf_ExtensionRangeOptions* google_protobuf_ExtensionRangeOptions_new(upb_Arena* arena) { + return (google_protobuf_ExtensionRangeOptions*)_upb_Message_New(&google__protobuf__ExtensionRangeOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_ExtensionRangeOptions* google_protobuf_ExtensionRangeOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_ExtensionRangeOptions* ret = google_protobuf_ExtensionRangeOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ExtensionRangeOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_ExtensionRangeOptions* google_protobuf_ExtensionRangeOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_ExtensionRangeOptions* ret = google_protobuf_ExtensionRangeOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ExtensionRangeOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_ExtensionRangeOptions_serialize_ex(const google_protobuf_ExtensionRangeOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_clear_declaration(google_protobuf_ExtensionRangeOptions* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_ExtensionRangeOptions_Declaration* const* google_protobuf_ExtensionRangeOptions_declaration(const google_protobuf_ExtensionRangeOptions* msg, + size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_ExtensionRangeOptions_Declaration* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_ExtensionRangeOptions_declaration_upb_array( + const google_protobuf_ExtensionRangeOptions* msg, size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_ExtensionRangeOptions_declaration_mutable_upb_array( + google_protobuf_ExtensionRangeOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_clear_verification(google_protobuf_ExtensionRangeOptions* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 64, UPB_SIZE(10, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_ExtensionRangeOptions_verification(const google_protobuf_ExtensionRangeOptions* msg) { + int32_t default_val = 1; + int32_t ret; + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 64, UPB_SIZE(10, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_has_verification(const google_protobuf_ExtensionRangeOptions* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 64, UPB_SIZE(10, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_clear_features(google_protobuf_ExtensionRangeOptions* msg) { + const upb_MiniTableField field = {50, UPB_SIZE(20, 24), 65, UPB_SIZE(8, 10), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_ExtensionRangeOptions_features(const google_protobuf_ExtensionRangeOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {50, UPB_SIZE(20, 24), 65, UPB_SIZE(8, 10), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_has_features(const google_protobuf_ExtensionRangeOptions* msg) { + const upb_MiniTableField field = {50, UPB_SIZE(20, 24), 65, UPB_SIZE(8, 10), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_clear_uninterpreted_option(google_protobuf_ExtensionRangeOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_ExtensionRangeOptions_uninterpreted_option_upb_array( + const google_protobuf_ExtensionRangeOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_ExtensionRangeOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_ExtensionRangeOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE google_protobuf_ExtensionRangeOptions_Declaration** google_protobuf_ExtensionRangeOptions_mutable_declaration(google_protobuf_ExtensionRangeOptions* msg, + size_t* size) { + upb_MiniTableField field = {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_ExtensionRangeOptions_Declaration**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_ExtensionRangeOptions_Declaration** google_protobuf_ExtensionRangeOptions_resize_declaration(google_protobuf_ExtensionRangeOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init); + return (google_protobuf_ExtensionRangeOptions_Declaration**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_ExtensionRangeOptions_Declaration* google_protobuf_ExtensionRangeOptions_add_declaration( + google_protobuf_ExtensionRangeOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 16), 0, 12, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_ExtensionRangeOptions_Declaration* sub = + (struct google_protobuf_ExtensionRangeOptions_Declaration*)_upb_Message_New(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_set_verification(google_protobuf_ExtensionRangeOptions* msg, int32_t value) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 64, UPB_SIZE(10, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_set_features(google_protobuf_ExtensionRangeOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {50, UPB_SIZE(20, 24), 65, UPB_SIZE(8, 10), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_ExtensionRangeOptions_mutable_features( + google_protobuf_ExtensionRangeOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_ExtensionRangeOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_ExtensionRangeOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_mutable_uninterpreted_option(google_protobuf_ExtensionRangeOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_resize_uninterpreted_option(google_protobuf_ExtensionRangeOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ExtensionRangeOptions_add_uninterpreted_option( + google_protobuf_ExtensionRangeOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(24, 32), 0, UPB_SIZE(6, 9), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.ExtensionRangeOptions.Declaration */ +UPB_INLINE google_protobuf_ExtensionRangeOptions_Declaration* google_protobuf_ExtensionRangeOptions_Declaration_new(upb_Arena* arena) { + return (google_protobuf_ExtensionRangeOptions_Declaration*)_upb_Message_New(&google__protobuf__ExtensionRangeOptions__Declaration_msg_init, arena); +} +UPB_INLINE google_protobuf_ExtensionRangeOptions_Declaration* google_protobuf_ExtensionRangeOptions_Declaration_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_ExtensionRangeOptions_Declaration* ret = google_protobuf_ExtensionRangeOptions_Declaration_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_ExtensionRangeOptions_Declaration* google_protobuf_ExtensionRangeOptions_Declaration_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_ExtensionRangeOptions_Declaration* ret = google_protobuf_ExtensionRangeOptions_Declaration_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_ExtensionRangeOptions_Declaration_serialize(const google_protobuf_ExtensionRangeOptions_Declaration* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_ExtensionRangeOptions_Declaration_serialize_ex(const google_protobuf_ExtensionRangeOptions_Declaration* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ExtensionRangeOptions__Declaration_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_number(google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_ExtensionRangeOptions_Declaration_number(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_number(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_full_name(google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_ExtensionRangeOptions_Declaration_full_name(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_full_name(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_type(google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(24, 32), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_ExtensionRangeOptions_Declaration_type(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {3, UPB_SIZE(24, 32), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_type(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(24, 32), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_reserved(google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {5, 9, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_reserved(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {5, 9, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_reserved(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {5, 9, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_repeated(google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {6, 10, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_repeated(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {6, 10, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_repeated(const google_protobuf_ExtensionRangeOptions_Declaration* msg) { + const upb_MiniTableField field = {6, 10, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_number(google_protobuf_ExtensionRangeOptions_Declaration* msg, int32_t value) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_full_name(google_protobuf_ExtensionRangeOptions_Declaration* msg, upb_StringView value) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_type(google_protobuf_ExtensionRangeOptions_Declaration* msg, upb_StringView value) { + const upb_MiniTableField field = {3, UPB_SIZE(24, 32), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_reserved(google_protobuf_ExtensionRangeOptions_Declaration* msg, bool value) { + const upb_MiniTableField field = {5, 9, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_repeated(google_protobuf_ExtensionRangeOptions_Declaration* msg, bool value) { + const upb_MiniTableField field = {6, 10, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.FieldDescriptorProto */ +UPB_INLINE google_protobuf_FieldDescriptorProto* google_protobuf_FieldDescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_FieldDescriptorProto*)_upb_Message_New(&google__protobuf__FieldDescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_FieldDescriptorProto* google_protobuf_FieldDescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FieldDescriptorProto* ret = google_protobuf_FieldDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldDescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FieldDescriptorProto* google_protobuf_FieldDescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FieldDescriptorProto* ret = google_protobuf_FieldDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldDescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldDescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FieldDescriptorProto_serialize_ex(const google_protobuf_FieldDescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldDescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_name(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {1, 32, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, 32, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {1, 32, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_extendee(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(40, 48), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, UPB_SIZE(40, 48), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(40, 48), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_number(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {3, 12, 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {3, 12, 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {3, 12, 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_label(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {4, 16, 67, UPB_SIZE(24, 25), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto* msg) { + int32_t default_val = 1; + int32_t ret; + const upb_MiniTableField field = {4, 16, 67, UPB_SIZE(24, 25), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {4, 16, 67, UPB_SIZE(24, 25), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_type(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {5, 20, 68, UPB_SIZE(22, 24), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto* msg) { + int32_t default_val = 1; + int32_t ret; + const upb_MiniTableField field = {5, 20, 68, UPB_SIZE(22, 24), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {5, 20, 68, UPB_SIZE(22, 24), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_type_name(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(48, 64), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {6, UPB_SIZE(48, 64), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(48, 64), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_default_value(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(56, 80), 70, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {7, UPB_SIZE(56, 80), 70, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(56, 80), 70, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_options(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {8, UPB_SIZE(24, 112), 71, UPB_SIZE(14, 17), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto* msg) { + const google_protobuf_FieldOptions* default_val = NULL; + const google_protobuf_FieldOptions* ret; + const upb_MiniTableField field = {8, UPB_SIZE(24, 112), 71, UPB_SIZE(14, 17), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {8, UPB_SIZE(24, 112), 71, UPB_SIZE(14, 17), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_oneof_index(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {9, UPB_SIZE(28, 24), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {9, UPB_SIZE(28, 24), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {9, UPB_SIZE(28, 24), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_json_name(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {10, UPB_SIZE(64, 96), 73, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {10, UPB_SIZE(64, 96), 73, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {10, UPB_SIZE(64, 96), 73, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_clear_proto3_optional(google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {17, 10, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_proto3_optional(const google_protobuf_FieldDescriptorProto* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {17, 10, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_proto3_optional(const google_protobuf_FieldDescriptorProto* msg) { + const upb_MiniTableField field = {17, 10, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, 32, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {2, UPB_SIZE(40, 48), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {3, 12, 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {4, 16, 67, UPB_SIZE(24, 25), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {5, 20, 68, UPB_SIZE(22, 24), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {6, UPB_SIZE(48, 64), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {7, UPB_SIZE(56, 80), 70, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto* msg, google_protobuf_FieldOptions* value) { + const upb_MiniTableField field = {8, UPB_SIZE(24, 112), 71, UPB_SIZE(14, 17), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_mutable_options( + google_protobuf_FieldDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_FieldOptions* sub = (struct google_protobuf_FieldOptions*)google_protobuf_FieldDescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FieldOptions*)_upb_Message_New(&google__protobuf__FieldOptions_msg_init, arena); + if (sub) google_protobuf_FieldDescriptorProto_set_options(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {9, UPB_SIZE(28, 24), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {10, UPB_SIZE(64, 96), 73, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldDescriptorProto_set_proto3_optional(google_protobuf_FieldDescriptorProto* msg, bool value) { + const upb_MiniTableField field = {17, 10, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.OneofDescriptorProto */ +UPB_INLINE google_protobuf_OneofDescriptorProto* google_protobuf_OneofDescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_OneofDescriptorProto*)_upb_Message_New(&google__protobuf__OneofDescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_OneofDescriptorProto* google_protobuf_OneofDescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_OneofDescriptorProto* ret = google_protobuf_OneofDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__OneofDescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_OneofDescriptorProto* google_protobuf_OneofDescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_OneofDescriptorProto* ret = google_protobuf_OneofDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__OneofDescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofDescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_OneofDescriptorProto_serialize_ex(const google_protobuf_OneofDescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofDescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_OneofDescriptorProto_clear_name(google_protobuf_OneofDescriptorProto* msg) { + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto* msg) { + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_OneofDescriptorProto_clear_options(google_protobuf_OneofDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 65, 3, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto* msg) { + const google_protobuf_OneofOptions* default_val = NULL; + const google_protobuf_OneofOptions* ret; + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 65, 3, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 65, 3, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto* msg, google_protobuf_OneofOptions* value) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 65, 3, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__OneofOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_mutable_options( + google_protobuf_OneofDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_OneofOptions* sub = (struct google_protobuf_OneofOptions*)google_protobuf_OneofDescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_OneofOptions*)_upb_Message_New(&google__protobuf__OneofOptions_msg_init, arena); + if (sub) google_protobuf_OneofDescriptorProto_set_options(msg, sub); + } + return sub; +} + +/* google.protobuf.EnumDescriptorProto */ +UPB_INLINE google_protobuf_EnumDescriptorProto* google_protobuf_EnumDescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_EnumDescriptorProto*)_upb_Message_New(&google__protobuf__EnumDescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_EnumDescriptorProto* google_protobuf_EnumDescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_EnumDescriptorProto* ret = google_protobuf_EnumDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumDescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_EnumDescriptorProto* google_protobuf_EnumDescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_EnumDescriptorProto* ret = google_protobuf_EnumDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumDescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_EnumDescriptorProto_serialize_ex(const google_protobuf_EnumDescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_clear_name(google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(32, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(32, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(32, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_clear_value(google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_EnumValueDescriptorProto* const* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_EnumValueDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_EnumDescriptorProto_value_upb_array( + const google_protobuf_EnumDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_EnumDescriptorProto_value_mutable_upb_array( + google_protobuf_EnumDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_clear_options(google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(13, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto* msg) { + const google_protobuf_EnumOptions* default_val = NULL; + const google_protobuf_EnumOptions* ret; + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(13, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(13, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_clear_reserved_range(google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_EnumDescriptorProto_EnumReservedRange* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_EnumDescriptorProto_reserved_range_upb_array( + const google_protobuf_EnumDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_EnumDescriptorProto_reserved_range_mutable_upb_array( + google_protobuf_EnumDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_clear_reserved_name(google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_EnumDescriptorProto_reserved_name_upb_array( + const google_protobuf_EnumDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_EnumDescriptorProto_reserved_name_mutable_upb_array( + google_protobuf_EnumDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_clear_visibility(google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 12), 66, UPB_SIZE(6, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_visibility(const google_protobuf_EnumDescriptorProto* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {6, UPB_SIZE(28, 12), 66, UPB_SIZE(6, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_visibility(const google_protobuf_EnumDescriptorProto* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 12), 66, UPB_SIZE(6, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(32, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_mutable_value(google_protobuf_EnumDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_EnumValueDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_resize_value(google_protobuf_EnumDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueDescriptorProto_msg_init); + return (google_protobuf_EnumValueDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumDescriptorProto_add_value( + google_protobuf_EnumDescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, 15, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_EnumValueDescriptorProto* sub = + (struct google_protobuf_EnumValueDescriptorProto*)_upb_Message_New(&google__protobuf__EnumValueDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto* msg, google_protobuf_EnumOptions* value) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(13, 14), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_mutable_options( + google_protobuf_EnumDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_EnumOptions* sub = (struct google_protobuf_EnumOptions*)google_protobuf_EnumDescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_EnumOptions*)_upb_Message_New(&google__protobuf__EnumOptions_msg_init, arena); + if (sub) google_protobuf_EnumDescriptorProto_set_options(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_mutable_reserved_range(google_protobuf_EnumDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_resize_reserved_range(google_protobuf_EnumDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init); + return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_add_reserved_range( + google_protobuf_EnumDescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {4, UPB_SIZE(20, 48), 0, UPB_SIZE(11, 13), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_EnumDescriptorProto_EnumReservedRange* sub = + (struct google_protobuf_EnumDescriptorProto_EnumReservedRange*)_upb_Message_New(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE upb_StringView* google_protobuf_EnumDescriptorProto_mutable_reserved_name(google_protobuf_EnumDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* google_protobuf_EnumDescriptorProto_resize_reserved_name(google_protobuf_EnumDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_EnumDescriptorProto_add_reserved_name(google_protobuf_EnumDescriptorProto* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_set_visibility(google_protobuf_EnumDescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {6, UPB_SIZE(28, 12), 66, UPB_SIZE(6, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.EnumDescriptorProto.EnumReservedRange */ +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_Arena* arena) { + return (google_protobuf_EnumDescriptorProto_EnumReservedRange*)_upb_Message_New(&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, arena); +} +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_EnumReservedRange_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_EnumDescriptorProto_EnumReservedRange* ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_EnumReservedRange_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_EnumDescriptorProto_EnumReservedRange* ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize_ex(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_clear_start(google_protobuf_EnumDescriptorProto_EnumReservedRange* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_clear_end(google_protobuf_EnumDescriptorProto_EnumReservedRange* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange* msg) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange* msg, int32_t value) { + const upb_MiniTableField field = {1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange* msg, int32_t value) { + const upb_MiniTableField field = {2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.EnumValueDescriptorProto */ +UPB_INLINE google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumValueDescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_EnumValueDescriptorProto*)_upb_Message_New(&google__protobuf__EnumValueDescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumValueDescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_EnumValueDescriptorProto* ret = google_protobuf_EnumValueDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumValueDescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumValueDescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_EnumValueDescriptorProto* ret = google_protobuf_EnumValueDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumValueDescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueDescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_EnumValueDescriptorProto_serialize_ex(const google_protobuf_EnumValueDescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueDescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_clear_name(google_protobuf_EnumValueDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_clear_number(google_protobuf_EnumValueDescriptorProto* msg) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto* msg) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_clear_options(google_protobuf_EnumValueDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 32), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto* msg) { + const google_protobuf_EnumValueOptions* default_val = NULL; + const google_protobuf_EnumValueOptions* ret; + const upb_MiniTableField field = {3, UPB_SIZE(16, 32), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 32), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto* msg, int32_t value) { + const upb_MiniTableField field = {2, 12, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto* msg, google_protobuf_EnumValueOptions* value) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 32), 66, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__EnumValueOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_mutable_options( + google_protobuf_EnumValueDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_EnumValueOptions* sub = (struct google_protobuf_EnumValueOptions*)google_protobuf_EnumValueDescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_EnumValueOptions*)_upb_Message_New(&google__protobuf__EnumValueOptions_msg_init, arena); + if (sub) google_protobuf_EnumValueDescriptorProto_set_options(msg, sub); + } + return sub; +} + +/* google.protobuf.ServiceDescriptorProto */ +UPB_INLINE google_protobuf_ServiceDescriptorProto* google_protobuf_ServiceDescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_ServiceDescriptorProto*)_upb_Message_New(&google__protobuf__ServiceDescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_ServiceDescriptorProto* google_protobuf_ServiceDescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_ServiceDescriptorProto* ret = google_protobuf_ServiceDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ServiceDescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_ServiceDescriptorProto* google_protobuf_ServiceDescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_ServiceDescriptorProto* ret = google_protobuf_ServiceDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ServiceDescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceDescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_ServiceDescriptorProto_serialize_ex(const google_protobuf_ServiceDescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceDescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_ServiceDescriptorProto_clear_name(google_protobuf_ServiceDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ServiceDescriptorProto_clear_method(google_protobuf_ServiceDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_MethodDescriptorProto* const* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto* msg, + size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_MethodDescriptorProto* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_ServiceDescriptorProto_method_upb_array( + const google_protobuf_ServiceDescriptorProto* msg, size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodDescriptorProto_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_ServiceDescriptorProto_method_mutable_upb_array( + google_protobuf_ServiceDescriptorProto* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_ServiceDescriptorProto_clear_options(google_protobuf_ServiceDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto* msg) { + const google_protobuf_ServiceOptions* default_val = NULL; + const google_protobuf_ServiceOptions* ret; + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_mutable_method(google_protobuf_ServiceDescriptorProto* msg, + size_t* size) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_MethodDescriptorProto**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_resize_method(google_protobuf_ServiceDescriptorProto* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodDescriptorProto_msg_init); + return (google_protobuf_MethodDescriptorProto**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_MethodDescriptorProto* google_protobuf_ServiceDescriptorProto_add_method( + google_protobuf_ServiceDescriptorProto* msg, upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 32), 0, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodDescriptorProto_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_MethodDescriptorProto* sub = + (struct google_protobuf_MethodDescriptorProto*)_upb_Message_New(&google__protobuf__MethodDescriptorProto_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto* msg, google_protobuf_ServiceOptions* value) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 40), 65, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__ServiceOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_mutable_options( + google_protobuf_ServiceDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_ServiceOptions* sub = (struct google_protobuf_ServiceOptions*)google_protobuf_ServiceDescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_ServiceOptions*)_upb_Message_New(&google__protobuf__ServiceOptions_msg_init, arena); + if (sub) google_protobuf_ServiceDescriptorProto_set_options(msg, sub); + } + return sub; +} + +/* google.protobuf.MethodDescriptorProto */ +UPB_INLINE google_protobuf_MethodDescriptorProto* google_protobuf_MethodDescriptorProto_new(upb_Arena* arena) { + return (google_protobuf_MethodDescriptorProto*)_upb_Message_New(&google__protobuf__MethodDescriptorProto_msg_init, arena); +} +UPB_INLINE google_protobuf_MethodDescriptorProto* google_protobuf_MethodDescriptorProto_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_MethodDescriptorProto* ret = google_protobuf_MethodDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__MethodDescriptorProto_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_MethodDescriptorProto* google_protobuf_MethodDescriptorProto_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_MethodDescriptorProto* ret = google_protobuf_MethodDescriptorProto_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__MethodDescriptorProto_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodDescriptorProto_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_MethodDescriptorProto_serialize_ex(const google_protobuf_MethodDescriptorProto* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodDescriptorProto_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_clear_name(google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_clear_input_type(google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(24, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, UPB_SIZE(24, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(24, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_clear_output_type(google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 48), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {3, UPB_SIZE(32, 48), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 48), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_clear_options(google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(12, 64), 67, 9, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto* msg) { + const google_protobuf_MethodOptions* default_val = NULL; + const google_protobuf_MethodOptions* ret; + const upb_MiniTableField field = {4, UPB_SIZE(12, 64), 67, 9, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodOptions_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(12, 64), 67, 9, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_clear_client_streaming(google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {5, 9, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {5, 9, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {5, 9, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_clear_server_streaming(google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {6, 10, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {6, 10, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto* msg) { + const upb_MiniTableField field = {6, 10, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {2, UPB_SIZE(24, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto* msg, upb_StringView value) { + const upb_MiniTableField field = {3, UPB_SIZE(32, 48), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto* msg, google_protobuf_MethodOptions* value) { + const upb_MiniTableField field = {4, UPB_SIZE(12, 64), 67, 9, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__MethodOptions_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_mutable_options( + google_protobuf_MethodDescriptorProto* msg, upb_Arena* arena) { + struct google_protobuf_MethodOptions* sub = (struct google_protobuf_MethodOptions*)google_protobuf_MethodDescriptorProto_options(msg); + if (sub == NULL) { + sub = (struct google_protobuf_MethodOptions*)_upb_Message_New(&google__protobuf__MethodOptions_msg_init, arena); + if (sub) google_protobuf_MethodDescriptorProto_set_options(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto* msg, bool value) { + const upb_MiniTableField field = {5, 9, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto* msg, bool value) { + const upb_MiniTableField field = {6, 10, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.FileOptions */ +UPB_INLINE google_protobuf_FileOptions* google_protobuf_FileOptions_new(upb_Arena* arena) { + return (google_protobuf_FileOptions*)_upb_Message_New(&google__protobuf__FileOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_FileOptions* google_protobuf_FileOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FileOptions* ret = google_protobuf_FileOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FileOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FileOptions* google_protobuf_FileOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FileOptions* ret = google_protobuf_FileOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FileOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FileOptions_serialize_ex(const google_protobuf_FileOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FileOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FileOptions_clear_java_package(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(32, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(32, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(32, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_java_outer_classname(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {8, 40, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {8, 40, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {8, 40, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_optimize_for(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {9, 20, 66, UPB_SIZE(57, 58), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions* msg) { + int32_t default_val = 1; + int32_t ret; + const upb_MiniTableField field = {9, 20, 66, UPB_SIZE(57, 58), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {9, 20, 66, UPB_SIZE(57, 58), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_java_multiple_files(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {10, 11, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {10, 11, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {10, 11, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_go_package(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {11, UPB_SIZE(48, 56), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {11, UPB_SIZE(48, 56), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {11, UPB_SIZE(48, 56), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_cc_generic_services(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {16, 12, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {16, 12, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {16, 12, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_java_generic_services(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {17, 13, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {17, 13, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {17, 13, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_py_generic_services(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {18, 14, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {18, 14, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {18, 14, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_java_generate_equals_and_hash(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {20, 15, 72, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {20, 15, 72, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {20, 15, 72, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_deprecated(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {23, 16, 73, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {23, 16, 73, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {23, 16, 73, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_java_string_check_utf8(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {27, 17, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {27, 17, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {27, 17, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_cc_enable_arenas(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {31, 18, 75, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions* msg) { + bool default_val = true; + bool ret; + const upb_MiniTableField field = {31, 18, 75, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {31, 18, 75, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_objc_class_prefix(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {36, UPB_SIZE(56, 72), 76, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {36, UPB_SIZE(56, 72), 76, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {36, UPB_SIZE(56, 72), 76, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_csharp_namespace(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {37, UPB_SIZE(64, 88), 77, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {37, UPB_SIZE(64, 88), 77, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {37, UPB_SIZE(64, 88), 77, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_swift_prefix(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {39, UPB_SIZE(72, 104), 78, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {39, UPB_SIZE(72, 104), 78, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {39, UPB_SIZE(72, 104), 78, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_php_class_prefix(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {40, UPB_SIZE(80, 120), 79, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {40, UPB_SIZE(80, 120), 79, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {40, UPB_SIZE(80, 120), 79, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_php_namespace(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {41, UPB_SIZE(88, 136), 80, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {41, UPB_SIZE(88, 136), 80, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {41, UPB_SIZE(88, 136), 80, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_php_metadata_namespace(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {44, UPB_SIZE(96, 152), 81, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_php_metadata_namespace(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {44, UPB_SIZE(96, 152), 81, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_php_metadata_namespace(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {44, UPB_SIZE(96, 152), 81, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_ruby_package(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {45, UPB_SIZE(104, 168), 82, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FileOptions_ruby_package(const google_protobuf_FileOptions* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {45, UPB_SIZE(104, 168), 82, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_ruby_package(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {45, UPB_SIZE(104, 168), 82, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_features(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {50, UPB_SIZE(24, 184), 83, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_FileOptions_features(const google_protobuf_FileOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {50, UPB_SIZE(24, 184), 83, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FileOptions_has_features(const google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {50, UPB_SIZE(24, 184), 83, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FileOptions_clear_uninterpreted_option(google_protobuf_FileOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FileOptions_uninterpreted_option_upb_array( + const google_protobuf_FileOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FileOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_FileOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(32, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {8, 40, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions* msg, int32_t value) { + const upb_MiniTableField field = {9, 20, 66, UPB_SIZE(57, 58), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {10, 11, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {11, UPB_SIZE(48, 56), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {16, 12, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {17, 13, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {18, 14, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {20, 15, 72, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {23, 16, 73, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {27, 17, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions* msg, bool value) { + const upb_MiniTableField field = {31, 18, 75, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {36, UPB_SIZE(56, 72), 76, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {37, UPB_SIZE(64, 88), 77, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {39, UPB_SIZE(72, 104), 78, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {40, UPB_SIZE(80, 120), 79, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {41, UPB_SIZE(88, 136), 80, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_php_metadata_namespace(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {44, UPB_SIZE(96, 152), 81, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_ruby_package(google_protobuf_FileOptions* msg, upb_StringView value) { + const upb_MiniTableField field = {45, UPB_SIZE(104, 168), 82, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FileOptions_set_features(google_protobuf_FileOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {50, UPB_SIZE(24, 184), 83, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_FileOptions_mutable_features( + google_protobuf_FileOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_FileOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_FileOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_mutable_uninterpreted_option(google_protobuf_FileOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_resize_uninterpreted_option(google_protobuf_FileOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FileOptions_add_uninterpreted_option( + google_protobuf_FileOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(28, 192), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.MessageOptions */ +UPB_INLINE google_protobuf_MessageOptions* google_protobuf_MessageOptions_new(upb_Arena* arena) { + return (google_protobuf_MessageOptions*)_upb_Message_New(&google__protobuf__MessageOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_MessageOptions* google_protobuf_MessageOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_MessageOptions* ret = google_protobuf_MessageOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__MessageOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_MessageOptions* google_protobuf_MessageOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_MessageOptions* ret = google_protobuf_MessageOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__MessageOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MessageOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_MessageOptions_serialize_ex(const google_protobuf_MessageOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MessageOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_MessageOptions_clear_message_set_wire_format(google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MessageOptions_clear_no_standard_descriptor_accessor(google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MessageOptions_clear_deprecated(google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MessageOptions_clear_map_entry(google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {7, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {7, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {7, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MessageOptions_clear_deprecated_legacy_json_field_conflicts(google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {11, 13, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MessageOptions_deprecated_legacy_json_field_conflicts(const google_protobuf_MessageOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {11, 13, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated_legacy_json_field_conflicts(const google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {11, 13, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MessageOptions_clear_features(google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {12, 16, 69, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_MessageOptions_features(const google_protobuf_MessageOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {12, 16, 69, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MessageOptions_has_features(const google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {12, 16, 69, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MessageOptions_clear_uninterpreted_option(google_protobuf_MessageOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_MessageOptions_uninterpreted_option_upb_array( + const google_protobuf_MessageOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_MessageOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_MessageOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions* msg, bool value) { + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions* msg, bool value) { + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions* msg, bool value) { + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions* msg, bool value) { + const upb_MiniTableField field = {7, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MessageOptions_set_deprecated_legacy_json_field_conflicts(google_protobuf_MessageOptions* msg, bool value) { + const upb_MiniTableField field = {11, 13, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MessageOptions_set_features(google_protobuf_MessageOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {12, 16, 69, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_MessageOptions_mutable_features( + google_protobuf_MessageOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_MessageOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_MessageOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_mutable_uninterpreted_option(google_protobuf_MessageOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_resize_uninterpreted_option(google_protobuf_MessageOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MessageOptions_add_uninterpreted_option( + google_protobuf_MessageOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.FieldOptions */ +UPB_INLINE google_protobuf_FieldOptions* google_protobuf_FieldOptions_new(upb_Arena* arena) { + return (google_protobuf_FieldOptions*)_upb_Message_New(&google__protobuf__FieldOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_FieldOptions* google_protobuf_FieldOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FieldOptions* ret = google_protobuf_FieldOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FieldOptions* google_protobuf_FieldOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FieldOptions* ret = google_protobuf_FieldOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FieldOptions_serialize_ex(const google_protobuf_FieldOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FieldOptions_clear_ctype(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {1, 16, 64, 42, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {1, 16, 64, 42, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {1, 16, 64, 42, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_packed(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_deprecated(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_lazy(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {5, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {5, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {5, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_jstype(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {6, 20, 68, UPB_SIZE(31, 32), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {6, 20, 68, UPB_SIZE(31, 32), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {6, 20, 68, UPB_SIZE(31, 32), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_weak(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {10, 13, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {10, 13, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {10, 13, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_unverified_lazy(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {15, 14, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FieldOptions_unverified_lazy(const google_protobuf_FieldOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {15, 14, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_unverified_lazy(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {15, 14, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_debug_redact(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {16, 15, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_FieldOptions_debug_redact(const google_protobuf_FieldOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {16, 15, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_debug_redact(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {16, 15, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_retention(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {17, 24, 72, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldOptions_retention(const google_protobuf_FieldOptions* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {17, 24, 72, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_retention(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {17, 24, 72, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_targets(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t const* google_protobuf_FieldOptions_targets(const google_protobuf_FieldOptions* msg, + size_t* size) { + const upb_MiniTableField field = {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FieldOptions_targets_upb_array( + const google_protobuf_FieldOptions* msg, size_t* size) { + const upb_MiniTableField field = {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FieldOptions_targets_mutable_upb_array( + google_protobuf_FieldOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FieldOptions_clear_edition_defaults(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FieldOptions_EditionDefault* const* google_protobuf_FieldOptions_edition_defaults(const google_protobuf_FieldOptions* msg, + size_t* size) { + const upb_MiniTableField field = {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__EditionDefault_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_FieldOptions_EditionDefault* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FieldOptions_edition_defaults_upb_array( + const google_protobuf_FieldOptions* msg, size_t* size) { + const upb_MiniTableField field = {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__EditionDefault_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FieldOptions_edition_defaults_mutable_upb_array( + google_protobuf_FieldOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__EditionDefault_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FieldOptions_clear_features(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {21, UPB_SIZE(36, 48), 73, UPB_SIZE(14, 19), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_FieldOptions_features(const google_protobuf_FieldOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {21, UPB_SIZE(36, 48), 73, UPB_SIZE(14, 19), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_features(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {21, UPB_SIZE(36, 48), 73, UPB_SIZE(14, 19), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_feature_support(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {22, UPB_SIZE(40, 56), 74, UPB_SIZE(12, 18), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FieldOptions_FeatureSupport* google_protobuf_FieldOptions_feature_support(const google_protobuf_FieldOptions* msg) { + const google_protobuf_FieldOptions_FeatureSupport* default_val = NULL; + const google_protobuf_FieldOptions_FeatureSupport* ret; + const upb_MiniTableField field = {22, UPB_SIZE(40, 56), 74, UPB_SIZE(12, 18), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__FeatureSupport_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_has_feature_support(const google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {22, UPB_SIZE(40, 56), 74, UPB_SIZE(12, 18), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_clear_uninterpreted_option(google_protobuf_FieldOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FieldOptions_uninterpreted_option_upb_array( + const google_protobuf_FieldOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FieldOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_FieldOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions* msg, int32_t value) { + const upb_MiniTableField field = {1, 16, 64, 42, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions* msg, bool value) { + const upb_MiniTableField field = {2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions* msg, bool value) { + const upb_MiniTableField field = {3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions* msg, bool value) { + const upb_MiniTableField field = {5, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions* msg, int32_t value) { + const upb_MiniTableField field = {6, 20, 68, UPB_SIZE(31, 32), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions* msg, bool value) { + const upb_MiniTableField field = {10, 13, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_unverified_lazy(google_protobuf_FieldOptions* msg, bool value) { + const upb_MiniTableField field = {15, 14, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_debug_redact(google_protobuf_FieldOptions* msg, bool value) { + const upb_MiniTableField field = {16, 15, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_set_retention(google_protobuf_FieldOptions* msg, int32_t value) { + const upb_MiniTableField field = {17, 24, 72, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE int32_t* google_protobuf_FieldOptions_mutable_targets(google_protobuf_FieldOptions* msg, + size_t* size) { + upb_MiniTableField field = {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE int32_t* google_protobuf_FieldOptions_resize_targets(google_protobuf_FieldOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (int32_t*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_FieldOptions_add_targets(google_protobuf_FieldOptions* msg, int32_t val, + upb_Arena* arena) { + upb_MiniTableField field = {19, UPB_SIZE(28, 32), 0, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE google_protobuf_FieldOptions_EditionDefault** google_protobuf_FieldOptions_mutable_edition_defaults(google_protobuf_FieldOptions* msg, + size_t* size) { + upb_MiniTableField field = {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__EditionDefault_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_FieldOptions_EditionDefault**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_FieldOptions_EditionDefault** google_protobuf_FieldOptions_resize_edition_defaults(google_protobuf_FieldOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__EditionDefault_msg_init); + return (google_protobuf_FieldOptions_EditionDefault**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_FieldOptions_EditionDefault* google_protobuf_FieldOptions_add_edition_defaults( + google_protobuf_FieldOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {20, UPB_SIZE(32, 40), 0, UPB_SIZE(16, 20), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__EditionDefault_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_FieldOptions_EditionDefault* sub = + (struct google_protobuf_FieldOptions_EditionDefault*)_upb_Message_New(&google__protobuf__FieldOptions__EditionDefault_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_FieldOptions_set_features(google_protobuf_FieldOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {21, UPB_SIZE(36, 48), 73, UPB_SIZE(14, 19), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_FieldOptions_mutable_features( + google_protobuf_FieldOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_FieldOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_FieldOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_FieldOptions_set_feature_support(google_protobuf_FieldOptions* msg, google_protobuf_FieldOptions_FeatureSupport* value) { + const upb_MiniTableField field = {22, UPB_SIZE(40, 56), 74, UPB_SIZE(12, 18), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__FeatureSupport_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FieldOptions_FeatureSupport* google_protobuf_FieldOptions_mutable_feature_support( + google_protobuf_FieldOptions* msg, upb_Arena* arena) { + struct google_protobuf_FieldOptions_FeatureSupport* sub = (struct google_protobuf_FieldOptions_FeatureSupport*)google_protobuf_FieldOptions_feature_support(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FieldOptions_FeatureSupport*)_upb_Message_New(&google__protobuf__FieldOptions__FeatureSupport_msg_init, arena); + if (sub) google_protobuf_FieldOptions_set_feature_support(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_mutable_uninterpreted_option(google_protobuf_FieldOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_resize_uninterpreted_option(google_protobuf_FieldOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FieldOptions_add_uninterpreted_option( + google_protobuf_FieldOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(44, 64), 0, UPB_SIZE(10, 17), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.FieldOptions.EditionDefault */ +UPB_INLINE google_protobuf_FieldOptions_EditionDefault* google_protobuf_FieldOptions_EditionDefault_new(upb_Arena* arena) { + return (google_protobuf_FieldOptions_EditionDefault*)_upb_Message_New(&google__protobuf__FieldOptions__EditionDefault_msg_init, arena); +} +UPB_INLINE google_protobuf_FieldOptions_EditionDefault* google_protobuf_FieldOptions_EditionDefault_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FieldOptions_EditionDefault* ret = google_protobuf_FieldOptions_EditionDefault_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldOptions__EditionDefault_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FieldOptions_EditionDefault* google_protobuf_FieldOptions_EditionDefault_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FieldOptions_EditionDefault* ret = google_protobuf_FieldOptions_EditionDefault_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldOptions__EditionDefault_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FieldOptions_EditionDefault_serialize(const google_protobuf_FieldOptions_EditionDefault* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__EditionDefault_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FieldOptions_EditionDefault_serialize_ex(const google_protobuf_FieldOptions_EditionDefault* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__EditionDefault_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FieldOptions_EditionDefault_clear_value(google_protobuf_FieldOptions_EditionDefault* msg) { + const upb_MiniTableField field = {2, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldOptions_EditionDefault_value(const google_protobuf_FieldOptions_EditionDefault* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_EditionDefault_has_value(const google_protobuf_FieldOptions_EditionDefault* msg) { + const upb_MiniTableField field = {2, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_EditionDefault_clear_edition(google_protobuf_FieldOptions_EditionDefault* msg) { + const upb_MiniTableField field = {3, 12, 65, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldOptions_EditionDefault_edition(const google_protobuf_FieldOptions_EditionDefault* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {3, 12, 65, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_EditionDefault_has_edition(const google_protobuf_FieldOptions_EditionDefault* msg) { + const upb_MiniTableField field = {3, 12, 65, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_FieldOptions_EditionDefault_set_value(google_protobuf_FieldOptions_EditionDefault* msg, upb_StringView value) { + const upb_MiniTableField field = {2, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_EditionDefault_set_edition(google_protobuf_FieldOptions_EditionDefault* msg, int32_t value) { + const upb_MiniTableField field = {3, 12, 65, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.FieldOptions.FeatureSupport */ +UPB_INLINE google_protobuf_FieldOptions_FeatureSupport* google_protobuf_FieldOptions_FeatureSupport_new(upb_Arena* arena) { + return (google_protobuf_FieldOptions_FeatureSupport*)_upb_Message_New(&google__protobuf__FieldOptions__FeatureSupport_msg_init, arena); +} +UPB_INLINE google_protobuf_FieldOptions_FeatureSupport* google_protobuf_FieldOptions_FeatureSupport_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FieldOptions_FeatureSupport* ret = google_protobuf_FieldOptions_FeatureSupport_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldOptions__FeatureSupport_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FieldOptions_FeatureSupport* google_protobuf_FieldOptions_FeatureSupport_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FieldOptions_FeatureSupport* ret = google_protobuf_FieldOptions_FeatureSupport_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FieldOptions__FeatureSupport_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FieldOptions_FeatureSupport_serialize(const google_protobuf_FieldOptions_FeatureSupport* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__FeatureSupport_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FieldOptions_FeatureSupport_serialize_ex(const google_protobuf_FieldOptions_FeatureSupport* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FieldOptions__FeatureSupport_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_clear_edition_introduced(google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {1, 12, 64, UPB_SIZE(15, 16), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldOptions_FeatureSupport_edition_introduced(const google_protobuf_FieldOptions_FeatureSupport* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {1, 12, 64, UPB_SIZE(15, 16), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_FeatureSupport_has_edition_introduced(const google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {1, 12, 64, UPB_SIZE(15, 16), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_clear_edition_deprecated(google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(13, 15), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldOptions_FeatureSupport_edition_deprecated(const google_protobuf_FieldOptions_FeatureSupport* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(13, 15), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_FeatureSupport_has_edition_deprecated(const google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(13, 15), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_clear_deprecation_warning(google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {3, 24, 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldOptions_FeatureSupport_deprecation_warning(const google_protobuf_FieldOptions_FeatureSupport* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {3, 24, 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_FeatureSupport_has_deprecation_warning(const google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {3, 24, 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_clear_edition_removed(google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {4, 20, 67, UPB_SIZE(8, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FieldOptions_FeatureSupport_edition_removed(const google_protobuf_FieldOptions_FeatureSupport* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {4, 20, 67, UPB_SIZE(8, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_FeatureSupport_has_edition_removed(const google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {4, 20, 67, UPB_SIZE(8, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_clear_removal_error(google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(32, 40), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_FieldOptions_FeatureSupport_removal_error(const google_protobuf_FieldOptions_FeatureSupport* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {5, UPB_SIZE(32, 40), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FieldOptions_FeatureSupport_has_removal_error(const google_protobuf_FieldOptions_FeatureSupport* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(32, 40), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_set_edition_introduced(google_protobuf_FieldOptions_FeatureSupport* msg, int32_t value) { + const upb_MiniTableField field = {1, 12, 64, UPB_SIZE(15, 16), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_set_edition_deprecated(google_protobuf_FieldOptions_FeatureSupport* msg, int32_t value) { + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(13, 15), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_set_deprecation_warning(google_protobuf_FieldOptions_FeatureSupport* msg, upb_StringView value) { + const upb_MiniTableField field = {3, 24, 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_set_edition_removed(google_protobuf_FieldOptions_FeatureSupport* msg, int32_t value) { + const upb_MiniTableField field = {4, 20, 67, UPB_SIZE(8, 11), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FieldOptions_FeatureSupport_set_removal_error(google_protobuf_FieldOptions_FeatureSupport* msg, upb_StringView value) { + const upb_MiniTableField field = {5, UPB_SIZE(32, 40), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.OneofOptions */ +UPB_INLINE google_protobuf_OneofOptions* google_protobuf_OneofOptions_new(upb_Arena* arena) { + return (google_protobuf_OneofOptions*)_upb_Message_New(&google__protobuf__OneofOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_OneofOptions* google_protobuf_OneofOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_OneofOptions* ret = google_protobuf_OneofOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__OneofOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_OneofOptions* google_protobuf_OneofOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_OneofOptions* ret = google_protobuf_OneofOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__OneofOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_OneofOptions_serialize_ex(const google_protobuf_OneofOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__OneofOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_OneofOptions_clear_features(google_protobuf_OneofOptions* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, 6, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_OneofOptions_features(const google_protobuf_OneofOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, 6, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_OneofOptions_has_features(const google_protobuf_OneofOptions* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, 6, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_OneofOptions_clear_uninterpreted_option(google_protobuf_OneofOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_OneofOptions_uninterpreted_option_upb_array( + const google_protobuf_OneofOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_OneofOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_OneofOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_OneofOptions_set_features(google_protobuf_OneofOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, 6, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_OneofOptions_mutable_features( + google_protobuf_OneofOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_OneofOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_OneofOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_mutable_uninterpreted_option(google_protobuf_OneofOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_resize_uninterpreted_option(google_protobuf_OneofOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_OneofOptions_add_uninterpreted_option( + google_protobuf_OneofOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 5), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.EnumOptions */ +UPB_INLINE google_protobuf_EnumOptions* google_protobuf_EnumOptions_new(upb_Arena* arena) { + return (google_protobuf_EnumOptions*)_upb_Message_New(&google__protobuf__EnumOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_EnumOptions* google_protobuf_EnumOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_EnumOptions* ret = google_protobuf_EnumOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_EnumOptions* google_protobuf_EnumOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_EnumOptions* ret = google_protobuf_EnumOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_EnumOptions_serialize_ex(const google_protobuf_EnumOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_EnumOptions_clear_allow_alias(google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {2, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {2, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {2, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumOptions_clear_deprecated(google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {3, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {3, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {3, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumOptions_clear_deprecated_legacy_json_field_conflicts(google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {6, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_EnumOptions_deprecated_legacy_json_field_conflicts(const google_protobuf_EnumOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {6, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated_legacy_json_field_conflicts(const google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {6, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumOptions_clear_features(google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(12, 16), 67, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_EnumOptions_features(const google_protobuf_EnumOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {7, UPB_SIZE(12, 16), 67, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumOptions_has_features(const google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(12, 16), 67, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumOptions_clear_uninterpreted_option(google_protobuf_EnumOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_EnumOptions_uninterpreted_option_upb_array( + const google_protobuf_EnumOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_EnumOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_EnumOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions* msg, bool value) { + const upb_MiniTableField field = {2, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions* msg, bool value) { + const upb_MiniTableField field = {3, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumOptions_set_deprecated_legacy_json_field_conflicts(google_protobuf_EnumOptions* msg, bool value) { + const upb_MiniTableField field = {6, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumOptions_set_features(google_protobuf_EnumOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {7, UPB_SIZE(12, 16), 67, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_EnumOptions_mutable_features( + google_protobuf_EnumOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_EnumOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_EnumOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_mutable_uninterpreted_option(google_protobuf_EnumOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_resize_uninterpreted_option(google_protobuf_EnumOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumOptions_add_uninterpreted_option( + google_protobuf_EnumOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.EnumValueOptions */ +UPB_INLINE google_protobuf_EnumValueOptions* google_protobuf_EnumValueOptions_new(upb_Arena* arena) { + return (google_protobuf_EnumValueOptions*)_upb_Message_New(&google__protobuf__EnumValueOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_EnumValueOptions* google_protobuf_EnumValueOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_EnumValueOptions* ret = google_protobuf_EnumValueOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumValueOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_EnumValueOptions* google_protobuf_EnumValueOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_EnumValueOptions* ret = google_protobuf_EnumValueOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__EnumValueOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_EnumValueOptions_serialize_ex(const google_protobuf_EnumValueOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__EnumValueOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_EnumValueOptions_clear_deprecated(google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumValueOptions_clear_features(google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 65, UPB_SIZE(12, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_EnumValueOptions_features(const google_protobuf_EnumValueOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 65, UPB_SIZE(12, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumValueOptions_has_features(const google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 65, UPB_SIZE(12, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumValueOptions_clear_debug_redact(google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {3, 10, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_EnumValueOptions_debug_redact(const google_protobuf_EnumValueOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {3, 10, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumValueOptions_has_debug_redact(const google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {3, 10, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumValueOptions_clear_feature_support(google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 24), 67, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FieldOptions_FeatureSupport* google_protobuf_EnumValueOptions_feature_support(const google_protobuf_EnumValueOptions* msg) { + const google_protobuf_FieldOptions_FeatureSupport* default_val = NULL; + const google_protobuf_FieldOptions_FeatureSupport* ret; + const upb_MiniTableField field = {4, UPB_SIZE(16, 24), 67, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__FeatureSupport_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_EnumValueOptions_has_feature_support(const google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 24), 67, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_EnumValueOptions_clear_uninterpreted_option(google_protobuf_EnumValueOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_EnumValueOptions_uninterpreted_option_upb_array( + const google_protobuf_EnumValueOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_EnumValueOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_EnumValueOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions* msg, bool value) { + const upb_MiniTableField field = {1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumValueOptions_set_features(google_protobuf_EnumValueOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 16), 65, UPB_SIZE(12, 13), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_EnumValueOptions_mutable_features( + google_protobuf_EnumValueOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_EnumValueOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_EnumValueOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_EnumValueOptions_set_debug_redact(google_protobuf_EnumValueOptions* msg, bool value) { + const upb_MiniTableField field = {3, 10, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_EnumValueOptions_set_feature_support(google_protobuf_EnumValueOptions* msg, google_protobuf_FieldOptions_FeatureSupport* value) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 24), 67, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FieldOptions__FeatureSupport_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FieldOptions_FeatureSupport* google_protobuf_EnumValueOptions_mutable_feature_support( + google_protobuf_EnumValueOptions* msg, upb_Arena* arena) { + struct google_protobuf_FieldOptions_FeatureSupport* sub = (struct google_protobuf_FieldOptions_FeatureSupport*)google_protobuf_EnumValueOptions_feature_support(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FieldOptions_FeatureSupport*)_upb_Message_New(&google__protobuf__FieldOptions__FeatureSupport_msg_init, arena); + if (sub) google_protobuf_EnumValueOptions_set_feature_support(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_mutable_uninterpreted_option(google_protobuf_EnumValueOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_resize_uninterpreted_option(google_protobuf_EnumValueOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumValueOptions_add_uninterpreted_option( + google_protobuf_EnumValueOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(20, 32), 0, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.ServiceOptions */ +UPB_INLINE google_protobuf_ServiceOptions* google_protobuf_ServiceOptions_new(upb_Arena* arena) { + return (google_protobuf_ServiceOptions*)_upb_Message_New(&google__protobuf__ServiceOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_ServiceOptions* google_protobuf_ServiceOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_ServiceOptions* ret = google_protobuf_ServiceOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ServiceOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_ServiceOptions* google_protobuf_ServiceOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_ServiceOptions* ret = google_protobuf_ServiceOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__ServiceOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_ServiceOptions_serialize_ex(const google_protobuf_ServiceOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__ServiceOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_ServiceOptions_clear_deprecated(google_protobuf_ServiceOptions* msg) { + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions* msg) { + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ServiceOptions_clear_features(google_protobuf_ServiceOptions* msg) { + const upb_MiniTableField field = {34, UPB_SIZE(12, 16), 65, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_ServiceOptions_features(const google_protobuf_ServiceOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {34, UPB_SIZE(12, 16), 65, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_ServiceOptions_has_features(const google_protobuf_ServiceOptions* msg) { + const upb_MiniTableField field = {34, UPB_SIZE(12, 16), 65, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_ServiceOptions_clear_uninterpreted_option(google_protobuf_ServiceOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_ServiceOptions_uninterpreted_option_upb_array( + const google_protobuf_ServiceOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_ServiceOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_ServiceOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions* msg, bool value) { + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_ServiceOptions_set_features(google_protobuf_ServiceOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {34, UPB_SIZE(12, 16), 65, UPB_SIZE(6, 7), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_ServiceOptions_mutable_features( + google_protobuf_ServiceOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_ServiceOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_ServiceOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_mutable_uninterpreted_option(google_protobuf_ServiceOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_resize_uninterpreted_option(google_protobuf_ServiceOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ServiceOptions_add_uninterpreted_option( + google_protobuf_ServiceOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(16, 24), 0, UPB_SIZE(4, 6), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.MethodOptions */ +UPB_INLINE google_protobuf_MethodOptions* google_protobuf_MethodOptions_new(upb_Arena* arena) { + return (google_protobuf_MethodOptions*)_upb_Message_New(&google__protobuf__MethodOptions_msg_init, arena); +} +UPB_INLINE google_protobuf_MethodOptions* google_protobuf_MethodOptions_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_MethodOptions* ret = google_protobuf_MethodOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__MethodOptions_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_MethodOptions* google_protobuf_MethodOptions_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_MethodOptions* ret = google_protobuf_MethodOptions_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__MethodOptions_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodOptions_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_MethodOptions_serialize_ex(const google_protobuf_MethodOptions* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__MethodOptions_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_MethodOptions_clear_deprecated(google_protobuf_MethodOptions* msg) { + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions* msg) { + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodOptions_clear_idempotency_level(google_protobuf_MethodOptions* msg) { + const upb_MiniTableField field = {34, 12, 65, 9, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {34, 12, 65, 9, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions* msg) { + const upb_MiniTableField field = {34, 12, 65, 9, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodOptions_clear_features(google_protobuf_MethodOptions* msg) { + const upb_MiniTableField field = {35, 16, 66, UPB_SIZE(7, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_MethodOptions_features(const google_protobuf_MethodOptions* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {35, 16, 66, UPB_SIZE(7, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_MethodOptions_has_features(const google_protobuf_MethodOptions* msg) { + const upb_MiniTableField field = {35, 16, 66, UPB_SIZE(7, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_MethodOptions_clear_uninterpreted_option(google_protobuf_MethodOptions* msg) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions* msg, + size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_MethodOptions_uninterpreted_option_upb_array( + const google_protobuf_MethodOptions* msg, size_t* size) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_MethodOptions_uninterpreted_option_mutable_upb_array( + google_protobuf_MethodOptions* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions* msg, bool value) { + const upb_MiniTableField field = {33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions* msg, int32_t value) { + const upb_MiniTableField field = {34, 12, 65, 9, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_MethodOptions_set_features(google_protobuf_MethodOptions* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {35, 16, 66, UPB_SIZE(7, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_MethodOptions_mutable_features( + google_protobuf_MethodOptions* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_MethodOptions_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_MethodOptions_set_features(msg, sub); + } + return sub; +} +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_mutable_uninterpreted_option(google_protobuf_MethodOptions* msg, + size_t* size) { + upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_resize_uninterpreted_option(google_protobuf_MethodOptions* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + return (google_protobuf_UninterpretedOption**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MethodOptions_add_uninterpreted_option( + google_protobuf_MethodOptions* msg, upb_Arena* arena) { + upb_MiniTableField field = {999, UPB_SIZE(20, 24), 0, UPB_SIZE(5, 7), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption* sub = + (struct google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.UninterpretedOption */ +UPB_INLINE google_protobuf_UninterpretedOption* google_protobuf_UninterpretedOption_new(upb_Arena* arena) { + return (google_protobuf_UninterpretedOption*)_upb_Message_New(&google__protobuf__UninterpretedOption_msg_init, arena); +} +UPB_INLINE google_protobuf_UninterpretedOption* google_protobuf_UninterpretedOption_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_UninterpretedOption* ret = google_protobuf_UninterpretedOption_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__UninterpretedOption_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_UninterpretedOption* google_protobuf_UninterpretedOption_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_UninterpretedOption* ret = google_protobuf_UninterpretedOption_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__UninterpretedOption_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_UninterpretedOption_serialize_ex(const google_protobuf_UninterpretedOption* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_UninterpretedOption_clear_name(google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption* msg, + size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption__NamePart_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_UninterpretedOption_NamePart* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_UninterpretedOption_name_upb_array( + const google_protobuf_UninterpretedOption* msg, size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption__NamePart_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_UninterpretedOption_name_mutable_upb_array( + google_protobuf_UninterpretedOption* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption__NamePart_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_UninterpretedOption_clear_identifier_value(google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {3, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {3, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {3, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_UninterpretedOption_clear_positive_int_value(google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(40, 72), 65, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption* msg) { + uint64_t default_val = (uint64_t)0ull; + uint64_t ret; + const upb_MiniTableField field = {4, UPB_SIZE(40, 72), 65, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(40, 72), 65, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_UninterpretedOption_clear_negative_int_value(google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(48, 80), 66, kUpb_NoSub, 3, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption* msg) { + int64_t default_val = (int64_t)0ll; + int64_t ret; + const upb_MiniTableField field = {5, UPB_SIZE(48, 80), 66, kUpb_NoSub, 3, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(48, 80), 66, kUpb_NoSub, 3, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_UninterpretedOption_clear_double_value(google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(56, 88), 67, kUpb_NoSub, 1, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption* msg) { + double default_val = 0; + double ret; + const upb_MiniTableField field = {6, UPB_SIZE(56, 88), 67, kUpb_NoSub, 1, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(56, 88), 67, kUpb_NoSub, 1, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_UninterpretedOption_clear_string_value(google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(24, 32), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {7, UPB_SIZE(24, 32), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {7, UPB_SIZE(24, 32), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_UninterpretedOption_clear_aggregate_value(google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {8, UPB_SIZE(32, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {8, UPB_SIZE(32, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption* msg) { + const upb_MiniTableField field = {8, UPB_SIZE(32, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_mutable_name(google_protobuf_UninterpretedOption* msg, + size_t* size) { + upb_MiniTableField field = {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption__NamePart_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_UninterpretedOption_NamePart**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_resize_name(google_protobuf_UninterpretedOption* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption__NamePart_msg_init); + return (google_protobuf_UninterpretedOption_NamePart**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_add_name( + google_protobuf_UninterpretedOption* msg, upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(12, 64), 0, UPB_SIZE(21, 22), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__UninterpretedOption__NamePart_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_UninterpretedOption_NamePart* sub = + (struct google_protobuf_UninterpretedOption_NamePart*)_upb_Message_New(&google__protobuf__UninterpretedOption__NamePart_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption* msg, upb_StringView value) { + const upb_MiniTableField field = {3, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption* msg, uint64_t value) { + const upb_MiniTableField field = {4, UPB_SIZE(40, 72), 65, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption* msg, int64_t value) { + const upb_MiniTableField field = {5, UPB_SIZE(48, 80), 66, kUpb_NoSub, 3, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption* msg, double value) { + const upb_MiniTableField field = {6, UPB_SIZE(56, 88), 67, kUpb_NoSub, 1, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption* msg, upb_StringView value) { + const upb_MiniTableField field = {7, UPB_SIZE(24, 32), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption* msg, upb_StringView value) { + const upb_MiniTableField field = {8, UPB_SIZE(32, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.UninterpretedOption.NamePart */ +UPB_INLINE google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_NamePart_new(upb_Arena* arena) { + return (google_protobuf_UninterpretedOption_NamePart*)_upb_Message_New(&google__protobuf__UninterpretedOption__NamePart_msg_init, arena); +} +UPB_INLINE google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_NamePart_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_UninterpretedOption_NamePart* ret = google_protobuf_UninterpretedOption_NamePart_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__UninterpretedOption__NamePart_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_NamePart_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_UninterpretedOption_NamePart* ret = google_protobuf_UninterpretedOption_NamePart_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__UninterpretedOption__NamePart_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption__NamePart_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_UninterpretedOption_NamePart_serialize_ex(const google_protobuf_UninterpretedOption_NamePart* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__UninterpretedOption__NamePart_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_clear_name_part(google_protobuf_UninterpretedOption_NamePart* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_clear_is_extension(google_protobuf_UninterpretedOption_NamePart* msg) { + const upb_MiniTableField field = {2, 9, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart* msg) { + bool default_val = false; + bool ret; + const upb_MiniTableField field = {2, 9, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart* msg) { + const upb_MiniTableField field = {2, 9, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart* msg, upb_StringView value) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart* msg, bool value) { + const upb_MiniTableField field = {2, 9, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.FeatureSet */ +UPB_INLINE google_protobuf_FeatureSet* google_protobuf_FeatureSet_new(upb_Arena* arena) { + return (google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); +} +UPB_INLINE google_protobuf_FeatureSet* google_protobuf_FeatureSet_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FeatureSet* ret = google_protobuf_FeatureSet_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSet_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FeatureSet* google_protobuf_FeatureSet_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FeatureSet* ret = google_protobuf_FeatureSet_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSet_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FeatureSet_serialize(const google_protobuf_FeatureSet* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FeatureSet_serialize_ex(const google_protobuf_FeatureSet* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FeatureSet_clear_field_presence(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {1, 12, 64, 24, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_field_presence(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {1, 12, 64, 24, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_field_presence(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {1, 12, 64, 24, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSet_clear_enum_type(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(22, 23), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_enum_type(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(22, 23), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_enum_type(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(22, 23), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSet_clear_repeated_field_encoding(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {3, 20, 66, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_repeated_field_encoding(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {3, 20, 66, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_repeated_field_encoding(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {3, 20, 66, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSet_clear_utf8_validation(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {4, 24, 67, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_utf8_validation(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {4, 24, 67, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_utf8_validation(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {4, 24, 67, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSet_clear_message_encoding(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {5, 28, 68, UPB_SIZE(16, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_message_encoding(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {5, 28, 68, UPB_SIZE(16, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_message_encoding(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {5, 28, 68, UPB_SIZE(16, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSet_clear_json_format(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {6, 32, 69, UPB_SIZE(14, 19), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_json_format(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {6, 32, 69, UPB_SIZE(14, 19), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_json_format(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {6, 32, 69, UPB_SIZE(14, 19), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSet_clear_enforce_naming_style(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {7, 36, 70, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_enforce_naming_style(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {7, 36, 70, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_enforce_naming_style(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {7, 36, 70, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSet_clear_default_symbol_visibility(google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {8, 40, 71, UPB_SIZE(10, 17), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSet_default_symbol_visibility(const google_protobuf_FeatureSet* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {8, 40, 71, UPB_SIZE(10, 17), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSet_has_default_symbol_visibility(const google_protobuf_FeatureSet* msg) { + const upb_MiniTableField field = {8, 40, 71, UPB_SIZE(10, 17), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_FeatureSet_set_field_presence(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {1, 12, 64, 24, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSet_set_enum_type(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {2, 16, 65, UPB_SIZE(22, 23), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSet_set_repeated_field_encoding(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {3, 20, 66, UPB_SIZE(20, 22), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSet_set_utf8_validation(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {4, 24, 67, UPB_SIZE(18, 21), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSet_set_message_encoding(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {5, 28, 68, UPB_SIZE(16, 20), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSet_set_json_format(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {6, 32, 69, UPB_SIZE(14, 19), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSet_set_enforce_naming_style(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {7, 36, 70, UPB_SIZE(12, 18), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSet_set_default_symbol_visibility(google_protobuf_FeatureSet* msg, int32_t value) { + const upb_MiniTableField field = {8, 40, 71, UPB_SIZE(10, 17), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.FeatureSet.VisibilityFeature */ +UPB_INLINE google_protobuf_FeatureSet_VisibilityFeature* google_protobuf_FeatureSet_VisibilityFeature_new(upb_Arena* arena) { + return (google_protobuf_FeatureSet_VisibilityFeature*)_upb_Message_New(&google__protobuf__FeatureSet__VisibilityFeature_msg_init, arena); +} +UPB_INLINE google_protobuf_FeatureSet_VisibilityFeature* google_protobuf_FeatureSet_VisibilityFeature_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FeatureSet_VisibilityFeature* ret = google_protobuf_FeatureSet_VisibilityFeature_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSet__VisibilityFeature_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FeatureSet_VisibilityFeature* google_protobuf_FeatureSet_VisibilityFeature_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FeatureSet_VisibilityFeature* ret = google_protobuf_FeatureSet_VisibilityFeature_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSet__VisibilityFeature_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FeatureSet_VisibilityFeature_serialize(const google_protobuf_FeatureSet_VisibilityFeature* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__VisibilityFeature_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FeatureSet_VisibilityFeature_serialize_ex(const google_protobuf_FeatureSet_VisibilityFeature* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSet__VisibilityFeature_msg_init, options, arena, &ptr, len); + return ptr; +} + + +/* google.protobuf.FeatureSetDefaults */ +UPB_INLINE google_protobuf_FeatureSetDefaults* google_protobuf_FeatureSetDefaults_new(upb_Arena* arena) { + return (google_protobuf_FeatureSetDefaults*)_upb_Message_New(&google__protobuf__FeatureSetDefaults_msg_init, arena); +} +UPB_INLINE google_protobuf_FeatureSetDefaults* google_protobuf_FeatureSetDefaults_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FeatureSetDefaults* ret = google_protobuf_FeatureSetDefaults_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSetDefaults_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FeatureSetDefaults* google_protobuf_FeatureSetDefaults_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FeatureSetDefaults* ret = google_protobuf_FeatureSetDefaults_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSetDefaults_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FeatureSetDefaults_serialize(const google_protobuf_FeatureSetDefaults* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FeatureSetDefaults_serialize_ex(const google_protobuf_FeatureSetDefaults* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_clear_defaults(google_protobuf_FeatureSetDefaults* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* const* google_protobuf_FeatureSetDefaults_defaults(const google_protobuf_FeatureSetDefaults* msg, + size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_FeatureSetDefaults_defaults_upb_array( + const google_protobuf_FeatureSetDefaults* msg, size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_FeatureSetDefaults_defaults_mutable_upb_array( + google_protobuf_FeatureSetDefaults* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_clear_minimum_edition(google_protobuf_FeatureSetDefaults* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 12), 64, UPB_SIZE(7, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSetDefaults_minimum_edition(const google_protobuf_FeatureSetDefaults* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {4, UPB_SIZE(16, 12), 64, UPB_SIZE(7, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSetDefaults_has_minimum_edition(const google_protobuf_FeatureSetDefaults* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 12), 64, UPB_SIZE(7, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_clear_maximum_edition(google_protobuf_FeatureSetDefaults* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 16), 65, UPB_SIZE(5, 8), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSetDefaults_maximum_edition(const google_protobuf_FeatureSetDefaults* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {5, UPB_SIZE(20, 16), 65, UPB_SIZE(5, 8), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSetDefaults_has_maximum_edition(const google_protobuf_FeatureSetDefaults* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 16), 65, UPB_SIZE(5, 8), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault** google_protobuf_FeatureSetDefaults_mutable_defaults(google_protobuf_FeatureSetDefaults* msg, + size_t* size) { + upb_MiniTableField field = {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault** google_protobuf_FeatureSetDefaults_resize_defaults(google_protobuf_FeatureSetDefaults* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init); + return (google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* google_protobuf_FeatureSetDefaults_add_defaults( + google_protobuf_FeatureSetDefaults* msg, upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 24), 0, UPB_SIZE(9, 10), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* sub = + (struct google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault*)_upb_Message_New(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_set_minimum_edition(google_protobuf_FeatureSetDefaults* msg, int32_t value) { + const upb_MiniTableField field = {4, UPB_SIZE(16, 12), 64, UPB_SIZE(7, 9), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_set_maximum_edition(google_protobuf_FeatureSetDefaults* msg, int32_t value) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 16), 65, UPB_SIZE(5, 8), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* google.protobuf.FeatureSetDefaults.FeatureSetEditionDefault */ +UPB_INLINE google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_new(upb_Arena* arena) { + return (google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault*)_upb_Message_New(&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, arena); +} +UPB_INLINE google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* ret = google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* ret = google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_serialize(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_serialize_ex(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_clear_edition(google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const upb_MiniTableField field = {3, 12, 64, UPB_SIZE(9, 10), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_edition(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {3, 12, 64, UPB_SIZE(9, 10), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_has_edition(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const upb_MiniTableField field = {3, 12, 64, UPB_SIZE(9, 10), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_clear_overridable_features(google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const upb_MiniTableField field = {4, 16, 65, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_overridable_features(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {4, 16, 65, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_has_overridable_features(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const upb_MiniTableField field = {4, 16, 65, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_clear_fixed_features(google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 24), 66, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_FeatureSet* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_fixed_features(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const google_protobuf_FeatureSet* default_val = NULL; + const google_protobuf_FeatureSet* ret; + const upb_MiniTableField field = {5, UPB_SIZE(20, 24), 66, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_has_fixed_features(const google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 24), 66, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE void google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_set_edition(google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg, int32_t value) { + const upb_MiniTableField field = {3, 12, 64, UPB_SIZE(9, 10), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_set_overridable_features(google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {4, 16, 65, UPB_SIZE(7, 9), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_mutable_overridable_features( + google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_overridable_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_set_overridable_features(msg, sub); + } + return sub; +} +UPB_INLINE void google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_set_fixed_features(google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg, google_protobuf_FeatureSet* value) { + const upb_MiniTableField field = {5, UPB_SIZE(20, 24), 66, UPB_SIZE(5, 8), 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__FeatureSet_msg_init); + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE struct google_protobuf_FeatureSet* google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_mutable_fixed_features( + google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault* msg, upb_Arena* arena) { + struct google_protobuf_FeatureSet* sub = (struct google_protobuf_FeatureSet*)google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_fixed_features(msg); + if (sub == NULL) { + sub = (struct google_protobuf_FeatureSet*)_upb_Message_New(&google__protobuf__FeatureSet_msg_init, arena); + if (sub) google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_set_fixed_features(msg, sub); + } + return sub; +} + +/* google.protobuf.SourceCodeInfo */ +UPB_INLINE google_protobuf_SourceCodeInfo* google_protobuf_SourceCodeInfo_new(upb_Arena* arena) { + return (google_protobuf_SourceCodeInfo*)_upb_Message_New(&google__protobuf__SourceCodeInfo_msg_init, arena); +} +UPB_INLINE google_protobuf_SourceCodeInfo* google_protobuf_SourceCodeInfo_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_SourceCodeInfo* ret = google_protobuf_SourceCodeInfo_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__SourceCodeInfo_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_SourceCodeInfo* google_protobuf_SourceCodeInfo_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_SourceCodeInfo* ret = google_protobuf_SourceCodeInfo_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__SourceCodeInfo_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_SourceCodeInfo_serialize_ex(const google_protobuf_SourceCodeInfo* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_SourceCodeInfo_clear_location(google_protobuf_SourceCodeInfo* msg) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_SourceCodeInfo_Location* const* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo* msg, + size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo__Location_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_SourceCodeInfo_Location* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_SourceCodeInfo_location_upb_array( + const google_protobuf_SourceCodeInfo* msg, size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo__Location_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_SourceCodeInfo_location_mutable_upb_array( + google_protobuf_SourceCodeInfo* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo__Location_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_mutable_location(google_protobuf_SourceCodeInfo* msg, + size_t* size) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo__Location_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_SourceCodeInfo_Location**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_resize_location(google_protobuf_SourceCodeInfo* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo__Location_msg_init); + return (google_protobuf_SourceCodeInfo_Location**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_add_location( + google_protobuf_SourceCodeInfo* msg, upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__SourceCodeInfo__Location_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_SourceCodeInfo_Location* sub = + (struct google_protobuf_SourceCodeInfo_Location*)_upb_Message_New(&google__protobuf__SourceCodeInfo__Location_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.SourceCodeInfo.Location */ +UPB_INLINE google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_Location_new(upb_Arena* arena) { + return (google_protobuf_SourceCodeInfo_Location*)_upb_Message_New(&google__protobuf__SourceCodeInfo__Location_msg_init, arena); +} +UPB_INLINE google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_Location_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_SourceCodeInfo_Location* ret = google_protobuf_SourceCodeInfo_Location_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__SourceCodeInfo__Location_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_Location_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_SourceCodeInfo_Location* ret = google_protobuf_SourceCodeInfo_Location_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__SourceCodeInfo__Location_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo__Location_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_SourceCodeInfo_Location_serialize_ex(const google_protobuf_SourceCodeInfo_Location* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__SourceCodeInfo__Location_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_clear_path(google_protobuf_SourceCodeInfo_Location* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location* msg, + size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_SourceCodeInfo_Location_path_upb_array( + const google_protobuf_SourceCodeInfo_Location* msg, size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_SourceCodeInfo_Location_path_mutable_upb_array( + google_protobuf_SourceCodeInfo_Location* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_clear_span(google_protobuf_SourceCodeInfo_Location* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location* msg, + size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_SourceCodeInfo_Location_span_upb_array( + const google_protobuf_SourceCodeInfo_Location* msg, size_t* size) { + const upb_MiniTableField field = {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_SourceCodeInfo_Location_span_mutable_upb_array( + google_protobuf_SourceCodeInfo_Location* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_clear_leading_comments(google_protobuf_SourceCodeInfo_Location* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(24, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {3, UPB_SIZE(24, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(24, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_clear_trailing_comments(google_protobuf_SourceCodeInfo_Location* msg) { + const upb_MiniTableField field = {4, 32, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {4, 32, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location* msg) { + const upb_MiniTableField field = {4, 32, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_clear_leading_detached_comments(google_protobuf_SourceCodeInfo_Location* msg) { + const upb_MiniTableField field = {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView const* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location* msg, + size_t* size) { + const upb_MiniTableField field = {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_SourceCodeInfo_Location_leading_detached_comments_upb_array( + const google_protobuf_SourceCodeInfo_Location* msg, size_t* size) { + const upb_MiniTableField field = {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_SourceCodeInfo_Location_leading_detached_comments_mutable_upb_array( + google_protobuf_SourceCodeInfo_Location* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_path(google_protobuf_SourceCodeInfo_Location* msg, + size_t* size) { + upb_MiniTableField field = {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_path(google_protobuf_SourceCodeInfo_Location* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (int32_t*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_path(google_protobuf_SourceCodeInfo_Location* msg, int32_t val, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_span(google_protobuf_SourceCodeInfo_Location* msg, + size_t* size) { + upb_MiniTableField field = {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_span(google_protobuf_SourceCodeInfo_Location* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (int32_t*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_span(google_protobuf_SourceCodeInfo_Location* msg, int32_t val, + upb_Arena* arena) { + upb_MiniTableField field = {2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location* msg, upb_StringView value) { + const upb_MiniTableField field = {3, UPB_SIZE(24, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location* msg, upb_StringView value) { + const upb_MiniTableField field = {4, 32, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE upb_StringView* google_protobuf_SourceCodeInfo_Location_mutable_leading_detached_comments(google_protobuf_SourceCodeInfo_Location* msg, + size_t* size) { + upb_MiniTableField field = {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (upb_StringView*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE upb_StringView* google_protobuf_SourceCodeInfo_Location_resize_leading_detached_comments(google_protobuf_SourceCodeInfo_Location* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (upb_StringView*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_leading_detached_comments(google_protobuf_SourceCodeInfo_Location* msg, upb_StringView val, + upb_Arena* arena) { + upb_MiniTableField field = {6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} + +/* google.protobuf.GeneratedCodeInfo */ +UPB_INLINE google_protobuf_GeneratedCodeInfo* google_protobuf_GeneratedCodeInfo_new(upb_Arena* arena) { + return (google_protobuf_GeneratedCodeInfo*)_upb_Message_New(&google__protobuf__GeneratedCodeInfo_msg_init, arena); +} +UPB_INLINE google_protobuf_GeneratedCodeInfo* google_protobuf_GeneratedCodeInfo_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_GeneratedCodeInfo* ret = google_protobuf_GeneratedCodeInfo_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__GeneratedCodeInfo_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_GeneratedCodeInfo* google_protobuf_GeneratedCodeInfo_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_GeneratedCodeInfo* ret = google_protobuf_GeneratedCodeInfo_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__GeneratedCodeInfo_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_GeneratedCodeInfo_serialize_ex(const google_protobuf_GeneratedCodeInfo* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_clear_annotation(google_protobuf_GeneratedCodeInfo* msg) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE const google_protobuf_GeneratedCodeInfo_Annotation* const* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo* msg, + size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (const google_protobuf_GeneratedCodeInfo_Annotation* const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_GeneratedCodeInfo_annotation_upb_array( + const google_protobuf_GeneratedCodeInfo* msg, size_t* size) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init); + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_GeneratedCodeInfo_annotation_mutable_upb_array( + google_protobuf_GeneratedCodeInfo* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_mutable_annotation(google_protobuf_GeneratedCodeInfo* msg, + size_t* size) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init); + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (google_protobuf_GeneratedCodeInfo_Annotation**)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_resize_annotation(google_protobuf_GeneratedCodeInfo* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init); + return (google_protobuf_GeneratedCodeInfo_Annotation**)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE struct google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_add_annotation( + google_protobuf_GeneratedCodeInfo* msg, upb_Arena* arena) { + upb_MiniTableField field = {1, 8, 0, UPB_SIZE(3, 4), 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + UPB_PRIVATE(_upb_MiniTable_StrongReference)(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init); + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return NULL; + } + struct google_protobuf_GeneratedCodeInfo_Annotation* sub = + (struct google_protobuf_GeneratedCodeInfo_Annotation*)_upb_Message_New(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init, arena); + if (!arr || !sub) return NULL; + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &sub, sizeof(sub)); + return sub; +} + +/* google.protobuf.GeneratedCodeInfo.Annotation */ +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_Annotation_new(upb_Arena* arena) { + return (google_protobuf_GeneratedCodeInfo_Annotation*)_upb_Message_New(&google__protobuf__GeneratedCodeInfo__Annotation_msg_init, arena); +} +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_Annotation_parse(const char* buf, size_t size, + upb_Arena* arena) { + google_protobuf_GeneratedCodeInfo_Annotation* ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, NULL, 0, + arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_Annotation_parse_ex( + const char* buf, size_t size, const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena) { + google_protobuf_GeneratedCodeInfo_Annotation* ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena); + if (!ret) return NULL; + if (upb_Decode(buf, size, UPB_UPCAST(ret), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, extreg, + options, arena) != kUpb_DecodeStatus_Ok) { + return NULL; + } + return ret; +} +UPB_INLINE char* google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation* msg, + upb_Arena* arena, size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, 0, arena, &ptr, len); + return ptr; +} +UPB_INLINE char* google_protobuf_GeneratedCodeInfo_Annotation_serialize_ex(const google_protobuf_GeneratedCodeInfo_Annotation* msg, + int options, upb_Arena* arena, + size_t* len) { + char* ptr; + (void)upb_Encode(UPB_UPCAST(msg), &google__protobuf__GeneratedCodeInfo__Annotation_msg_init, options, arena, &ptr, len); + return ptr; +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_clear_path(google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation* msg, + size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t const*)upb_Array_DataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +// +UPB_INLINE const upb_Array* _google_protobuf_GeneratedCodeInfo_Annotation_path_upb_array( + const google_protobuf_GeneratedCodeInfo_Annotation* msg, size_t* size) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + const upb_Array* arr = upb_Message_GetArray(UPB_UPCAST(msg), &field); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} + +UPB_INLINE upb_Array* _google_protobuf_GeneratedCodeInfo_Annotation_path_mutable_upb_array( + google_protobuf_GeneratedCodeInfo_Annotation* msg, size_t* size, upb_Arena* arena) { + const upb_MiniTableField field = {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray(UPB_UPCAST(msg), + &field, arena); + if (size) { + *size = arr ? arr->UPB_PRIVATE(size) : 0; + } + return arr; +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_clear_source_file(google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(28, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE upb_StringView google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + upb_StringView default_val = upb_StringView_FromString(""); + upb_StringView ret; + const upb_MiniTableField field = {2, UPB_SIZE(28, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {2, UPB_SIZE(28, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_clear_begin(google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_clear_end(google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 16), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + int32_t default_val = (int32_t)0; + int32_t ret; + const upb_MiniTableField field = {4, UPB_SIZE(20, 16), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 16), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_clear_semantic(google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 20), 67, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_ClearBaseField(UPB_UPCAST(msg), &field); +} +UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_semantic(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + int32_t default_val = 0; + int32_t ret; + const upb_MiniTableField field = {5, UPB_SIZE(24, 20), 67, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + _upb_Message_GetNonExtensionField(UPB_UPCAST(msg), &field, + &default_val, &ret); + return ret; +} +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_semantic(const google_protobuf_GeneratedCodeInfo_Annotation* msg) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 20), 67, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + return upb_Message_HasBaseField(UPB_UPCAST(msg), &field); +} + +UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_mutable_path(google_protobuf_GeneratedCodeInfo_Annotation* msg, + size_t* size) { + upb_MiniTableField field = {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetMutableArray(UPB_UPCAST(msg), &field); + if (arr) { + if (size) *size = arr->UPB_PRIVATE(size); + return (int32_t*)upb_Array_MutableDataPtr(arr); + } else { + if (size) *size = 0; + return NULL; + } +} + +UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_resize_path(google_protobuf_GeneratedCodeInfo_Annotation* msg, + size_t size, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + return (int32_t*)upb_Message_ResizeArrayUninitialized( + UPB_UPCAST(msg), &field, size, arena); +} +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_add_path(google_protobuf_GeneratedCodeInfo_Annotation* msg, int32_t val, + upb_Arena* arena) { + upb_MiniTableField field = {1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)}; + upb_Array* arr = upb_Message_GetOrCreateMutableArray( + UPB_UPCAST(msg), &field, arena); + if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)( + arr, arr->UPB_PRIVATE(size) + 1, arena)) { + return false; + } + UPB_PRIVATE(_upb_Array_Set) + (arr, arr->UPB_PRIVATE(size) - 1, &val, sizeof(val)); + return true; +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation* msg, upb_StringView value) { + const upb_MiniTableField field = {2, UPB_SIZE(28, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation* msg, int32_t value) { + const upb_MiniTableField field = {3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation* msg, int32_t value) { + const upb_MiniTableField field = {4, UPB_SIZE(20, 16), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} +UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_semantic(google_protobuf_GeneratedCodeInfo_Annotation* msg, int32_t value) { + const upb_MiniTableField field = {5, UPB_SIZE(24, 20), 67, UPB_SIZE(3, 4), 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)}; + upb_Message_SetBaseField((upb_Message*)msg, &field, &value); +} + +/* Max size 32 is google.protobuf.FileOptions */ +/* Max size 64 is google.protobuf.FileOptions */ +#define _UPB_MAXOPT_SIZE UPB_SIZE(112, 200) +#ifdef __cplusplus + } /* extern "C" */ +#endif + + +#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_H_ */ +#endif + +// IWYU pragma: end_exports + +#endif // GOOGLE_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H__ + +// Forward declarations for circular references. +typedef struct upb_DefPool upb_DefPool; +typedef struct upb_EnumDef upb_EnumDef; +typedef struct upb_EnumReservedRange upb_EnumReservedRange; +typedef struct upb_EnumValueDef upb_EnumValueDef; +typedef struct upb_ExtensionRange upb_ExtensionRange; +typedef struct upb_FieldDef upb_FieldDef; +typedef struct upb_FileDef upb_FileDef; +typedef struct upb_MessageDef upb_MessageDef; +typedef struct upb_MessageReservedRange upb_MessageReservedRange; +typedef struct upb_MethodDef upb_MethodDef; +typedef struct upb_OneofDef upb_OneofDef; +typedef struct upb_ServiceDef upb_ServiceDef; + +// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE ///////////////////////// + +typedef struct upb_DefBuilder upb_DefBuilder; + +#endif /* UPB_REFLECTION_COMMON_H_ */ + +#ifndef UPB_REFLECTION_DEF_TYPE_H_ +#define UPB_REFLECTION_DEF_TYPE_H_ + + +// Must be last. + +// Inside a symtab we store tagged pointers to specific def types. +typedef enum { + UPB_DEFTYPE_MASK = 7, + + // Only inside symtab table. + UPB_DEFTYPE_EXT = 0, + UPB_DEFTYPE_MSG = 1, + UPB_DEFTYPE_ENUM = 2, + UPB_DEFTYPE_ENUMVAL = 3, + UPB_DEFTYPE_SERVICE = 4, + + // Only inside message table. + UPB_DEFTYPE_FIELD = 0, + UPB_DEFTYPE_ONEOF = 1, + + // Only inside service table. + UPB_DEFTYPE_METHOD = 0, +} upb_deftype_t; + +#ifdef __cplusplus +extern "C" { +#endif + +// Our 3-bit pointer tagging requires all pointers to be multiples of 8. +// The arena will always yield 8-byte-aligned addresses, however we put +// the defs into arrays. For each element in the array to be 8-byte-aligned, +// the sizes of each def type must also be a multiple of 8. +// +// If any of these asserts fail, we need to add or remove padding on 32-bit +// machines (64-bit machines will have 8-byte alignment already due to +// pointers, which all of these structs have). +UPB_INLINE void _upb_DefType_CheckPadding(size_t size) { + UPB_ASSERT((size & UPB_DEFTYPE_MASK) == 0); +} + +upb_deftype_t _upb_DefType_Type(upb_value v); + +upb_value _upb_DefType_Pack(const void* ptr, upb_deftype_t type); + +const void* _upb_DefType_Unpack(upb_value v, upb_deftype_t type); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_DEF_TYPE_H_ */ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API void upb_DefPool_Free(upb_DefPool* s); + +UPB_API upb_DefPool* upb_DefPool_New(void); + +UPB_API const google_protobuf_FeatureSetDefaults* upb_DefPool_FeatureSetDefaults( + const upb_DefPool* s); + +UPB_API bool upb_DefPool_SetFeatureSetDefaults(upb_DefPool* s, + const char* serialized_defaults, + size_t serialized_len, + upb_Status* status); + +UPB_API const upb_MessageDef* upb_DefPool_FindMessageByName( + const upb_DefPool* s, const char* sym); + +const upb_MessageDef* upb_DefPool_FindMessageByNameWithSize( + const upb_DefPool* s, const char* sym, size_t len); + +UPB_API const upb_EnumDef* upb_DefPool_FindEnumByName(const upb_DefPool* s, + const char* sym); + +UPB_API const upb_EnumDef* upb_DefPool_FindEnumByNameWithSize( + const upb_DefPool* s, const char* sym, size_t len); + +UPB_API const upb_EnumValueDef* upb_DefPool_FindEnumValueByName( + const upb_DefPool* s, const char* sym); + +UPB_API const upb_EnumValueDef* upb_DefPool_FindEnumValueByNameWithSize( + const upb_DefPool* s, const char* sym, size_t len); + +UPB_API const upb_FileDef* upb_DefPool_FindFileByName(const upb_DefPool* s, + const char* name); + +const upb_FileDef* upb_DefPool_FindFileByNameWithSize(const upb_DefPool* s, + const char* name, + size_t len); + +const upb_FieldDef* upb_DefPool_FindExtensionByMiniTable( + const upb_DefPool* s, const upb_MiniTableExtension* ext); + +UPB_API const upb_FieldDef* upb_DefPool_FindExtensionByName( + const upb_DefPool* s, const char* sym); + +const upb_FieldDef* upb_DefPool_FindExtensionByNameWithSize( + const upb_DefPool* s, const char* name, size_t size); + +const upb_FieldDef* upb_DefPool_FindExtensionByNumber(const upb_DefPool* s, + const upb_MessageDef* m, + int32_t fieldnum); + +UPB_API const upb_ServiceDef* upb_DefPool_FindServiceByName( + const upb_DefPool* s, const char* name); + +const upb_ServiceDef* upb_DefPool_FindServiceByNameWithSize( + const upb_DefPool* s, const char* name, size_t size); + +const upb_FileDef* upb_DefPool_FindFileContainingSymbol(const upb_DefPool* s, + const char* name); + +UPB_API const upb_FileDef* upb_DefPool_AddFile( + upb_DefPool* s, const google_protobuf_FileDescriptorProto* file_proto, + upb_Status* status); + +UPB_API const upb_ExtensionRegistry* upb_DefPool_ExtensionRegistry( + const upb_DefPool* s); + +const upb_FieldDef** upb_DefPool_GetAllExtensions(const upb_DefPool* s, + const upb_MessageDef* m, + size_t* count); + +// If called, closed enums will be treated as open enums. This is non-standard +// behavior and will cause conformance tests to fail, but it is more useful +// behavior overall and can be used in situations where where the +// non-conformance is acceptable. +// +// This function may only be called immediately after upb_DefPool_New(). +// It is an error to call it on an existing def pool or after defs have +// already been added to the pool. +// +// Note: we still require that implicit presence fields have zero as their +// default value. +UPB_API void upb_DefPool_DisableClosedEnumChecking(upb_DefPool* s); +bool upb_DefPool_ClosedEnumCheckingDisabled(const upb_DefPool* s); + +// If called, implicit field presence will be disabled. +// This is non-standard behavior and will cause conformance tests to fail, but +// it can be used in situations where where the non-conformance is acceptable. +// +// This function may only be called immediately after upb_DefPool_New(). +// It is an error to call it on an existing def pool or after defs have +// already been added to the pool. +UPB_API void upb_DefPool_DisableImplicitFieldPresence(upb_DefPool* s); +bool upb_DefPool_ImplicitFieldPresenceDisabled(const upb_DefPool* s); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_DEF_POOL_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_ENUM_DEF_H_ +#define UPB_REFLECTION_ENUM_DEF_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +bool upb_EnumDef_CheckNumber(const upb_EnumDef* e, int32_t num); +const upb_MessageDef* upb_EnumDef_ContainingType(const upb_EnumDef* e); +int32_t upb_EnumDef_Default(const upb_EnumDef* e); +UPB_API const upb_FileDef* upb_EnumDef_File(const upb_EnumDef* e); +const upb_EnumValueDef* upb_EnumDef_FindValueByName(const upb_EnumDef* e, + const char* name); +UPB_API const upb_EnumValueDef* upb_EnumDef_FindValueByNameWithSize( + const upb_EnumDef* e, const char* name, size_t size); +UPB_API const upb_EnumValueDef* upb_EnumDef_FindValueByNumber( + const upb_EnumDef* e, int32_t num); +UPB_API const char* upb_EnumDef_FullName(const upb_EnumDef* e); +bool upb_EnumDef_HasOptions(const upb_EnumDef* e); +bool upb_EnumDef_IsClosed(const upb_EnumDef* e); + +// Creates a mini descriptor string for an enum, returns true on success. +bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a, + upb_StringView* out); + +const char* upb_EnumDef_Name(const upb_EnumDef* e); +const google_protobuf_EnumOptions* upb_EnumDef_Options(const upb_EnumDef* e); +const google_protobuf_FeatureSet* upb_EnumDef_ResolvedFeatures(const upb_EnumDef* e); +google_protobuf_SymbolVisibility upb_EnumDef_Visibility(const upb_EnumDef* e); + +upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i); +int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e); + +const upb_EnumReservedRange* upb_EnumDef_ReservedRange(const upb_EnumDef* e, + int i); +int upb_EnumDef_ReservedRangeCount(const upb_EnumDef* e); + +UPB_API const upb_EnumValueDef* upb_EnumDef_Value(const upb_EnumDef* e, int i); +UPB_API int upb_EnumDef_ValueCount(const upb_EnumDef* e); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_ENUM_DEF_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_ENUM_VALUE_DEF_H_ +#define UPB_REFLECTION_ENUM_VALUE_DEF_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +const upb_EnumDef* upb_EnumValueDef_Enum(const upb_EnumValueDef* v); +const char* upb_EnumValueDef_FullName(const upb_EnumValueDef* v); +bool upb_EnumValueDef_HasOptions(const upb_EnumValueDef* v); +uint32_t upb_EnumValueDef_Index(const upb_EnumValueDef* v); +UPB_API const char* upb_EnumValueDef_Name(const upb_EnumValueDef* v); +UPB_API int32_t upb_EnumValueDef_Number(const upb_EnumValueDef* v); +const google_protobuf_EnumValueOptions* upb_EnumValueDef_Options( + const upb_EnumValueDef* v); +const google_protobuf_FeatureSet* upb_EnumValueDef_ResolvedFeatures( + const upb_EnumValueDef* e); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_ENUM_VALUE_DEF_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_EXTENSION_RANGE_H_ +#define UPB_REFLECTION_EXTENSION_RANGE_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t upb_ExtensionRange_Start(const upb_ExtensionRange* r); +int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r); + +bool upb_ExtensionRange_HasOptions(const upb_ExtensionRange* r); +const google_protobuf_ExtensionRangeOptions* upb_ExtensionRange_Options( + const upb_ExtensionRange* r); +const google_protobuf_FeatureSet* upb_ExtensionRange_ResolvedFeatures( + const upb_ExtensionRange* e); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_EXTENSION_RANGE_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_FIELD_DEF_H_ +#define UPB_REFLECTION_FIELD_DEF_H_ + +#include + + +// Must be last. + +// Maximum field number allowed for FieldDefs. +// This is an inherent limit of the protobuf wire format. +#define kUpb_MaxFieldNumber ((1 << 29) - 1) + +#ifdef __cplusplus +extern "C" { +#endif + +const upb_OneofDef* upb_FieldDef_ContainingOneof(const upb_FieldDef* f); +UPB_API const upb_MessageDef* upb_FieldDef_ContainingType( + const upb_FieldDef* f); +UPB_API upb_CType upb_FieldDef_CType(const upb_FieldDef* f); +UPB_API upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f); +UPB_API const upb_EnumDef* upb_FieldDef_EnumSubDef(const upb_FieldDef* f); +const upb_MessageDef* upb_FieldDef_ExtensionScope(const upb_FieldDef* f); +UPB_API const upb_FileDef* upb_FieldDef_File(const upb_FieldDef* f); +const char* upb_FieldDef_FullName(const upb_FieldDef* f); +bool upb_FieldDef_HasDefault(const upb_FieldDef* f); +bool upb_FieldDef_HasJsonName(const upb_FieldDef* f); +bool upb_FieldDef_HasOptions(const upb_FieldDef* f); +UPB_API bool upb_FieldDef_HasPresence(const upb_FieldDef* f); +bool upb_FieldDef_HasSubDef(const upb_FieldDef* f); +uint32_t upb_FieldDef_Index(const upb_FieldDef* f); +UPB_API bool upb_FieldDef_IsEnum(const upb_FieldDef* f); +bool upb_FieldDef_IsExtension(const upb_FieldDef* f); +UPB_API bool upb_FieldDef_IsMap(const upb_FieldDef* f); +bool upb_FieldDef_IsOptional(const upb_FieldDef* f); +bool _upb_FieldDef_IsPackable(const upb_FieldDef* f); +UPB_API bool upb_FieldDef_IsPacked(const upb_FieldDef* f); +bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f); +UPB_API bool upb_FieldDef_IsRepeated(const upb_FieldDef* f); +UPB_API bool upb_FieldDef_IsRequired(const upb_FieldDef* f); +bool upb_FieldDef_IsString(const upb_FieldDef* f); +UPB_API bool upb_FieldDef_IsSubMessage(const upb_FieldDef* f); +UPB_API const char* upb_FieldDef_JsonName(const upb_FieldDef* f); +UPB_API upb_Label upb_FieldDef_Label(const upb_FieldDef* f); +uint32_t upb_FieldDef_LayoutIndex(const upb_FieldDef* f); +UPB_API const upb_MessageDef* upb_FieldDef_MessageSubDef(const upb_FieldDef* f); +bool _upb_FieldDef_ValidateUtf8(const upb_FieldDef* f); +bool _upb_FieldDef_IsGroupLike(const upb_FieldDef* f); + +// Creates a mini descriptor string for a field, returns true on success. +bool upb_FieldDef_MiniDescriptorEncode(const upb_FieldDef* f, upb_Arena* a, + upb_StringView* out); + +const upb_MiniTableField* upb_FieldDef_MiniTable(const upb_FieldDef* f); +const upb_MiniTableExtension* upb_FieldDef_MiniTableExtension( + const upb_FieldDef* f); +UPB_API const char* upb_FieldDef_Name(const upb_FieldDef* f); +UPB_API uint32_t upb_FieldDef_Number(const upb_FieldDef* f); +const google_protobuf_FieldOptions* upb_FieldDef_Options(const upb_FieldDef* f); +const google_protobuf_FeatureSet* upb_FieldDef_ResolvedFeatures(const upb_FieldDef* f); +UPB_API const upb_OneofDef* upb_FieldDef_RealContainingOneof( + const upb_FieldDef* f); +UPB_API upb_FieldType upb_FieldDef_Type(const upb_FieldDef* f); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_FIELD_DEF_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_FILE_DEF_H_ +#define UPB_REFLECTION_FILE_DEF_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API const char* upb_FileDef_EditionName(int edition); + +const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i); +int upb_FileDef_DependencyCount(const upb_FileDef* f); +bool upb_FileDef_HasOptions(const upb_FileDef* f); +UPB_API const char* upb_FileDef_Name(const upb_FileDef* f); +const google_protobuf_FileOptions* upb_FileDef_Options(const upb_FileDef* f); +const google_protobuf_FeatureSet* upb_FileDef_ResolvedFeatures(const upb_FileDef* f); +const char* upb_FileDef_Package(const upb_FileDef* f); +google_protobuf_Edition upb_FileDef_Edition(const upb_FileDef* f); +UPB_API const upb_DefPool* upb_FileDef_Pool(const upb_FileDef* f); + +const upb_FileDef* upb_FileDef_PublicDependency(const upb_FileDef* f, int i); +int upb_FileDef_PublicDependencyCount(const upb_FileDef* f); + +const upb_ServiceDef* upb_FileDef_Service(const upb_FileDef* f, int i); +int upb_FileDef_ServiceCount(const upb_FileDef* f); + +const upb_EnumDef* upb_FileDef_TopLevelEnum(const upb_FileDef* f, int i); +int upb_FileDef_TopLevelEnumCount(const upb_FileDef* f); + +const upb_FieldDef* upb_FileDef_TopLevelExtension(const upb_FileDef* f, int i); +int upb_FileDef_TopLevelExtensionCount(const upb_FileDef* f); + +const upb_MessageDef* upb_FileDef_TopLevelMessage(const upb_FileDef* f, int i); +int upb_FileDef_TopLevelMessageCount(const upb_FileDef* f); + +const upb_FileDef* upb_FileDef_WeakDependency(const upb_FileDef* f, int i); +int upb_FileDef_WeakDependencyCount(const upb_FileDef* f); + +// Returns whether |symbol| is transitively included by |f| +bool upb_FileDef_Resolves(const upb_FileDef* f, const char* symbol); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_FILE_DEF_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_MESSAGE_DEF_H_ +#define UPB_REFLECTION_MESSAGE_DEF_H_ + + +// Must be last. + +// Well-known field tag numbers for map-entry messages. +#define kUpb_MapEntry_KeyFieldNumber 1 +#define kUpb_MapEntry_ValueFieldNumber 2 + +// Well-known field tag numbers for Any messages. +#define kUpb_Any_TypeFieldNumber 1 +#define kUpb_Any_ValueFieldNumber 2 + +// Well-known field tag numbers for duration messages. +#define kUpb_Duration_SecondsFieldNumber 1 +#define kUpb_Duration_NanosFieldNumber 2 + +// Well-known field tag numbers for timestamp messages. +#define kUpb_Timestamp_SecondsFieldNumber 1 +#define kUpb_Timestamp_NanosFieldNumber 2 + +// All the different kind of well known type messages. For simplicity of check, +// number wrappers and string wrappers are grouped together. Make sure the +// order and number of these groups are not changed. +typedef enum { + kUpb_WellKnown_Unspecified, + kUpb_WellKnown_Any, + kUpb_WellKnown_FieldMask, + kUpb_WellKnown_Duration, + kUpb_WellKnown_Timestamp, + + // number wrappers + kUpb_WellKnown_DoubleValue, + kUpb_WellKnown_FloatValue, + kUpb_WellKnown_Int64Value, + kUpb_WellKnown_UInt64Value, + kUpb_WellKnown_Int32Value, + kUpb_WellKnown_UInt32Value, + + // string wrappers + kUpb_WellKnown_StringValue, + kUpb_WellKnown_BytesValue, + kUpb_WellKnown_BoolValue, + kUpb_WellKnown_Value, + kUpb_WellKnown_ListValue, + kUpb_WellKnown_Struct, +} upb_WellKnown; + +#ifdef __cplusplus +extern "C" { +#endif + +const upb_MessageDef* upb_MessageDef_ContainingType(const upb_MessageDef* m); + +const upb_ExtensionRange* upb_MessageDef_ExtensionRange(const upb_MessageDef* m, + int i); +int upb_MessageDef_ExtensionRangeCount(const upb_MessageDef* m); + +UPB_API const upb_FieldDef* upb_MessageDef_Field(const upb_MessageDef* m, + int i); +UPB_API int upb_MessageDef_FieldCount(const upb_MessageDef* m); + +UPB_API const upb_FileDef* upb_MessageDef_File(const upb_MessageDef* m); + +// Returns a field by either JSON name or regular proto name. +const upb_FieldDef* upb_MessageDef_FindByJsonNameWithSize( + const upb_MessageDef* m, const char* name, size_t size); +UPB_INLINE const upb_FieldDef* upb_MessageDef_FindByJsonName( + const upb_MessageDef* m, const char* name) { + return upb_MessageDef_FindByJsonNameWithSize(m, name, strlen(name)); +} + +// Lookup of either field or oneof by name. Returns whether either was found. +// If the return is true, then the found def will be set, and the non-found +// one set to NULL. +UPB_API bool upb_MessageDef_FindByNameWithSize(const upb_MessageDef* m, + const char* name, size_t size, + const upb_FieldDef** f, + const upb_OneofDef** o); +UPB_INLINE bool upb_MessageDef_FindByName(const upb_MessageDef* m, + const char* name, + const upb_FieldDef** f, + const upb_OneofDef** o) { + return upb_MessageDef_FindByNameWithSize(m, name, strlen(name), f, o); +} + +const upb_FieldDef* upb_MessageDef_FindFieldByName(const upb_MessageDef* m, + const char* name); +UPB_API const upb_FieldDef* upb_MessageDef_FindFieldByNameWithSize( + const upb_MessageDef* m, const char* name, size_t size); +UPB_API const upb_FieldDef* upb_MessageDef_FindFieldByNumber( + const upb_MessageDef* m, uint32_t i); +const upb_OneofDef* upb_MessageDef_FindOneofByName(const upb_MessageDef* m, + const char* name); +UPB_API const upb_OneofDef* upb_MessageDef_FindOneofByNameWithSize( + const upb_MessageDef* m, const char* name, size_t size); +UPB_API const char* upb_MessageDef_FullName(const upb_MessageDef* m); +bool upb_MessageDef_HasOptions(const upb_MessageDef* m); +bool upb_MessageDef_IsMapEntry(const upb_MessageDef* m); +bool upb_MessageDef_IsMessageSet(const upb_MessageDef* m); + +// Creates a mini descriptor string for a message, returns true on success. +bool upb_MessageDef_MiniDescriptorEncode(const upb_MessageDef* m, upb_Arena* a, + upb_StringView* out); + +UPB_API const upb_MiniTable* upb_MessageDef_MiniTable(const upb_MessageDef* m); +const char* upb_MessageDef_Name(const upb_MessageDef* m); + +const upb_EnumDef* upb_MessageDef_NestedEnum(const upb_MessageDef* m, int i); +const upb_FieldDef* upb_MessageDef_NestedExtension(const upb_MessageDef* m, + int i); +const upb_MessageDef* upb_MessageDef_NestedMessage(const upb_MessageDef* m, + int i); + +int upb_MessageDef_NestedEnumCount(const upb_MessageDef* m); +int upb_MessageDef_NestedExtensionCount(const upb_MessageDef* m); +int upb_MessageDef_NestedMessageCount(const upb_MessageDef* m); + +UPB_API const upb_OneofDef* upb_MessageDef_Oneof(const upb_MessageDef* m, + int i); +UPB_API int upb_MessageDef_OneofCount(const upb_MessageDef* m); +int upb_MessageDef_RealOneofCount(const upb_MessageDef* m); + +const google_protobuf_MessageOptions* upb_MessageDef_Options(const upb_MessageDef* m); +const google_protobuf_FeatureSet* upb_MessageDef_ResolvedFeatures( + const upb_MessageDef* m); + +upb_StringView upb_MessageDef_ReservedName(const upb_MessageDef* m, int i); +int upb_MessageDef_ReservedNameCount(const upb_MessageDef* m); + +const upb_MessageReservedRange* upb_MessageDef_ReservedRange( + const upb_MessageDef* m, int i); +int upb_MessageDef_ReservedRangeCount(const upb_MessageDef* m); + +UPB_API upb_WellKnown upb_MessageDef_WellKnownType(const upb_MessageDef* m); +UPB_API google_protobuf_SymbolVisibility +upb_MessageDef_Visibility(const upb_MessageDef* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_MESSAGE_DEF_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_METHOD_DEF_H_ +#define UPB_REFLECTION_METHOD_DEF_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m); +const char* upb_MethodDef_FullName(const upb_MethodDef* m); +bool upb_MethodDef_HasOptions(const upb_MethodDef* m); +int upb_MethodDef_Index(const upb_MethodDef* m); +UPB_API const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m); +UPB_API const char* upb_MethodDef_Name(const upb_MethodDef* m); +UPB_API const google_protobuf_MethodOptions* upb_MethodDef_Options( + const upb_MethodDef* m); +const google_protobuf_FeatureSet* upb_MethodDef_ResolvedFeatures(const upb_MethodDef* m); +UPB_API const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m); +UPB_API bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m); +UPB_API const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_METHOD_DEF_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_ONEOF_DEF_H_ +#define UPB_REFLECTION_ONEOF_DEF_H_ + +#include +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API const upb_MessageDef* upb_OneofDef_ContainingType( + const upb_OneofDef* o); +UPB_API const upb_FieldDef* upb_OneofDef_Field(const upb_OneofDef* o, int i); +UPB_API int upb_OneofDef_FieldCount(const upb_OneofDef* o); +const char* upb_OneofDef_FullName(const upb_OneofDef* o); +bool upb_OneofDef_HasOptions(const upb_OneofDef* o); +uint32_t upb_OneofDef_Index(const upb_OneofDef* o); +bool upb_OneofDef_IsSynthetic(const upb_OneofDef* o); +const upb_FieldDef* upb_OneofDef_LookupName(const upb_OneofDef* o, + const char* name); +const upb_FieldDef* upb_OneofDef_LookupNameWithSize(const upb_OneofDef* o, + const char* name, + size_t size); +const upb_FieldDef* upb_OneofDef_LookupNumber(const upb_OneofDef* o, + uint32_t num); +UPB_API const char* upb_OneofDef_Name(const upb_OneofDef* o); +int upb_OneofDef_numfields(const upb_OneofDef* o); +const google_protobuf_OneofOptions* upb_OneofDef_Options(const upb_OneofDef* o); +const google_protobuf_FeatureSet* upb_OneofDef_ResolvedFeatures(const upb_OneofDef* o); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_ONEOF_DEF_H_ */ + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_SERVICE_DEF_H_ +#define UPB_REFLECTION_SERVICE_DEF_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_API const upb_FileDef* upb_ServiceDef_File(const upb_ServiceDef* s); +const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s, + const char* name); +const upb_MethodDef* upb_ServiceDef_FindMethodByNameWithSize( + const upb_ServiceDef* s, const char* name, size_t len); +UPB_API const char* upb_ServiceDef_FullName(const upb_ServiceDef* s); +bool upb_ServiceDef_HasOptions(const upb_ServiceDef* s); +int upb_ServiceDef_Index(const upb_ServiceDef* s); +UPB_API const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s, + int i); +UPB_API int upb_ServiceDef_MethodCount(const upb_ServiceDef* s); +const char* upb_ServiceDef_Name(const upb_ServiceDef* s); +UPB_API const google_protobuf_ServiceOptions* upb_ServiceDef_Options( + const upb_ServiceDef* s); +const google_protobuf_FeatureSet* upb_ServiceDef_ResolvedFeatures( + const upb_ServiceDef* s); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_SERVICE_DEF_H_ */ +// IWYU pragma: end_exports + +#endif /* UPB_REFLECTION_DEF_H_ */ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_Arena* _upb_DefPool_Arena(const upb_DefPool* s); +size_t _upb_DefPool_BytesLoaded(const upb_DefPool* s); +upb_ExtensionRegistry* _upb_DefPool_ExtReg(const upb_DefPool* s); + +bool _upb_DefPool_InsertExt(upb_DefPool* s, const upb_MiniTableExtension* ext, + const upb_FieldDef* f); +bool _upb_DefPool_InsertSym(upb_DefPool* s, upb_StringView sym, upb_value v, + upb_Status* status); +bool _upb_DefPool_LookupSym(const upb_DefPool* s, const char* sym, size_t size, + upb_value* v); + +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 { + struct _upb_DefPool_Init** deps; // Dependencies of this file. + const upb_MiniTableFile* layout; + const char* filename; + upb_StringView descriptor; // Serialized descriptor. +} _upb_DefPool_Init; + +bool _upb_DefPool_LoadDefInit(upb_DefPool* s, const _upb_DefPool_Init* init); + +// Should only be directly called by tests. This variant lets us suppress +// the use of compiled-in tables, forcing a rebuild of the tables at runtime. +bool _upb_DefPool_LoadDefInitEx(upb_DefPool* s, const _upb_DefPool_Init* init, + bool rebuild_minitable); + +const upb_ExtensionRegistry* _upb_DefPool_GeneratedExtensionRegistry( + const upb_DefPool* s); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_DEF_POOL_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_ +#define UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_ + +#include +#include +#include +#include + + +// Must be last. + +// We want to copy the options verbatim into the destination options proto. +// We use serialize+parse as our deep copy. +#define UPB_DEF_SET_OPTIONS(target, desc_type, options_type, proto) \ + if (google_protobuf_##desc_type##_has_options(proto)) { \ + size_t size; \ + char* pb = google_protobuf_##options_type##_serialize( \ + google_protobuf_##desc_type##_options(proto), ctx->tmp_arena, &size); \ + if (!pb) _upb_DefBuilder_OomErr(ctx); \ + target = google_protobuf_##options_type##_parse_ex( \ + pb, size, _upb_DefPool_GeneratedExtensionRegistry(ctx->symtab), 0, \ + _upb_DefBuilder_Arena(ctx)); \ + if (!target) _upb_DefBuilder_OomErr(ctx); \ + } else { \ + target = (const google_protobuf_##options_type*)kUpbDefOptDefault; \ + } + +#ifdef __cplusplus +extern "C" { +#endif + +struct upb_DefBuilder { + upb_DefPool* symtab; + upb_strtable feature_cache; // Caches features by identity. + google_protobuf_FeatureSet* legacy_features; // For computing legacy features. + char* tmp_buf; // Temporary buffer in tmp_arena. + size_t tmp_buf_size; // Size of temporary buffer. + upb_FileDef* file; // File we are building. + upb_Arena* arena; // Allocate defs here. + 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. + jmp_buf err; // longjmp() on error. +}; + +extern const char* kUpbDefOptDefault; + +// ctx->status has already been set elsewhere so just fail/longjmp() +UPB_NORETURN void _upb_DefBuilder_FailJmp(upb_DefBuilder* ctx); + +UPB_NORETURN void _upb_DefBuilder_Errf(upb_DefBuilder* ctx, const char* fmt, + ...) UPB_PRINTF(2, 3); +UPB_NORETURN void _upb_DefBuilder_OomErr(upb_DefBuilder* ctx); + +const char* _upb_DefBuilder_MakeFullName(upb_DefBuilder* ctx, + const char* prefix, + upb_StringView name); + +// Given a symbol and the base symbol inside which it is defined, +// find the symbol's definition. +const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx, + const char* from_name_dbg, + const char* base, upb_StringView sym, + upb_deftype_t* type); + +const void* _upb_DefBuilder_Resolve(upb_DefBuilder* ctx, + const char* from_name_dbg, const char* base, + upb_StringView sym, upb_deftype_t type); + +char _upb_DefBuilder_ParseEscape(upb_DefBuilder* ctx, const upb_FieldDef* f, + const char** src, const char* end); + +const char* _upb_DefBuilder_FullToShort(const char* fullname); + +UPB_INLINE void* _upb_DefBuilder_Alloc(upb_DefBuilder* ctx, size_t bytes) { + if (bytes == 0) return NULL; + void* ret = upb_Arena_Malloc(ctx->arena, bytes); + if (!ret) _upb_DefBuilder_OomErr(ctx); + return ret; +} + +/* Allocates an array of `count` elements, checking for size_t overflow */ +UPB_INLINE void* _upb_DefBuilder_AllocCounted(upb_DefBuilder* ctx, size_t size, + size_t count) { + if (count == 0) return NULL; + if (SIZE_MAX / size < count) { + _upb_DefBuilder_OomErr(ctx); + } + return _upb_DefBuilder_Alloc(ctx, size * count); +} + +#define UPB_DEFBUILDER_ALLOCARRAY(ctx, type, count) \ + ((type*)_upb_DefBuilder_AllocCounted(ctx, sizeof(type), (count))) + +// Adds a symbol |v| to the symtab, which must be a def pointer previously +// packed with pack_def(). The def's pointer to upb_FileDef* must be set before +// adding, so we know which entries to remove if building this file fails. +UPB_INLINE void _upb_DefBuilder_Add(upb_DefBuilder* ctx, const char* name, + upb_value v) { + upb_StringView sym = {.data = name, .size = strlen(name)}; + bool ok = _upb_DefPool_InsertSym(ctx->symtab, sym, v, ctx->status); + if (!ok) _upb_DefBuilder_FailJmp(ctx); +} + +UPB_INLINE upb_Arena* _upb_DefBuilder_Arena(const upb_DefBuilder* ctx) { + return ctx->arena; +} + +UPB_INLINE upb_FileDef* _upb_DefBuilder_File(const upb_DefBuilder* ctx) { + return ctx->file; +} + +// This version of CheckIdent() is only called by other, faster versions after +// they detect a parsing error. +void _upb_DefBuilder_CheckIdentSlow(upb_DefBuilder* ctx, upb_StringView name, + bool full); + +// Verify a full identifier string. This is slightly more complicated than +// verifying a relative identifier string because we must track '.' chars. +UPB_INLINE void _upb_DefBuilder_CheckIdentFull(upb_DefBuilder* ctx, + upb_StringView name) { + bool good = name.size > 0; + bool start = true; + + for (size_t i = 0; i < name.size; i++) { + const char c = name.data[i]; + const char d = c | 0x20; // force lowercase + const bool is_alpha = (('a' <= d) & (d <= 'z')) | (c == '_'); + const bool is_numer = ('0' <= c) & (c <= '9') & !start; + const bool is_dot = (c == '.') & !start; + + good &= is_alpha | is_numer | is_dot; + start = is_dot; + } + + if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, true); +} + +UPB_INLINE bool _upb_DefBuilder_IsLegacyEdition(google_protobuf_Edition edition) { + // Should only be called for a real edition, not a placeholder like + // EDITION_LEGACY. + UPB_ASSERT(edition >= google_protobuf_EDITION_PROTO2); + return edition <= google_protobuf_EDITION_PROTO3; +} + +// Returns true if the returned feature set is new and must be populated. +bool _upb_DefBuilder_GetOrCreateFeatureSet(upb_DefBuilder* ctx, + const google_protobuf_FeatureSet* parent, + upb_StringView key, + google_protobuf_FeatureSet** set); + +const google_protobuf_FeatureSet* _upb_DefBuilder_DoResolveFeatures( + upb_DefBuilder* ctx, const google_protobuf_FeatureSet* parent, + const google_protobuf_FeatureSet* child, bool is_implicit); + +UPB_INLINE const google_protobuf_FeatureSet* _upb_DefBuilder_ResolveFeatures( + upb_DefBuilder* ctx, const google_protobuf_FeatureSet* parent, + const google_protobuf_FeatureSet* child) { + return _upb_DefBuilder_DoResolveFeatures(ctx, parent, child, false); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_FILE_DEF_INTERNAL_H_ +#define UPB_REFLECTION_FILE_DEF_INTERNAL_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +const upb_MiniTableExtension* _upb_FileDef_ExtensionMiniTable( + const upb_FileDef* f, int i); +const int32_t* _upb_FileDef_PublicDependencyIndexes(const upb_FileDef* f); +const int32_t* _upb_FileDef_WeakDependencyIndexes(const upb_FileDef* f); +bool _upb_FileDef_ClosedEnumCheckingDisabled(const upb_FileDef* f); +bool _upb_FileDef_ImplicitFieldPresenceDisabled(const upb_FileDef* f); + +// upb_FileDef_Package() returns "" if f->package is NULL, this does not. +const char* _upb_FileDef_RawPackage(const upb_FileDef* f); + +void _upb_FileDef_Create(upb_DefBuilder* ctx, + const google_protobuf_FileDescriptorProto* file_proto); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_FILE_DEF_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_MESSAGE_DEF_INTERNAL_H_ +#define UPB_REFLECTION_MESSAGE_DEF_INTERNAL_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_MessageDef* _upb_MessageDef_At(const upb_MessageDef* m, int i); +bool _upb_MessageDef_InMessageSet(const upb_MessageDef* m); +bool _upb_MessageDef_Insert(upb_MessageDef* m, const char* name, size_t size, + upb_value v, upb_Arena* a); +void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m, + const upb_FieldDef* f); +bool _upb_MessageDef_IsValidExtensionNumber(const upb_MessageDef* m, int n); +void _upb_MessageDef_CreateMiniTable(upb_DefBuilder* ctx, upb_MessageDef* m); +void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx, + const upb_MessageDef* m); +void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m); + +// Allocate and initialize an array of |n| message defs. +upb_MessageDef* _upb_MessageDefs_New( + upb_DefBuilder* ctx, int n, const google_protobuf_DescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, + const upb_MessageDef* containing_type); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_MESSAGE_DEF_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_ +#define UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_ + +// This file contains the serialized FeatureSetDefaults object for +// language-independent features and (possibly at some point) for upb-specific +// features. This is used for feature resolution under Editions. +// NOLINTBEGIN +// clang-format off +#define UPB_INTERNAL_UPB_EDITION_DEFAULTS "\n\027\030\204\007\"\000*\020\010\001\020\002\030\002 \003(\0010\0028\002@\001\n\027\030\347\007\"\000*\020\010\002\020\001\030\001 \002(\0010\0018\002@\001\n\027\030\350\007\"\014\010\001\020\001\030\001 \002(\0010\001*\0048\002@\001\n\027\030\351\007\"\020\010\001\020\001\030\001 \002(\0010\0018\001@\002*\000\n\027\030\217N\"\020\010\001\020\001\030\001 \002(\0010\0018\003@\002*\000 \346\007(\351\007" +// clang-format on +// NOLINTEND + +#endif // UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_ + +#ifndef UPB_REFLECTION_DESC_STATE_INTERNAL_H_ +#define UPB_REFLECTION_DESC_STATE_INTERNAL_H_ + + +// Must be last. + +// Manages the storage for mini descriptor strings as they are being encoded. +// TODO: Move some of this state directly into the encoder, maybe. +typedef struct { + upb_MtDataEncoder e; + size_t bufsize; + char* buf; + char* ptr; +} upb_DescState; + +#ifdef __cplusplus +extern "C" { +#endif + +UPB_INLINE void _upb_DescState_Init(upb_DescState* d) { + d->bufsize = kUpb_MtDataEncoder_MinSize * 2; + d->buf = NULL; + d->ptr = NULL; +} + +bool _upb_DescState_Grow(upb_DescState* d, upb_Arena* a); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_DESC_STATE_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_ENUM_DEF_INTERNAL_H_ +#define UPB_REFLECTION_ENUM_DEF_INTERNAL_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_EnumDef* _upb_EnumDef_At(const upb_EnumDef* e, int i); +bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a); +const upb_MiniTableEnum* _upb_EnumDef_MiniTable(const upb_EnumDef* e); + +// Allocate and initialize an array of |n| enum defs. +upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n, + const google_protobuf_EnumDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, + const upb_MessageDef* containing_type); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_ENUM_DEF_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_ENUM_RESERVED_RANGE_INTERNAL_H_ +#define UPB_REFLECTION_ENUM_RESERVED_RANGE_INTERNAL_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_EnumReservedRange* _upb_EnumReservedRange_At(const upb_EnumReservedRange* r, + int i); + +// Allocate and initialize an array of |n| reserved ranges owned by |e|. +upb_EnumReservedRange* _upb_EnumReservedRanges_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* protos, + const upb_EnumDef* e); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_ENUM_RESERVED_RANGE_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_ENUM_VALUE_DEF_INTERNAL_H_ +#define UPB_REFLECTION_ENUM_VALUE_DEF_INTERNAL_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_EnumValueDef* _upb_EnumValueDef_At(const upb_EnumValueDef* v, int i); + +// Allocate and initialize an array of |n| enum value defs owned by |e|. +upb_EnumValueDef* _upb_EnumValueDefs_New( + upb_DefBuilder* ctx, const char* prefix, int n, + const google_protobuf_EnumValueDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, upb_EnumDef* e, bool* is_sorted); + +const upb_EnumValueDef** _upb_EnumValueDefs_Sorted(const upb_EnumValueDef* v, + size_t n, upb_Arena* a); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_ENUM_VALUE_DEF_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_INTERNAL_STRDUP2_H_ +#define UPB_REFLECTION_INTERNAL_STRDUP2_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Variant that works with a length-delimited rather than NULL-delimited string, +// as supported by strtable. +char* upb_strdup2(const char* s, size_t len, upb_Arena* a); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_INTERNAL_STRDUP2_H_ */ + +#ifndef UPB_REFLECTION_EXTENSION_RANGE_INTERNAL_H_ +#define UPB_REFLECTION_EXTENSION_RANGE_INTERNAL_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i); + +// Allocate and initialize an array of |n| extension ranges owned by |m|. +upb_ExtensionRange* _upb_ExtensionRanges_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_DescriptorProto_ExtensionRange* const* protos, + const google_protobuf_FeatureSet* parent_features, const upb_MessageDef* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_EXTENSION_RANGE_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_FIELD_DEF_INTERNAL_H_ +#define UPB_REFLECTION_FIELD_DEF_INTERNAL_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_FieldDef* _upb_FieldDef_At(const upb_FieldDef* f, int i); + +bool _upb_FieldDef_IsClosedEnum(const upb_FieldDef* f); +bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f); +int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f); +uint64_t _upb_FieldDef_Modifiers(const upb_FieldDef* f); +void _upb_FieldDef_Resolve(upb_DefBuilder* ctx, const char* prefix, + upb_FieldDef* f); +void _upb_FieldDef_BuildMiniTableExtension(upb_DefBuilder* ctx, + const upb_FieldDef* f); + +// Allocate and initialize an array of |n| extensions (field defs). +upb_FieldDef* _upb_Extensions_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_FieldDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, const char* prefix, + upb_MessageDef* m); + +// Allocate and initialize an array of |n| field defs. +upb_FieldDef* _upb_FieldDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_FieldDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, const char* prefix, + upb_MessageDef* m, bool* is_sorted); + +// Allocate and return a list of pointers to the |n| field defs in |ff|, +// sorted by field number. +const upb_FieldDef** _upb_FieldDefs_Sorted(const upb_FieldDef* f, int n, + upb_Arena* a); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_FIELD_DEF_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_ONEOF_DEF_INTERNAL_H_ +#define UPB_REFLECTION_ONEOF_DEF_INTERNAL_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_OneofDef* _upb_OneofDef_At(const upb_OneofDef* o, int i); +void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o, + const upb_FieldDef* f, const char* name, size_t size); + +// Allocate and initialize an array of |n| oneof defs owned by |m|. +upb_OneofDef* _upb_OneofDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_OneofDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, upb_MessageDef* m); + +size_t _upb_OneofDefs_Finalize(upb_DefBuilder* ctx, upb_MessageDef* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_ONEOF_DEF_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_SERVICE_DEF_INTERNAL_H_ +#define UPB_REFLECTION_SERVICE_DEF_INTERNAL_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_ServiceDef* _upb_ServiceDef_At(const upb_ServiceDef* s, int i); + +// Allocate and initialize an array of |n| service defs. +upb_ServiceDef* _upb_ServiceDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_ServiceDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features); + +void _upb_ServiceDef_InsertMethod(upb_DefBuilder* ctx, upb_ServiceDef* s, + const upb_MethodDef* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_SERVICE_DEF_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_MESSAGE_H_ +#define UPB_REFLECTION_MESSAGE_H_ + +#include + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +// Returns a mutable pointer to a map, array, or submessage value. If the given +// arena is non-NULL this will construct a new object if it was not previously +// present. May not be called for primitive fields. +UPB_API upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg, + const upb_FieldDef* f, + upb_Arena* a); + +// Returns the field that is set in the oneof, or NULL if none are set. +UPB_API const upb_FieldDef* upb_Message_WhichOneofByDef(const upb_Message* msg, + const upb_OneofDef* o); + +// Clear all data and unknown fields. +void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m); + +// Clears any field presence and sets the value back to its default. +UPB_API void upb_Message_ClearFieldByDef(upb_Message* msg, + const upb_FieldDef* f); + +// May only be called for fields where upb_FieldDef_HasPresence(f) == true. +UPB_API bool upb_Message_HasFieldByDef(const upb_Message* msg, + const upb_FieldDef* f); + +// Returns the value in the message associated with this field def. +UPB_API upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg, + const upb_FieldDef* f); + +// Sets the given field to the given value. For a msg/array/map/string, the +// caller must ensure that the target data outlives |msg| (by living either in +// the same arena or a different arena that outlives it). +// +// Returns false if allocation fails. +UPB_API bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f, + upb_MessageValue val, upb_Arena* a); + +// Iterate over present fields. +// +// size_t iter = kUpb_Message_Begin; +// const upb_FieldDef *f; +// upb_MessageValue val; +// while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) { +// process_field(f, val); +// } +// +// If ext_pool is NULL, no extensions will be returned. If the given symtab +// returns extensions that don't match what is in this message, those extensions +// will be skipped. + +#define kUpb_Message_Begin -1 + +UPB_API bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m, + const upb_DefPool* ext_pool, + const upb_FieldDef** f, upb_MessageValue* val, + size_t* iter); + +// Clears all unknown field data from this message and all submessages. +UPB_API bool upb_Message_DiscardUnknown(upb_Message* msg, + const upb_MessageDef* m, + const upb_DefPool* ext_pool, + int maxdepth); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_MESSAGE_H_ */ + +#ifndef UPB_REFLECTION_MESSAGE_RESERVED_RANGE_INTERNAL_H_ +#define UPB_REFLECTION_MESSAGE_RESERVED_RANGE_INTERNAL_H_ + + +// IWYU pragma: private, include "upb/reflection/def.h" + +#ifndef UPB_REFLECTION_MESSAGE_RESERVED_RANGE_H_ +#define UPB_REFLECTION_MESSAGE_RESERVED_RANGE_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t upb_MessageReservedRange_Start(const upb_MessageReservedRange* r); +int32_t upb_MessageReservedRange_End(const upb_MessageReservedRange* r); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_MESSAGE_RESERVED_RANGE_H_ */ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_MessageReservedRange* _upb_MessageReservedRange_At( + const upb_MessageReservedRange* r, int i); + +// Allocate and initialize an array of |n| reserved ranges owned by |m|. +upb_MessageReservedRange* _upb_MessageReservedRanges_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_DescriptorProto_ReservedRange* const* protos, + const upb_MessageDef* m); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_MESSAGE_RESERVED_RANGE_INTERNAL_H_ */ + +#ifndef UPB_REFLECTION_METHOD_DEF_INTERNAL_H_ +#define UPB_REFLECTION_METHOD_DEF_INTERNAL_H_ + + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +upb_MethodDef* _upb_MethodDef_At(const upb_MethodDef* m, int i); + +// Allocate and initialize an array of |n| method defs owned by |s|. +upb_MethodDef* _upb_MethodDefs_New( + upb_DefBuilder* ctx, int n, + const google_protobuf_MethodDescriptorProto* const* protos, + const google_protobuf_FeatureSet* parent_features, upb_ServiceDef* s); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_REFLECTION_METHOD_DEF_INTERNAL_H_ */ + +#ifndef UPB_TEXT_ENCODE_DEBUG_H_ +#define UPB_TEXT_ENCODE_DEBUG_H_ + +#include + + +#ifndef UPB_TEXT_OPTIONS_H_ +#define UPB_TEXT_OPTIONS_H_ + +enum { + // When set, prints everything on a single line. + UPB_TXTENC_SINGLELINE = 1, + + // When set, unknown fields are not printed. + UPB_TXTENC_SKIPUNKNOWN = 2, + + // When set, maps are *not* sorted (this avoids allocating tmp mem). + UPB_TXTENC_NOSORT = 4 +}; + +#endif // UPB_TEXT_OPTIONS_H_ + +// Must be last. + +#ifdef __cplusplus +extern "C" { +#endif + +/* Encodes the given |msg| to a pseudo-text format: Instead of printing field + * name to value entries, it will print field number to value entries; much like + * how unknown fields are printed in upb_TextEncode in this directory's + * encode.h. |mt| should correspond to the |msg|'s minitable. + * + * Output is placed in the given buffer, and always NULL-terminated. The output + * size (excluding NULL) is returned. This means that a return value >= |size| + * implies that the output was truncated. (These are the same semantics as + * snprintf()). */ +UPB_API size_t upb_DebugString(const upb_Message* msg, const upb_MiniTable* mt, + int options, char* buf, size_t size); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /* UPB_TEXT_ENCODE_DEBUG_H_ */ + +#ifndef UPB_TEXT_ENCODE_INTERNAL_H_ +#define UPB_TEXT_ENCODE_INTERNAL_H_ + +#include +#include + + +#ifndef UPB_PORT_VSNPRINTF_COMPAT_H_ +#define UPB_PORT_VSNPRINTF_COMPAT_H_ + +// Must be last. + +UPB_INLINE int _upb_vsnprintf(char* buf, size_t size, const char* fmt, + va_list ap) { +#if defined(__MINGW64__) || defined(__MINGW32__) || defined(_MSC_VER) + // The msvc runtime has a non-conforming vsnprintf() that requires the + // following compatibility code to become conformant. + int n = -1; + if (size != 0) n = _vsnprintf_s(buf, size, _TRUNCATE, fmt, ap); + if (n == -1) n = _vscprintf(fmt, ap); + return n; +#else + return vsnprintf(buf, size, fmt, ap); +#endif +} + + +#endif // UPB_PORT_VSNPRINTF_COMPAT_H_ +#include "utf8_range.h" + +// Must be last. + +typedef struct { + char *buf, *ptr, *end; + size_t overflow; + int indent_depth; + int options; + const struct upb_DefPool* ext_pool; + _upb_mapsorter sorter; +} txtenc; + +UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_PutBytes)(txtenc* e, + const void* data, + size_t len) { + size_t have = e->end - e->ptr; + if (UPB_LIKELY(have >= len)) { + memcpy(e->ptr, data, len); + e->ptr += len; + } else { + if (have) { + memcpy(e->ptr, data, have); + e->ptr += have; + } + e->overflow += (len - have); + } +} + +UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_PutStr)(txtenc* e, + const char* str) { + UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, str, strlen(str)); +} + +UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Printf)(txtenc* e, const char* fmt, + ...) { + size_t n; + size_t have = e->end - e->ptr; + va_list args; + + va_start(args, fmt); + n = _upb_vsnprintf(e->ptr, have, fmt, args); + va_end(args); + + if (UPB_LIKELY(have > n)) { + e->ptr += n; + } else { + e->ptr = UPB_PTRADD(e->ptr, have); + e->overflow += (n - have); + } +} + +UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Indent)(txtenc* e) { + if ((e->options & UPB_TXTENC_SINGLELINE) == 0) { + int i = e->indent_depth; + while (i-- > 0) { + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, " "); + } + } +} + +UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_EndField)(txtenc* e) { + if (e->options & UPB_TXTENC_SINGLELINE) { + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, " "); + } else { + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\n"); + } +} + +UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Escaped)(txtenc* e, + unsigned char ch) { + switch (ch) { + case '\n': + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\n"); + break; + case '\r': + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\r"); + break; + case '\t': + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\t"); + break; + case '\"': + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\\""); + break; + case '\'': + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\'"); + break; + case '\\': + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\\\"); + break; + default: + UPB_PRIVATE(_upb_TextEncode_Printf)(e, "\\%03o", ch); + break; + } +} + +// Returns true if `ch` needs to be escaped in TextFormat, independent of any +// UTF-8 validity issues. +UPB_INLINE bool UPB_PRIVATE(_upb_DefinitelyNeedsEscape)(unsigned char ch) { + if (ch < 32) return true; + switch (ch) { + case '\"': + case '\'': + case '\\': + case 127: + return true; + } + return false; +} + +UPB_INLINE bool UPB_PRIVATE(_upb_AsciiIsPrint)(unsigned char ch) { + return ch >= 32 && ch < 127; +} + +// Returns true if this is a high byte that requires UTF-8 validation. If the +// UTF-8 validation fails, we must escape the byte. +UPB_INLINE bool UPB_PRIVATE(_upb_NeedsUtf8Validation)(unsigned char ch) { + return ch > 127; +} + +// Returns the number of bytes in the prefix of `val` that do not need escaping. +// This is like utf8_range::SpanStructurallyValid(), except that it also +// terminates at any ASCII char that needs to be escaped in TextFormat (any char +// that has `DefinitelyNeedsEscape(ch) == true`). +// +// If we could get a variant of utf8_range::SpanStructurallyValid() that could +// terminate on any of these chars, that might be more efficient, but it would +// be much more complicated to modify that heavily SIMD code. +UPB_INLINE size_t UPB_PRIVATE(_SkipPassthroughBytes)(const char* ptr, + size_t size) { + for (size_t i = 0; i < size; i++) { + unsigned char uc = ptr[i]; + if (UPB_PRIVATE(_upb_DefinitelyNeedsEscape)(uc)) return i; + if (UPB_PRIVATE(_upb_NeedsUtf8Validation)(uc)) { + // Find the end of this region of consecutive high bytes, so that we only + // give high bytes to the UTF-8 checker. This avoids needing to perform + // a second scan of the ASCII characters looking for characters that + // need escaping. + // + // We assume that high bytes are less frequent than plain, printable ASCII + // bytes, so we accept the double-scan of high bytes. + size_t end = i + 1; + for (; end < size; end++) { + if (!UPB_PRIVATE(_upb_NeedsUtf8Validation)(ptr[end])) break; + } + size_t n = end - i; + size_t ok = utf8_range_ValidPrefix(ptr + i, n); + if (ok != n) return i + ok; + i += ok - 1; + } + } + return size; +} + +UPB_INLINE void UPB_PRIVATE(_upb_HardenedPrintString)(txtenc* e, + const char* ptr, + size_t len) { + // Print as UTF-8, while guarding against any invalid UTF-8 in the string + // field. + // + // If in the future we have a guaranteed invariant that invalid UTF-8 will + // never be present, we could avoid the UTF-8 check here. + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\""); + const char* end = ptr + len; + while (ptr < end) { + size_t n = UPB_PRIVATE(_SkipPassthroughBytes)(ptr, end - ptr); + if (n != 0) { + UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, ptr, n); + ptr += n; + if (ptr == end) break; + } + + // If repeated calls to CEscape() and PrintString() are expensive, we could + // consider batching them, at the cost of some complexity. + UPB_PRIVATE(_upb_TextEncode_Escaped)(e, *ptr); + ptr++; + } + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\""); +} + +UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Bytes)(txtenc* e, + upb_StringView data) { + const char* ptr = data.data; + const char* end = ptr + data.size; + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\""); + for (; ptr < end; ptr++) { + unsigned char uc = *ptr; + if (UPB_PRIVATE(_upb_AsciiIsPrint)(uc) && + !UPB_PRIVATE(_upb_DefinitelyNeedsEscape)(uc)) { + UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, ptr, 1); + } else { + UPB_PRIVATE(_upb_TextEncode_Escaped)(e, uc); + } + } + UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\""); +} + +UPB_INLINE size_t UPB_PRIVATE(_upb_TextEncode_Nullz)(txtenc* e, size_t size) { + size_t ret = e->ptr - e->buf + e->overflow; + + if (size > 0) { + if (e->ptr == e->end) e->ptr--; + *e->ptr = '\0'; + } + + return ret; +} + +const char* UPB_PRIVATE(_upb_TextEncode_Unknown)(txtenc* e, const char* ptr, + upb_EpsCopyInputStream* stream, + int groupnum); + +void UPB_PRIVATE(_upb_TextEncode_ParseUnknown)(txtenc* e, + const upb_Message* msg); + +// Must not be called for ctype = kUpb_CType_Enum, as they require different +// handling depending on whether or not we're doing reflection-based encoding. +void UPB_PRIVATE(_upb_TextEncode_Scalar)(txtenc* e, upb_MessageValue val, + upb_CType ctype); + + +#endif // UPB_TEXT_ENCODE_INTERNAL_H_ + +#ifndef UPB_WIRE_INTERNAL_CONSTANTS_H_ +#define UPB_WIRE_INTERNAL_CONSTANTS_H_ + +#define kUpb_WireFormat_DefaultDepthLimit 100 + +// MessageSet wire format is: +// message MessageSet { +// repeated group Item = 1 { +// required int32 type_id = 2; +// required bytes message = 3; +// } +// } + +enum { + kUpb_MsgSet_Item = 1, + kUpb_MsgSet_TypeId = 2, + kUpb_MsgSet_Message = 3, +}; + +#endif /* UPB_WIRE_INTERNAL_CONSTANTS_H_ */ + +/* + * Internal implementation details of the decoder that are shared between + * decode.c and decode_fast.c. + */ + +#ifndef UPB_WIRE_INTERNAL_DECODER_H_ +#define UPB_WIRE_INTERNAL_DECODER_H_ + +#include +#include +#include +#include + +#include "utf8_range.h" + +// Must be last. + +#define DECODE_NOGROUP (uint32_t)-1 + +typedef struct upb_Decoder { + upb_EpsCopyInputStream input; + const upb_ExtensionRegistry* extreg; + upb_Message* original_msg; // Pointer to preserve data to + int depth; // Tracks recursion depth to bound stack usage. + uint32_t end_group; // field number of END_GROUP tag, else DECODE_NOGROUP. + uint16_t options; + bool missing_required; + bool message_is_done; + union { + upb_Arena arena; + void* foo[UPB_ARENA_SIZE_HACK / sizeof(void*)]; + }; + upb_ErrorHandler err; + +#ifndef NDEBUG + char* trace_buf; + char* trace_ptr; + char* trace_end; +#endif +} upb_Decoder; + +UPB_INLINE const char* upb_Decoder_Init(upb_Decoder* d, const char* buf, + size_t size, + const upb_ExtensionRegistry* extreg, + int options, upb_Arena* arena, + char* trace_buf, size_t trace_size) { + upb_ErrorHandler_Init(&d->err); + upb_EpsCopyInputStream_InitWithErrorHandler(&d->input, &buf, size, &d->err); + + UPB_STATIC_ASSERT((int)kUpb_DecodeStatus_Ok == (int)kUpb_ErrorCode_Ok, + "mismatched error codes"); + UPB_STATIC_ASSERT( + (int)kUpb_DecodeStatus_OutOfMemory == (int)kUpb_ErrorCode_OutOfMemory, + "mismatched error codes"); + UPB_STATIC_ASSERT( + (int)kUpb_DecodeStatus_Malformed == (int)kUpb_ErrorCode_Malformed, + "mismatched error codes"); + + if (options & kUpb_DecodeOption_AlwaysValidateUtf8) { + // Fasttable decoder does not support this option. + options |= kUpb_DecodeOption_DisableFastTable; + } + + d->extreg = extreg; + d->depth = upb_DecodeOptions_GetEffectiveMaxDepth(options); + d->end_group = DECODE_NOGROUP; + d->options = (uint16_t)options; + d->missing_required = false; + d->message_is_done = false; +#ifndef NDEBUG + d->trace_buf = trace_buf; + d->trace_ptr = trace_buf; + d->trace_end = UPB_PTRADD(trace_buf, trace_size); +#endif + if (trace_buf) *trace_buf = 0; // Null-terminate. + + // Violating the encapsulation of the arena for performance reasons. + // This is a temporary arena that we swap into and swap out of when we are + // done. The temporary arena only needs to be able to handle allocation, + // not fuse or free, so it does not need many of the members to be initialized + // (particularly parent_or_count). + UPB_PRIVATE(_upb_Arena_SwapIn)(&d->arena, arena); + return buf; +} + +UPB_INLINE upb_DecodeStatus upb_Decoder_Destroy(upb_Decoder* d, + upb_Arena* arena) { + UPB_PRIVATE(_upb_Arena_SwapOut)(arena, &d->arena); + return (upb_DecodeStatus)d->err.code; +} + +#ifndef NDEBUG +UPB_INLINE bool _upb_Decoder_TraceBufferFull(upb_Decoder* d) { + return d->trace_ptr == d->trace_end - 1; +} + +UPB_INLINE bool _upb_Decoder_TraceBufferAlmostFull(upb_Decoder* d) { + return d->trace_ptr == d->trace_end - 2; +} +#endif + +UPB_INLINE char* _upb_Decoder_TraceNext(upb_Decoder* d) { +#ifndef NDEBUG + return _upb_Decoder_TraceBufferAlmostFull(d) ? NULL : d->trace_ptr + 1; +#else + return NULL; +#endif +} + +UPB_INLINE char* _upb_Decoder_TracePtr(upb_Decoder* d) { +#ifndef NDEBUG + return d->trace_ptr; +#else + return NULL; +#endif +} + +// Trace events are used to trace the progress of the decoder. +// Events: +// 'D' Fast dispatch +// 'F' Field successfully parsed fast. +// '<' Fallback to MiniTable parser. +// 'M' Field successfully parsed with MiniTable. +// 'X' Truncated -- trace buffer is full, further events were discarded. +UPB_INLINE void _upb_Decoder_Trace(upb_Decoder* d, char event) { +#ifndef NDEBUG + if (d->trace_ptr == NULL) return; + if (_upb_Decoder_TraceBufferFull(d)) { + d->trace_ptr[-1] = 'X'; // Truncated. + return; + } + d->trace_ptr[0] = event; + d->trace_ptr[1] = '\0'; + d->trace_ptr++; +#endif +}; + +UPB_INLINE +bool _upb_Decoder_VerifyUtf8Inline(const char* ptr, int len) { + return utf8_range_IsValid(ptr, len); +} + +const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr, + const upb_Message* msg, + const upb_MiniTable* m); + +/* x86-64 pointers always have the high 16 bits matching. So we can shift + * left 8 and right 8 without loss of information. */ +UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) { + return ((intptr_t)tablep << 8) | tablep->UPB_PRIVATE(table_mask); +} + +UPB_INLINE const upb_MiniTable* decode_totablep(intptr_t table) { + return (const upb_MiniTable*)(table >> 8); +} + +const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr, + upb_Message* msg, + const upb_MiniTable* layout); + +UPB_INLINE bool _upb_Decoder_FieldRequiresUtf8Validation( + const upb_Decoder* d, const upb_MiniTableField* field) { + if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_String) return true; + + if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bytes && + (field->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsAlternate) && + (d->options & kUpb_DecodeOption_AlwaysValidateUtf8)) { + return true; + } + + return false; +} + +UPB_INLINE bool _upb_Decoder_ReadString(upb_Decoder* d, const char** ptr, + size_t size, upb_StringView* sv, + bool validate_utf8) { + upb_StringView tmp; + *ptr = + upb_EpsCopyInputStream_ReadStringAlwaysAlias(&d->input, *ptr, size, &tmp); + if (*ptr == NULL) return false; + if (validate_utf8 && !utf8_range_IsValid(tmp.data, tmp.size)) { + upb_ErrorHandler_ThrowError(&d->err, kUpb_DecodeStatus_BadUtf8); + return false; + } + if ((d->options & kUpb_DecodeOption_AliasString) == 0) { + char* data = (char*)upb_Arena_Malloc(&d->arena, tmp.size); + if (!data) return false; + memcpy(data, tmp.data, tmp.size); + tmp.data = data; + } + *sv = tmp; + return true; +} + + +#endif /* UPB_WIRE_INTERNAL_DECODER_H_ */ +#ifndef GOOGLE_UPB_UPB_WIRE_WRITER_H__ +#define GOOGLE_UPB_UPB_WIRE_WRITER_H__ + +#include + +// Must be last. + +UPB_FORCEINLINE uint32_t +UPB_PRIVATE(upb_WireWriter_VarintUnusedSizeFromLeadingZeros64)(uint64_t clz) { + // Calculate how many bytes of the possible 10 bytes we will *not* encode, + // because they are part of a zero prefix. For the number 300, it would use 2 + // bytes encoded, so the number of bytes to skip would be 8. Adding 7 to the + // clz input ensures that we're rounding up. + return (((uint32_t)clz + 7) * 9) >> 6; +} + + +#endif // GOOGLE_UPB_UPB_WIRE_WRITER_H__ + +// This should #undef all macros #defined in def.inc + +/* +#undef UPB_SIZE +#undef UPB_PTR_AT +#undef UPB_SIZEOF_FLEX +#undef UPB_SIZEOF_FLEX_WOULD_OVERFLOW +#undef UPB_MAPTYPE_STRING +#undef UPB_EXPORT +#undef UPB_INLINE +#undef UPB_API +#undef UPBC_API +#undef UPB_API_INLINE +#undef UPB_API_INLINE_IF_NOT_GCC +#undef UPB_ALIGN_UP +#undef UPB_ALIGN_DOWN +#undef UPB_ALIGN_MALLOC +#undef UPB_ALIGN_OF +#undef UPB_ALIGN_AS +#undef UPB_STATIC_ASSERT +#undef UPB_STATIC_ASSERT_CONCAT +#undef UPB_STATIC_ASSERT_CONCAT_IMPL +#undef UPB_LIKELY +#undef UPB_UNLIKELY +#undef UPB_UNPREDICTABLE +#undef UPB_FORCEINLINE +#undef UPB_NOINLINE +#undef UPB_NORETURN +#undef UPB_PRINTF +#undef UPB_NODEREF +#undef UPB_MAX +#undef UPB_MIN +#undef UPB_UNUSED +#undef UPB_ASSUME +#undef UPB_ASSERT +#undef UPB_UNREACHABLE +#undef UPB_DEFAULT_MAX_BLOCK_SIZE +#undef UPB_SETJMP +#undef UPB_LONGJMP +#undef UPB_PTRADD +#undef UPB_MUSTTAIL +#undef UPB_PRESERVE_MOST +#undef UPB_PRESERVE_NONE +#undef UPB_FASTTABLE_SUPPORTED +#undef UPB_FASTTABLE_MASK +#undef UPB_FASTTABLE +#undef UPB_FASTTABLE_INIT +#undef UPB_POISON_MEMORY_REGION +#undef UPB_UNPOISON_MEMORY_REGION +#undef UPB_ASAN +#undef UPB_HWASAN +#undef UPB_HWASAN_POISON_TAG +#undef UPB_MSAN +#undef UPB_MALLOC_ALIGN +#undef UPB_TSAN +#undef UPB_DEPRECATED +#undef UPB_GNUC_MIN +#undef UPB_DESCRIPTOR_UPB_H_FILENAME +#undef UPB_DESC_MINITABLE +#undef UPB_IS_GOOGLE3 +#undef UPB_ATOMIC +#undef UPB_USE_C11_ATOMICS +#undef UPB_USE_MSC_ATOMICS +#undef UPB_PRIVATE +#undef UPB_ONLYBITS +#undef UPB_LINKARR_DECLARE +#undef UPB_LINKARR_APPEND +#undef UPB_LINKARR_START +#undef UPB_LINKARR_STOP +#undef UPB_FUTURE_BREAKING_CHANGES +#undef UPB_HAS_ATTRIBUTE +#undef UPB_HAS_CPP_ATTRIBUTE +#undef UPB_HAS_BUILTIN +#undef UPB_HAS_EXTENSION +#undef UPB_HAS_FEATURE +#undef UPB_XSAN_MEMBER +#undef UPB_XSAN +#undef UPB_XSAN_STRUCT_SIZE +#undef UPB_ENABLE_REF_CYCLE_CHECKS +#undef UPB_ARM64_ASM +#undef UPB_ARM64_BTI_DEFAULT +#undef UPB_DEPRECATE_AND_INLINE +#undef UPB_MAYBE_ASSUME +#undef UPB_ATTR_CONST +*/ diff --git a/third_party/upb/utf8_range.c b/third_party/upb/utf8_range.c new file mode 100644 index 000000000..f3301cc27 --- /dev/null +++ b/third_party/upb/utf8_range.c @@ -0,0 +1,207 @@ +// Copyright 2023 Google LLC +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. + +/* This is a wrapper for the Google range-sse.cc algorithm which checks whether + * a sequence of bytes is a valid UTF-8 sequence and finds the longest valid + * prefix of the UTF-8 sequence. + * + * The key difference is that it checks for as much ASCII symbols as possible + * and then falls back to the range-sse.cc algorithm. The changes to the + * algorithm are cosmetic, mostly to trick the clang compiler to produce optimal + * code. + * + * For API see the utf8_validity.h header. + */ +#include "utf8_range.h" + +#include +#include +#include + +#if defined(__GNUC__) +#define FORCE_INLINE_ATTR __attribute__((always_inline)) inline +#elif defined(_MSC_VER) +#define FORCE_INLINE_ATTR __forceinline +#else +#define FORCE_INLINE_ATTR inline +#endif + +static FORCE_INLINE_ATTR uint64_t utf8_range_UnalignedLoad64( + const void* p) { + uint64_t t; + memcpy(&t, p, sizeof t); + return t; +} + +static FORCE_INLINE_ATTR int utf8_range_AsciiIsAscii(unsigned char c) { + return c < 128; +} + +static FORCE_INLINE_ATTR int utf8_range_IsTrailByteOk(const char c) { + return (int8_t)(c) <= (int8_t)(0xBF); +} + +/* If return_position is false then it returns 1 if |data| is a valid utf8 + * sequence, otherwise returns 0. + * If return_position is set to true, returns the length in bytes of the prefix + of |data| that is all structurally valid UTF-8. + */ +static size_t utf8_range_ValidateUTF8Naive(const char* data, const char* end, + int return_position) { + /* We return err_pos in the loop which is always 0 if !return_position */ + size_t err_pos = 0; + size_t codepoint_bytes = 0; + /* The early check is done because of early continue's on codepoints of all + * sizes, i.e. we first check for ascii and if it is, we call continue, then + * for 2 byte codepoints, etc. This is done in order to reduce indentation and + * improve readability of the codepoint validity check. + */ + while (data + codepoint_bytes < end) { + if (return_position) { + err_pos += codepoint_bytes; + } + data += codepoint_bytes; + const size_t len = end - data; + const unsigned char byte1 = data[0]; + + /* We do not skip many ascii bytes at the same time as this function is + used for tail checking (< 16 bytes) and for non x86 platforms. We also + don't think that cases where non-ASCII codepoints are followed by ascii + happen often. For small strings it also introduces some penalty. For + purely ascii UTF8 strings (which is the overwhelming case) we call + SkipAscii function which is multiplatform and extremely fast. + */ + /* [00..7F] ASCII -> 1 byte */ + if (utf8_range_AsciiIsAscii(byte1)) { + codepoint_bytes = 1; + continue; + } + /* [C2..DF], [80..BF] -> 2 bytes */ + if (len >= 2 && byte1 >= 0xC2 && byte1 <= 0xDF && + utf8_range_IsTrailByteOk(data[1])) { + codepoint_bytes = 2; + continue; + } + if (len >= 3) { + const unsigned char byte2 = data[1]; + const unsigned char byte3 = data[2]; + + /* Is byte2, byte3 between [0x80, 0xBF] + * Check for 0x80 was done above. + */ + if (!utf8_range_IsTrailByteOk(byte2) || + !utf8_range_IsTrailByteOk(byte3)) { + return err_pos; + } + + if (/* E0, A0..BF, 80..BF */ + ((byte1 == 0xE0 && byte2 >= 0xA0) || + /* E1..EC, 80..BF, 80..BF */ + (byte1 >= 0xE1 && byte1 <= 0xEC) || + /* ED, 80..9F, 80..BF */ + (byte1 == 0xED && byte2 <= 0x9F) || + /* EE..EF, 80..BF, 80..BF */ + (byte1 >= 0xEE && byte1 <= 0xEF))) { + codepoint_bytes = 3; + continue; + } + if (len >= 4) { + const unsigned char byte4 = data[3]; + /* Is byte4 between 0x80 ~ 0xBF */ + if (!utf8_range_IsTrailByteOk(byte4)) { + return err_pos; + } + + if (/* F0, 90..BF, 80..BF, 80..BF */ + ((byte1 == 0xF0 && byte2 >= 0x90) || + /* F1..F3, 80..BF, 80..BF, 80..BF */ + (byte1 >= 0xF1 && byte1 <= 0xF3) || + /* F4, 80..8F, 80..BF, 80..BF */ + (byte1 == 0xF4 && byte2 <= 0x8F))) { + codepoint_bytes = 4; + continue; + } + } + } + return err_pos; + } + if (return_position) { + err_pos += codepoint_bytes; + } + /* if return_position is false, this returns 1. + * if return_position is true, this returns err_pos. + */ + return err_pos + (1 - return_position); +} + +#if defined(__SSE4_1__) || (defined(__ARM_NEON) && defined(__ARM_64BIT_STATE)) +/* Returns the number of bytes needed to skip backwards to get to the first + byte of codepoint. + */ +static inline int utf8_range_CodepointSkipBackwards(int32_t codepoint_word) { + const int8_t* const codepoint = (const int8_t*)(&codepoint_word); + if (!utf8_range_IsTrailByteOk(codepoint[3])) { + return 1; + } else if (!utf8_range_IsTrailByteOk(codepoint[2])) { + return 2; + } else if (!utf8_range_IsTrailByteOk(codepoint[1])) { + return 3; + } + return 0; +} +#endif // __SSE4_1__ + +/* Skipping over ASCII as much as possible, per 8 bytes. It is intentional + as most strings to check for validity consist only of 1 byte codepoints. + */ +static inline const char* utf8_range_SkipAscii(const char* data, + const char* end) { + while (8 <= end - data && + (utf8_range_UnalignedLoad64(data) & 0x8080808080808080) == 0) { + data += 8; + } + while (data < end && utf8_range_AsciiIsAscii(*data)) { + ++data; + } + return data; +} + +#if defined(__SSE4_1__) +#include "utf8_range_sse.inc" +#elif defined(__ARM_NEON) && defined(__ARM_64BIT_STATE) +#include "utf8_range_neon.inc" +#endif + +static FORCE_INLINE_ATTR size_t utf8_range_Validate( + const char* data, size_t len, int return_position) { + if (len == 0) return 1 - return_position; + // Save buffer start address for later use + const char* const data_original = data; + const char* const end = data + len; + data = utf8_range_SkipAscii(data, end); + /* SIMD algorithm always outperforms the naive version for any data of + length >=16. + */ + if (end - data < 16) { + return (return_position ? (data - data_original) : 0) + + utf8_range_ValidateUTF8Naive(data, end, return_position); + } +#if defined(__SSE4_1__) || (defined(__ARM_NEON) && defined(__ARM_64BIT_STATE)) + return utf8_range_ValidateUTF8Simd( + data_original, data, end, return_position); +#else + return (return_position ? (data - data_original) : 0) + + utf8_range_ValidateUTF8Naive(data, end, return_position); +#endif +} + +bool utf8_range_IsValid(const char* data, size_t len) { + return utf8_range_Validate(data, len, /*return_position=*/0) != 0; +} + +size_t utf8_range_ValidPrefix(const char* data, size_t len) { + return utf8_range_Validate(data, len, /*return_position=*/1); +} diff --git a/third_party/upb/utf8_range.h b/third_party/upb/utf8_range.h new file mode 100644 index 000000000..5942d80ec --- /dev/null +++ b/third_party/upb/utf8_range.h @@ -0,0 +1,23 @@ +#ifndef THIRD_PARTY_UTF8_RANGE_UTF8_RANGE_H_ +#define THIRD_PARTY_UTF8_RANGE_UTF8_RANGE_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// Returns 1 if the sequence of characters is a valid UTF-8 sequence, otherwise +// 0. +bool utf8_range_IsValid(const char* data, size_t len); + +// Returns the length in bytes of the prefix of str that is all +// structurally valid UTF-8. +size_t utf8_range_ValidPrefix(const char* data, size_t len); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // THIRD_PARTY_UTF8_RANGE_UTF8_RANGE_H_ From d7cae938bc5a287a7df200cc7d73119827317459 Mon Sep 17 00:00:00 2001 From: Taku Kudo Date: Sun, 12 Jul 2026 22:08:52 +0000 Subject: [PATCH 2/5] Fix compilation warnings/errors on macOS (clang) - Fix member variable initialization order in SentencePieceTextWrapper - Remove unused variable sub_msg in NBestSentencePieceTextWrapper::add_nbests TAG=agy CONV=e46efd90-ae25-4cfc-8385-5381e11ee2ec --- src/builtin_upb/upb_wrapper.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/builtin_upb/upb_wrapper.h b/src/builtin_upb/upb_wrapper.h index 7e964ec05..fe1d6ceab 100644 --- a/src/builtin_upb/upb_wrapper.h +++ b/src/builtin_upb/upb_wrapper.h @@ -860,10 +860,10 @@ class ModelProtoWrapper { class SentencePieceTextWrapper { public: - SentencePieceTextWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { + SentencePieceTextWrapper() : owns_msg_(true), arena_(upb_Arena_New()) { msg_ = sentencepiece_SentencePieceText_new(arena_); } - explicit SentencePieceTextWrapper(std::nullptr_t) : msg_(nullptr), arena_(nullptr), owns_msg_(false) {} + explicit SentencePieceTextWrapper(std::nullptr_t) : msg_(nullptr), owns_msg_(false), arena_(nullptr) {} virtual ~SentencePieceTextWrapper() { if (owns_msg_ && arena_) { @@ -1446,8 +1446,7 @@ inline const upb_Arena* NBestSentencePieceText_Sub::arena() const { return paren inline NBestSentencePieceText_Sub* upb::NBestSentencePieceTextWrapper::add_nbests() { if (!msg_) return nullptr; - sentencepiece_SentencePieceText* sub_msg = - sentencepiece_NBestSentencePieceText_add_nbests(msg_, arena_); + sentencepiece_NBestSentencePieceText_add_nbests(msg_, arena_); int index = nbests_size() - 1; From c8a7b80e4ecd8bc9d0bce4c001c5e7ca05777aa2 Mon Sep 17 00:00:00 2001 From: Taku Kudo Date: Mon, 13 Jul 2026 16:22:01 +0000 Subject: [PATCH 3/5] Migrate SentencePiece to UPB C++ API - Implement C++ wrappers in upb_wrapper.h replacing protobuf-lite with UPB. - Fix decoding space trimming by adding child-parent sync (on_change_). - Achieve thread-safe concurrent inference via eager cache loading. - Add comprehensive upb_wrapper_test.cc (OOB safety, multi-thread stress, lazy-init/UAF validation, lazy-init concurrency race). - Document API differences and anti-patterns in builtin_upb/README.txt. - Map UPB_ATOMIC to std::atomic under C++ in third_party/upb/upb.h to maintain thread-safe lock-free atomic operations. - Map upb_Atomic_* macros to std::atomic member functions under C++ to fix 32-bit Linux/GCC stdatomic compatibility. - Fix upb_StringView alignment assert in third_party/upb/upb.c to support m68k 2-byte alignment. - Install sentencepiece.pb.h, sentencepiece_model.pb.h and upb_wrapper.h as public headers in src/CMakeLists.txt to fix Python pip Wheel builds. - Suppress MSVC warnings C4334 and C5287 for Windows/ARM64 upb builds. - Resolve dynamic include paths in python/setup.py under PEP 517 build isolation. - Enable SPM_USE_UPB=ON in python/build_bundled.sh to ensure UPB headers are installed during Unix bundled builds. - Fix MSVC pointer cast and designated initializer errors in third_party/upb/upb.h. - Format modified upb.h and upb.c to fit 80 characters limit and remove trailing spaces. - Make UPB the default build mode (SPM_USE_PROTOC=OFF) and add SPM_USE_PROTOC option (default OFF) to build with legacy protoc (protobuf-lite). - Ignore python/build/ directory in .gitignore to prevent compiler cache pollution in Docker CI. - Copy C++ core files to python/sentencepiece prior to calling cibuildwheel in .github/workflows/wheel.yml. - Synchronize const cache pointers when mutable spec is obtained in ModelProtoWrapper to prevent Segmentation faults on dynamic spec overrides in Python. - Pre-initialize cache objects (normalizer_spec_cache_ and denormalizer_spec_cache_) in ModelProtoWrapper default constructor using const default specs. - Simplify normalizer_spec() and denormalizer_spec() getters to return pre-allocated cache references, avoiding dynamic lazy-initialization and eliminating data races. - Fix Use-After-Free (UAF) bug in on_change lambdas where cached wrappers were replaced with new std::unique_ptr instances, causing raw pointers stored in C++ core to become dangling. - Specify shell: bash for wheel build steps in wheel.yml to support copying core source files on Windows runners. - Link sentencepiece_train.lib conditionally on Windows in python/setup.py to resolve trainer and normalizer symbols during cibuildwheel builds. - Skip logstream redirection test (test_train_with_logstream) under Abseil Logging (Google3) in python/test/clean_sentencepiece_test_manual.py due to stderr caching. TAG=agy CONV=e46efd90-ae25-4cfc-8385-5381e11ee2ec --- .github/workflows/wheel.yml | 9 + .gitignore | 1 + CMakeLists.txt | 4 +- python/build_bundled.sh | 7 + python/setup.py | 55 +- .../test/clean_sentencepiece_test_manual.py | 6 +- src/CMakeLists.txt | 41 +- src/builtin_upb/README.txt | 131 ++ src/builtin_upb/upb_wrapper.h | 1312 +++++++++++------ src/builtin_upb/upb_wrapper_test.cc | 296 ++++ third_party/upb/upb.c | 10 +- third_party/upb/upb.h | 64 +- third_party/upb/utf8_range_neon.inc | 117 ++ third_party/upb/utf8_range_sse.inc | 272 ++++ 14 files changed, 1794 insertions(+), 531 deletions(-) create mode 100644 src/builtin_upb/upb_wrapper_test.cc create mode 100644 third_party/upb/utf8_range_neon.inc create mode 100644 third_party/upb/utf8_range_sse.inc diff --git a/.github/workflows/wheel.yml b/.github/workflows/wheel.yml index 3bd9bd5f1..edbac111a 100644 --- a/.github/workflows/wheel.yml +++ b/.github/workflows/wheel.yml @@ -64,7 +64,15 @@ jobs: - name: Build wheels working-directory: python + shell: bash run: | + mkdir -p sentencepiece + for i in CMakeLists.txt LICENSE README.md VERSION.txt cmake config.h.in sentencepiece.pc.in src third_party + do + echo "copying ../${i} sentencepiece/${i}" + rm -rf "sentencepiece/${i}" + cp -f -R "../${i}" sentencepiece + done mkdir -p src/sentencepiece/package_data cp ../data/*.bin src/sentencepiece/package_data python -m cibuildwheel --output-dir wheelhouse @@ -79,6 +87,7 @@ jobs: - name: Build sdist archive working-directory: python + shell: bash run: | sh build_sdist.sh diff --git a/.gitignore b/.gitignore index 9a96c8698..ca3c1d219 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,7 @@ python/sentencepiece python/test/m_*.model python/test/m_*.vocab python/test/sp_*.pickle +python/build third_party/absl diff --git a/CMakeLists.txt b/CMakeLists.txt index 01c516905..22fe6602c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ project(sentencepiece VERSION ${SPM_VERSION} LANGUAGES C CXX) option(SPM_ENABLE_NFKC_COMPILE "Enables NFKC compile" OFF) option(SPM_ENABLE_SHARED "Builds shared libaries in addition to static libraries." ON) -option(SPM_USE_UPB "Use upb for core engine" ON) +option(SPM_USE_PROTOC "Use protoc (protobuf-lite) for core engine" OFF) option(SPM_BUILD_TEST "Builds test binaries." OFF) option(SPM_ENABLE_TCMALLOC "Enable TCMalloc if available." ON) option(SPM_TCMALLOC_STATIC "Link static library of TCMALLOC." OFF) @@ -111,7 +111,7 @@ else() endif() if (MSVC) - add_definitions("/wd4267 /wd4244 /wd4305 /Zc:strictStrings /utf-8") + add_definitions("/wd4267 /wd4244 /wd4305 /wd4334 /wd5287 /Zc:strictStrings /utf-8") if (SPM_ENABLE_MSVC_MT_BUILD) string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_CXX_FLAGS_MINSIZEREL}) diff --git a/python/build_bundled.sh b/python/build_bundled.sh index dbabca92b..2b68ebbc7 100755 --- a/python/build_bundled.sh +++ b/python/build_bundled.sh @@ -18,9 +18,16 @@ else SRC_DIR=./sentencepiece fi +if [ "${SPM_USE_PROTOC}" = "1" ]; then + SPM_USE_PROTOC_FLAG="-DSPM_USE_PROTOC=ON" +else + SPM_USE_PROTOC_FLAG="-DSPM_USE_PROTOC=OFF" +fi + cmake ${SRC_DIR} -B ${BUILD_DIR} \ -DSPM_ENABLE_SHARED=OFF \ -DSPM_DISABLE_EMBEDDED_DATA=ON \ + ${SPM_USE_PROTOC_FLAG} \ -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \ -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ -DCMAKE_CXX_FLAGS="-fPIC -fvisibility=default -ffunction-sections -fdata-sections" diff --git a/python/setup.py b/python/setup.py index 3fdc15b13..503f61d3a 100755 --- a/python/setup.py +++ b/python/setup.py @@ -35,8 +35,6 @@ with open('src/sentencepiece/_version.py') as f: line = f.readline().strip() __version__ = line.split('=')[1].strip().strip("'") - - def is_gil_disabled(): return sysconfig.get_config_var('Py_GIL_DISABLED') @@ -59,10 +57,19 @@ def find_abseil_lib(search_root): def get_protobuf_includes(): prefix = '/I' if os.name == 'nt' else '-I' + # Check if SPM_USE_PROTOC is requested + use_protoc = os.environ.get('SPM_USE_PROTOC', '0') == '1' + if use_protoc: + return [] + + if os.path.exists(os.path.join('.', 'sentencepiece', 'src', 'CMakeLists.txt')): + src_root = os.path.join('.', 'sentencepiece') + else: + src_root = '..' paths = [ - '../src', - '../src/builtin_upb', - '../third_party/upb', + os.path.join(src_root, 'src'), + os.path.join(src_root, 'src', 'builtin_upb'), + os.path.join(src_root, 'third_party', 'upb'), ] return [prefix + os.path.normpath(p) for p in paths] @@ -74,8 +81,11 @@ def get_cflags_and_libs(root): cflags = [ '-std=c++17', '-I' + os.path.normpath(os.path.join(root, 'include')), - '-DSPM_USE_UPB=1', - ] + get_protobuf_includes() + ] + use_protoc = os.environ.get('SPM_USE_PROTOC', '0') == '1' + if use_protoc: + cflags.append('-DSPM_USE_PROTOC=1') + cflags += get_protobuf_includes() libs = [] sp_lib = os.path.join(root, lib_dir, 'libsentencepiece.a') train_lib = os.path.join(root, lib_dir, 'libsentencepiece_train.a') @@ -161,26 +171,37 @@ class build_ext_win(_build_ext): def build_extension(self, ext): # Must pre-install sentencepice into build directory. arch = get_win_arch() + use_protoc = os.environ.get('SPM_USE_PROTOC', '0') == '1' if os.path.exists('..\\build_{}\\root\\lib'.format(arch)): cflags = [ '/std:c++17', '/I' + os.path.normpath('..\\build_{}\\root\\include'.format(arch)), - '/DSPM_USE_UPB=1', - ] + get_protobuf_includes() + ] + if use_protoc: + cflags.append('/DSPM_USE_PROTOC=1') + cflags += get_protobuf_includes() libs = [ '..\\build_{}\\root\\lib\\sentencepiece.lib'.format(arch), ] + train_lib = '..\\build_{}\\root\\lib\\sentencepiece_train.lib'.format(arch) + if os.path.exists(train_lib): + libs.append(train_lib) libs.extend(find_abseil_lib('..\\build_{}\\third_party'.format(arch))) elif os.path.exists('..\\build\\root\\lib'): cflags = [ '/std:c++17', '/I' + os.path.normpath('..\\build\\root\\include'), - '/DSPM_USE_UPB=1', - ] + get_protobuf_includes() + ] + if use_protoc: + cflags.append('/DSPM_USE_PROTOC=1') + cflags += get_protobuf_includes() libs = [ '..\\build\\root\\lib\\sentencepiece.lib', ] + train_lib = '..\\build\\root\\lib\\sentencepiece_train.lib' + if os.path.exists(train_lib): + libs.append(train_lib) libs.extend(find_abseil_lib('..\\build\\third_party')) else: # build library locally with cmake and vc++. @@ -199,8 +220,7 @@ def build_extension(self, ext): '-B', 'build', '-DSPM_ENABLE_SHARED=OFF', - '-DSPM_USE_UPB=ON', - # '-DCMAKE_SHARED_LINKER_FLAGS="/OPT:REF /OPT:ICF /LTCG"', + '-DSPM_USE_PROTOC=ON' if use_protoc else '-DSPM_USE_PROTOC=OFF', '-DCMAKE_INSTALL_PREFIX=build\\root', ]) subprocess.check_call([ @@ -217,11 +237,16 @@ def build_extension(self, ext): cflags = [ '/std:c++17', '/I' + os.path.normpath('.\\build\\root\\include'), - '/DSPM_USE_UPB=1', - ] + get_protobuf_includes() + ] + if use_protoc: + cflags.append('/DSPM_USE_PROTOC=1') + cflags += get_protobuf_includes() libs = [ '.\\build\\root\\lib\\sentencepiece.lib', ] + train_lib = '.\\build\\root\\lib\\sentencepiece_train.lib' + if os.path.exists(train_lib): + libs.append(train_lib) libs.extend(find_abseil_lib('.\\build\\third_party')) cflags.extend(find_absl_include(is_msvc=True)) diff --git a/python/test/clean_sentencepiece_test_manual.py b/python/test/clean_sentencepiece_test_manual.py index 812d5c0bf..e11afe0ce 100644 --- a/python/test/clean_sentencepiece_test_manual.py +++ b/python/test/clean_sentencepiece_test_manual.py @@ -983,7 +983,11 @@ def test_train_with_logstream(self): ) # Check if log file is not empty self.assertTrue(os.path.exists(log_path)) - self.assertTrue(os.path.getsize(log_path) > 0) + if os.path.getsize(log_path) == 0: + self.skipTest( + "Logstream redirection is not supported under Abseil Logging " + "(standard Google3 behavior where absl::log caches stderr)." + ) finally: os.remove(log_path) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f7d03409d..b10ea7f79 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,7 +24,7 @@ list(APPEND SPM_LIBS absl::check) list(APPEND SPM_LIBS absl::random_random) list(APPEND SPM_LIBS absl::time) -if (NOT SPM_USE_UPB) +if (SPM_USE_PROTOC) if (SPM_PROTOBUF_PROVIDER STREQUAL "package") find_package(Protobuf REQUIRED) include_directories(${Protobuf_INCLUDE_DIRS}) @@ -36,6 +36,16 @@ if (NOT SPM_USE_UPB) else() message(FATAL_ERROR "Unsupported protobuf provider: ${SPM_PROTOBUF_PROVIDER}") endif() +else() + # In UPB mode, manually specify our protobuf wrapper headers so they are + # installed as public headers. + set(SPM_PROTO_HDRS + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/sentencepiece.pb.h + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/upb_wrapper.h + ) + set(SPM_MODEL_PROTO_HDRS + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/sentencepiece_model.pb.h + ) endif() include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) @@ -72,7 +82,7 @@ set(SPM_COMMON_HDRS unigram_model.h ) -if (SPM_USE_UPB) +if (NOT SPM_USE_PROTOC) set(UPB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb/upb.c ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb/utf8_range.c @@ -152,6 +162,10 @@ set(SPM_TEST_SRCS word_model_trainer_test.cc pretokenizer_for_training_test.cc) +if (SPM_USE_UPB) + list(APPEND SPM_TEST_SRCS builtin_upb/upb_wrapper_test.cc) +endif() + if (SPM_NLCODEC_BPE) set(NLCODEC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../contrib/nlcodec) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..) @@ -208,14 +222,14 @@ endif() if (SPM_ENABLE_SHARED) add_library(sentencepiece SHARED ${SPM_SRCS}) - if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) + if (SPM_INTERNAL_PROTO_INC AND SPM_USE_PROTOC) target_include_directories(sentencepiece PUBLIC ${SPM_INTERNAL_PROTO_INC}) endif() if (ANDROID) target_link_libraries(sentencepiece PRIVATE log) endif() add_library(sentencepiece_train SHARED ${SPM_TRAIN_SRCS}) - if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) + if (SPM_INTERNAL_PROTO_INC AND SPM_USE_PROTOC) target_include_directories(sentencepiece_train PUBLIC ${SPM_INTERNAL_PROTO_INC}) endif() if (ANDROID) @@ -224,42 +238,45 @@ if (SPM_ENABLE_SHARED) endif() add_library(sentencepiece-static STATIC ${SPM_SRCS}) -if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) +if (SPM_INTERNAL_PROTO_INC AND SPM_USE_PROTOC) target_include_directories(sentencepiece-static PUBLIC ${SPM_INTERNAL_PROTO_INC}) endif() add_library(sentencepiece_train-static STATIC ${SPM_TRAIN_SRCS}) -if (SPM_INTERNAL_PROTO_INC AND NOT SPM_USE_UPB) +if (SPM_INTERNAL_PROTO_INC AND SPM_USE_PROTOC) target_include_directories(sentencepiece_train-static PUBLIC ${SPM_INTERNAL_PROTO_INC}) endif() -if (SPM_USE_UPB) +if (NOT SPM_USE_PROTOC) if (SPM_ENABLE_SHARED) target_link_libraries(sentencepiece PUBLIC ${SPM_LIBS}) - target_compile_definitions(sentencepiece PUBLIC SPM_USE_UPB=1) target_include_directories(sentencepiece PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb ) target_link_libraries(sentencepiece_train PUBLIC ${SPM_LIBS} sentencepiece) - target_compile_definitions(sentencepiece_train PUBLIC SPM_USE_UPB=1) target_include_directories(sentencepiece_train PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb ) endif() target_link_libraries(sentencepiece-static INTERFACE ${SPM_LIBS}) - target_compile_definitions(sentencepiece-static PUBLIC SPM_USE_UPB=1) target_include_directories(sentencepiece-static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb ) target_link_libraries(sentencepiece_train-static INTERFACE sentencepiece-static ${SPM_LIBS}) - target_compile_definitions(sentencepiece_train-static PUBLIC SPM_USE_UPB=1) target_include_directories(sentencepiece_train-static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/upb ) else() + if (SPM_ENABLE_SHARED) + target_compile_definitions(sentencepiece PUBLIC SPM_USE_PROTOC=1) + target_compile_definitions(sentencepiece_train PUBLIC SPM_USE_PROTOC=1) + endif() + target_compile_definitions(sentencepiece-static PUBLIC SPM_USE_PROTOC=1) + target_compile_definitions(sentencepiece_train-static PUBLIC SPM_USE_PROTOC=1) + target_link_libraries(sentencepiece-static INTERFACE ${SPM_LIBS}) if (SPM_ENABLE_SHARED) target_link_libraries(sentencepiece PUBLIC ${SPM_LIBS}) @@ -295,7 +312,7 @@ set_target_properties(sentencepiece_train-static PROPERTIES OUTPUT_NAME "sentenc if (NOT WIN32) set(CMAKE_CXX_FLAGS "-O3 -Wall -fPIC ${CMAKE_CXX_FLAGS}") - if (NOT SPM_USE_UPB) + if (SPM_USE_PROTOC) set_source_files_properties( sentencepiece.pb.cc sentencepiece_model.pb.cc PROPERTIES COMPILE_FLAGS "-Wno-misleading-indentation") diff --git a/src/builtin_upb/README.txt b/src/builtin_upb/README.txt index 19c533f87..ffd787df1 100644 --- a/src/builtin_upb/README.txt +++ b/src/builtin_upb/README.txt @@ -18,3 +18,134 @@ How to generate upb stub files: These generated files (*.upb.h, *.upb.c) are checked into VCS to allow building without upb compiler dependency. + + +Differences from Standard Protobuf C++ API +=========================================== + +This UPB wrapper (`upb_wrapper.h`) emulates the standard Protobuf C++ API, +but there are several structural and behavioral differences due to the +underlying arena-based memory model of UPB: + +1. Memory Lifecycle (Arena-based vs Object-based): + - In standard Protobuf, each message object manages its own memory + independently. + - In this UPB wrapper, all sub-message elements share the memory pool + allocated on the root message's `upb_Arena`. + - Modifying a child wrapper object directly updates the raw memory on the + arena. To ensure consistency, a callback mechanism (`on_change_`) is used + to notify parent wrappers so they can rebuild or update their C++ wrapper + caches. + +2. Mutable Accessors (`mutable_xxx()`): + - In standard Protobuf, `mutable_xxx()` always returns a pointer to the + sub-message. + - In this wrapper, calling `mutable_xxx()` for the first time dynamically + allocates the sub-message structure on the arena and sets it onto the + parent message, triggering cache synchronization. + +3. Repeated Fields Operations: + - Unlike standard Protobuf which exposes `RepeatedPtrField` or + `RepeatedField` directly, UPB wrapper restricts repeated array operations + to specific helper methods (`add_xxx()`, `set_xxx_at()`, `clear_xxx()`) + due to UPB's C-API array limitations. + +4. Thread Safety (Eager Cache Loading): + - Standard Protobuf guarantees thread safety for concurrent read access + (const getters). + - To achieve the same thread safety in the UPB wrapper under concurrent + inference, we completely eliminated Lazy-Initialization from all `const` + getters (e.g. `pieces() const`). + - Instead, caches are eagerly initialized in `OnArenaReset()` right after + parsing or loading a model. Lazy-Init is only retained in non-const getters + for manual model construction in single-threaded environments. + +5. Out-of-Bounds (OOB) Protection: + - Standard Protobuf typically crashes (assertion failure or SegFault) when + accessing index out-of-bounds on repeated fields. + - This wrapper provides safer OOB protection: accessing invalid indices + (e.g. `-1` or `size`) via `pieces(i)` or `nbests(i)` returns a static + default empty instance or fallback default value rather than crashing. + +6. Reflection and Metadata: + - Dynamic reflection APIs (`GetDescriptor()`, `GetReflection()`) are not + supported. Only static accessors required by SentencePiece are + implemented. + + +Anti-Patterns (NG Examples) +============================ + +Avoid the following patterns to prevent crashes, memory corruption, or race +conditions: + +1. Dangling Sub-message Reference (Dangling Pointer): + Never keep or use references to child wrappers (like `NormalizerSpec` or + `TrainerSpec`) after the parent `ModelProto` is destroyed. Sub-messages + share the parent's `upb_Arena` lifetime. + + // --- NG EXAMPLE --- + const NormalizerSpec* spec = nullptr; + { + ModelProto model; + model.ParseFromString(data); + spec = &model.normalizer_spec(); // Holds reference to the arena memory + } // 'model' is destroyed here, freeing the arena. + + std::cout << spec->name(); // CRASH! Use-after-free on deleted arena. + + +2. Concurrent Access to Non-const or Mutable Getters: + While `const` getters (e.g. `pieces(i) const`) are thread-safe for + concurrent read access during inference, non-const getters (e.g. `pieces(i)`) + and mutable accessors (e.g. `mutable_pieces(idx)` or + `mutable_normalizer_spec()`) are not thread-safe and will cause Data Races or + memory corruption. + + // --- NG EXAMPLE (Multi-threaded context) --- + void ThreadWorker(ModelProto* shared_model) { + // NG: Using mutable or non-const accessors concurrently triggers internal + // memory allocation on the arena and cache synchronization callbacks. + auto* spec = shared_model->mutable_normalizer_spec(); + spec->set_add_dummy_prefix(false); + } + + // --- CORRECT WAY --- + void ThreadWorker(const ModelProto* shared_model) { + // OK: Const accessor is read-only and completely thread-safe. + const auto& spec = shared_model->normalizer_spec(); + std::cout << spec.add_dummy_prefix(); + } + + +3. Out-of-bounds Array Mutation: + Do not modify repeated fields using out-of-bounds indices via raw setters + like `set_piece_at`. You must first allocate the element using `add_xxx()`. + + // --- NG EXAMPLE --- + ModelProto model; // 'pieces' size is 0 + model.set_piece_at(5, "token"); // CRASH! Out-of-bounds array write. + + // --- CORRECT WAY --- + auto* piece = model.add_pieces(); // Allocates on the arena and increments size. + piece->set_piece("token"); + + +4. Excessive Copy Assignment in Loops (Arena Bloat): + Assigning wrappers using `operator=` triggers serialization and re-allocation + on the parent's arena. Avoid repeated copy assignments inside loops. + + // --- NG EXAMPLE --- + ModelProto model; + NormalizerSpec local_spec; + local_spec.set_name("nfkc"); + + for (int i = 0; i < 1000; ++i) { + // NG: Allocates new memory on the parent arena during each assignment, + // leading to memory bloat because arena memory is only freed as a whole. + *model.mutable_normalizer_spec() = local_spec; + } + + // --- CORRECT WAY --- + // Modify directly via mutable accessor to avoid temporary allocations. + model.mutable_normalizer_spec()->set_name("nfkc"); diff --git a/src/builtin_upb/upb_wrapper.h b/src/builtin_upb/upb_wrapper.h index fe1d6ceab..d93dbdc41 100644 --- a/src/builtin_upb/upb_wrapper.h +++ b/src/builtin_upb/upb_wrapper.h @@ -15,22 +15,24 @@ #ifndef CORE_UPB_WRAPPER_H_ #define CORE_UPB_WRAPPER_H_ -#include -#include -#include -#include #include -#include +#include +#include #include #include -#include +#include +#include +#include +#include #include "third_party/absl/strings/string_view.h" #include "third_party/absl/types/span.h" + + // Include the upb generated headers -#include "sentencepiece_model.upb.h" #include "sentencepiece.upb.h" +#include "sentencepiece_model.upb.h" namespace sentencepiece { class ProtoStr : public absl::string_view { @@ -40,10 +42,28 @@ class ProtoStr : public absl::string_view { ProtoStr(upb_StringView sv) : absl::string_view(sv.data, sv.size) {} operator std::string() const { return std::string(*this); } - bool operator==(const std::string& other) const { return absl::string_view(*this) == other; } - friend bool operator==(const std::string& a, const ProtoStr& b) { return a == absl::string_view(b); } + bool operator==(const std::string& other) const { + return absl::string_view(*this) == other; + } + friend bool operator==(const std::string& a, const ProtoStr& b) { + return a == absl::string_view(b); + } }; +inline upb_StringView MakeUpbString(absl::string_view val, upb_Arena* arena) { + upb_StringView sv; + if (arena && !val.empty()) { + char* ptr = static_cast(upb_Arena_Malloc(arena, val.size())); + memcpy(ptr, val.data(), val.size()); + sv.data = ptr; + sv.size = val.size(); + } else { + sv.data = nullptr; + sv.size = 0; + } + return sv; +} + class TrainerSpec; class NormalizerSpec; class SelfTestData; @@ -70,7 +90,7 @@ class ModelProto_SentencePiece { ModelProto_SentencePiece(upb::ModelProtoWrapper* parent, int index) : parent_(parent), index_(index) {} ModelProto_SentencePiece() : parent_(nullptr), index_(-1) {} - + inline ProtoStr piece() const; inline float score() const; inline Type type() const; @@ -80,6 +100,7 @@ class ModelProto_SentencePiece { set_piece(absl::string_view(data, size)); } inline void set_score(float score); + private: upb::ModelProtoWrapper* parent_; int index_; @@ -87,44 +108,44 @@ class ModelProto_SentencePiece { namespace upb { -#define DEFINE_UPB_SERIALIZATION_METHODS(ClassName, CPrefix) \ - std::string SerializeAsString() const { \ - if (!msg_) return ""; \ - size_t len; \ - upb_Arena* tmp_arena = upb_Arena_New(); \ - char* ptr = CPrefix##_serialize(msg_, tmp_arena, &len); \ - std::string ret; \ - if (ptr) { \ - ret.assign(ptr, len); \ - } \ - upb_Arena_Free(tmp_arena); \ - return ret; \ - } \ - bool ParseFromArray(const void* data, int size) { \ - if (owns_msg_ && arena_) { \ - upb_Arena_Free(arena_); \ - } \ - arena_ = upb_Arena_New(); \ - owns_msg_ = true; \ +#define DEFINE_UPB_SERIALIZATION_METHODS(ClassName, CPrefix) \ + std::string SerializeAsString() const { \ + if (!msg_) return ""; \ + size_t len; \ + upb_Arena* tmp_arena = upb_Arena_New(); \ + char* ptr = CPrefix##_serialize(msg_, tmp_arena, &len); \ + std::string ret; \ + if (ptr) { \ + ret.assign(ptr, len); \ + } \ + upb_Arena_Free(tmp_arena); \ + return ret; \ + } \ + bool ParseFromArray(const void* data, int size) { \ + if (owns_msg_ && arena_) { \ + upb_Arena_Free(arena_); \ + } \ + arena_ = upb_Arena_New(); \ + owns_msg_ = true; \ msg_ = CPrefix##_parse(reinterpret_cast(data), size, arena_); \ - OnArenaReset(); \ - return msg_ != nullptr; \ - } \ - bool ParseFromString(const std::string& bytes) { \ - return ParseFromArray(bytes.data(), bytes.size()); \ - } \ - void CopyFrom(const ClassName& other) { \ - std::string bytes = other.SerializeAsString(); \ - ParseFromString(bytes); \ - } \ - void Clear() { \ - if (owns_msg_ && arena_) { \ - upb_Arena_Free(arena_); \ - } \ - arena_ = upb_Arena_New(); \ - owns_msg_ = true; \ - msg_ = CPrefix##_new(arena_); \ - OnArenaReset(); \ + OnArenaReset(); \ + return msg_ != nullptr; \ + } \ + bool ParseFromString(const std::string& bytes) { \ + return ParseFromArray(bytes.data(), bytes.size()); \ + } \ + void CopyFrom(const ClassName& other) { \ + std::string bytes = other.SerializeAsString(); \ + ParseFromString(bytes); \ + } \ + void Clear() { \ + if (owns_msg_ && arena_) { \ + upb_Arena_Free(arena_); \ + } \ + arena_ = upb_Arena_New(); \ + owns_msg_ = true; \ + msg_ = CPrefix##_new(arena_); \ + OnArenaReset(); \ } class SentencePieceTextWrapper; @@ -132,10 +153,11 @@ class SentencePieceTextWrapper; class SentencePieceText_SentencePiece { public: - SentencePieceText_SentencePiece(upb::SentencePieceTextWrapper* parent, int index) + SentencePieceText_SentencePiece(upb::SentencePieceTextWrapper* parent, + int index) : parent_(parent), index_(index) {} SentencePieceText_SentencePiece() : parent_(nullptr), index_(-1) {} - + inline absl::string_view piece() const; inline void set_piece(absl::string_view piece); inline void set_piece(const char* data, size_t size); @@ -153,12 +175,14 @@ class SentencePieceText_SentencePiece { inline uint32_t end() const; inline void set_end(uint32_t end); - friend void swap(SentencePieceText_SentencePiece a, SentencePieceText_SentencePiece b); - + friend void swap(SentencePieceText_SentencePiece a, + SentencePieceText_SentencePiece b); + static const SentencePieceText_SentencePiece& default_instance() { static SentencePieceText_SentencePiece instance(nullptr, -1); return instance; } + private: upb::SentencePieceTextWrapper* parent_; int index_; @@ -173,12 +197,11 @@ class NBestSentencePieceTextWrapper; class SelfTestDataWrapper; class SelfTestData_SampleWrapper; - - class RepeatedStringWrapper { public: - RepeatedStringWrapper(const upb_StringView* arr, size_t size) : arr_(arr), size_(size) {} - + RepeatedStringWrapper(const upb_StringView* arr, size_t size) + : arr_(arr), size_(size) {} + class Iterator { public: using iterator_category = std::forward_iterator_tag; @@ -187,14 +210,25 @@ class RepeatedStringWrapper { using pointer = ProtoStr*; using reference = ProtoStr; - Iterator(const upb_StringView* arr, size_t index) : arr_(arr), index_(index) {} - Iterator& operator++() { ++index_; return *this; } - Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } - bool operator==(const Iterator& other) const { return index_ == other.index_; } - bool operator!=(const Iterator& other) const { return index_ != other.index_; } - ProtoStr operator*() const { - return ProtoStr(arr_[index_]); + Iterator(const upb_StringView* arr, size_t index) + : arr_(arr), index_(index) {} + Iterator& operator++() { + ++index_; + return *this; } + Iterator operator++(int) { + Iterator tmp = *this; + ++index_; + return tmp; + } + bool operator==(const Iterator& other) const { + return index_ == other.index_; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; + } + ProtoStr operator*() const { return ProtoStr(arr_[index_]); } + private: const upb_StringView* arr_; size_t index_; @@ -212,114 +246,126 @@ class RepeatedStringWrapper { }; #define DEFINE_UPB_PRIMITIVE_ACCESSOR(FieldName, Type, DefaultVal, UpbPrefix) \ - Type FieldName() const { return msg_ ? UpbPrefix##_##FieldName(msg_) : DefaultVal; } \ - void set_##FieldName(Type val) { \ - if (msg_) { \ - UpbPrefix##_set_##FieldName(msg_, val); \ - } \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - } \ - } - -#define DEFINE_UPB_STRING_ACCESSOR(FieldName, DefaultVal, UpbPrefix) \ - ProtoStr FieldName() const { \ + Type FieldName() const { \ + return msg_ ? UpbPrefix##_##FieldName(msg_) : DefaultVal; \ + } \ + void set_##FieldName(Type val) { \ + if (msg_) { \ + UpbPrefix##_set_##FieldName(msg_, val); \ + if (on_change_) on_change_(msg_); \ + } \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + if (on_change_) on_change_(msg_); \ + } \ + } + +#define DEFINE_UPB_STRING_ACCESSOR(FieldName, DefaultVal, UpbPrefix) \ + ProtoStr FieldName() const { \ if (!msg_ || !UpbPrefix##_has_##FieldName(msg_)) return DefaultVal; \ - return UpbPrefix##_##FieldName(msg_); \ - } \ - void set_##FieldName(absl::string_view val) { \ - if (msg_ && arena_) { \ - upb_StringView sv; \ - char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); \ - memcpy(ptr, val.data(), val.size()); \ - sv.data = ptr; \ - sv.size = val.size(); \ - UpbPrefix##_set_##FieldName(msg_, sv); \ - } \ - } \ - void set_##FieldName(const char* data, size_t size) { \ - set_##FieldName(absl::string_view(data, size)); \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - } \ + return UpbPrefix##_##FieldName(msg_); \ + } \ + void set_##FieldName(absl::string_view val) { \ + if (msg_ && arena_) { \ + UpbPrefix##_set_##FieldName(msg_, MakeUpbString(val, arena_)); \ + if (on_change_) on_change_(msg_); \ + } \ + } \ + void set_##FieldName(const char* data, size_t size) { \ + set_##FieldName(absl::string_view(data, size)); \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + if (on_change_) on_change_(msg_); \ + } \ } #define DEFINE_UPB_HAS_FIELD_ACCESSOR(FieldName, UpbPrefix) \ - bool has_##FieldName() const { return msg_ && UpbPrefix##_has_##FieldName(msg_); } + bool has_##FieldName() const { \ + return msg_ && UpbPrefix##_has_##FieldName(msg_); \ + } #define DEFINE_UPB_ENUM_ACCESSOR(FieldName, EnumType, DefaultVal, UpbPrefix) \ - EnumType FieldName() const { \ - return msg_ ? static_cast(UpbPrefix##_##FieldName(msg_)) : DefaultVal; \ - } \ - void set_##FieldName(EnumType type) { \ - if (msg_) { \ - UpbPrefix##_set_##FieldName(msg_, static_cast(type)); \ - } \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - } \ - } - -#define DEFINE_UPB_REPEATED_STRING_ACCESSOR(FieldName, UpbPrefix) \ - int FieldName##_size() const { \ - if (!msg_) return 0; \ - size_t size; \ - UpbPrefix##_##FieldName(msg_, &size); \ - return size; \ - } \ - ProtoStr FieldName(int index) const { \ - if (!msg_) return ""; \ - size_t size; \ - const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ - if (index < 0 || static_cast(index) >= size) return ""; \ - return arr[index]; \ - } \ - RepeatedStringWrapper FieldName() const { \ - if (!msg_) return RepeatedStringWrapper(nullptr, 0); \ - size_t size; \ - const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ - return RepeatedStringWrapper(arr, size); \ - } \ - void add_##FieldName(const std::string& val) { \ - if (msg_ && arena_) { \ - upb_StringView sv; \ - char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); \ - memcpy(ptr, val.data(), val.size()); \ - sv.data = ptr; \ - sv.size = val.size(); \ - UpbPrefix##_add_##FieldName(msg_, sv, arena_); \ - } \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - } \ + EnumType FieldName() const { \ + return msg_ ? static_cast(UpbPrefix##_##FieldName(msg_)) \ + : DefaultVal; \ + } \ + void set_##FieldName(EnumType type) { \ + if (msg_) { \ + UpbPrefix##_set_##FieldName(msg_, static_cast(type)); \ + if (on_change_) on_change_(msg_); \ + } \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + if (on_change_) on_change_(msg_); \ + } \ + } + +#define DEFINE_UPB_REPEATED_STRING_ACCESSOR(FieldName, UpbPrefix) \ + int FieldName##_size() const { \ + if (!msg_) return 0; \ + size_t size; \ + UpbPrefix##_##FieldName(msg_, &size); \ + return size; \ + } \ + ProtoStr FieldName(int index) const { \ + if (!msg_) return ""; \ + size_t size; \ + const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ + if (index < 0 || static_cast(index) >= size) return ""; \ + return arr[index]; \ + } \ + RepeatedStringWrapper FieldName() const { \ + if (!msg_) return RepeatedStringWrapper(nullptr, 0); \ + size_t size; \ + const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ + return RepeatedStringWrapper(arr, size); \ + } \ + void add_##FieldName(const std::string& val) { \ + if (msg_ && arena_) { \ + UpbPrefix##_add_##FieldName(msg_, MakeUpbString(val, arena_), arena_); \ + if (on_change_) on_change_(msg_); \ + } \ + } \ + void clear_##FieldName() { \ + if (msg_) { \ + UpbPrefix##_clear_##FieldName(msg_); \ + if (on_change_) on_change_(msg_); \ + } \ } class TrainerSpecWrapper { friend class upb::ModelProtoWrapper; + public: TrainerSpecWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { msg_ = sentencepiece_TrainerSpec_new(arena_); } - explicit TrainerSpecWrapper(const sentencepiece_TrainerSpec* msg, upb_Arena* arena = nullptr) - : msg_(const_cast(msg)), arena_(arena), owns_msg_(false) {} - - TrainerSpecWrapper(const sentencepiece_TrainerSpec* msg, upb_Arena* arena, - std::function on_change) - : msg_(const_cast(msg)), arena_(arena), owns_msg_(false), on_change_(on_change) {} - - TrainerSpecWrapper(const TrainerSpecWrapper& other) : arena_(upb_Arena_New()), owns_msg_(true) { + explicit TrainerSpecWrapper(const sentencepiece_TrainerSpec* msg, + upb_Arena* arena = nullptr) + : msg_(const_cast(msg)), + arena_(arena), + owns_msg_(false) {} + + TrainerSpecWrapper( + const sentencepiece_TrainerSpec* msg, upb_Arena* arena, + std::function on_change) + : msg_(const_cast(msg)), + arena_(arena), + owns_msg_(false), + on_change_(on_change) {} + + TrainerSpecWrapper(const TrainerSpecWrapper& other) + : arena_(upb_Arena_New()), owns_msg_(true) { msg_ = sentencepiece_TrainerSpec_new(arena_); CopyFrom(other); } - + TrainerSpecWrapper& operator=(const TrainerSpecWrapper& other) { if (this != &other) { if (owns_msg_) { @@ -331,7 +377,8 @@ class TrainerSpecWrapper { if (other.msg_) { size_t size = 0; upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = sentencepiece_TrainerSpec_serialize(other.msg_, tmp_arena, &size); + char* buf = + sentencepiece_TrainerSpec_serialize(other.msg_, tmp_arena, &size); if (buf) { msg_ = sentencepiece_TrainerSpec_parse(buf, size, arena_); if (on_change_) on_change_(msg_); @@ -352,7 +399,8 @@ class TrainerSpecWrapper { } } - DEFINE_UPB_SERIALIZATION_METHODS(TrainerSpecWrapper, sentencepiece_TrainerSpec) + DEFINE_UPB_SERIALIZATION_METHODS(TrainerSpecWrapper, + sentencepiece_TrainerSpec) void OnArenaReset() {} @@ -366,55 +414,87 @@ class TrainerSpecWrapper { DEFINE_UPB_REPEATED_STRING_ACCESSOR(input, sentencepiece_TrainerSpec) DEFINE_UPB_STRING_ACCESSOR(input_format, "", sentencepiece_TrainerSpec) DEFINE_UPB_STRING_ACCESSOR(model_prefix, "", sentencepiece_TrainerSpec) - DEFINE_UPB_ENUM_ACCESSOR(model_type, ModelType, UNIGRAM, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(vocab_size, int32_t, 8000, sentencepiece_TrainerSpec) - DEFINE_UPB_REPEATED_STRING_ACCESSOR(accept_language, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(self_test_sample_size, int32_t, 0, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(enable_differential_privacy, bool, false, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(differential_privacy_noise_level, float, 0.0f, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(differential_privacy_clipping_threshold, uint64_t, 0, sentencepiece_TrainerSpec) - - DEFINE_UPB_PRIMITIVE_ACCESSOR(character_coverage, float, 0.9995f, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(input_sentence_size, uint64_t, 0, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(shuffle_input_sentence, bool, true, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(seed_sentencepiece_size, int32_t, 1000000, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(shrinking_factor, float, 0.75f, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(max_sentence_length, int32_t, 4192, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(num_threads, int32_t, 16, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(num_sub_iterations, int32_t, 2, sentencepiece_TrainerSpec) - - DEFINE_UPB_PRIMITIVE_ACCESSOR(max_sentencepiece_length, int32_t, 16, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_unicode_script, bool, true, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_number, bool, true, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_whitespace, bool, true, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(treat_whitespace_as_suffix, bool, false, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(allow_whitespace_only_pieces, bool, false, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(split_digits, bool, false, sentencepiece_TrainerSpec) - DEFINE_UPB_STRING_ACCESSOR(pretokenization_delimiter, "", sentencepiece_TrainerSpec) - - DEFINE_UPB_REPEATED_STRING_ACCESSOR(control_symbols, sentencepiece_TrainerSpec) - DEFINE_UPB_REPEATED_STRING_ACCESSOR(user_defined_symbols, sentencepiece_TrainerSpec) + DEFINE_UPB_ENUM_ACCESSOR(model_type, ModelType, UNIGRAM, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(vocab_size, int32_t, 8000, + sentencepiece_TrainerSpec) + DEFINE_UPB_REPEATED_STRING_ACCESSOR(accept_language, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(self_test_sample_size, int32_t, 0, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(enable_differential_privacy, bool, false, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(differential_privacy_noise_level, float, 0.0f, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(differential_privacy_clipping_threshold, + uint64_t, 0, sentencepiece_TrainerSpec) + + DEFINE_UPB_PRIMITIVE_ACCESSOR(character_coverage, float, 0.9995f, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(input_sentence_size, uint64_t, 0, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(shuffle_input_sentence, bool, true, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(seed_sentencepiece_size, int32_t, 1000000, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(shrinking_factor, float, 0.75f, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(max_sentence_length, int32_t, 4192, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(num_threads, int32_t, 16, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(num_sub_iterations, int32_t, 2, + sentencepiece_TrainerSpec) + + DEFINE_UPB_PRIMITIVE_ACCESSOR(max_sentencepiece_length, int32_t, 16, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_unicode_script, bool, true, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_number, bool, true, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_by_whitespace, bool, true, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(treat_whitespace_as_suffix, bool, false, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(allow_whitespace_only_pieces, bool, false, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(split_digits, bool, false, + sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(pretokenization_delimiter, "", + sentencepiece_TrainerSpec) + + DEFINE_UPB_REPEATED_STRING_ACCESSOR(control_symbols, + sentencepiece_TrainerSpec) + DEFINE_UPB_REPEATED_STRING_ACCESSOR(user_defined_symbols, + sentencepiece_TrainerSpec) DEFINE_UPB_STRING_ACCESSOR(required_chars, "", sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(byte_fallback, bool, false, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(vocabulary_output_piece_score, bool, true, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(hard_vocab_limit, bool, true, sentencepiece_TrainerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(use_all_vocab, bool, false, sentencepiece_TrainerSpec) - + DEFINE_UPB_PRIMITIVE_ACCESSOR(byte_fallback, bool, false, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(vocabulary_output_piece_score, bool, true, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(hard_vocab_limit, bool, true, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(use_all_vocab, bool, false, + sentencepiece_TrainerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(unk_id, int32_t, 0, sentencepiece_TrainerSpec) DEFINE_UPB_PRIMITIVE_ACCESSOR(bos_id, int32_t, 1, sentencepiece_TrainerSpec) DEFINE_UPB_PRIMITIVE_ACCESSOR(eos_id, int32_t, 2, sentencepiece_TrainerSpec) DEFINE_UPB_PRIMITIVE_ACCESSOR(pad_id, int32_t, -1, sentencepiece_TrainerSpec) - + DEFINE_UPB_STRING_ACCESSOR(unk_piece, "", sentencepiece_TrainerSpec) DEFINE_UPB_STRING_ACCESSOR(bos_piece, "", sentencepiece_TrainerSpec) DEFINE_UPB_STRING_ACCESSOR(eos_piece, "", sentencepiece_TrainerSpec) DEFINE_UPB_STRING_ACCESSOR(pad_piece, "", sentencepiece_TrainerSpec) - + DEFINE_UPB_HAS_FIELD_ACCESSOR(unk_surface, sentencepiece_TrainerSpec) - DEFINE_UPB_STRING_ACCESSOR(unk_surface, " \xE2\x81\x87 ", sentencepiece_TrainerSpec) - - DEFINE_UPB_PRIMITIVE_ACCESSOR(train_extremely_large_corpus, bool, false, sentencepiece_TrainerSpec) - DEFINE_UPB_STRING_ACCESSOR(seed_sentencepieces_file, "", sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(unk_surface, " \xE2\x81\x87 ", + sentencepiece_TrainerSpec) + + DEFINE_UPB_PRIMITIVE_ACCESSOR(train_extremely_large_corpus, bool, false, + sentencepiece_TrainerSpec) + DEFINE_UPB_STRING_ACCESSOR(seed_sentencepieces_file, "", + sentencepiece_TrainerSpec) private: sentencepiece_TrainerSpec* msg_; @@ -425,22 +505,31 @@ class TrainerSpecWrapper { class NormalizerSpecWrapper { friend class upb::ModelProtoWrapper; + public: NormalizerSpecWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { msg_ = sentencepiece_NormalizerSpec_new(arena_); } - explicit NormalizerSpecWrapper(const sentencepiece_NormalizerSpec* msg, upb_Arena* arena = nullptr) - : msg_(const_cast(msg)), arena_(arena), owns_msg_(false) {} - - NormalizerSpecWrapper(const sentencepiece_NormalizerSpec* msg, upb_Arena* arena, - std::function on_change) - : msg_(const_cast(msg)), arena_(arena), owns_msg_(false), on_change_(on_change) {} - - NormalizerSpecWrapper(const NormalizerSpecWrapper& other) : arena_(upb_Arena_New()), owns_msg_(true) { + explicit NormalizerSpecWrapper(const sentencepiece_NormalizerSpec* msg, + upb_Arena* arena = nullptr) + : msg_(const_cast(msg)), + arena_(arena), + owns_msg_(false) {} + + NormalizerSpecWrapper( + const sentencepiece_NormalizerSpec* msg, upb_Arena* arena, + std::function on_change) + : msg_(const_cast(msg)), + arena_(arena), + owns_msg_(false), + on_change_(on_change) {} + + NormalizerSpecWrapper(const NormalizerSpecWrapper& other) + : arena_(upb_Arena_New()), owns_msg_(true) { msg_ = sentencepiece_NormalizerSpec_new(arena_); CopyFrom(other); } - + NormalizerSpecWrapper& operator=(const NormalizerSpecWrapper& other) { if (this != &other) { if (owns_msg_) { @@ -452,7 +541,8 @@ class NormalizerSpecWrapper { if (other.msg_) { size_t size = 0; upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = sentencepiece_NormalizerSpec_serialize(other.msg_, tmp_arena, &size); + char* buf = sentencepiece_NormalizerSpec_serialize(other.msg_, + tmp_arena, &size); if (buf) { msg_ = sentencepiece_NormalizerSpec_parse(buf, size, arena_); if (on_change_) on_change_(msg_); @@ -473,16 +563,22 @@ class NormalizerSpecWrapper { } } - DEFINE_UPB_SERIALIZATION_METHODS(NormalizerSpecWrapper, sentencepiece_NormalizerSpec) + DEFINE_UPB_SERIALIZATION_METHODS(NormalizerSpecWrapper, + sentencepiece_NormalizerSpec) void OnArenaReset() {} DEFINE_UPB_STRING_ACCESSOR(name, "", sentencepiece_NormalizerSpec) - DEFINE_UPB_STRING_ACCESSOR(precompiled_charsmap, "", sentencepiece_NormalizerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(add_dummy_prefix, bool, true, sentencepiece_NormalizerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(remove_extra_whitespaces, bool, true, sentencepiece_NormalizerSpec) - DEFINE_UPB_PRIMITIVE_ACCESSOR(escape_whitespaces, bool, true, sentencepiece_NormalizerSpec) - DEFINE_UPB_STRING_ACCESSOR(normalization_rule_tsv, "", sentencepiece_NormalizerSpec) + DEFINE_UPB_STRING_ACCESSOR(precompiled_charsmap, "", + sentencepiece_NormalizerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(add_dummy_prefix, bool, true, + sentencepiece_NormalizerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(remove_extra_whitespaces, bool, true, + sentencepiece_NormalizerSpec) + DEFINE_UPB_PRIMITIVE_ACCESSOR(escape_whitespaces, bool, true, + sentencepiece_NormalizerSpec) + DEFINE_UPB_STRING_ACCESSOR(normalization_rule_tsv, "", + sentencepiece_NormalizerSpec) private: sentencepiece_NormalizerSpec* msg_; @@ -493,42 +589,39 @@ class NormalizerSpecWrapper { class SelfTestData_SampleWrapper { public: - explicit SelfTestData_SampleWrapper(const sentencepiece_SelfTestData_Sample* msg) + explicit SelfTestData_SampleWrapper( + const sentencepiece_SelfTestData_Sample* msg) : msg_(msg), mutable_msg_(nullptr), arena_(nullptr) {} - SelfTestData_SampleWrapper(sentencepiece_SelfTestData_Sample* msg, upb_Arena* arena) + SelfTestData_SampleWrapper(sentencepiece_SelfTestData_Sample* msg, + upb_Arena* arena) : msg_(msg), mutable_msg_(msg), arena_(arena) {} ProtoStr input() const { if (!msg_ || !sentencepiece_SelfTestData_Sample_has_input(msg_)) return ""; return sentencepiece_SelfTestData_Sample_input(msg_); } ProtoStr expected() const { - if (!msg_ || !sentencepiece_SelfTestData_Sample_has_expected(msg_)) return ""; + if (!msg_ || !sentencepiece_SelfTestData_Sample_has_expected(msg_)) + return ""; return sentencepiece_SelfTestData_Sample_expected(msg_); } void set_input(const std::string& val) { if (mutable_msg_ && arena_) { - upb_StringView sv; - char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); - memcpy(ptr, val.data(), val.size()); - sv.data = ptr; - sv.size = val.size(); - sentencepiece_SelfTestData_Sample_set_input(mutable_msg_, sv); + sentencepiece_SelfTestData_Sample_set_input(mutable_msg_, + MakeUpbString(val, arena_)); } } void set_expected(const std::string& val) { if (mutable_msg_ && arena_) { - upb_StringView sv; - char* ptr = (char*)upb_Arena_Malloc(arena_, val.size()); - memcpy(ptr, val.data(), val.size()); - sv.data = ptr; - sv.size = val.size(); - sentencepiece_SelfTestData_Sample_set_expected(mutable_msg_, sv); + sentencepiece_SelfTestData_Sample_set_expected( + mutable_msg_, MakeUpbString(val, arena_)); } } + private: const sentencepiece_SelfTestData_Sample* msg_; sentencepiece_SelfTestData_Sample* mutable_msg_; upb_Arena* arena_; + std::function on_change_; }; class SelfTestDataWrapper { @@ -537,10 +630,11 @@ class SelfTestDataWrapper { : msg_(msg), mutable_msg_(nullptr), arena_(nullptr) {} SelfTestDataWrapper(sentencepiece_SelfTestData* msg, upb_Arena* arena) : msg_(msg), mutable_msg_(msg), arena_(arena) {} - + class SamplesRepeatedWrapper { public: - SamplesRepeatedWrapper(const sentencepiece_SelfTestData_Sample* const* elements, size_t count) + SamplesRepeatedWrapper( + const sentencepiece_SelfTestData_Sample* const* elements, size_t count) : elements_(elements), count_(count) {} class Iterator { public: @@ -550,14 +644,28 @@ class SelfTestDataWrapper { using pointer = SelfTestData_SampleWrapper*; using reference = SelfTestData_SampleWrapper; - Iterator(const sentencepiece_SelfTestData_Sample* const* elements, size_t index) : elements_(elements), index_(index) {} - Iterator& operator++() { ++index_; return *this; } - Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } - bool operator==(const Iterator& other) const { return index_ == other.index_; } - bool operator!=(const Iterator& other) const { return index_ != other.index_; } + Iterator(const sentencepiece_SelfTestData_Sample* const* elements, + size_t index) + : elements_(elements), index_(index) {} + Iterator& operator++() { + ++index_; + return *this; + } + Iterator operator++(int) { + Iterator tmp = *this; + ++index_; + return tmp; + } + bool operator==(const Iterator& other) const { + return index_ == other.index_; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; + } SelfTestData_SampleWrapper operator*() const { return SelfTestData_SampleWrapper(elements_[index_]); } + private: const sentencepiece_SelfTestData_Sample* const* elements_; size_t index_; @@ -570,6 +678,7 @@ class SelfTestDataWrapper { SelfTestData_SampleWrapper operator[](size_t index) const { return SelfTestData_SampleWrapper(elements_[index]); } + private: const sentencepiece_SelfTestData_Sample* const* elements_; size_t count_; @@ -583,36 +692,46 @@ class SelfTestDataWrapper { } return SamplesRepeatedWrapper(elements, size); } - int samples_size() const { - return samples().size(); - } + int samples_size() const { return samples().size(); } SelfTestData_SampleWrapper* add_samples() { if (mutable_msg_ && arena_) { sentencepiece_SelfTestData_add_samples(mutable_msg_, arena_); int new_index = samples_size() - 1; sample_wrappers_.resize(samples_size()); size_t size; - sentencepiece_SelfTestData_Sample** samples = sentencepiece_SelfTestData_mutable_samples(mutable_msg_, &size); - sample_wrappers_[new_index] = std::make_unique(samples[new_index], arena_); + sentencepiece_SelfTestData_Sample** samples = + sentencepiece_SelfTestData_mutable_samples(mutable_msg_, &size); + sample_wrappers_[new_index] = + std::make_unique(samples[new_index], + arena_); return sample_wrappers_[new_index].get(); } return nullptr; } + private: const sentencepiece_SelfTestData* msg_; sentencepiece_SelfTestData* mutable_msg_; upb_Arena* arena_; - mutable std::vector> sample_wrappers_; + mutable std::vector> + sample_wrappers_; }; class ModelProtoWrapper { public: ModelProtoWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { msg_ = sentencepiece_ModelProto_new(arena_); + normalizer_spec_cache_ = std::make_unique( + sentencepiece_ModelProto_normalizer_spec(msg_), arena_); + denormalizer_spec_cache_ = std::make_unique( + sentencepiece_ModelProto_denormalizer_spec(msg_), arena_); } - explicit ModelProtoWrapper(const sentencepiece_ModelProto* msg, upb_Arena* arena) - : msg_(const_cast(msg)), arena_(arena), owns_msg_(false) {} + explicit ModelProtoWrapper(const sentencepiece_ModelProto* msg, + upb_Arena* arena) + : msg_(const_cast(msg)), + arena_(arena), + owns_msg_(false) {} virtual ~ModelProtoWrapper() { if (owns_msg_ && arena_) { @@ -642,6 +761,12 @@ class ModelProtoWrapper { denormalizer_spec_cache_.reset(); mutable_denormalizer_spec_.reset(); mutable_self_test_data_.reset(); + if (msg_) { + normalizer_spec_cache_ = std::make_unique( + sentencepiece_ModelProto_normalizer_spec(msg_), arena_); + denormalizer_spec_cache_ = std::make_unique( + sentencepiece_ModelProto_denormalizer_spec(msg_), arena_); + } } bool ParseFromIstream(std::istream* input) { @@ -658,7 +783,8 @@ class ModelProtoWrapper { } inline ::sentencepiece::ModelProto_SentencePiece pieces(int index) const { - return ::sentencepiece::ModelProto_SentencePiece(const_cast(this), index); + return ::sentencepiece::ModelProto_SentencePiece( + const_cast(this), index); } inline ::sentencepiece::ModelProto_SentencePiece pieces(int index) { return ::sentencepiece::ModelProto_SentencePiece(this, index); @@ -675,12 +801,20 @@ class ModelProtoWrapper { using pointer = ModelProto_SentencePiece*; using reference = ModelProto_SentencePiece; - Iterator(const ModelProtoWrapper* parent, int index) : parent_(parent), index_(index) {} + Iterator(const ModelProtoWrapper* parent, int index) + : parent_(parent), index_(index) {} ModelProto_SentencePiece operator*() const { - return ModelProto_SentencePiece(const_cast(parent_), index_); + return ModelProto_SentencePiece(const_cast(parent_), + index_); } - Iterator& operator++() { ++index_; return *this; } - bool operator!=(const Iterator& other) const { return index_ != other.index_; } + Iterator& operator++() { + ++index_; + return *this; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; + } + private: const ModelProtoWrapper* parent_; int index_; @@ -688,13 +822,12 @@ class ModelProtoWrapper { Iterator begin() const { return Iterator(parent_, 0); } Iterator end() const { return Iterator(parent_, parent_->pieces_size()); } int size() const { return parent_->pieces_size(); } + private: const ModelProtoWrapper* parent_; }; - PiecesRepeatedWrapper pieces() const { - return PiecesRepeatedWrapper(this); - } + PiecesRepeatedWrapper pieces() const { return PiecesRepeatedWrapper(this); } class MutablePiecesRepeatedWrapper { public: @@ -707,31 +840,76 @@ class ModelProtoWrapper { using pointer = ModelProto_SentencePiece*; using reference = ModelProto_SentencePiece&; - Iterator(ModelProtoWrapper* parent, int index) : parent_(parent), index_(index) {} + Iterator(ModelProtoWrapper* parent, int index) + : parent_(parent), index_(index) {} ModelProto_SentencePiece& operator*() const { return *(parent_->mutable_pieces(index_)); } - Iterator& operator++() { ++index_; return *this; } - Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } - Iterator& operator--() { --index_; return *this; } - Iterator operator--(int) { Iterator tmp = *this; --index_; return tmp; } - - Iterator& operator+=(difference_type n) { index_ += n; return *this; } - Iterator& operator-=(difference_type n) { index_ -= n; return *this; } - - friend Iterator operator+(Iterator it, difference_type n) { it += n; return it; } - friend Iterator operator+(difference_type n, Iterator it) { it += n; return it; } - friend Iterator operator-(Iterator it, difference_type n) { it -= n; return it; } - friend difference_type operator-(const Iterator& a, const Iterator& b) { return a.index_ - b.index_; } - - ModelProto_SentencePiece& operator[](difference_type n) const { return *(parent_->mutable_pieces(index_ + n)); } - - bool operator==(const Iterator& other) const { return index_ == other.index_ && parent_ == other.parent_; } + Iterator& operator++() { + ++index_; + return *this; + } + Iterator operator++(int) { + Iterator tmp = *this; + ++index_; + return tmp; + } + Iterator& operator--() { + --index_; + return *this; + } + Iterator operator--(int) { + Iterator tmp = *this; + --index_; + return tmp; + } + + Iterator& operator+=(difference_type n) { + index_ += n; + return *this; + } + Iterator& operator-=(difference_type n) { + index_ -= n; + return *this; + } + + friend Iterator operator+(Iterator it, difference_type n) { + it += n; + return it; + } + friend Iterator operator+(difference_type n, Iterator it) { + it += n; + return it; + } + friend Iterator operator-(Iterator it, difference_type n) { + it -= n; + return it; + } + friend difference_type operator-(const Iterator& a, const Iterator& b) { + return a.index_ - b.index_; + } + + ModelProto_SentencePiece& operator[](difference_type n) const { + return *(parent_->mutable_pieces(index_ + n)); + } + + bool operator==(const Iterator& other) const { + return index_ == other.index_ && parent_ == other.parent_; + } bool operator!=(const Iterator& other) const { return !(*this == other); } - bool operator<(const Iterator& other) const { return index_ < other.index_; } - bool operator>(const Iterator& other) const { return index_ > other.index_; } - bool operator<=(const Iterator& other) const { return index_ <= other.index_; } - bool operator>=(const Iterator& other) const { return index_ >= other.index_; } + bool operator<(const Iterator& other) const { + return index_ < other.index_; + } + bool operator>(const Iterator& other) const { + return index_ > other.index_; + } + bool operator<=(const Iterator& other) const { + return index_ <= other.index_; + } + bool operator>=(const Iterator& other) const { + return index_ >= other.index_; + } + private: ModelProtoWrapper* parent_; int index_; @@ -739,14 +917,18 @@ class ModelProtoWrapper { Iterator begin() { return Iterator(parent_, 0); } Iterator end() { return Iterator(parent_, parent_->pieces_size()); } int size() const { return parent_->pieces_size(); } - ModelProto_SentencePiece* Mutable(int index) { return parent_->mutable_pieces(index); } + ModelProto_SentencePiece* Mutable(int index) { + return parent_->mutable_pieces(index); + } + private: ModelProtoWrapper* parent_; }; MutablePiecesRepeatedWrapper* mutable_pieces() { if (!mutable_pieces_wrapper_) { - mutable_pieces_wrapper_ = std::make_unique(this); + mutable_pieces_wrapper_ = + std::make_unique(this); } return mutable_pieces_wrapper_.get(); } @@ -758,39 +940,43 @@ class ModelProtoWrapper { ProtoStr piece_at(int index) const { size_t size = 0; - const sentencepiece_ModelProto_SentencePiece* const* pieces = sentencepiece_ModelProto_pieces(msg_, &size); + const sentencepiece_ModelProto_SentencePiece* const* pieces = + sentencepiece_ModelProto_pieces(msg_, &size); if (index < 0 || static_cast(index) >= size) return ""; - return ProtoStr(sentencepiece_ModelProto_SentencePiece_piece(pieces[index])); + return ProtoStr( + sentencepiece_ModelProto_SentencePiece_piece(pieces[index])); } float score_at(int index) const { size_t size = 0; - const sentencepiece_ModelProto_SentencePiece* const* pieces = sentencepiece_ModelProto_pieces(msg_, &size); + const sentencepiece_ModelProto_SentencePiece* const* pieces = + sentencepiece_ModelProto_pieces(msg_, &size); if (index < 0 || static_cast(index) >= size) return 0.0f; return sentencepiece_ModelProto_SentencePiece_score(pieces[index]); } int type_at(int index) const { size_t size = 0; - const sentencepiece_ModelProto_SentencePiece* const* pieces = sentencepiece_ModelProto_pieces(msg_, &size); + const sentencepiece_ModelProto_SentencePiece* const* pieces = + sentencepiece_ModelProto_pieces(msg_, &size); if (index < 0 || static_cast(index) >= size) return 0; return sentencepiece_ModelProto_SentencePiece_type(pieces[index]); } void set_type_at(int index, int type) { if (msg_) { size_t size = 0; - sentencepiece_ModelProto_SentencePiece** pieces = sentencepiece_ModelProto_mutable_pieces(msg_, &size); - sentencepiece_ModelProto_SentencePiece_set_type(pieces[index], static_cast(type)); + sentencepiece_ModelProto_SentencePiece** pieces = + sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_SentencePiece_set_type( + pieces[index], + static_cast(type)); } } void set_piece_at(int index, absl::string_view piece) { if (msg_) { size_t size = 0; - sentencepiece_ModelProto_SentencePiece** pieces = sentencepiece_ModelProto_mutable_pieces(msg_, &size); - upb_StringView sv; - char* ptr = (char*)upb_Arena_Malloc(arena_, piece.size()); - memcpy(ptr, piece.data(), piece.size()); - sv.data = ptr; - sv.size = piece.size(); - sentencepiece_ModelProto_SentencePiece_set_piece(pieces[index], sv); + sentencepiece_ModelProto_SentencePiece** pieces = + sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_SentencePiece_set_piece( + pieces[index], MakeUpbString(piece, arena_)); } } void set_piece_at(int index, const std::string& piece) { @@ -799,7 +985,8 @@ class ModelProtoWrapper { void set_score_at(int index, float score) { if (msg_) { size_t size = 0; - sentencepiece_ModelProto_SentencePiece** pieces = sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_SentencePiece** pieces = + sentencepiece_ModelProto_mutable_pieces(msg_, &size); sentencepiece_ModelProto_SentencePiece_set_score(pieces[index], score); } } @@ -809,7 +996,7 @@ class ModelProtoWrapper { } inline TrainerSpec trainer_spec() const; inline ::sentencepiece::TrainerSpec* mutable_trainer_spec() const; - + inline bool has_normalizer_spec() const { return msg_ && sentencepiece_ModelProto_has_normalizer_spec(msg_); } @@ -835,7 +1022,7 @@ class ModelProtoWrapper { sentencepiece_ModelProto* msg_; upb_Arena* arena_; bool owns_msg_; - + std::vector> piece_wrappers_; std::unique_ptr mutable_pieces_wrapper_; mutable std::unique_ptr mutable_normalizer_spec_; @@ -844,14 +1031,15 @@ class ModelProtoWrapper { mutable std::unique_ptr denormalizer_spec_cache_; mutable std::unique_ptr mutable_denormalizer_spec_; mutable std::unique_ptr mutable_self_test_data_; - + private: void LazyInitPieceWrappersCache() { if (piece_wrappers_.size() != pieces_size()) { piece_wrappers_.resize(pieces_size()); for (int i = 0; i < pieces_size(); ++i) { if (!piece_wrappers_[i]) { - piece_wrappers_[i] = std::make_unique(this, i); + piece_wrappers_[i] = + std::make_unique(this, i); } } } @@ -863,7 +1051,8 @@ class SentencePieceTextWrapper { SentencePieceTextWrapper() : owns_msg_(true), arena_(upb_Arena_New()) { msg_ = sentencepiece_SentencePieceText_new(arena_); } - explicit SentencePieceTextWrapper(std::nullptr_t) : msg_(nullptr), owns_msg_(false), arena_(nullptr) {} + explicit SentencePieceTextWrapper(std::nullptr_t) + : msg_(nullptr), owns_msg_(false), arena_(nullptr) {} virtual ~SentencePieceTextWrapper() { if (owns_msg_ && arena_) { @@ -871,7 +1060,8 @@ class SentencePieceTextWrapper { } } - SentencePieceTextWrapper(const SentencePieceTextWrapper& other) : SentencePieceTextWrapper() { + SentencePieceTextWrapper(const SentencePieceTextWrapper& other) + : SentencePieceTextWrapper() { CopyFrom(other); } SentencePieceTextWrapper& operator=(const SentencePieceTextWrapper& other) { @@ -882,19 +1072,19 @@ class SentencePieceTextWrapper { return *this; } - DEFINE_UPB_SERIALIZATION_METHODS(SentencePieceTextWrapper, sentencepiece_SentencePieceText) + DEFINE_UPB_SERIALIZATION_METHODS(SentencePieceTextWrapper, + sentencepiece_SentencePieceText) void OnArenaReset() { piece_wrappers_.clear(); mutable_pieces_wrapper_.reset(); + if (msg_) { + LazyInitPieceWrappersCache(); + } } - virtual sentencepiece_SentencePieceText* mutable_msg() { - return msg_; - } - virtual const sentencepiece_SentencePieceText* msg() const { - return msg_; - } + virtual sentencepiece_SentencePieceText* mutable_msg() { return msg_; } + virtual const sentencepiece_SentencePieceText* msg() const { return msg_; } virtual upb_Arena* arena() { return arena_; } virtual const upb_Arena* arena() const { return arena_; } @@ -913,19 +1103,19 @@ class SentencePieceTextWrapper { return absl::string_view(sv.data, sv.size); } void set_text(absl::string_view text) { - upb_StringView sv; - char* ptr = (char*)upb_Arena_Malloc(arena(), text.size()); - memcpy(ptr, text.data(), text.size()); - sv.data = ptr; - sv.size = text.size(); - sentencepiece_SentencePieceText_set_text(mutable_msg(), sv); + sentencepiece_SentencePieceText_set_text(mutable_msg(), + MakeUpbString(text, arena())); } void set_text(const char* data, size_t size) { set_text(absl::string_view(data, size)); } - float score() const { return msg() ? sentencepiece_SentencePieceText_score(msg()) : 0.0; } - void set_score(float score) { sentencepiece_SentencePieceText_set_score(mutable_msg(), score); } + float score() const { + return msg() ? sentencepiece_SentencePieceText_score(msg()) : 0.0; + } + void set_score(float score) { + sentencepiece_SentencePieceText_set_score(mutable_msg(), score); + } int pieces_size() const { const auto* m = msg(); @@ -934,21 +1124,27 @@ class SentencePieceTextWrapper { sentencepiece_SentencePieceText_pieces(m, &size); return size; } - + const SentencePieceText_SentencePiece& pieces(int index) const { - LazyInitPieceWrappersCache(); + if (index < 0 || static_cast(index) >= piece_wrappers_.size()) { + return SentencePieceText_SentencePiece::default_instance(); + } return *piece_wrappers_[index]; } SentencePieceText_SentencePiece& pieces(int index) { LazyInitPieceWrappersCache(); + if (index < 0 || static_cast(index) >= piece_wrappers_.size()) { + static SentencePieceText_SentencePiece dummy(nullptr, -1); + return dummy; + } return *piece_wrappers_[index]; } void ReservePieces(int size) { if (mutable_msg()) { - // Do nothing. upb doesn't have a simple "reserve capacity" API, - // and resize_pieces actually grows the array with uninitialized elements. + // Do nothing. upb doesn't have a simple "reserve capacity" API, + // and resize_pieces actually grows the array with uninitialized elements. } } @@ -956,7 +1152,8 @@ class SentencePieceTextWrapper { if (!mutable_msg()) return nullptr; sentencepiece_SentencePieceText_add_pieces(mutable_msg(), arena()); int index = pieces_size() - 1; - auto wrapper = std::make_unique(this, index); + auto wrapper = + std::make_unique(this, index); SentencePieceText_SentencePiece* ptr = wrapper.get(); piece_wrappers_.push_back(std::move(wrapper)); return ptr; @@ -964,7 +1161,8 @@ class SentencePieceTextWrapper { class ConstPiecesRepeatedWrapper { public: - ConstPiecesRepeatedWrapper(const SentencePieceTextWrapper* parent) : parent_(parent) {} + ConstPiecesRepeatedWrapper(const SentencePieceTextWrapper* parent) + : parent_(parent) {} class Iterator { public: using iterator_category = std::forward_iterator_tag; @@ -973,12 +1171,20 @@ class SentencePieceTextWrapper { using pointer = SentencePieceText_SentencePiece*; using reference = SentencePieceText_SentencePiece&; - Iterator(const SentencePieceTextWrapper* parent, int index) : parent_(parent), index_(index) {} + Iterator(const SentencePieceTextWrapper* parent, int index) + : parent_(parent), index_(index) {} SentencePieceText_SentencePiece& operator*() const { - return *(const_cast(parent_)->mutable_pieces(index_)); + return *(const_cast(parent_)->mutable_pieces( + index_)); } - Iterator& operator++() { ++index_; return *this; } - bool operator!=(const Iterator& other) const { return index_ != other.index_; } + Iterator& operator++() { + ++index_; + return *this; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; + } + private: const SentencePieceTextWrapper* parent_; int index_; @@ -986,8 +1192,9 @@ class SentencePieceTextWrapper { Iterator begin() const { return Iterator(parent_, 0); } Iterator end() const { return Iterator(parent_, parent_->pieces_size()); } int size() const { return parent_->pieces_size(); } + private: - const SentencePieceTextWrapper* parent_; + const SentencePieceTextWrapper* parent_; }; ConstPiecesRepeatedWrapper pieces() const { @@ -996,7 +1203,8 @@ class SentencePieceTextWrapper { class MutablePiecesRepeatedWrapper { public: - MutablePiecesRepeatedWrapper(SentencePieceTextWrapper* parent) : parent_(parent) {} + MutablePiecesRepeatedWrapper(SentencePieceTextWrapper* parent) + : parent_(parent) {} class Iterator { public: using iterator_category = std::random_access_iterator_tag; @@ -1005,33 +1213,78 @@ class SentencePieceTextWrapper { using pointer = SentencePieceText_SentencePiece*; using reference = SentencePieceText_SentencePiece&; - Iterator(SentencePieceTextWrapper* parent, int index) : parent_(parent), index_(index) {} + Iterator(SentencePieceTextWrapper* parent, int index) + : parent_(parent), index_(index) {} Iterator() : parent_(nullptr), index_(-1) {} - + SentencePieceText_SentencePiece& operator*() const { return *(parent_->mutable_pieces(index_)); } - Iterator& operator++() { ++index_; return *this; } - Iterator operator++(int) { Iterator tmp = *this; ++index_; return tmp; } - Iterator& operator--() { --index_; return *this; } - Iterator operator--(int) { Iterator tmp = *this; --index_; return tmp; } - - Iterator& operator+=(difference_type n) { index_ += n; return *this; } - Iterator& operator-=(difference_type n) { index_ -= n; return *this; } - - friend Iterator operator+(Iterator it, difference_type n) { it += n; return it; } - friend Iterator operator+(difference_type n, Iterator it) { it += n; return it; } - friend Iterator operator-(Iterator it, difference_type n) { it -= n; return it; } - friend difference_type operator-(const Iterator& a, const Iterator& b) { return a.index_ - b.index_; } - - SentencePieceText_SentencePiece& operator[](difference_type n) const { return *(parent_->mutable_pieces(index_ + n)); } - - bool operator==(const Iterator& other) const { return index_ == other.index_ && parent_ == other.parent_; } + Iterator& operator++() { + ++index_; + return *this; + } + Iterator operator++(int) { + Iterator tmp = *this; + ++index_; + return tmp; + } + Iterator& operator--() { + --index_; + return *this; + } + Iterator operator--(int) { + Iterator tmp = *this; + --index_; + return tmp; + } + + Iterator& operator+=(difference_type n) { + index_ += n; + return *this; + } + Iterator& operator-=(difference_type n) { + index_ -= n; + return *this; + } + + friend Iterator operator+(Iterator it, difference_type n) { + it += n; + return it; + } + friend Iterator operator+(difference_type n, Iterator it) { + it += n; + return it; + } + friend Iterator operator-(Iterator it, difference_type n) { + it -= n; + return it; + } + friend difference_type operator-(const Iterator& a, const Iterator& b) { + return a.index_ - b.index_; + } + + SentencePieceText_SentencePiece& operator[](difference_type n) const { + return *(parent_->mutable_pieces(index_ + n)); + } + + bool operator==(const Iterator& other) const { + return index_ == other.index_ && parent_ == other.parent_; + } bool operator!=(const Iterator& other) const { return !(*this == other); } - bool operator<(const Iterator& other) const { return index_ < other.index_; } - bool operator>(const Iterator& other) const { return index_ > other.index_; } - bool operator<=(const Iterator& other) const { return index_ <= other.index_; } - bool operator>=(const Iterator& other) const { return index_ >= other.index_; } + bool operator<(const Iterator& other) const { + return index_ < other.index_; + } + bool operator>(const Iterator& other) const { + return index_ > other.index_; + } + bool operator<=(const Iterator& other) const { + return index_ <= other.index_; + } + bool operator>=(const Iterator& other) const { + return index_ >= other.index_; + } + private: SentencePieceTextWrapper* parent_; int index_; @@ -1043,14 +1296,18 @@ class SentencePieceTextWrapper { SentencePieceText_SentencePiece* Add() { return parent_->add_pieces(); } void SwapElements(int i, int j) { parent_->SwapElementsData(i, j); } void Reserve(int size) { parent_->ReservePieces(size); } - SentencePieceText_SentencePiece* Mutable(int index) { return parent_->mutable_pieces(index); } + SentencePieceText_SentencePiece* Mutable(int index) { + return parent_->mutable_pieces(index); + } + private: SentencePieceTextWrapper* parent_; }; MutablePiecesRepeatedWrapper* mutable_pieces() { if (!mutable_pieces_wrapper_) { - mutable_pieces_wrapper_ = std::make_unique(this); + mutable_pieces_wrapper_ = + std::make_unique(this); } return mutable_pieces_wrapper_.get(); } @@ -1060,25 +1317,21 @@ class SentencePieceTextWrapper { return piece_wrappers_[index].get(); } - - absl::string_view piece_at(int index) const { size_t size = 0; const auto* pieces = sentencepiece_SentencePieceText_pieces(msg(), &size); if (index < 0 || index >= size) return ""; - upb_StringView sv = sentencepiece_SentencePieceText_SentencePiece_piece(pieces[index]); + upb_StringView sv = + sentencepiece_SentencePieceText_SentencePiece_piece(pieces[index]); return absl::string_view(sv.data, sv.size); } - + void set_piece_at(int index, absl::string_view piece) { size_t size = 0; - auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); - upb_StringView sv; - char* ptr = (char*)upb_Arena_Malloc(arena(), piece.size()); - memcpy(ptr, piece.data(), piece.size()); - sv.data = ptr; - sv.size = piece.size(); - sentencepiece_SentencePieceText_SentencePiece_set_piece(pieces[index], sv); + auto** pieces = + sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + sentencepiece_SentencePieceText_SentencePiece_set_piece( + pieces[index], MakeUpbString(piece, arena())); } uint32_t id_at(int index) const { @@ -1089,7 +1342,8 @@ class SentencePieceTextWrapper { } void set_id_at(int index, uint32_t id) { size_t size = 0; - auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + auto** pieces = + sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); sentencepiece_SentencePieceText_SentencePiece_set_id(pieces[index], id); } @@ -1097,18 +1351,16 @@ class SentencePieceTextWrapper { size_t size = 0; const auto* pieces = sentencepiece_SentencePieceText_pieces(msg(), &size); if (index < 0 || index >= size) return ""; - upb_StringView sv = sentencepiece_SentencePieceText_SentencePiece_surface(pieces[index]); + upb_StringView sv = + sentencepiece_SentencePieceText_SentencePiece_surface(pieces[index]); return absl::string_view(sv.data, sv.size); } void set_surface_at(int index, absl::string_view surface) { size_t size = 0; - auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); - upb_StringView sv; - char* ptr = (char*)upb_Arena_Malloc(arena(), surface.size()); - memcpy(ptr, surface.data(), surface.size()); - sv.data = ptr; - sv.size = surface.size(); - sentencepiece_SentencePieceText_SentencePiece_set_surface(pieces[index], sv); + auto** pieces = + sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + sentencepiece_SentencePieceText_SentencePiece_set_surface( + pieces[index], MakeUpbString(surface, arena())); } uint32_t begin_at(int index) const { @@ -1119,8 +1371,10 @@ class SentencePieceTextWrapper { } void set_begin_at(int index, uint32_t begin) { size_t size = 0; - auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); - sentencepiece_SentencePieceText_SentencePiece_set_begin(pieces[index], begin); + auto** pieces = + sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + sentencepiece_SentencePieceText_SentencePiece_set_begin(pieces[index], + begin); } uint32_t end_at(int index) const { @@ -1131,13 +1385,15 @@ class SentencePieceTextWrapper { } void set_end_at(int index, uint32_t end) { size_t size = 0; - auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + auto** pieces = + sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); sentencepiece_SentencePieceText_SentencePiece_set_end(pieces[index], end); } void SwapElementsData(int i, int j) { size_t size = 0; - auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + auto** pieces = + sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); std::swap(pieces[i], pieces[j]); } @@ -1146,22 +1402,21 @@ class SentencePieceTextWrapper { piece_wrappers_.resize(pieces_size()); for (int i = 0; i < pieces_size(); ++i) { if (!piece_wrappers_[i]) { - piece_wrappers_[i] = std::make_unique( - const_cast(this), i); + piece_wrappers_[i] = + std::make_unique( + const_cast(this), i); } } } } private: - - - sentencepiece_SentencePieceText* msg_; bool owns_msg_; upb_Arena* arena_; - - mutable std::vector> piece_wrappers_; + + mutable std::vector> + piece_wrappers_; std::unique_ptr mutable_pieces_wrapper_; }; @@ -1176,10 +1431,12 @@ class NBestSentencePieceTextWrapper { } } - NBestSentencePieceTextWrapper(const NBestSentencePieceTextWrapper& other) : NBestSentencePieceTextWrapper() { + NBestSentencePieceTextWrapper(const NBestSentencePieceTextWrapper& other) + : NBestSentencePieceTextWrapper() { CopyFrom(other); } - NBestSentencePieceTextWrapper& operator=(const NBestSentencePieceTextWrapper& other) { + NBestSentencePieceTextWrapper& operator=( + const NBestSentencePieceTextWrapper& other) { if (this != &other) { Clear(); CopyFrom(other); @@ -1187,14 +1444,20 @@ class NBestSentencePieceTextWrapper { return *this; } - DEFINE_UPB_SERIALIZATION_METHODS(NBestSentencePieceTextWrapper, sentencepiece_NBestSentencePieceText) + DEFINE_UPB_SERIALIZATION_METHODS(NBestSentencePieceTextWrapper, + sentencepiece_NBestSentencePieceText) void OnArenaReset() { nbest_wrappers_.clear(); + if (msg_) { + LazyInitNbestWrappersCache(); + } } virtual sentencepiece_NBestSentencePieceText* mutable_msg() { return msg_; } - virtual const sentencepiece_NBestSentencePieceText* msg() const { return msg_; } + virtual const sentencepiece_NBestSentencePieceText* msg() const { + return msg_; + } virtual upb_Arena* arena() { return arena_; } virtual const upb_Arena* arena() const { return arena_; } @@ -1216,7 +1479,8 @@ class NBestSentencePieceTextWrapper { class ConstNbestsRepeatedWrapper { public: - ConstNbestsRepeatedWrapper(const NBestSentencePieceTextWrapper* parent) : parent_(parent) {} + ConstNbestsRepeatedWrapper(const NBestSentencePieceTextWrapper* parent) + : parent_(parent) {} class Iterator { public: using iterator_category = std::forward_iterator_tag; @@ -1225,10 +1489,17 @@ class NBestSentencePieceTextWrapper { using pointer = const NBestSentencePieceText_Sub*; using reference = const NBestSentencePieceText_Sub&; - Iterator(const NBestSentencePieceTextWrapper* parent, int index) : parent_(parent), index_(index) {} + Iterator(const NBestSentencePieceTextWrapper* parent, int index) + : parent_(parent), index_(index) {} inline const NBestSentencePieceText_Sub& operator*() const; - Iterator& operator++() { ++index_; return *this; } - bool operator!=(const Iterator& other) const { return index_ != other.index_; } + Iterator& operator++() { + ++index_; + return *this; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; + } + private: const NBestSentencePieceTextWrapper* parent_; int index_; @@ -1236,6 +1507,7 @@ class NBestSentencePieceTextWrapper { Iterator begin() const { return Iterator(parent_, 0); } Iterator end() const { return Iterator(parent_, parent_->nbests_size()); } int size() const { return parent_->nbests_size(); } + private: const NBestSentencePieceTextWrapper* parent_; }; @@ -1244,8 +1516,6 @@ class NBestSentencePieceTextWrapper { return ConstNbestsRepeatedWrapper(this); } - - void LazyInitNbestWrappersCache() const { if (nbest_wrappers_.size() != nbests_size()) { nbest_wrappers_.resize(nbests_size()); @@ -1262,23 +1532,23 @@ class NBestSentencePieceTextWrapper { sentencepiece_NBestSentencePieceText* msg_; upb_Arena* arena_; bool owns_msg_; - - mutable std::vector> nbest_wrappers_; + + mutable std::vector> + nbest_wrappers_; + private: }; } // namespace upb - - class TrainerSpec : public sentencepiece::upb::TrainerSpecWrapper { public: - using TrainerSpecWrapper::TrainerSpecWrapper; + using TrainerSpecWrapper::BPE; + using TrainerSpecWrapper::CHAR; using TrainerSpecWrapper::ModelType; + using TrainerSpecWrapper::TrainerSpecWrapper; using TrainerSpecWrapper::UNIGRAM; - using TrainerSpecWrapper::BPE; using TrainerSpecWrapper::WORD; - using TrainerSpecWrapper::CHAR; static const TrainerSpec& default_instance() { static TrainerSpec instance(nullptr); @@ -1335,22 +1605,25 @@ class NBestSentencePieceText; class NBestSentencePieceText_Sub : public SentencePieceText { public: - NBestSentencePieceText_Sub(upb::NBestSentencePieceTextWrapper* parent, int index) + NBestSentencePieceText_Sub(upb::NBestSentencePieceTextWrapper* parent, + int index) : SentencePieceText(nullptr), parent_(parent), index_(index) {} - + inline sentencepiece_SentencePieceText* mutable_msg() override; inline const sentencepiece_SentencePieceText* msg() const override; inline upb_Arena* arena() override; inline const upb_Arena* arena() const override; + private: upb::NBestSentencePieceTextWrapper* parent_; int index_; }; -class NBestSentencePieceText : public sentencepiece::upb::NBestSentencePieceTextWrapper { +class NBestSentencePieceText + : public sentencepiece::upb::NBestSentencePieceTextWrapper { public: - using NBestSentencePieceTextWrapper::NBestSentencePieceTextWrapper; using NBestSentencePieceTextWrapper::nbests; + using NBestSentencePieceTextWrapper::NBestSentencePieceTextWrapper; using SentencePieceTextWrapperSub = NBestSentencePieceText_Sub; inline const NBestSentencePieceText_Sub& nbests(int index) const; @@ -1358,8 +1631,9 @@ class NBestSentencePieceText : public sentencepiece::upb::NBestSentencePieceText class MutableNbestsRepeatedWrapper { public: - MutableNbestsRepeatedWrapper(NBestSentencePieceText* parent) : parent_(parent) {} - + MutableNbestsRepeatedWrapper(NBestSentencePieceText* parent) + : parent_(parent) {} + class Iterator { public: using iterator_category = std::forward_iterator_tag; @@ -1368,97 +1642,105 @@ class NBestSentencePieceText : public sentencepiece::upb::NBestSentencePieceText using pointer = NBestSentencePieceText_Sub*; using reference = NBestSentencePieceText_Sub&; - Iterator(NBestSentencePieceText* parent, int index) : parent_(parent), index_(index) {} + Iterator(NBestSentencePieceText* parent, int index) + : parent_(parent), index_(index) {} inline NBestSentencePieceText_Sub& operator*() const { - return *(parent_->mutable_nbests_at(index_)); + return *(parent_->mutable_nbests_at(index_)); + } + Iterator& operator++() { + ++index_; + return *this; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; } - Iterator& operator++() { ++index_; return *this; } - bool operator!=(const Iterator& other) const { return index_ != other.index_; } + private: NBestSentencePieceText* parent_; int index_; }; - + Iterator begin() { return Iterator(parent_, 0); } Iterator end() { return Iterator(parent_, parent_->nbests_size()); } int size() const { return parent_->nbests_size(); } - + NBestSentencePieceText_Sub* Add() { return parent_->add_nbests(); } + private: NBestSentencePieceText* parent_; }; MutableNbestsRepeatedWrapper* mutable_nbests() { if (!mutable_nbests_wrapper_) { - mutable_nbests_wrapper_ = std::make_unique(this); + mutable_nbests_wrapper_ = + std::make_unique(this); } return mutable_nbests_wrapper_.get(); } - + inline NBestSentencePieceText_Sub* mutable_nbests_at(int index); static const NBestSentencePieceText& default_instance() { static NBestSentencePieceText instance; return instance; } + private: std::unique_ptr mutable_nbests_wrapper_; }; - - inline ProtoStr ModelProto_SentencePiece::piece() const { - return parent_->piece_at(index_); + return parent_ ? parent_->piece_at(index_) : ""; } inline float ModelProto_SentencePiece::score() const { - return parent_->score_at(index_); + return parent_ ? parent_->score_at(index_) : 0.0f; } inline ModelProto_SentencePiece::Type ModelProto_SentencePiece::type() const { - return static_cast(parent_->type_at(index_)); + return parent_ ? static_cast(parent_->type_at(index_)) : NORMAL; } inline void ModelProto_SentencePiece::set_type(Type type) { - parent_->set_type_at(index_, type); + if (parent_) parent_->set_type_at(index_, type); } inline TrainerSpec upb::ModelProtoWrapper::trainer_spec() const { - return TrainerSpec(msg_ ? sentencepiece_ModelProto_trainer_spec(msg_) : nullptr, arena_); + return TrainerSpec( + msg_ ? sentencepiece_ModelProto_trainer_spec(msg_) : nullptr, arena_); } inline const NormalizerSpec& upb::ModelProtoWrapper::normalizer_spec() const { - if (!normalizer_spec_cache_) { - normalizer_spec_cache_ = std::make_unique( - msg_ ? sentencepiece_ModelProto_normalizer_spec(msg_) : nullptr, arena_); - } - return *normalizer_spec_cache_; + static const NormalizerSpec default_instance(nullptr); + return normalizer_spec_cache_ ? *normalizer_spec_cache_ : default_instance; } inline const NormalizerSpec& upb::ModelProtoWrapper::denormalizer_spec() const { - if (!denormalizer_spec_cache_) { - denormalizer_spec_cache_ = std::make_unique( - msg_ ? sentencepiece_ModelProto_denormalizer_spec(msg_) : nullptr, arena_); - } - return *denormalizer_spec_cache_; + static const NormalizerSpec default_instance(nullptr); + return denormalizer_spec_cache_ ? *denormalizer_spec_cache_ : default_instance; } inline SelfTestData upb::ModelProtoWrapper::self_test_data() const { - return SelfTestData(msg_ ? sentencepiece_ModelProto_self_test_data(msg_) : nullptr); + return SelfTestData(msg_ ? sentencepiece_ModelProto_self_test_data(msg_) + : nullptr); } -inline upb_Arena* NBestSentencePieceText_Sub::arena() { return parent_->arena(); } -inline const upb_Arena* NBestSentencePieceText_Sub::arena() const { return parent_->arena(); } +inline upb_Arena* NBestSentencePieceText_Sub::arena() { + return parent_ ? parent_->arena() : nullptr; +} +inline const upb_Arena* NBestSentencePieceText_Sub::arena() const { + return parent_ ? parent_->arena() : nullptr; +} -inline NBestSentencePieceText_Sub* upb::NBestSentencePieceTextWrapper::add_nbests() { +inline NBestSentencePieceText_Sub* +upb::NBestSentencePieceTextWrapper::add_nbests() { if (!msg_) return nullptr; sentencepiece_NBestSentencePieceText_add_nbests(msg_, arena_); - + int index = nbests_size() - 1; - - auto wrapper = std::make_unique( - this, index); + + auto wrapper = std::make_unique(this, index); NBestSentencePieceText_Sub* ptr = wrapper.get(); nbest_wrappers_.push_back(std::move(wrapper)); return ptr; } -inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper::nbests(int index) const { - LazyInitNbestWrappersCache(); +inline const NBestSentencePieceText_Sub& +upb::NBestSentencePieceTextWrapper::nbests(int index) const { if (index < 0 || static_cast(index) >= nbest_wrappers_.size()) { static const NBestSentencePieceText_Sub default_sub(nullptr, -1); return default_sub; @@ -1466,20 +1748,28 @@ inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper::nbe return *nbest_wrappers_[index]; } -inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper::ConstNbestsRepeatedWrapper::Iterator::operator*() const { +inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper:: + ConstNbestsRepeatedWrapper::Iterator::operator*() const { return parent_->nbests(index_); } -inline sentencepiece_SentencePieceText* NBestSentencePieceText_Sub::mutable_msg() { +inline sentencepiece_SentencePieceText* +NBestSentencePieceText_Sub::mutable_msg() { + if (!parent_ || index_ < 0) return nullptr; size_t size = 0; sentencepiece_SentencePieceText** nbests = - sentencepiece_NBestSentencePieceText_mutable_nbests(parent_->mutable_msg(), &size); + sentencepiece_NBestSentencePieceText_mutable_nbests( + parent_->mutable_msg(), &size); + if (static_cast(index_) >= size) return nullptr; return nbests[index_]; } -inline const sentencepiece_SentencePieceText* NBestSentencePieceText_Sub::msg() const { +inline const sentencepiece_SentencePieceText* NBestSentencePieceText_Sub::msg() + const { + if (!parent_ || index_ < 0) return nullptr; size_t size = 0; const sentencepiece_SentencePieceText* const* nbests = sentencepiece_NBestSentencePieceText_nbests(parent_->msg(), &size); + if (static_cast(index_) >= size) return nullptr; return nbests[index_]; } @@ -1489,12 +1779,14 @@ inline absl::string_view SentencePieceText_SentencePiece::piece() const { } return parent_->piece_at(index_); } -inline void SentencePieceText_SentencePiece::set_piece(absl::string_view piece) { +inline void SentencePieceText_SentencePiece::set_piece( + absl::string_view piece) { if (parent_) { parent_->set_piece_at(index_, piece); } } -inline void SentencePieceText_SentencePiece::set_piece(const char* data, size_t size) { +inline void SentencePieceText_SentencePiece::set_piece(const char* data, + size_t size) { set_piece(absl::string_view(data, size)); } @@ -1511,12 +1803,14 @@ inline absl::string_view SentencePieceText_SentencePiece::surface() const { } return parent_->surface_at(index_); } -inline void SentencePieceText_SentencePiece::set_surface(absl::string_view surface) { +inline void SentencePieceText_SentencePiece::set_surface( + absl::string_view surface) { if (parent_) { parent_->set_surface_at(index_, surface); } } -inline void SentencePieceText_SentencePiece::set_surface(const char* data, size_t size) { +inline void SentencePieceText_SentencePiece::set_surface(const char* data, + size_t size) { set_surface(absl::string_view(data, size)); } @@ -1534,59 +1828,88 @@ inline void SentencePieceText_SentencePiece::set_end(uint32_t end) { if (parent_) parent_->set_end_at(index_, end); } -inline void swap(SentencePieceText_SentencePiece a, SentencePieceText_SentencePiece b) { +inline void swap(SentencePieceText_SentencePiece a, + SentencePieceText_SentencePiece b) { a.parent_->SwapElementsData(a.index_, b.index_); } - - -inline const NBestSentencePieceText_Sub& NBestSentencePieceText::nbests(int index) const { +inline const NBestSentencePieceText_Sub& NBestSentencePieceText::nbests( + int index) const { return NBestSentencePieceTextWrapper::nbests(index); } inline NBestSentencePieceText_Sub* NBestSentencePieceText::add_nbests() { return NBestSentencePieceTextWrapper::add_nbests(); } -inline NBestSentencePieceText_Sub* NBestSentencePieceText::mutable_nbests_at(int index) { +inline NBestSentencePieceText_Sub* NBestSentencePieceText::mutable_nbests_at( + int index) { if (nbest_wrappers_.size() != nbests_size()) { nbest_wrappers_.resize(nbests_size()); for (int i = 0; i < nbests_size(); ++i) { if (!nbest_wrappers_[i]) { - nbest_wrappers_[i] = std::make_unique(this, i); + nbest_wrappers_[i] = + std::make_unique(this, i); } } } return nbest_wrappers_[index].get(); } -inline ::sentencepiece::NormalizerSpec* upb::ModelProtoWrapper::mutable_normalizer_spec() const { +inline ::sentencepiece::NormalizerSpec* +upb::ModelProtoWrapper::mutable_normalizer_spec() const { if (!mutable_normalizer_spec_) { if (msg_) { - sentencepiece_NormalizerSpec* sub_msg = sentencepiece_ModelProto_mutable_normalizer_spec(msg_, arena_); - mutable_normalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>( - sub_msg, arena_, [this](const sentencepiece_NormalizerSpec* new_sub_msg) { - sentencepiece_ModelProto_set_normalizer_spec( - this->msg_, const_cast(new_sub_msg)); - this->normalizer_spec_cache_.reset(); - }); + sentencepiece_NormalizerSpec* sub_msg = + sentencepiece_ModelProto_mutable_normalizer_spec(msg_, arena_); + if (normalizer_spec_cache_) { + normalizer_spec_cache_->msg_ = sub_msg; + } + mutable_normalizer_spec_ = + std::make_unique<::sentencepiece::NormalizerSpec>( + sub_msg, arena_, + [this](const sentencepiece_NormalizerSpec* new_sub_msg) { + sentencepiece_ModelProto_set_normalizer_spec( + this->msg_, + const_cast(new_sub_msg)); + if (this->normalizer_spec_cache_) { + this->normalizer_spec_cache_->msg_ = + const_cast( + sentencepiece_ModelProto_normalizer_spec(this->msg_)); + } + }); } else { - mutable_normalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); + mutable_normalizer_spec_ = + std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); } } return mutable_normalizer_spec_.get(); } -inline ::sentencepiece::NormalizerSpec* upb::ModelProtoWrapper::mutable_denormalizer_spec() const { +inline ::sentencepiece::NormalizerSpec* +upb::ModelProtoWrapper::mutable_denormalizer_spec() const { if (!mutable_denormalizer_spec_) { if (msg_) { - sentencepiece_NormalizerSpec* sub_msg = sentencepiece_ModelProto_mutable_denormalizer_spec(msg_, arena_); - mutable_denormalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>( - sub_msg, arena_, [this](const sentencepiece_NormalizerSpec* new_sub_msg) { - sentencepiece_ModelProto_set_denormalizer_spec( - this->msg_, const_cast(new_sub_msg)); - this->denormalizer_spec_cache_.reset(); - }); + sentencepiece_NormalizerSpec* sub_msg = + sentencepiece_ModelProto_mutable_denormalizer_spec(msg_, arena_); + if (denormalizer_spec_cache_) { + denormalizer_spec_cache_->msg_ = sub_msg; + } + mutable_denormalizer_spec_ = + std::make_unique<::sentencepiece::NormalizerSpec>( + sub_msg, arena_, + [this](const sentencepiece_NormalizerSpec* new_sub_msg) { + sentencepiece_ModelProto_set_denormalizer_spec( + this->msg_, + const_cast(new_sub_msg)); + if (this->denormalizer_spec_cache_) { + this->denormalizer_spec_cache_->msg_ = + const_cast( + sentencepiece_ModelProto_denormalizer_spec( + this->msg_)); + } + }); } else { - mutable_denormalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); + mutable_denormalizer_spec_ = + std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); } } return mutable_denormalizer_spec_.get(); @@ -1597,35 +1920,45 @@ inline ModelProto_SentencePiece* ModelProto::add_pieces() { sentencepiece_ModelProto_add_pieces(msg_, arena_); int new_index = pieces_size() - 1; piece_wrappers_.resize(pieces_size()); - piece_wrappers_[new_index] = std::make_unique(this, new_index); + piece_wrappers_[new_index] = + std::make_unique(this, new_index); return piece_wrappers_[new_index].get(); } return nullptr; } -inline ::sentencepiece::TrainerSpec* upb::ModelProtoWrapper::mutable_trainer_spec() const { +inline ::sentencepiece::TrainerSpec* +upb::ModelProtoWrapper::mutable_trainer_spec() const { if (!mutable_trainer_spec_) { if (msg_) { - sentencepiece_TrainerSpec* sub_msg = sentencepiece_ModelProto_mutable_trainer_spec(msg_, arena_); + sentencepiece_TrainerSpec* sub_msg = + sentencepiece_ModelProto_mutable_trainer_spec(msg_, arena_); mutable_trainer_spec_ = std::make_unique<::sentencepiece::TrainerSpec>( - sub_msg, arena_, [this](const sentencepiece_TrainerSpec* new_sub_msg) { + sub_msg, arena_, + [this](const sentencepiece_TrainerSpec* new_sub_msg) { sentencepiece_ModelProto_set_trainer_spec( - this->msg_, const_cast(new_sub_msg)); + this->msg_, + const_cast(new_sub_msg)); }); } else { - mutable_trainer_spec_ = std::make_unique<::sentencepiece::TrainerSpec>(nullptr); + mutable_trainer_spec_ = + std::make_unique<::sentencepiece::TrainerSpec>(nullptr); } } return mutable_trainer_spec_.get(); } -inline ::sentencepiece::SelfTestData* upb::ModelProtoWrapper::mutable_self_test_data() const { +inline ::sentencepiece::SelfTestData* +upb::ModelProtoWrapper::mutable_self_test_data() const { if (!mutable_self_test_data_) { if (msg_) { - sentencepiece_SelfTestData* sub_msg = sentencepiece_ModelProto_mutable_self_test_data(msg_, arena_); - mutable_self_test_data_ = std::make_unique<::sentencepiece::SelfTestData>(sub_msg, arena_); + sentencepiece_SelfTestData* sub_msg = + sentencepiece_ModelProto_mutable_self_test_data(msg_, arena_); + mutable_self_test_data_ = + std::make_unique<::sentencepiece::SelfTestData>(sub_msg, arena_); } else { - mutable_self_test_data_ = std::make_unique<::sentencepiece::SelfTestData>(nullptr, nullptr); + mutable_self_test_data_ = + std::make_unique<::sentencepiece::SelfTestData>(nullptr, nullptr); } } return mutable_self_test_data_.get(); @@ -1635,39 +1968,50 @@ inline void upb::ModelProtoWrapper::set_trainer_spec(const TrainerSpec& spec) { if (msg_ && spec.msg_) { size_t size = 0; upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = sentencepiece_TrainerSpec_serialize(spec.msg_, tmp_arena, &size); + char* buf = + sentencepiece_TrainerSpec_serialize(spec.msg_, tmp_arena, &size); if (buf) { - sentencepiece_TrainerSpec* sub_msg = sentencepiece_TrainerSpec_parse(buf, size, arena_); + sentencepiece_TrainerSpec* sub_msg = + sentencepiece_TrainerSpec_parse(buf, size, arena_); sentencepiece_ModelProto_set_trainer_spec(msg_, sub_msg); - mutable_trainer_spec_ = std::make_unique<::sentencepiece::TrainerSpec>(sub_msg, arena_); + mutable_trainer_spec_ = + std::make_unique<::sentencepiece::TrainerSpec>(sub_msg, arena_); } upb_Arena_Free(tmp_arena); } } -inline void upb::ModelProtoWrapper::set_normalizer_spec(const NormalizerSpec& spec) { +inline void upb::ModelProtoWrapper::set_normalizer_spec( + const NormalizerSpec& spec) { if (msg_ && spec.msg_) { size_t size = 0; upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); + char* buf = + sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); if (buf) { - sentencepiece_NormalizerSpec* sub_msg = sentencepiece_NormalizerSpec_parse(buf, size, arena_); + sentencepiece_NormalizerSpec* sub_msg = + sentencepiece_NormalizerSpec_parse(buf, size, arena_); sentencepiece_ModelProto_set_normalizer_spec(msg_, sub_msg); - normalizer_spec_cache_ = std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); + normalizer_spec_cache_ = + std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); } upb_Arena_Free(tmp_arena); } } -inline void upb::ModelProtoWrapper::set_denormalizer_spec(const NormalizerSpec& spec) { +inline void upb::ModelProtoWrapper::set_denormalizer_spec( + const NormalizerSpec& spec) { if (msg_ && spec.msg_) { size_t size = 0; upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); + char* buf = + sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); if (buf) { - sentencepiece_NormalizerSpec* sub_msg = sentencepiece_NormalizerSpec_parse(buf, size, arena_); + sentencepiece_NormalizerSpec* sub_msg = + sentencepiece_NormalizerSpec_parse(buf, size, arena_); sentencepiece_ModelProto_set_denormalizer_spec(msg_, sub_msg); - mutable_denormalizer_spec_ = std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); + mutable_denormalizer_spec_ = + std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); } upb_Arena_Free(tmp_arena); } diff --git a/src/builtin_upb/upb_wrapper_test.cc b/src/builtin_upb/upb_wrapper_test.cc new file mode 100644 index 000000000..d5d9344c4 --- /dev/null +++ b/src/builtin_upb/upb_wrapper_test.cc @@ -0,0 +1,296 @@ +// Copyright 2026 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "builtin_upb/upb_wrapper.h" + +#include "testharness.h" + +namespace sentencepiece { +namespace { + +// 1. Basic Serialization and Multi-write / Empty string support +TEST(UpbWrapperTest, BasicSerializationAndAccessors) { + ModelProto model; + + EXPECT_EQ(model.pieces_size(), 0); + EXPECT_FALSE(model.has_normalizer_spec()); + + // Test primitive setters + auto* trainer_spec = model.mutable_trainer_spec(); + trainer_spec->set_vocab_size(1000); + trainer_spec->set_character_coverage(0.999f); + trainer_spec->set_model_type(TrainerSpec::BPE); + + // Test nested string setters (NFKC / Normalizer) + auto* normalizer_spec = model.mutable_normalizer_spec(); + normalizer_spec->set_name("nfkc"); + + // Verify multiple overwrites on same fields + trainer_spec->set_vocab_size(2000); + normalizer_spec->set_name("identity"); + + // Test empty string assignment (valid edge-case) + normalizer_spec->set_precompiled_charsmap(""); + + // Reflection checks + EXPECT_EQ(model.trainer_spec().vocab_size(), 2000); + EXPECT_NEAR(model.trainer_spec().character_coverage(), 0.999f, 1e-5); + EXPECT_EQ(model.trainer_spec().model_type(), TrainerSpec::BPE); + EXPECT_EQ(model.normalizer_spec().name(), "identity"); + EXPECT_EQ(model.normalizer_spec().precompiled_charsmap(), ""); + + // Clear fields and verify defaults + trainer_spec->clear_vocab_size(); + EXPECT_EQ(model.trainer_spec().vocab_size(), 8000); // Default protobuf value + + // Serialization cycle + std::string serialized = model.SerializeAsString(); + EXPECT_FALSE(serialized.empty()); + + ModelProto model2; + EXPECT_TRUE(model2.ParseFromString(serialized)); + EXPECT_EQ(model2.trainer_spec().vocab_size(), 8000); + EXPECT_EQ(model2.normalizer_spec().name(), "identity"); + EXPECT_EQ(model2.normalizer_spec().precompiled_charsmap(), ""); +} + +// 2. Comprehensive Out-of-Bounds safety test across all wrapper classes +TEST(UpbWrapperTest, RepeatedAccessAndBoundsSafety) { + ModelProto model; + + // Add pieces to ModelProto + auto* p1 = model.add_pieces(); + p1->set_piece("hello"); + p1->set_score(1.5f); + p1->set_type(ModelProto_SentencePiece::NORMAL); + + auto* p2 = model.add_pieces(); + p2->set_piece("world"); + p2->set_score(2.5f); + + EXPECT_EQ(model.pieces_size(), 2); + + // Normal access checks + EXPECT_EQ(model.piece_at(0), "hello"); + EXPECT_NEAR(model.score_at(0), 1.5f, 1e-5); + EXPECT_EQ(model.piece_at(1), "world"); + EXPECT_NEAR(model.score_at(1), 2.5f, 1e-5); + + // ModelProto Bounds Safety Checks + EXPECT_EQ(model.piece_at(-1), ""); + EXPECT_EQ(model.piece_at(2), ""); + EXPECT_EQ(model.piece_at(100), ""); + EXPECT_NEAR(model.score_at(-1), 0.0f, 1e-5); + EXPECT_NEAR(model.score_at(2), 0.0f, 1e-5); + EXPECT_EQ(model.type_at(-1), 0); + EXPECT_EQ(model.type_at(2), 0); + + // SentencePieceTextWrapper Bounds Safety Checks + SentencePieceText spt; + EXPECT_EQ(spt.pieces_size(), 0); + // Access empty SentencePieceText should return default safe items or values + EXPECT_EQ(spt.text(), ""); + EXPECT_NEAR(spt.score(), 0.0f, 1e-5); + + auto* sp_item = spt.add_pieces(); + sp_item->set_piece("token"); + sp_item->set_surface("surface"); + sp_item->set_id(42); + + EXPECT_EQ(spt.pieces_size(), 1); + EXPECT_EQ(spt.piece_at(0), "token"); + EXPECT_EQ(spt.surface_at(0), "surface"); + EXPECT_EQ(spt.id_at(0), 42); + + // Test OOB for SentencePieceText + EXPECT_EQ(spt.piece_at(-1), ""); + EXPECT_EQ(spt.piece_at(1), ""); + EXPECT_EQ(spt.surface_at(-1), ""); + EXPECT_EQ(spt.surface_at(1), ""); + EXPECT_EQ(spt.id_at(-1), 0); + EXPECT_EQ(spt.id_at(1), 0); + EXPECT_EQ(spt.begin_at(-1), 0); + EXPECT_EQ(spt.begin_at(1), 0); + EXPECT_EQ(spt.end_at(-1), 0); + EXPECT_EQ(spt.end_at(1), 0); + + // NBestSentencePieceTextWrapper Bounds Safety Checks + NBestSentencePieceText nbest; + EXPECT_EQ(nbest.nbests_size(), 0); + // Accessing OOB NBest items should return a static default empty wrapper + // reference + const auto& default_sub1 = nbest.nbests(-1); + const auto& default_sub2 = nbest.nbests(0); + EXPECT_EQ(default_sub1.pieces_size(), 0); + EXPECT_EQ(default_sub2.pieces_size(), 0); + + auto* sub = nbest.add_nbests(); + auto* sub_sp = sub->add_pieces(); + sub_sp->set_piece("nbest_token"); + + EXPECT_EQ(nbest.nbests_size(), 1); + EXPECT_EQ(nbest.nbests(0).pieces_size(), 1); + EXPECT_EQ(nbest.nbests(0).piece_at(0), "nbest_token"); + + // Test OOB sub elements + EXPECT_EQ(nbest.nbests(0).piece_at(-1), ""); + EXPECT_EQ(nbest.nbests(0).piece_at(1), ""); +} + +// 3. Child to Parent Synchronization & Cache Rebuilding Verification +TEST(UpbWrapperTest, ChildParentSyncAndRebuilding) { + ModelProto model; + + EXPECT_FALSE(model.has_normalizer_spec()); + + // 1. Test string field sync on normalizer_spec + auto* normalizer = model.mutable_normalizer_spec(); + normalizer->set_name("custom_norm"); + EXPECT_TRUE(model.has_normalizer_spec()); + // Verify Eagerly rebuilt cache holds the correct updated value + EXPECT_EQ(model.normalizer_spec().name(), "custom_norm"); + + // 2. Test primitive field sync (e.g. add_dummy_prefix = false) + // This was previously failing due to primitive accessor macros missing + // on_change_ calls + normalizer->set_add_dummy_prefix(false); + EXPECT_FALSE(model.normalizer_spec().add_dummy_prefix()); + + // 3. Test enum field sync on trainer_spec + auto* trainer = model.mutable_trainer_spec(); + trainer->set_model_type(TrainerSpec::WORD); + EXPECT_EQ(model.trainer_spec().model_type(), TrainerSpec::WORD); + + // 4. Test repeated string field sync on trainer_spec + trainer->add_control_symbols(""); + EXPECT_EQ(model.trainer_spec().control_symbols_size(), 1); + EXPECT_EQ(model.trainer_spec().control_symbols(0), ""); + + // 5. Test Copy and Assignment cache consistency + ModelProto model_copy(model); // Copy constructor + EXPECT_FALSE(model_copy.normalizer_spec().add_dummy_prefix()); + EXPECT_EQ(model_copy.trainer_spec().model_type(), TrainerSpec::WORD); + + ModelProto model_assigned; + model_assigned = model; // Copy assignment operator + EXPECT_FALSE(model_assigned.normalizer_spec().add_dummy_prefix()); + EXPECT_EQ(model_assigned.trainer_spec().model_type(), TrainerSpec::WORD); +} + +// 4. High-load concurrent read thread safety verification +TEST(UpbWrapperTest, ThreadSafetyHighLoad) { + ModelProto model; + + // Add 5000 items to put pressure on memory layout + for (int i = 0; i < 5000; ++i) { + auto* p = model.add_pieces(); + p->set_piece("tok_" + std::to_string(i)); + p->set_score(static_cast(i * 0.1)); + p->set_type(ModelProto_SentencePiece::NORMAL); + } + + auto* norm = model.mutable_normalizer_spec(); + norm->set_name("ts_norm"); + norm->set_add_dummy_prefix(false); + + std::string serialized = model.SerializeAsString(); + + auto const_model = std::make_shared(); + EXPECT_TRUE(const_model->ParseFromString(serialized)); + + // Spawn 20 threads performing high-frequency read access on const wrappers + std::vector threads; + for (int t = 0; t < 20; ++t) { + threads.emplace_back([const_model, t]() { + for (int i = 0; i < 500; ++i) { + // Access global specs + EXPECT_EQ(const_model->pieces_size(), 5000); + EXPECT_EQ(const_model->normalizer_spec().name(), "ts_norm"); + EXPECT_FALSE(const_model->normalizer_spec().add_dummy_prefix()); + + // Read specific index pieces dynamically + int idx = (i * (t + 1)) % 5000; + EXPECT_EQ(const_model->piece_at(idx), "tok_" + std::to_string(idx)); + EXPECT_NEAR(const_model->score_at(idx), static_cast(idx * 0.1), + 1e-5); + EXPECT_EQ(const_model->type_at(idx), ModelProto_SentencePiece::NORMAL); + } + }); + } + + for (auto& th : threads) { + th.join(); + } +} + +// 5. Test Pre-initialization of spec caches and prevent UAF on modifications +TEST(UpbWrapperTest, MutableSpecPointerSyncAndLazyInit) { + ModelProto model; + + // Verify pre-initialization on programmatically prepared models. + // Prior to the fix, normalizer_spec() returned default_instance (add_dummy_prefix=true) + // because the cache remained null when fields were mutated before any serialization. + auto* norm_mutable = model.mutable_normalizer_spec(); + norm_mutable->set_add_dummy_prefix(false); + norm_mutable->set_remove_extra_whitespaces(false); + + EXPECT_FALSE(model.normalizer_spec().add_dummy_prefix()); + EXPECT_FALSE(model.normalizer_spec().remove_extra_whitespaces()); + + // Capture the pointer of normalizer_spec() cache wrapper. + const NormalizerSpec* cached_spec_ptr = &model.normalizer_spec(); + + // Perform modifications that trigger on_change() lambda. + norm_mutable->set_escape_whitespaces(false); + + // Verify the address of normalizer_spec_cache_ remains identical + // (We must not recreate the cached wrapper instance with std::make_unique, + // otherwise existing raw pointers cached in sentencepiece::Normalizer become dangling). + EXPECT_EQ(cached_spec_ptr, &model.normalizer_spec()); + + // Verify values are correctly updated and safe to read via old pointer. + EXPECT_FALSE(cached_spec_ptr->add_dummy_prefix()); + EXPECT_FALSE(cached_spec_ptr->remove_extra_whitespaces()); + EXPECT_FALSE(cached_spec_ptr->escape_whitespaces()); +} + +// 6. Verify that concurrent reads on specs are thread-safe and do not trigger data races. +TEST(UpbWrapperTest, ThreadSafetyLazyInitRace) { + ModelProto model; + + // Manually construct NormalizerSpec. Cache is pre-allocated and won't be null. + auto* norm_mutable = model.mutable_normalizer_spec(); + norm_mutable->set_add_dummy_prefix(false); + + // Spawn threads reading normalizer_spec() concurrently. + // The cache is pre-allocated and getters are pure read-only operations, preventing data races. + std::vector threads; + for (int t = 0; t < 20; ++t) { + threads.emplace_back([&model]() { + for (int i = 0; i < 100; ++i) { + EXPECT_FALSE(model.normalizer_spec().add_dummy_prefix()); + } + }); + } + + for (auto& th : threads) { + th.join(); + } +} + +} // namespace +} // namespace sentencepiece diff --git a/third_party/upb/upb.c b/third_party/upb/upb.c index 2d0421eec..0ccd16b7f 100644 --- a/third_party/upb/upb.c +++ b/third_party/upb/upb.c @@ -6873,9 +6873,13 @@ static size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep, static size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep, upb_MiniTablePlatform platform) { - enum { string_view_align_32 = 4, string_view_align_64 = 8 }; - UPB_STATIC_ASSERT(UPB_ALIGN_OF(upb_StringView) == - UPB_SIZE(string_view_align_32, string_view_align_64), + enum { + string_view_align_32 = UPB_ALIGN_OF(upb_StringView), + string_view_align_64 = 8 + }; + UPB_STATIC_ASSERT(UPB_ALIGN_OF(upb_StringView) == 8 || + UPB_ALIGN_OF(upb_StringView) == 4 || + UPB_ALIGN_OF(upb_StringView) == 2, "StringView size mismatch"); static const uint8_t kRepToAlign32[] = { diff --git a/third_party/upb/upb.h b/third_party/upb/upb.h index 51b41d4a2..fd8afc244 100644 --- a/third_party/upb/upb.h +++ b/third_party/upb/upb.h @@ -378,16 +378,21 @@ Error, UINTPTR_MAX is undefined #define UPB_LONGJMP(buf, val) longjmp(buf, val) #endif -#if ((__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)) || \ +#ifdef __cplusplus +#define UPB_USE_CPP_ATOMICS +#elif ((__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)) || \ UPB_HAS_EXTENSION(c_atomic) || \ defined(__GNUC__) // GCC supported atomics as an extension before it - // supported __has_extension + // supported __has_extension #define UPB_USE_C11_ATOMICS #elif defined(_MSC_VER) #define UPB_USE_MSC_ATOMICS #endif -#if defined(UPB_USE_C11_ATOMICS) +#if defined(UPB_USE_CPP_ATOMICS) +#include +#define UPB_ATOMIC(T) std::atomic +#elif defined(UPB_USE_C11_ATOMICS) #define UPB_ATOMIC(T) _Atomic(T) #elif defined(UPB_USE_MSC_ATOMICS) #define UPB_ATOMIC(T) volatile T @@ -6464,7 +6469,35 @@ int upb_Unicode_ToUTF8(uint32_t cp, char* out); #define UPB_PORT_ATOMIC_H_ -#ifdef UPB_USE_C11_ATOMICS +#ifdef UPB_USE_CPP_ATOMICS + +#include + +#ifdef __cplusplus +using std::memory_order_relaxed; +using std::memory_order_consume; +using std::memory_order_acquire; +using std::memory_order_release; +using std::memory_order_acq_rel; +using std::memory_order_seq_cst; +#endif + +#define upb_Atomic_Init(addr, val) (addr)->store(val, std::memory_order_relaxed) +#define upb_Atomic_Load(addr, order) (addr)->load(order) +#define upb_Atomic_Store(addr, val, order) (addr)->store(val, order) +#define upb_Atomic_Exchange(addr, val, order) (addr)->exchange(val, order) +#define upb_Atomic_CompareExchangeStrong(addr, expected, desired, \ + success_order, failure_order) \ + (addr)->compare_exchange_strong(*(expected), desired, success_order, \ + failure_order) +#define upb_Atomic_CompareExchangeWeak(addr, expected, desired, success_order, \ + failure_order) \ + (addr)->compare_exchange_weak(*(expected), desired, success_order, \ + failure_order) +#define upb_Atomic_Add(addr, val, order) (addr)->fetch_add(val, order) +#define upb_Atomic_Sub(addr, val, order) (addr)->fetch_sub(val, order) + +#elif defined(UPB_USE_C11_ATOMICS) // IWYU pragma: begin_exports #include @@ -6501,7 +6534,8 @@ int upb_Unicode_ToUTF8(uint32_t cp, char* out); static int32_t upb_Atomic_LoadMsc32(int32_t volatile* addr) { // Compare exchange with an unlikely value reduces the risk of a spurious // (but harmless) store - return _InterlockedCompareExchange(addr, 0xDEADC0DE, 0xDEADC0DE); + return _InterlockedCompareExchange((long volatile*)addr, 0xDEADC0DE, + 0xDEADC0DE); } #pragma intrinsic(_InterlockedCompareExchange) @@ -6509,7 +6543,8 @@ static bool upb_Atomic_CompareExchangeMscP32(int32_t volatile* addr, int32_t* expected, int32_t desired) { int32_t expect_val = *expected; - int32_t actual_val = _InterlockedCompareExchange(addr, desired, expect_val); + int32_t actual_val = _InterlockedCompareExchange( + (long volatile*)addr, desired, expect_val); if (expect_val != actual_val) { *expected = actual_val; return false; @@ -6524,7 +6559,8 @@ static bool upb_Atomic_CompareExchangeMscP32(int32_t volatile* addr, static uintptr_t upb_Atomic_LoadMsc64(uint64_t volatile* addr) { // Compare exchange with an unlikely value reduces the risk of a spurious // (but harmless) store - return _InterlockedCompareExchange64(addr, 0xDEADC0DEBAADF00D, + return _InterlockedCompareExchange64((__int64 volatile*)addr, + 0xDEADC0DEBAADF00D, 0xDEADC0DEBAADF00D); } @@ -6533,8 +6569,8 @@ static bool upb_Atomic_CompareExchangeMscP64(uint64_t volatile* addr, uint64_t* expected, uint64_t desired) { uint64_t expect_val = *expected; - uint64_t actual_val = - _InterlockedCompareExchange64(addr, desired, expect_val); + uint64_t actual_val = _InterlockedCompareExchange64( + (__int64 volatile*)addr, desired, expect_val); if (expect_val != actual_val) { *expected = actual_val; return false; @@ -6596,9 +6632,9 @@ static bool upb_Atomic_CompareExchangeMscP64(uint64_t volatile* addr, UPB_INLINE void _upb_Atomic_StoreP(void volatile* addr, uint64_t val, size_t size) { if (size == sizeof(int32_t)) { - (void)_InterlockedExchange((int32_t volatile*)addr, (int32_t)val); + (void)_InterlockedExchange((long volatile*)addr, (int32_t)val); } else { - (void)_InterlockedExchange64((uint64_t volatile*)addr, val); + (void)_InterlockedExchange64((__int64 volatile*)addr, val); } } @@ -6619,9 +6655,9 @@ UPB_INLINE int64_t _upb_Atomic_LoadP(void volatile* addr, size_t size) { UPB_INLINE int64_t _upb_Atomic_ExchangeP(void volatile* addr, uint64_t val, size_t size) { if (size == sizeof(int32_t)) { - return (int64_t)_InterlockedExchange((int32_t volatile*)addr, (int32_t)val); + return (int64_t)_InterlockedExchange((long volatile*)addr, (int32_t)val); } else { - return (int64_t)_InterlockedExchange64((uint64_t volatile*)addr, val); + return (int64_t)_InterlockedExchange64((__int64 volatile*)addr, val); } } @@ -16945,7 +16981,7 @@ UPB_INLINE void* _upb_DefBuilder_AllocCounted(upb_DefBuilder* ctx, size_t size, // adding, so we know which entries to remove if building this file fails. UPB_INLINE void _upb_DefBuilder_Add(upb_DefBuilder* ctx, const char* name, upb_value v) { - upb_StringView sym = {.data = name, .size = strlen(name)}; + upb_StringView sym = {name, strlen(name)}; bool ok = _upb_DefPool_InsertSym(ctx->symtab, sym, v, ctx->status); if (!ok) _upb_DefBuilder_FailJmp(ctx); } diff --git a/third_party/upb/utf8_range_neon.inc b/third_party/upb/utf8_range_neon.inc new file mode 100644 index 000000000..ab73754d8 --- /dev/null +++ b/third_party/upb/utf8_range_neon.inc @@ -0,0 +1,117 @@ +#include + +/* This code is almost the same as SSE implementation, please reference + * utf8-range-sse.inc for detailed explanation. + * The only difference is the range adjustment step. NEON code is more + * straightforward. + */ + +static FORCE_INLINE_ATTR size_t utf8_range_ValidateUTF8Simd( + const char* data_original, const char* data, const char* end, + int return_position) { + const uint8x16_t first_len_tbl = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, + }; + const uint8x16_t first_range_tbl = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, + }; + const uint8x16_t range_min_tbl = { + 0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80, + 0xC2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + }; + const uint8x16_t range_max_tbl = { + 0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F, + 0xF4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + /* Range adjustment in NEON uint8x16x2 table. Note that lanes are interleaved + * in register. The table below is plotted vertically to ease understanding. + * The 1st column is for E0~EF, 2nd column for F0~FF. + */ + // clang-format off + const uint8_t range_adjust_tbl_data[] = { + /* index -> 0~15 16~31 <- index */ + /* E0 -> */ 2, 3, /* <- F0 */ + 0, 0, + 0, 0, + 0, 0, + 0, 4, /* <- F4 */ + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + /* ED -> */ 3, 0, + 0, 0, + 0, 0, + }; + // clang-format on + const uint8x16x2_t range_adjust_tbl = vld2q_u8(range_adjust_tbl_data); + + const uint8x16_t const_1 = vdupq_n_u8(1); + const uint8x16_t const_2 = vdupq_n_u8(2); + const uint8x16_t const_e0 = vdupq_n_u8(0xE0); + + uint8x16_t prev_input = vdupq_n_u8(0); + uint8x16_t prev_first_len = vdupq_n_u8(0); + uint8x16_t error = vdupq_n_u8(0); + + while (end - data >= 16) { + const uint8x16_t input = vld1q_u8((const uint8_t*)data); + + const uint8x16_t high_nibbles = vshrq_n_u8(input, 4); + + const uint8x16_t first_len = vqtbl1q_u8(first_len_tbl, high_nibbles); + + uint8x16_t range = vqtbl1q_u8(first_range_tbl, high_nibbles); + + range = vorrq_u8(range, vextq_u8(prev_first_len, first_len, 15)); + + uint8x16_t shift2 = vextq_u8(prev_first_len, first_len, 14); + shift2 = vqsubq_u8(shift2, const_1); + range = vorrq_u8(range, shift2); + + uint8x16_t shift3 = vextq_u8(prev_first_len, first_len, 13); + shift3 = vqsubq_u8(shift3, const_2); + range = vorrq_u8(range, shift3); + + uint8x16_t shift1 = vextq_u8(prev_input, input, 15); + shift1 = vsubq_u8(shift1, const_e0); + range = vaddq_u8(range, vqtbl2q_u8(range_adjust_tbl, shift1)); + + const uint8x16_t min_range = vqtbl1q_u8(range_min_tbl, range); + const uint8x16_t max_range = vqtbl1q_u8(range_max_tbl, range); + + if (return_position) { + error = vcltq_u8(input, min_range); + error = vorrq_u8(error, vcgtq_u8(input, max_range)); + if (vmaxvq_u32(vreinterpretq_u32_u8(error))) { + break; + } + } else { + error = vorrq_u8(error, vcltq_u8(input, min_range)); + error = vorrq_u8(error, vcgtq_u8(input, max_range)); + } + + prev_input = input; + prev_first_len = first_len; + + data += 16; + } + + if (return_position && data == data_original) { + return utf8_range_ValidateUTF8Naive(data, end, return_position); + } + const int32_t prev = vgetq_lane_s32(vreinterpretq_s32_u8(prev_input), 3); + data -= utf8_range_CodepointSkipBackwards(prev); + if (return_position) { + return (data - data_original) + + utf8_range_ValidateUTF8Naive(data, end, return_position); + } + if (vmaxvq_u32(vreinterpretq_u32_u8(error))) { + return 0; + } + return utf8_range_ValidateUTF8Naive(data, end, return_position); +} diff --git a/third_party/upb/utf8_range_sse.inc b/third_party/upb/utf8_range_sse.inc new file mode 100644 index 000000000..c93fa963c --- /dev/null +++ b/third_party/upb/utf8_range_sse.inc @@ -0,0 +1,272 @@ +#include +#include +#include + +static FORCE_INLINE_ATTR size_t utf8_range_ValidateUTF8Simd( + const char* data_original, const char* data, const char* end, + int return_position) { + /* This code checks that utf-8 ranges are structurally valid 16 bytes at once + * using superscalar instructions. + * The mapping between ranges of codepoint and their corresponding utf-8 + * sequences is below. + */ + + /* + * U+0000...U+007F 00...7F + * U+0080...U+07FF C2...DF 80...BF + * U+0800...U+0FFF E0 A0...BF 80...BF + * U+1000...U+CFFF E1...EC 80...BF 80...BF + * U+D000...U+D7FF ED 80...9F 80...BF + * U+E000...U+FFFF EE...EF 80...BF 80...BF + * U+10000...U+3FFFF F0 90...BF 80...BF 80...BF + * U+40000...U+FFFFF F1...F3 80...BF 80...BF 80...BF + * U+100000...U+10FFFF F4 80...8F 80...BF 80...BF + */ + + /* First we compute the type for each byte, as given by the table below. + * This type will be used as an index later on. + */ + + /* + * Index Min Max Byte Type + * 0 00 7F Single byte sequence + * 1,2,3 80 BF Second, third and fourth byte for many of the sequences. + * 4 A0 BF Second byte after E0 + * 5 80 9F Second byte after ED + * 6 90 BF Second byte after F0 + * 7 80 8F Second byte after F4 + * 8 C2 F4 First non ASCII byte + * 9..15 7F 80 Invalid byte + */ + + /* After the first step we compute the index for all bytes, then we permute + the bytes according to their indices to check the ranges from the range + table. + * The range for a given type can be found in the range_min_table and + range_max_table, the range for type/index X is in range_min_table[X] ... + range_max_table[X]. + */ + + /* Algorithm: + * Put index zero to all bytes. + * Find all non ASCII characters, give them index 8. + * For each tail byte in a codepoint sequence, give it an index corresponding + to the 1 based index from the end. + * If the first byte of the codepoint is in the [C0...DF] range, we write + index 1 in the following byte. + * If the first byte of the codepoint is in the range [E0...EF], we write + indices 2 and 1 in the next two bytes. + * If the first byte of the codepoint is in the range [F0...FF] we write + indices 3,2,1 into the next three bytes. + * For finding the number of bytes we need to look at high nibbles (4 bits) + and do the lookup from the table, it can be done with shift by 4 + shuffle + instructions. We call it `first_len`. + * Then we shift first_len by 8 bits to get the indices of the 2nd bytes. + * Saturating sub 1 and shift by 8 bits to get the indices of the 3rd bytes. + * Again to get the indices of the 4th bytes. + * Take OR of all that 4 values and check within range. + */ + /* For example: + * input C3 80 68 E2 80 20 A6 F0 A0 80 AC 20 F0 93 80 80 + * first_len 1 0 0 2 0 0 0 3 0 0 0 0 3 0 0 0 + * 1st byte 8 0 0 8 0 0 0 8 0 0 0 0 8 0 0 0 + * 2nd byte 0 1 0 0 2 0 0 0 3 0 0 0 0 3 0 0 // Shift + sub + * 3rd byte 0 0 0 0 0 1 0 0 0 2 0 0 0 0 2 0 // Shift + sub + * 4th byte 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 // Shift + sub + * Index 8 1 0 8 2 1 0 8 3 2 1 0 8 3 2 1 // OR of results + */ + + /* Checking for errors: + * Error checking is done by looking up the high nibble (4 bits) of each byte + against an error checking table. + * Because the lookup value for the second byte depends of the value of the + first byte in codepoint, we use saturated operations to adjust the index. + * Specifically we need to add 2 for E0, 3 for ED, 3 for F0 and 4 for F4 to + match the correct index. + * If we subtract from all bytes EF then EO -> 241, ED -> 254, F0 -> 1, + F4 -> 5 + * Do saturating sub 240, then E0 -> 1, ED -> 14 and we can do lookup to + match the adjustment + * Add saturating 112, then F0 -> 113, F4 -> 117, all that were > 16 will + be more 128 and lookup in ef_fe_table will return 0 but for F0 + and F4 it will be 4 and 5 accordingly + */ + /* + * Then just check the appropriate ranges with greater/smaller equal + instructions. Check tail with a naive algorithm. + * To save from previous 16 byte checks we just align previous_first_len to + get correct continuations of the codepoints. + */ + + /* + * Map high nibble of "First Byte" to legal character length minus 1 + * 0x00 ~ 0xBF --> 0 + * 0xC0 ~ 0xDF --> 1 + * 0xE0 ~ 0xEF --> 2 + * 0xF0 ~ 0xFF --> 3 + */ + const __m128i first_len_table = + _mm_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3); + + /* Map "First Byte" to 8-th item of range table (0xC2 ~ 0xF4) */ + const __m128i first_range_table = + _mm_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8); + + /* + * Range table, map range index to min and max values + */ + const __m128i range_min_table = + _mm_setr_epi8(0x00, 0x80, 0x80, 0x80, 0xA0, 0x80, 0x90, 0x80, 0xC2, 0x7F, + 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F); + + const __m128i range_max_table = + _mm_setr_epi8(0x7F, 0xBF, 0xBF, 0xBF, 0xBF, 0x9F, 0xBF, 0x8F, 0xF4, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80); + + /* + * Tables for fast handling of four special First Bytes(E0,ED,F0,F4), after + * which the Second Byte are not 80~BF. It contains "range index adjustment". + * +------------+---------------+------------------+----------------+ + * | First Byte | original range| range adjustment | adjusted range | + * +------------+---------------+------------------+----------------+ + * | E0 | 2 | 2 | 4 | + * +------------+---------------+------------------+----------------+ + * | ED | 2 | 3 | 5 | + * +------------+---------------+------------------+----------------+ + * | F0 | 3 | 3 | 6 | + * +------------+---------------+------------------+----------------+ + * | F4 | 4 | 4 | 8 | + * +------------+---------------+------------------+----------------+ + */ + + /* df_ee_table[1] -> E0, df_ee_table[14] -> ED as ED - E0 = 13 */ + // The values represent the adjustment in the Range Index table for a correct + // index. + const __m128i df_ee_table = + _mm_setr_epi8(0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0); + + /* ef_fe_table[1] -> F0, ef_fe_table[5] -> F4, F4 - F0 = 4 */ + // The values represent the adjustment in the Range Index table for a correct + // index. + const __m128i ef_fe_table = + _mm_setr_epi8(0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + + __m128i prev_input = _mm_set1_epi8(0); + __m128i prev_first_len = _mm_set1_epi8(0); + __m128i error = _mm_set1_epi8(0); + + while (end - data >= 16) { + const __m128i input = _mm_loadu_si128((const __m128i*)(data)); + + /* high_nibbles = input >> 4 */ + const __m128i high_nibbles = + _mm_and_si128(_mm_srli_epi16(input, 4), _mm_set1_epi8(0x0F)); + + /* first_len = legal character length minus 1 */ + /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */ + /* first_len = first_len_table[high_nibbles] */ + __m128i first_len = _mm_shuffle_epi8(first_len_table, high_nibbles); + + /* First Byte: set range index to 8 for bytes within 0xC0 ~ 0xFF */ + /* range = first_range_table[high_nibbles] */ + __m128i range = _mm_shuffle_epi8(first_range_table, high_nibbles); + + /* Second Byte: set range index to first_len */ + /* 0 for 00~7F, 1 for C0~DF, 2 for E0~EF, 3 for F0~FF */ + /* range |= (first_len, prev_first_len) << 1 byte */ + range = _mm_or_si128(range, _mm_alignr_epi8(first_len, prev_first_len, 15)); + + /* Third Byte: set range index to saturate_sub(first_len, 1) */ + /* 0 for 00~7F, 0 for C0~DF, 1 for E0~EF, 2 for F0~FF */ + __m128i tmp1; + __m128i tmp2; + /* tmp1 = saturate_sub(first_len, 1) */ + tmp1 = _mm_subs_epu8(first_len, _mm_set1_epi8(1)); + /* tmp2 = saturate_sub(prev_first_len, 1) */ + tmp2 = _mm_subs_epu8(prev_first_len, _mm_set1_epi8(1)); + /* range |= (tmp1, tmp2) << 2 bytes */ + range = _mm_or_si128(range, _mm_alignr_epi8(tmp1, tmp2, 14)); + + /* Fourth Byte: set range index to saturate_sub(first_len, 2) */ + /* 0 for 00~7F, 0 for C0~DF, 0 for E0~EF, 1 for F0~FF */ + /* tmp1 = saturate_sub(first_len, 2) */ + tmp1 = _mm_subs_epu8(first_len, _mm_set1_epi8(2)); + /* tmp2 = saturate_sub(prev_first_len, 2) */ + tmp2 = _mm_subs_epu8(prev_first_len, _mm_set1_epi8(2)); + /* range |= (tmp1, tmp2) << 3 bytes */ + range = _mm_or_si128(range, _mm_alignr_epi8(tmp1, tmp2, 13)); + + /* + * Now we have below range indices calculated + * Correct cases: + * - 8 for C0~FF + * - 3 for 1st byte after F0~FF + * - 2 for 1st byte after E0~EF or 2nd byte after F0~FF + * - 1 for 1st byte after C0~DF or 2nd byte after E0~EF or + * 3rd byte after F0~FF + * - 0 for others + * Error cases: + * >9 for non ascii First Byte overlapping + * E.g., F1 80 C2 90 --> 8 3 10 2, where 10 indicates error + */ + + /* Adjust Second Byte range for special First Bytes(E0,ED,F0,F4) */ + /* Overlaps lead to index 9~15, which are illegal in range table */ + __m128i shift1; + __m128i pos; + __m128i range2; + /* shift1 = (input, prev_input) << 1 byte */ + shift1 = _mm_alignr_epi8(input, prev_input, 15); + pos = _mm_sub_epi8(shift1, _mm_set1_epi8(0xEF)); + /* + * shift1: | EF F0 ... FE | FF 00 ... ... DE | DF E0 ... EE | + * pos: | 0 1 15 | 16 17 239| 240 241 255| + * pos-240: | 0 0 0 | 0 0 0 | 0 1 15 | + * pos+112: | 112 113 127| >= 128 | >= 128 | + */ + tmp1 = _mm_subs_epu8(pos, _mm_set1_epi8(-16)); + range2 = _mm_shuffle_epi8(df_ee_table, tmp1); + tmp2 = _mm_adds_epu8(pos, _mm_set1_epi8(112)); + range2 = _mm_add_epi8(range2, _mm_shuffle_epi8(ef_fe_table, tmp2)); + + range = _mm_add_epi8(range, range2); + + /* Load min and max values per calculated range index */ + __m128i min_range = _mm_shuffle_epi8(range_min_table, range); + __m128i max_range = _mm_shuffle_epi8(range_max_table, range); + + /* Check value range */ + if (return_position) { + error = _mm_cmplt_epi8(input, min_range); + error = _mm_or_si128(error, _mm_cmpgt_epi8(input, max_range)); + /* 5% performance drop from this conditional branch */ + if (!_mm_testz_si128(error, error)) { + break; + } + } else { + error = _mm_or_si128(error, _mm_cmplt_epi8(input, min_range)); + error = _mm_or_si128(error, _mm_cmpgt_epi8(input, max_range)); + } + + prev_input = input; + prev_first_len = first_len; + + data += 16; + } + /* If we got to the end, we don't need to skip any bytes backwards */ + if (return_position && data == data_original) { + return utf8_range_ValidateUTF8Naive(data, end, return_position); + } + /* Find previous codepoint (not 80~BF) */ + data -= utf8_range_CodepointSkipBackwards(_mm_extract_epi32(prev_input, 3)); + if (return_position) { + return (data - data_original) + + utf8_range_ValidateUTF8Naive(data, end, return_position); + } + /* Test if there was any error */ + if (!_mm_testz_si128(error, error)) { + return 0; + } + /* Check the tail */ + return utf8_range_ValidateUTF8Naive(data, end, return_position); +} From e587429324eee3675d12a5e583ed8eabfa2343f4 Mon Sep 17 00:00:00 2001 From: Taku Kudo Date: Tue, 14 Jul 2026 00:43:10 +0000 Subject: [PATCH 4/5] Fix UPB wrapper safety bugs, refactor for DRY, and migrate to Composition - Fix OOB write & null dereference in ModelProto/spt/nbest wrappers. - Fix heap-buffer-overflow in builder.cc caused by untyped string_view.data() log. - DRY up wrappers using boundary-check macros and template cache initializer. - Extract proto-independent wrapper helpers and DEFINE_UPB_* macros into reusable `upb_message_wrapper.h`. - Migrate wrapper memory & arena lifecycle management from raw pointers to Composition using `UpbMessageHolder`. - Maintain original reference-return spec for API compatibility. - Eliminate boilerplate setter implementations using `UPB_WRAPPER_COPY_AND_SET_SUB_MSG` macro. - DRY up boilerplate assignment operators using `DEFINE_UPB_ASSIGNMENT_OPERATOR` macro. - Refactor accessor macros to delegate actual logic to type-safe C++ helper templates. - Consolidate complex mutable spec getter cache synchronizations into `GetOrCreateMutableSpec` template method in `ModelProtoWrapper`. - DRY up string accessors in `SelfTestData_SampleWrapper` using `DEFINE_UPB_STRING_ACCESSOR` macro. TAG=agy CONV=e46efd90-ae25-4cfc-8385-5381e11ee2ec --- src/CMakeLists.txt | 3 +- src/builder.cc | 2 +- src/builtin_upb/upb_message_wrapper.h | 469 +++++++++++++++ src/builtin_upb/upb_wrapper.h | 795 ++++++++------------------ src/builtin_upb/upb_wrapper_test.cc | 63 ++ 5 files changed, 760 insertions(+), 572 deletions(-) create mode 100644 src/builtin_upb/upb_message_wrapper.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b10ea7f79..8ff3196f6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,6 +42,7 @@ else() set(SPM_PROTO_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/sentencepiece.pb.h ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/upb_wrapper.h + ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/upb_message_wrapper.h ) set(SPM_MODEL_PROTO_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/builtin_upb/sentencepiece_model.pb.h @@ -162,7 +163,7 @@ set(SPM_TEST_SRCS word_model_trainer_test.cc pretokenizer_for_training_test.cc) -if (SPM_USE_UPB) +if (NOT SPM_USE_PROTOC) list(APPEND SPM_TEST_SRCS builtin_upb/upb_wrapper_test.cc) endif() diff --git a/src/builder.cc b/src/builder.cc index e47bd9ff4..fca233f54 100644 --- a/src/builder.cc +++ b/src/builder.cc @@ -674,7 +674,7 @@ absl::Status Builder::BuildNFD_CFMap(CharsMap* chars_map) { // static absl::Status Builder::LoadCharsMap(absl::string_view filename, CharsMap* chars_map) { - LOG(INFO) << "Loading mapping file: " << filename.data(); + LOG(INFO) << "Loading mapping file: " << filename; RET_CHECK(chars_map); auto input = filesystem::NewReadableFile(filename); diff --git a/src/builtin_upb/upb_message_wrapper.h b/src/builtin_upb/upb_message_wrapper.h new file mode 100644 index 000000000..a1ef06424 --- /dev/null +++ b/src/builtin_upb/upb_message_wrapper.h @@ -0,0 +1,469 @@ +// Copyright 2016 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef CORE_UPB_MESSAGE_WRAPPER_H_ +#define CORE_UPB_MESSAGE_WRAPPER_H_ + +#include +#include +#include +#include +#include + +#include "third_party/absl/strings/string_view.h" +#include "upb/upb.h" + +#define UPB_WRAPPER_RET_IF_OOB(index, size) \ + if ((index) < 0 || static_cast(index) >= static_cast(size)) return + +#define UPB_WRAPPER_RET_VAL_IF_OOB(index, size, val) \ + if ((index) < 0 || static_cast(index) >= static_cast(size)) return (val) + +namespace sentencepiece { +namespace upb_internal { + +template +class UpbMessageHolder { + public: + UpbMessageHolder(MsgType* msg, upb_Arena* arena, bool owns) + : msg_(reinterpret_cast(msg)), arena_(arena), owns_msg_(owns) {} + UpbMessageHolder() : msg_(nullptr), arena_(nullptr), owns_msg_(false) {} + + ~UpbMessageHolder() { + Free(); + } + + // Copy is prohibited, Move is allowed to ensure single ownership + UpbMessageHolder(const UpbMessageHolder&) = delete; + UpbMessageHolder& operator=(const UpbMessageHolder&) = delete; + + UpbMessageHolder(UpbMessageHolder&& other) noexcept { + msg_ = other.msg_; + arena_ = other.arena_; + owns_msg_ = other.owns_msg_; + other.msg_ = nullptr; + other.arena_ = nullptr; + other.owns_msg_ = false; + } + + UpbMessageHolder& operator=(UpbMessageHolder&& other) noexcept { + if (this != &other) { + Free(); + msg_ = other.msg_; + arena_ = other.arena_; + owns_msg_ = other.owns_msg_; + other.msg_ = nullptr; + other.arena_ = nullptr; + other.owns_msg_ = false; + } + return *this; + } + + void Reset(MsgType* msg, upb_Arena* arena, bool owns) { + Free(); + msg_ = reinterpret_cast(msg); + arena_ = arena; + owns_msg_ = owns; + } + + MsgType* msg() const { return reinterpret_cast(msg_); } + upb_Arena* arena() const { return arena_; } + bool owns_msg() const { return owns_msg_; } + + private: + void Free() { + if (owns_msg_ && arena_) { + upb_Arena_Free(arena_); + } + msg_ = nullptr; + arena_ = nullptr; + owns_msg_ = false; + } + + upb_Message* msg_ = nullptr; + upb_Arena* arena_ = nullptr; + bool owns_msg_ = false; +}; + +template +inline void LazyInitCache(std::vector>& cache, + ParentType* parent, + size_t expected_size) { + if (cache.size() != expected_size) { + cache.resize(expected_size); + for (size_t i = 0; i < expected_size; ++i) { + if (!cache[i]) { + cache[i] = std::make_unique(parent, i); + } + } + } +} + +} // namespace upb_internal + +class ProtoStr : public absl::string_view { + public: + ProtoStr(absl::string_view sv) : absl::string_view(sv) {} + ProtoStr(const char* s) : absl::string_view(s ? s : "") {} + ProtoStr(upb_StringView sv) : absl::string_view(sv.data, sv.size) {} + + operator std::string() const { return std::string(*this); } + bool operator==(const std::string& other) const { + return absl::string_view(*this) == other; + } + friend bool operator==(const std::string& a, const ProtoStr& b) { + return a == absl::string_view(b); + } +}; + +inline upb_StringView MakeUpbString(absl::string_view val, upb_Arena* arena) { + upb_StringView sv; + if (arena && !val.empty()) { + char* ptr = static_cast(upb_Arena_Malloc(arena, val.size())); + memcpy(ptr, val.data(), val.size()); + sv.data = ptr; + sv.size = val.size(); + } else { + sv.data = nullptr; + sv.size = 0; + } + return sv; +} + +class RepeatedStringWrapper { + public: + RepeatedStringWrapper(const upb_StringView* arr, size_t size) + : arr_(arr), size_(size) {} + + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = ProtoStr; + using difference_type = std::ptrdiff_t; + using pointer = ProtoStr*; + using reference = ProtoStr; + + Iterator(const upb_StringView* arr, size_t index) + : arr_(arr), index_(index) {} + Iterator& operator++() { + ++index_; + return *this; + } + Iterator operator++(int) { + Iterator tmp = *this; + ++index_; + return tmp; + } + bool operator==(const Iterator& other) const { + return index_ == other.index_; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; + } + ProtoStr operator*() const { return ProtoStr(arr_[index_]); } + + private: + const upb_StringView* arr_; + size_t index_; + }; + + Iterator begin() const { return Iterator(arr_, 0); } + Iterator end() const { return Iterator(arr_, size_); } + size_t size() const { return size_; } + bool empty() const { return size_ == 0; } + ProtoStr operator[](size_t index) const { return ProtoStr(arr_[index]); } + + private: + const upb_StringView* arr_; + size_t size_; +}; + +namespace upb_internal { + +// 1. Primitive field helpers +template +inline FieldType GetPrimitiveField(const UpbMessageHolder& holder, + FieldType (*get_fn)(const MsgType*), + FieldType default_val) { + return holder.msg() ? get_fn(holder.msg()) : default_val; +} + +template +inline void SetPrimitiveField(UpbMessageHolder& holder, + void (*set_fn)(MsgType*, FieldType), + FieldType val, + const std::function& on_change) { + if (holder.msg()) { + set_fn(holder.msg(), val); + if (on_change) on_change(holder.msg()); + } +} + +template +inline void ClearField(UpbMessageHolder& holder, + void (*clear_fn)(MsgType*), + const std::function& on_change) { + if (holder.msg()) { + clear_fn(holder.msg()); + if (on_change) on_change(holder.msg()); + } +} + +// 2. String field helpers +template +inline ProtoStr GetStringField(const UpbMessageHolder& holder, + bool (*has_fn)(const MsgType*), + upb_StringView (*get_fn)(const MsgType*), + ProtoStr default_val) { + if (!holder.msg() || !has_fn(holder.msg())) return default_val; + return get_fn(holder.msg()); +} + +template +inline void SetStringField(UpbMessageHolder& holder, + void (*set_fn)(MsgType*, upb_StringView), + absl::string_view val, + const std::function& on_change) { + if (holder.msg() && holder.arena()) { + set_fn(holder.msg(), MakeUpbString(val, holder.arena())); + if (on_change) on_change(holder.msg()); + } +} + +template +inline bool HasField(const UpbMessageHolder& holder, + bool (*has_fn)(const MsgType*)) { + return holder.msg() && has_fn(holder.msg()); +} + +// 3. Enum field helpers +template +inline EnumType GetEnumField(const UpbMessageHolder& holder, + RawType (*get_fn)(const MsgType*), + EnumType default_val) { + return holder.msg() ? static_cast(get_fn(holder.msg())) : default_val; +} + +template +inline void SetEnumField(UpbMessageHolder& holder, + void (*set_fn)(MsgType*, RawType), + EnumType val, + const std::function& on_change) { + if (holder.msg()) { + set_fn(holder.msg(), static_cast(val)); + if (on_change) on_change(holder.msg()); + } +} + +// 4. Repeated string field helpers +template +inline int GetRepeatedStringSize(const UpbMessageHolder& holder, + const upb_StringView* (*get_fn)(const MsgType*, size_t*)) { + if (!holder.msg()) return 0; + size_t size = 0; + get_fn(holder.msg(), &size); + return size; +} + +template +inline ProtoStr GetRepeatedStringElement(const UpbMessageHolder& holder, + const upb_StringView* (*get_fn)(const MsgType*, size_t*), + int index) { + if (!holder.msg()) return ""; + size_t size = 0; + const upb_StringView* arr = get_fn(holder.msg(), &size); + if (index < 0 || static_cast(index) >= size) return ""; + return arr[index]; +} + +template +inline RepeatedStringWrapper GetRepeatedStringWrapper(const UpbMessageHolder& holder, + const upb_StringView* (*get_fn)(const MsgType*, size_t*)) { + if (!holder.msg()) return RepeatedStringWrapper(nullptr, 0); + size_t size = 0; + const upb_StringView* arr = get_fn(holder.msg(), &size); + return RepeatedStringWrapper(arr, size); +} + +template +inline void AddRepeatedString(UpbMessageHolder& holder, + bool (*add_fn)(MsgType*, upb_StringView, upb_Arena*), + const std::string& val, + const std::function& on_change) { + if (holder.msg() && holder.arena()) { + add_fn(holder.msg(), MakeUpbString(val, holder.arena()), holder.arena()); + if (on_change) on_change(holder.msg()); + } +} + +} // namespace upb_internal + +#define DEFINE_UPB_PRIMITIVE_ACCESSOR(FieldName, Type, DefaultVal, UpbPrefix) \ + Type FieldName() const { \ + return sentencepiece::upb_internal::GetPrimitiveField< \ + UpbPrefix, Type>(holder_, UpbPrefix##_##FieldName, DefaultVal); \ + } \ + void set_##FieldName(Type val) { \ + sentencepiece::upb_internal::SetPrimitiveField< \ + UpbPrefix, Type>(holder_, UpbPrefix##_set_##FieldName, val, on_change_); \ + } \ + void clear_##FieldName() { \ + sentencepiece::upb_internal::ClearField< \ + UpbPrefix>(holder_, UpbPrefix##_clear_##FieldName, on_change_); \ + } + +#define DEFINE_UPB_STRING_ACCESSOR(FieldName, DefaultVal, UpbPrefix) \ + ProtoStr FieldName() const { \ + return sentencepiece::upb_internal::GetStringField< \ + UpbPrefix>(holder_, UpbPrefix##_has_##FieldName, \ + UpbPrefix##_##FieldName, DefaultVal); \ + } \ + void set_##FieldName(absl::string_view val) { \ + sentencepiece::upb_internal::SetStringField< \ + UpbPrefix>(holder_, UpbPrefix##_set_##FieldName, val, on_change_); \ + } \ + void set_##FieldName(const char* data, size_t size) { \ + set_##FieldName(absl::string_view(data, size)); \ + } \ + void clear_##FieldName() { \ + sentencepiece::upb_internal::ClearField< \ + UpbPrefix>(holder_, UpbPrefix##_clear_##FieldName, on_change_); \ + } + +#define DEFINE_UPB_HAS_FIELD_ACCESSOR(FieldName, UpbPrefix) \ + bool has_##FieldName() const { \ + return sentencepiece::upb_internal::HasField< \ + UpbPrefix>(holder_, UpbPrefix##_has_##FieldName); \ + } + +#define DEFINE_UPB_ENUM_ACCESSOR(FieldName, EnumType, DefaultVal, UpbPrefix) \ + EnumType FieldName() const { \ + return sentencepiece::upb_internal::GetEnumField< \ + UpbPrefix, EnumType, int32_t>(holder_, UpbPrefix##_##FieldName, DefaultVal); \ + } \ + void set_##FieldName(EnumType type) { \ + sentencepiece::upb_internal::SetEnumField< \ + UpbPrefix, EnumType, int32_t>(holder_, UpbPrefix##_set_##FieldName, type, on_change_); \ + } \ + void clear_##FieldName() { \ + sentencepiece::upb_internal::ClearField< \ + UpbPrefix>(holder_, UpbPrefix##_clear_##FieldName, on_change_); \ + } + +#define DEFINE_UPB_REPEATED_STRING_ACCESSOR(FieldName, UpbPrefix) \ + int FieldName##_size() const { \ + return sentencepiece::upb_internal::GetRepeatedStringSize< \ + UpbPrefix>(holder_, UpbPrefix##_##FieldName); \ + } \ + ProtoStr FieldName(int index) const { \ + return sentencepiece::upb_internal::GetRepeatedStringElement< \ + UpbPrefix>(holder_, UpbPrefix##_##FieldName, index); \ + } \ + RepeatedStringWrapper FieldName() const { \ + return sentencepiece::upb_internal::GetRepeatedStringWrapper< \ + UpbPrefix>(holder_, UpbPrefix##_##FieldName); \ + } \ + void add_##FieldName(const std::string& val) { \ + sentencepiece::upb_internal::AddRepeatedString< \ + UpbPrefix>(holder_, UpbPrefix##_add_##FieldName, val, on_change_); \ + } \ + void clear_##FieldName() { \ + sentencepiece::upb_internal::ClearField< \ + UpbPrefix>(holder_, UpbPrefix##_clear_##FieldName, on_change_); \ + } + +namespace upb { + +#define DEFINE_UPB_SERIALIZATION_METHODS(ClassName, CPrefix) \ + std::string SerializeAsString() const { \ + if (!holder_.msg()) return ""; \ + size_t len; \ + upb_Arena* tmp_arena = upb_Arena_New(); \ + char* ptr = CPrefix##_serialize(holder_.msg(), tmp_arena, &len); \ + std::string ret; \ + if (ptr) { \ + ret.assign(ptr, len); \ + } \ + upb_Arena_Free(tmp_arena); \ + return ret; \ + } \ + bool ParseFromArray(const void* data, int size) { \ + upb_Arena* new_arena = upb_Arena_New(); \ + CPrefix* new_msg = CPrefix##_parse(reinterpret_cast(data), size, new_arena); \ + if (!new_msg) { \ + upb_Arena_Free(new_arena); \ + return false; \ + } \ + holder_.Reset(new_msg, new_arena, true); \ + OnArenaReset(); \ + return true; \ + } \ + bool ParseFromString(const std::string& bytes) { \ + return ParseFromArray(bytes.data(), bytes.size()); \ + } \ + void CopyFrom(const ClassName& other) { \ + std::string bytes = other.SerializeAsString(); \ + ParseFromString(bytes); \ + } \ + void Clear() { \ + upb_Arena* new_arena = upb_Arena_New(); \ + CPrefix* new_msg = CPrefix##_new(new_arena); \ + holder_.Reset(new_msg, new_arena, true); \ + OnArenaReset(); \ + } + +#define UPB_WRAPPER_COPY_AND_SET_SUB_MSG(ParentMsg, SrcWrapper, SetFn, ParseFn, CacheVar, WrapperClass, Arena) \ + do { \ + std::string bytes = (SrcWrapper).SerializeAsString(); \ + if (!bytes.empty()) { \ + auto* sub_msg = ParseFn(bytes.data(), bytes.size(), Arena); \ + if (sub_msg) { \ + SetFn(ParentMsg, sub_msg); \ + (CacheVar) = std::make_unique(sub_msg, Arena); \ + } \ + } \ + } while (0) + +#define DEFINE_UPB_ASSIGNMENT_OPERATOR(ClassName, CPrefix) \ + ClassName& operator=(const ClassName& other) { \ + if (this != &other) { \ + if (holder_.owns_msg()) { \ + Clear(); \ + CopyFrom(other); \ + } else { \ + if (other.holder_.msg()) { \ + size_t size = 0; \ + upb_Arena* tmp_arena = upb_Arena_New(); \ + char* buf = \ + CPrefix##_serialize(other.holder_.msg(), tmp_arena, &size); \ + if (buf) { \ + auto* parsed_msg = \ + CPrefix##_parse(buf, size, holder_.arena()); \ + holder_.Reset(parsed_msg, holder_.arena(), false); \ + if (on_change_) on_change_(holder_.msg()); \ + } \ + upb_Arena_Free(tmp_arena); \ + } else { \ + holder_.Reset(nullptr, holder_.arena(), false); \ + if (on_change_) on_change_(nullptr); \ + } \ + } \ + } \ + return *this; \ + } + +} // namespace upb +} // namespace sentencepiece + +#endif // CORE_UPB_MESSAGE_WRAPPER_H_ diff --git a/src/builtin_upb/upb_wrapper.h b/src/builtin_upb/upb_wrapper.h index d93dbdc41..6ba4aa2a7 100644 --- a/src/builtin_upb/upb_wrapper.h +++ b/src/builtin_upb/upb_wrapper.h @@ -33,36 +33,9 @@ // Include the upb generated headers #include "sentencepiece.upb.h" #include "sentencepiece_model.upb.h" +#include "builtin_upb/upb_message_wrapper.h" namespace sentencepiece { -class ProtoStr : public absl::string_view { - public: - ProtoStr(absl::string_view sv) : absl::string_view(sv) {} - ProtoStr(const char* s) : absl::string_view(s ? s : "") {} - ProtoStr(upb_StringView sv) : absl::string_view(sv.data, sv.size) {} - - operator std::string() const { return std::string(*this); } - bool operator==(const std::string& other) const { - return absl::string_view(*this) == other; - } - friend bool operator==(const std::string& a, const ProtoStr& b) { - return a == absl::string_view(b); - } -}; - -inline upb_StringView MakeUpbString(absl::string_view val, upb_Arena* arena) { - upb_StringView sv; - if (arena && !val.empty()) { - char* ptr = static_cast(upb_Arena_Malloc(arena, val.size())); - memcpy(ptr, val.data(), val.size()); - sv.data = ptr; - sv.size = val.size(); - } else { - sv.data = nullptr; - sv.size = 0; - } - return sv; -} class TrainerSpec; class NormalizerSpec; @@ -108,45 +81,7 @@ class ModelProto_SentencePiece { namespace upb { -#define DEFINE_UPB_SERIALIZATION_METHODS(ClassName, CPrefix) \ - std::string SerializeAsString() const { \ - if (!msg_) return ""; \ - size_t len; \ - upb_Arena* tmp_arena = upb_Arena_New(); \ - char* ptr = CPrefix##_serialize(msg_, tmp_arena, &len); \ - std::string ret; \ - if (ptr) { \ - ret.assign(ptr, len); \ - } \ - upb_Arena_Free(tmp_arena); \ - return ret; \ - } \ - bool ParseFromArray(const void* data, int size) { \ - if (owns_msg_ && arena_) { \ - upb_Arena_Free(arena_); \ - } \ - arena_ = upb_Arena_New(); \ - owns_msg_ = true; \ - msg_ = CPrefix##_parse(reinterpret_cast(data), size, arena_); \ - OnArenaReset(); \ - return msg_ != nullptr; \ - } \ - bool ParseFromString(const std::string& bytes) { \ - return ParseFromArray(bytes.data(), bytes.size()); \ - } \ - void CopyFrom(const ClassName& other) { \ - std::string bytes = other.SerializeAsString(); \ - ParseFromString(bytes); \ - } \ - void Clear() { \ - if (owns_msg_ && arena_) { \ - upb_Arena_Free(arena_); \ - } \ - arena_ = upb_Arena_New(); \ - owns_msg_ = true; \ - msg_ = CPrefix##_new(arena_); \ - OnArenaReset(); \ - } + class SentencePieceTextWrapper; } // namespace upb @@ -197,207 +132,35 @@ class NBestSentencePieceTextWrapper; class SelfTestDataWrapper; class SelfTestData_SampleWrapper; -class RepeatedStringWrapper { - public: - RepeatedStringWrapper(const upb_StringView* arr, size_t size) - : arr_(arr), size_(size) {} - - class Iterator { - public: - using iterator_category = std::forward_iterator_tag; - using value_type = ProtoStr; - using difference_type = std::ptrdiff_t; - using pointer = ProtoStr*; - using reference = ProtoStr; - - Iterator(const upb_StringView* arr, size_t index) - : arr_(arr), index_(index) {} - Iterator& operator++() { - ++index_; - return *this; - } - Iterator operator++(int) { - Iterator tmp = *this; - ++index_; - return tmp; - } - bool operator==(const Iterator& other) const { - return index_ == other.index_; - } - bool operator!=(const Iterator& other) const { - return index_ != other.index_; - } - ProtoStr operator*() const { return ProtoStr(arr_[index_]); } - - private: - const upb_StringView* arr_; - size_t index_; - }; - - Iterator begin() const { return Iterator(arr_, 0); } - Iterator end() const { return Iterator(arr_, size_); } - size_t size() const { return size_; } - bool empty() const { return size_ == 0; } - ProtoStr operator[](size_t index) const { return ProtoStr(arr_[index]); } - - private: - const upb_StringView* arr_; - size_t size_; -}; -#define DEFINE_UPB_PRIMITIVE_ACCESSOR(FieldName, Type, DefaultVal, UpbPrefix) \ - Type FieldName() const { \ - return msg_ ? UpbPrefix##_##FieldName(msg_) : DefaultVal; \ - } \ - void set_##FieldName(Type val) { \ - if (msg_) { \ - UpbPrefix##_set_##FieldName(msg_, val); \ - if (on_change_) on_change_(msg_); \ - } \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - if (on_change_) on_change_(msg_); \ - } \ - } - -#define DEFINE_UPB_STRING_ACCESSOR(FieldName, DefaultVal, UpbPrefix) \ - ProtoStr FieldName() const { \ - if (!msg_ || !UpbPrefix##_has_##FieldName(msg_)) return DefaultVal; \ - return UpbPrefix##_##FieldName(msg_); \ - } \ - void set_##FieldName(absl::string_view val) { \ - if (msg_ && arena_) { \ - UpbPrefix##_set_##FieldName(msg_, MakeUpbString(val, arena_)); \ - if (on_change_) on_change_(msg_); \ - } \ - } \ - void set_##FieldName(const char* data, size_t size) { \ - set_##FieldName(absl::string_view(data, size)); \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - if (on_change_) on_change_(msg_); \ - } \ - } - -#define DEFINE_UPB_HAS_FIELD_ACCESSOR(FieldName, UpbPrefix) \ - bool has_##FieldName() const { \ - return msg_ && UpbPrefix##_has_##FieldName(msg_); \ - } - -#define DEFINE_UPB_ENUM_ACCESSOR(FieldName, EnumType, DefaultVal, UpbPrefix) \ - EnumType FieldName() const { \ - return msg_ ? static_cast(UpbPrefix##_##FieldName(msg_)) \ - : DefaultVal; \ - } \ - void set_##FieldName(EnumType type) { \ - if (msg_) { \ - UpbPrefix##_set_##FieldName(msg_, static_cast(type)); \ - if (on_change_) on_change_(msg_); \ - } \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - if (on_change_) on_change_(msg_); \ - } \ - } - -#define DEFINE_UPB_REPEATED_STRING_ACCESSOR(FieldName, UpbPrefix) \ - int FieldName##_size() const { \ - if (!msg_) return 0; \ - size_t size; \ - UpbPrefix##_##FieldName(msg_, &size); \ - return size; \ - } \ - ProtoStr FieldName(int index) const { \ - if (!msg_) return ""; \ - size_t size; \ - const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ - if (index < 0 || static_cast(index) >= size) return ""; \ - return arr[index]; \ - } \ - RepeatedStringWrapper FieldName() const { \ - if (!msg_) return RepeatedStringWrapper(nullptr, 0); \ - size_t size; \ - const upb_StringView* arr = UpbPrefix##_##FieldName(msg_, &size); \ - return RepeatedStringWrapper(arr, size); \ - } \ - void add_##FieldName(const std::string& val) { \ - if (msg_ && arena_) { \ - UpbPrefix##_add_##FieldName(msg_, MakeUpbString(val, arena_), arena_); \ - if (on_change_) on_change_(msg_); \ - } \ - } \ - void clear_##FieldName() { \ - if (msg_) { \ - UpbPrefix##_clear_##FieldName(msg_); \ - if (on_change_) on_change_(msg_); \ - } \ - } class TrainerSpecWrapper { friend class upb::ModelProtoWrapper; public: - TrainerSpecWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { - msg_ = sentencepiece_TrainerSpec_new(arena_); + TrainerSpecWrapper() { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_TrainerSpec_new(arena), arena, true); } explicit TrainerSpecWrapper(const sentencepiece_TrainerSpec* msg, upb_Arena* arena = nullptr) - : msg_(const_cast(msg)), - arena_(arena), - owns_msg_(false) {} + : holder_(const_cast(msg), arena, false) {} TrainerSpecWrapper( const sentencepiece_TrainerSpec* msg, upb_Arena* arena, std::function on_change) - : msg_(const_cast(msg)), - arena_(arena), - owns_msg_(false), + : holder_(const_cast(msg), arena, false), on_change_(on_change) {} - TrainerSpecWrapper(const TrainerSpecWrapper& other) - : arena_(upb_Arena_New()), owns_msg_(true) { - msg_ = sentencepiece_TrainerSpec_new(arena_); + TrainerSpecWrapper(const TrainerSpecWrapper& other) { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_TrainerSpec_new(arena), arena, true); CopyFrom(other); } - TrainerSpecWrapper& operator=(const TrainerSpecWrapper& other) { - if (this != &other) { - if (owns_msg_) { - if (arena_) upb_Arena_Free(arena_); - arena_ = upb_Arena_New(); - owns_msg_ = true; - CopyFrom(other); - } else { - if (other.msg_) { - size_t size = 0; - upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = - sentencepiece_TrainerSpec_serialize(other.msg_, tmp_arena, &size); - if (buf) { - msg_ = sentencepiece_TrainerSpec_parse(buf, size, arena_); - if (on_change_) on_change_(msg_); - } - upb_Arena_Free(tmp_arena); - } else { - msg_ = nullptr; - if (on_change_) on_change_(msg_); - } - } - } - return *this; - } + DEFINE_UPB_ASSIGNMENT_OPERATOR(TrainerSpecWrapper, sentencepiece_TrainerSpec) - virtual ~TrainerSpecWrapper() { - if (owns_msg_ && arena_) { - upb_Arena_Free(arena_); - } - } + virtual ~TrainerSpecWrapper() = default; DEFINE_UPB_SERIALIZATION_METHODS(TrainerSpecWrapper, sentencepiece_TrainerSpec) @@ -497,9 +260,7 @@ class TrainerSpecWrapper { sentencepiece_TrainerSpec) private: - sentencepiece_TrainerSpec* msg_; - upb_Arena* arena_; - bool owns_msg_ = false; + mutable sentencepiece::upb_internal::UpbMessageHolder holder_; std::function on_change_; }; @@ -507,61 +268,29 @@ class NormalizerSpecWrapper { friend class upb::ModelProtoWrapper; public: - NormalizerSpecWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { - msg_ = sentencepiece_NormalizerSpec_new(arena_); + NormalizerSpecWrapper() { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_NormalizerSpec_new(arena), arena, true); } explicit NormalizerSpecWrapper(const sentencepiece_NormalizerSpec* msg, upb_Arena* arena = nullptr) - : msg_(const_cast(msg)), - arena_(arena), - owns_msg_(false) {} + : holder_(const_cast(msg), arena, false) {} NormalizerSpecWrapper( const sentencepiece_NormalizerSpec* msg, upb_Arena* arena, std::function on_change) - : msg_(const_cast(msg)), - arena_(arena), - owns_msg_(false), + : holder_(const_cast(msg), arena, false), on_change_(on_change) {} - NormalizerSpecWrapper(const NormalizerSpecWrapper& other) - : arena_(upb_Arena_New()), owns_msg_(true) { - msg_ = sentencepiece_NormalizerSpec_new(arena_); + NormalizerSpecWrapper(const NormalizerSpecWrapper& other) { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_NormalizerSpec_new(arena), arena, true); CopyFrom(other); } - NormalizerSpecWrapper& operator=(const NormalizerSpecWrapper& other) { - if (this != &other) { - if (owns_msg_) { - if (arena_) upb_Arena_Free(arena_); - arena_ = upb_Arena_New(); - owns_msg_ = true; - CopyFrom(other); - } else { - if (other.msg_) { - size_t size = 0; - upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = sentencepiece_NormalizerSpec_serialize(other.msg_, - tmp_arena, &size); - if (buf) { - msg_ = sentencepiece_NormalizerSpec_parse(buf, size, arena_); - if (on_change_) on_change_(msg_); - } - upb_Arena_Free(tmp_arena); - } else { - msg_ = nullptr; - if (on_change_) on_change_(msg_); - } - } - } - return *this; - } + DEFINE_UPB_ASSIGNMENT_OPERATOR(NormalizerSpecWrapper, sentencepiece_NormalizerSpec) - virtual ~NormalizerSpecWrapper() { - if (owns_msg_ && arena_) { - upb_Arena_Free(arena_); - } - } + virtual ~NormalizerSpecWrapper() = default; DEFINE_UPB_SERIALIZATION_METHODS(NormalizerSpecWrapper, sentencepiece_NormalizerSpec) @@ -581,9 +310,7 @@ class NormalizerSpecWrapper { sentencepiece_NormalizerSpec) private: - sentencepiece_NormalizerSpec* msg_; - upb_Arena* arena_; - bool owns_msg_ = false; + mutable sentencepiece::upb_internal::UpbMessageHolder holder_; std::function on_change_; }; @@ -591,45 +318,24 @@ class SelfTestData_SampleWrapper { public: explicit SelfTestData_SampleWrapper( const sentencepiece_SelfTestData_Sample* msg) - : msg_(msg), mutable_msg_(nullptr), arena_(nullptr) {} + : holder_(const_cast(msg), nullptr, false) {} SelfTestData_SampleWrapper(sentencepiece_SelfTestData_Sample* msg, upb_Arena* arena) - : msg_(msg), mutable_msg_(msg), arena_(arena) {} - ProtoStr input() const { - if (!msg_ || !sentencepiece_SelfTestData_Sample_has_input(msg_)) return ""; - return sentencepiece_SelfTestData_Sample_input(msg_); - } - ProtoStr expected() const { - if (!msg_ || !sentencepiece_SelfTestData_Sample_has_expected(msg_)) - return ""; - return sentencepiece_SelfTestData_Sample_expected(msg_); - } - void set_input(const std::string& val) { - if (mutable_msg_ && arena_) { - sentencepiece_SelfTestData_Sample_set_input(mutable_msg_, - MakeUpbString(val, arena_)); - } - } - void set_expected(const std::string& val) { - if (mutable_msg_ && arena_) { - sentencepiece_SelfTestData_Sample_set_expected( - mutable_msg_, MakeUpbString(val, arena_)); - } - } + : holder_(msg, arena, false) {} + DEFINE_UPB_STRING_ACCESSOR(input, "", sentencepiece_SelfTestData_Sample) + DEFINE_UPB_STRING_ACCESSOR(expected, "", sentencepiece_SelfTestData_Sample) private: - const sentencepiece_SelfTestData_Sample* msg_; - sentencepiece_SelfTestData_Sample* mutable_msg_; - upb_Arena* arena_; + sentencepiece::upb_internal::UpbMessageHolder holder_; std::function on_change_; }; class SelfTestDataWrapper { public: explicit SelfTestDataWrapper(const sentencepiece_SelfTestData* msg) - : msg_(msg), mutable_msg_(nullptr), arena_(nullptr) {} + : holder_(const_cast(msg), nullptr, false) {} SelfTestDataWrapper(sentencepiece_SelfTestData* msg, upb_Arena* arena) - : msg_(msg), mutable_msg_(msg), arena_(arena) {} + : holder_(msg, arena, false) {} class SamplesRepeatedWrapper { public: @@ -687,57 +393,52 @@ class SelfTestDataWrapper { SamplesRepeatedWrapper samples() const { size_t size = 0; const sentencepiece_SelfTestData_Sample* const* elements = nullptr; - if (msg_) { - elements = sentencepiece_SelfTestData_samples(msg_, &size); + const auto* m = static_cast(holder_.msg()); + if (m) { + elements = sentencepiece_SelfTestData_samples(m, &size); } return SamplesRepeatedWrapper(elements, size); } int samples_size() const { return samples().size(); } SelfTestData_SampleWrapper* add_samples() { - if (mutable_msg_ && arena_) { - sentencepiece_SelfTestData_add_samples(mutable_msg_, arena_); + auto* m = static_cast(holder_.msg()); + if (m && holder_.arena()) { + sentencepiece_SelfTestData_add_samples(m, holder_.arena()); int new_index = samples_size() - 1; sample_wrappers_.resize(samples_size()); size_t size; sentencepiece_SelfTestData_Sample** samples = - sentencepiece_SelfTestData_mutable_samples(mutable_msg_, &size); + sentencepiece_SelfTestData_mutable_samples(m, &size); sample_wrappers_[new_index] = std::make_unique(samples[new_index], - arena_); + holder_.arena()); return sample_wrappers_[new_index].get(); } return nullptr; } private: - const sentencepiece_SelfTestData* msg_; - sentencepiece_SelfTestData* mutable_msg_; - upb_Arena* arena_; + mutable sentencepiece::upb_internal::UpbMessageHolder holder_; mutable std::vector> sample_wrappers_; }; class ModelProtoWrapper { public: - ModelProtoWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { - msg_ = sentencepiece_ModelProto_new(arena_); + ModelProtoWrapper() { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_ModelProto_new(arena), arena, true); normalizer_spec_cache_ = std::make_unique( - sentencepiece_ModelProto_normalizer_spec(msg_), arena_); + sentencepiece_ModelProto_normalizer_spec(static_cast(holder_.msg())), holder_.arena()); denormalizer_spec_cache_ = std::make_unique( - sentencepiece_ModelProto_denormalizer_spec(msg_), arena_); + sentencepiece_ModelProto_denormalizer_spec(static_cast(holder_.msg())), holder_.arena()); } explicit ModelProtoWrapper(const sentencepiece_ModelProto* msg, upb_Arena* arena) - : msg_(const_cast(msg)), - arena_(arena), - owns_msg_(false) {} + : holder_(const_cast(msg), arena, false) {} - virtual ~ModelProtoWrapper() { - if (owns_msg_ && arena_) { - upb_Arena_Free(arena_); - } - } + virtual ~ModelProtoWrapper() = default; ModelProtoWrapper(const ModelProtoWrapper& other) : ModelProtoWrapper() { CopyFrom(other); @@ -761,11 +462,12 @@ class ModelProtoWrapper { denormalizer_spec_cache_.reset(); mutable_denormalizer_spec_.reset(); mutable_self_test_data_.reset(); - if (msg_) { + const auto* m = static_cast(holder_.msg()); + if (m) { normalizer_spec_cache_ = std::make_unique( - sentencepiece_ModelProto_normalizer_spec(msg_), arena_); + sentencepiece_ModelProto_normalizer_spec(m), holder_.arena()); denormalizer_spec_cache_ = std::make_unique( - sentencepiece_ModelProto_denormalizer_spec(msg_), arena_); + sentencepiece_ModelProto_denormalizer_spec(m), holder_.arena()); } } @@ -776,9 +478,9 @@ class ModelProtoWrapper { } int pieces_size() const { - if (!msg_) return 0; + if (!holder_.msg()) return 0; size_t size = 0; - sentencepiece_ModelProto_pieces(msg_, &size); + sentencepiece_ModelProto_pieces(static_cast(holder_.msg()), &size); return size; } @@ -934,77 +636,90 @@ class ModelProtoWrapper { } inline ModelProto_SentencePiece* mutable_pieces(int index) { + UPB_WRAPPER_RET_VAL_IF_OOB(index, pieces_size(), nullptr); LazyInitPieceWrappersCache(); return piece_wrappers_[index].get(); } ProtoStr piece_at(int index) const { size_t size = 0; + const auto* m = static_cast(holder_.msg()); const sentencepiece_ModelProto_SentencePiece* const* pieces = - sentencepiece_ModelProto_pieces(msg_, &size); - if (index < 0 || static_cast(index) >= size) return ""; + sentencepiece_ModelProto_pieces(m, &size); + UPB_WRAPPER_RET_VAL_IF_OOB(index, size, ""); return ProtoStr( sentencepiece_ModelProto_SentencePiece_piece(pieces[index])); } float score_at(int index) const { size_t size = 0; + const auto* m = static_cast(holder_.msg()); const sentencepiece_ModelProto_SentencePiece* const* pieces = - sentencepiece_ModelProto_pieces(msg_, &size); - if (index < 0 || static_cast(index) >= size) return 0.0f; + sentencepiece_ModelProto_pieces(m, &size); + UPB_WRAPPER_RET_VAL_IF_OOB(index, size, 0.0f); return sentencepiece_ModelProto_SentencePiece_score(pieces[index]); } int type_at(int index) const { size_t size = 0; + const auto* m = static_cast(holder_.msg()); const sentencepiece_ModelProto_SentencePiece* const* pieces = - sentencepiece_ModelProto_pieces(msg_, &size); - if (index < 0 || static_cast(index) >= size) return 0; + sentencepiece_ModelProto_pieces(m, &size); + UPB_WRAPPER_RET_VAL_IF_OOB(index, size, 0); return sentencepiece_ModelProto_SentencePiece_type(pieces[index]); } void set_type_at(int index, int type) { - if (msg_) { + auto* m = static_cast(holder_.msg()); + if (m) { size_t size = 0; sentencepiece_ModelProto_SentencePiece** pieces = - sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_mutable_pieces(m, &size); + UPB_WRAPPER_RET_IF_OOB(index, size); sentencepiece_ModelProto_SentencePiece_set_type( pieces[index], static_cast(type)); } } void set_piece_at(int index, absl::string_view piece) { - if (msg_) { + auto* m = static_cast(holder_.msg()); + if (m) { size_t size = 0; sentencepiece_ModelProto_SentencePiece** pieces = - sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_mutable_pieces(m, &size); + UPB_WRAPPER_RET_IF_OOB(index, size); sentencepiece_ModelProto_SentencePiece_set_piece( - pieces[index], MakeUpbString(piece, arena_)); + pieces[index], MakeUpbString(piece, holder_.arena())); } } void set_piece_at(int index, const std::string& piece) { set_piece_at(index, absl::string_view(piece)); } void set_score_at(int index, float score) { - if (msg_) { + auto* m = static_cast(holder_.msg()); + if (m) { size_t size = 0; sentencepiece_ModelProto_SentencePiece** pieces = - sentencepiece_ModelProto_mutable_pieces(msg_, &size); + sentencepiece_ModelProto_mutable_pieces(m, &size); + UPB_WRAPPER_RET_IF_OOB(index, size); sentencepiece_ModelProto_SentencePiece_set_score(pieces[index], score); } } inline bool has_trainer_spec() const { - return msg_ && sentencepiece_ModelProto_has_trainer_spec(msg_); + const auto* m = static_cast(holder_.msg()); + return m && sentencepiece_ModelProto_has_trainer_spec(m); } inline TrainerSpec trainer_spec() const; inline ::sentencepiece::TrainerSpec* mutable_trainer_spec() const; inline bool has_normalizer_spec() const { - return msg_ && sentencepiece_ModelProto_has_normalizer_spec(msg_); + const auto* m = static_cast(holder_.msg()); + return m && sentencepiece_ModelProto_has_normalizer_spec(m); } inline const NormalizerSpec& normalizer_spec() const; inline ::sentencepiece::NormalizerSpec* mutable_normalizer_spec() const; inline bool has_denormalizer_spec() const { - return msg_ && sentencepiece_ModelProto_has_denormalizer_spec(msg_); + const auto* m = static_cast(holder_.msg()); + return m && sentencepiece_ModelProto_has_denormalizer_spec(m); } inline const NormalizerSpec& denormalizer_spec() const; inline ::sentencepiece::NormalizerSpec* mutable_denormalizer_spec() const; @@ -1013,15 +728,14 @@ class ModelProtoWrapper { inline void set_denormalizer_spec(const NormalizerSpec& spec); inline bool has_self_test_data() const { - return msg_ && sentencepiece_ModelProto_has_self_test_data(msg_); + const auto* m = static_cast(holder_.msg()); + return m && sentencepiece_ModelProto_has_self_test_data(m); } inline SelfTestData self_test_data() const; inline ::sentencepiece::SelfTestData* mutable_self_test_data() const; protected: - sentencepiece_ModelProto* msg_; - upb_Arena* arena_; - bool owns_msg_; + mutable sentencepiece::upb_internal::UpbMessageHolder holder_; std::vector> piece_wrappers_; std::unique_ptr mutable_pieces_wrapper_; @@ -1033,32 +747,54 @@ class ModelProtoWrapper { mutable std::unique_ptr mutable_self_test_data_; private: - void LazyInitPieceWrappersCache() { - if (piece_wrappers_.size() != pieces_size()) { - piece_wrappers_.resize(pieces_size()); - for (int i = 0; i < pieces_size(); ++i) { - if (!piece_wrappers_[i]) { - piece_wrappers_[i] = - std::make_unique(this, i); + template + inline SpecWrapperType* GetOrCreateMutableSpec( + std::unique_ptr& mutable_spec_cache, + std::unique_ptr* spec_cache_ptr = nullptr) const { + if (!mutable_spec_cache) { + auto* m = static_cast(holder_.msg()); + if (m) { + CSpecType* sub_msg = MutableFn(m, holder_.arena()); + if (spec_cache_ptr && *spec_cache_ptr) { + (*spec_cache_ptr)->holder_.Reset(sub_msg, holder_.arena(), false); } + auto* non_const_this = const_cast(this); + mutable_spec_cache = std::make_unique( + sub_msg, holder_.arena(), + [non_const_this, spec_cache_ptr](const CSpecType* new_sub_msg) { + auto* parent_m = static_cast(non_const_this->holder_.msg()); + SetFn(parent_m, const_cast(new_sub_msg)); + if (spec_cache_ptr && *spec_cache_ptr) { + auto* m = static_cast(non_const_this->holder_.msg()); + auto* spec = const_cast(GetFn(m)); + (*spec_cache_ptr)->holder_.Reset(spec, non_const_this->holder_.arena(), false); + } + }); + } else { + mutable_spec_cache = std::make_unique(nullptr); } } + return mutable_spec_cache.get(); + } + + void LazyInitPieceWrappersCache() { + upb_internal::LazyInitCache(piece_wrappers_, this, pieces_size()); } }; class SentencePieceTextWrapper { public: - SentencePieceTextWrapper() : owns_msg_(true), arena_(upb_Arena_New()) { - msg_ = sentencepiece_SentencePieceText_new(arena_); + SentencePieceTextWrapper() { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_SentencePieceText_new(arena), arena, true); } explicit SentencePieceTextWrapper(std::nullptr_t) - : msg_(nullptr), owns_msg_(false), arena_(nullptr) {} + : holder_(nullptr, nullptr, false) {} - virtual ~SentencePieceTextWrapper() { - if (owns_msg_ && arena_) { - upb_Arena_Free(arena_); - } - } + virtual ~SentencePieceTextWrapper() = default; SentencePieceTextWrapper(const SentencePieceTextWrapper& other) : SentencePieceTextWrapper() { @@ -1078,15 +814,20 @@ class SentencePieceTextWrapper { void OnArenaReset() { piece_wrappers_.clear(); mutable_pieces_wrapper_.reset(); - if (msg_) { + if (holder_.msg()) { LazyInitPieceWrappersCache(); } } - virtual sentencepiece_SentencePieceText* mutable_msg() { return msg_; } - virtual const sentencepiece_SentencePieceText* msg() const { return msg_; } - virtual upb_Arena* arena() { return arena_; } - virtual const upb_Arena* arena() const { return arena_; } + virtual sentencepiece_SentencePieceText* mutable_msg() { + return static_cast(holder_.msg()); + } + virtual const sentencepiece_SentencePieceText* msg() const { + return static_cast(holder_.msg()); + } + + virtual upb_Arena* arena() { return holder_.arena(); } + virtual const upb_Arena* arena() const { return holder_.arena(); } void clear_pieces() { if (mutable_msg()) { @@ -1126,18 +867,15 @@ class SentencePieceTextWrapper { } const SentencePieceText_SentencePiece& pieces(int index) const { - if (index < 0 || static_cast(index) >= piece_wrappers_.size()) { - return SentencePieceText_SentencePiece::default_instance(); - } + UPB_WRAPPER_RET_VAL_IF_OOB(index, piece_wrappers_.size(), + SentencePieceText_SentencePiece::default_instance()); return *piece_wrappers_[index]; } SentencePieceText_SentencePiece& pieces(int index) { LazyInitPieceWrappersCache(); - if (index < 0 || static_cast(index) >= piece_wrappers_.size()) { - static SentencePieceText_SentencePiece dummy(nullptr, -1); - return dummy; - } + static SentencePieceText_SentencePiece dummy(nullptr, -1); + UPB_WRAPPER_RET_VAL_IF_OOB(index, piece_wrappers_.size(), dummy); return *piece_wrappers_[index]; } @@ -1313,6 +1051,7 @@ class SentencePieceTextWrapper { } SentencePieceText_SentencePiece* mutable_pieces(int index) { + if (index < 0 || index >= pieces_size()) return nullptr; LazyInitPieceWrappersCache(); return piece_wrappers_[index].get(); } @@ -1330,6 +1069,7 @@ class SentencePieceTextWrapper { size_t size = 0; auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + if (index < 0 || static_cast(index) >= size) return; sentencepiece_SentencePieceText_SentencePiece_set_piece( pieces[index], MakeUpbString(piece, arena())); } @@ -1344,6 +1084,7 @@ class SentencePieceTextWrapper { size_t size = 0; auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + if (index < 0 || static_cast(index) >= size) return; sentencepiece_SentencePieceText_SentencePiece_set_id(pieces[index], id); } @@ -1359,6 +1100,7 @@ class SentencePieceTextWrapper { size_t size = 0; auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + if (index < 0 || static_cast(index) >= size) return; sentencepiece_SentencePieceText_SentencePiece_set_surface( pieces[index], MakeUpbString(surface, arena())); } @@ -1373,6 +1115,7 @@ class SentencePieceTextWrapper { size_t size = 0; auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + if (index < 0 || static_cast(index) >= size) return; sentencepiece_SentencePieceText_SentencePiece_set_begin(pieces[index], begin); } @@ -1387,6 +1130,7 @@ class SentencePieceTextWrapper { size_t size = 0; auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + if (index < 0 || static_cast(index) >= size) return; sentencepiece_SentencePieceText_SentencePiece_set_end(pieces[index], end); } @@ -1394,26 +1138,19 @@ class SentencePieceTextWrapper { size_t size = 0; auto** pieces = sentencepiece_SentencePieceText_mutable_pieces(mutable_msg(), &size); + UPB_WRAPPER_RET_IF_OOB(i, size); + UPB_WRAPPER_RET_IF_OOB(j, size); std::swap(pieces[i], pieces[j]); } void LazyInitPieceWrappersCache() const { - if (piece_wrappers_.size() != pieces_size()) { - piece_wrappers_.resize(pieces_size()); - for (int i = 0; i < pieces_size(); ++i) { - if (!piece_wrappers_[i]) { - piece_wrappers_[i] = - std::make_unique( - const_cast(this), i); - } - } - } + upb_internal::LazyInitCache( + piece_wrappers_, const_cast(this), + pieces_size()); } private: - sentencepiece_SentencePieceText* msg_; - bool owns_msg_; - upb_Arena* arena_; + mutable sentencepiece::upb_internal::UpbMessageHolder holder_; mutable std::vector> piece_wrappers_; @@ -1422,14 +1159,11 @@ class SentencePieceTextWrapper { class NBestSentencePieceTextWrapper { public: - NBestSentencePieceTextWrapper() : arena_(upb_Arena_New()), owns_msg_(true) { - msg_ = sentencepiece_NBestSentencePieceText_new(arena_); - } - virtual ~NBestSentencePieceTextWrapper() { - if (owns_msg_ && arena_) { - upb_Arena_Free(arena_); - } + NBestSentencePieceTextWrapper() { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_NBestSentencePieceText_new(arena), arena, true); } + virtual ~NBestSentencePieceTextWrapper() = default; NBestSentencePieceTextWrapper(const NBestSentencePieceTextWrapper& other) : NBestSentencePieceTextWrapper() { @@ -1449,28 +1183,31 @@ class NBestSentencePieceTextWrapper { void OnArenaReset() { nbest_wrappers_.clear(); - if (msg_) { + if (holder_.msg()) { LazyInitNbestWrappersCache(); } } - virtual sentencepiece_NBestSentencePieceText* mutable_msg() { return msg_; } + virtual sentencepiece_NBestSentencePieceText* mutable_msg() { + return static_cast(holder_.msg()); + } virtual const sentencepiece_NBestSentencePieceText* msg() const { - return msg_; + return static_cast(holder_.msg()); } - virtual upb_Arena* arena() { return arena_; } - virtual const upb_Arena* arena() const { return arena_; } + virtual upb_Arena* arena() { return holder_.arena(); } + virtual const upb_Arena* arena() const { return holder_.arena(); } + int nbests_size() const { - if (!msg_) return 0; + if (!holder_.msg()) return 0; size_t size = 0; - sentencepiece_NBestSentencePieceText_nbests(msg_, &size); + sentencepiece_NBestSentencePieceText_nbests(holder_.msg(), &size); return size; } void ReserveNbests(int size) { - if (msg_) { - sentencepiece_NBestSentencePieceText_resize_nbests(msg_, size, arena_); + if (holder_.msg()) { + sentencepiece_NBestSentencePieceText_resize_nbests(holder_.msg(), size, holder_.arena()); } } @@ -1517,21 +1254,13 @@ class NBestSentencePieceTextWrapper { } void LazyInitNbestWrappersCache() const { - if (nbest_wrappers_.size() != nbests_size()) { - nbest_wrappers_.resize(nbests_size()); - } - for (int i = 0; i < nbests_size(); ++i) { - if (!nbest_wrappers_[i]) { - nbest_wrappers_[i] = std::make_unique( - const_cast(this), i); - } - } + upb_internal::LazyInitCache( + nbest_wrappers_, const_cast(this), + nbests_size()); } protected: - sentencepiece_NBestSentencePieceText* msg_; - upb_Arena* arena_; - bool owns_msg_; + mutable sentencepiece::upb_internal::UpbMessageHolder holder_; mutable std::vector> nbest_wrappers_; @@ -1703,8 +1432,9 @@ inline void ModelProto_SentencePiece::set_type(Type type) { } inline TrainerSpec upb::ModelProtoWrapper::trainer_spec() const { + const auto* m = static_cast(holder_.msg()); return TrainerSpec( - msg_ ? sentencepiece_ModelProto_trainer_spec(msg_) : nullptr, arena_); + m ? sentencepiece_ModelProto_trainer_spec(m) : nullptr, holder_.arena()); } inline const NormalizerSpec& upb::ModelProtoWrapper::normalizer_spec() const { static const NormalizerSpec default_instance(nullptr); @@ -1715,7 +1445,8 @@ inline const NormalizerSpec& upb::ModelProtoWrapper::denormalizer_spec() const { return denormalizer_spec_cache_ ? *denormalizer_spec_cache_ : default_instance; } inline SelfTestData upb::ModelProtoWrapper::self_test_data() const { - return SelfTestData(msg_ ? sentencepiece_ModelProto_self_test_data(msg_) + const auto* m = static_cast(holder_.msg()); + return SelfTestData(m ? sentencepiece_ModelProto_self_test_data(m) : nullptr); } @@ -1728,8 +1459,9 @@ inline const upb_Arena* NBestSentencePieceText_Sub::arena() const { inline NBestSentencePieceText_Sub* upb::NBestSentencePieceTextWrapper::add_nbests() { - if (!msg_) return nullptr; - sentencepiece_NBestSentencePieceText_add_nbests(msg_, arena_); + auto* m = static_cast(holder_.msg()); + if (!m) return nullptr; + sentencepiece_NBestSentencePieceText_add_nbests(m, holder_.arena()); int index = nbests_size() - 1; @@ -1741,10 +1473,8 @@ upb::NBestSentencePieceTextWrapper::add_nbests() { inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper::nbests(int index) const { - if (index < 0 || static_cast(index) >= nbest_wrappers_.size()) { - static const NBestSentencePieceText_Sub default_sub(nullptr, -1); - return default_sub; - } + static const NBestSentencePieceText_Sub default_sub(nullptr, -1); + UPB_WRAPPER_RET_VAL_IF_OOB(index, nbest_wrappers_.size(), default_sub); return *nbest_wrappers_[index]; } @@ -1842,82 +1572,33 @@ inline NBestSentencePieceText_Sub* NBestSentencePieceText::add_nbests() { } inline NBestSentencePieceText_Sub* NBestSentencePieceText::mutable_nbests_at( int index) { - if (nbest_wrappers_.size() != nbests_size()) { - nbest_wrappers_.resize(nbests_size()); - for (int i = 0; i < nbests_size(); ++i) { - if (!nbest_wrappers_[i]) { - nbest_wrappers_[i] = - std::make_unique(this, i); - } - } - } + UPB_WRAPPER_RET_VAL_IF_OOB(index, nbests_size(), nullptr); + LazyInitNbestWrappersCache(); return nbest_wrappers_[index].get(); } inline ::sentencepiece::NormalizerSpec* upb::ModelProtoWrapper::mutable_normalizer_spec() const { - if (!mutable_normalizer_spec_) { - if (msg_) { - sentencepiece_NormalizerSpec* sub_msg = - sentencepiece_ModelProto_mutable_normalizer_spec(msg_, arena_); - if (normalizer_spec_cache_) { - normalizer_spec_cache_->msg_ = sub_msg; - } - mutable_normalizer_spec_ = - std::make_unique<::sentencepiece::NormalizerSpec>( - sub_msg, arena_, - [this](const sentencepiece_NormalizerSpec* new_sub_msg) { - sentencepiece_ModelProto_set_normalizer_spec( - this->msg_, - const_cast(new_sub_msg)); - if (this->normalizer_spec_cache_) { - this->normalizer_spec_cache_->msg_ = - const_cast( - sentencepiece_ModelProto_normalizer_spec(this->msg_)); - } - }); - } else { - mutable_normalizer_spec_ = - std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); - } - } - return mutable_normalizer_spec_.get(); + return GetOrCreateMutableSpec<::sentencepiece::NormalizerSpec, sentencepiece_NormalizerSpec, + sentencepiece_ModelProto_mutable_normalizer_spec, + sentencepiece_ModelProto_set_normalizer_spec, + sentencepiece_ModelProto_normalizer_spec>( + mutable_normalizer_spec_, &normalizer_spec_cache_); } inline ::sentencepiece::NormalizerSpec* upb::ModelProtoWrapper::mutable_denormalizer_spec() const { - if (!mutable_denormalizer_spec_) { - if (msg_) { - sentencepiece_NormalizerSpec* sub_msg = - sentencepiece_ModelProto_mutable_denormalizer_spec(msg_, arena_); - if (denormalizer_spec_cache_) { - denormalizer_spec_cache_->msg_ = sub_msg; - } - mutable_denormalizer_spec_ = - std::make_unique<::sentencepiece::NormalizerSpec>( - sub_msg, arena_, - [this](const sentencepiece_NormalizerSpec* new_sub_msg) { - sentencepiece_ModelProto_set_denormalizer_spec( - this->msg_, - const_cast(new_sub_msg)); - if (this->denormalizer_spec_cache_) { - this->denormalizer_spec_cache_->msg_ = - const_cast( - sentencepiece_ModelProto_denormalizer_spec( - this->msg_)); - } - }); - } else { - mutable_denormalizer_spec_ = - std::make_unique<::sentencepiece::NormalizerSpec>(nullptr); - } - } - return mutable_denormalizer_spec_.get(); + return GetOrCreateMutableSpec<::sentencepiece::NormalizerSpec, sentencepiece_NormalizerSpec, + sentencepiece_ModelProto_mutable_denormalizer_spec, + sentencepiece_ModelProto_set_denormalizer_spec, + sentencepiece_ModelProto_denormalizer_spec>( + mutable_denormalizer_spec_, &denormalizer_spec_cache_); } inline ModelProto_SentencePiece* ModelProto::add_pieces() { - if (msg_) { - sentencepiece_ModelProto_add_pieces(msg_, arena_); + auto* m = static_cast(holder_.msg()); + if (m) { + sentencepiece_ModelProto_add_pieces(m, holder_.arena()); int new_index = pieces_size() - 1; piece_wrappers_.resize(pieces_size()); piece_wrappers_[new_index] = @@ -1929,33 +1610,22 @@ inline ModelProto_SentencePiece* ModelProto::add_pieces() { inline ::sentencepiece::TrainerSpec* upb::ModelProtoWrapper::mutable_trainer_spec() const { - if (!mutable_trainer_spec_) { - if (msg_) { - sentencepiece_TrainerSpec* sub_msg = - sentencepiece_ModelProto_mutable_trainer_spec(msg_, arena_); - mutable_trainer_spec_ = std::make_unique<::sentencepiece::TrainerSpec>( - sub_msg, arena_, - [this](const sentencepiece_TrainerSpec* new_sub_msg) { - sentencepiece_ModelProto_set_trainer_spec( - this->msg_, - const_cast(new_sub_msg)); - }); - } else { - mutable_trainer_spec_ = - std::make_unique<::sentencepiece::TrainerSpec>(nullptr); - } - } - return mutable_trainer_spec_.get(); + return GetOrCreateMutableSpec<::sentencepiece::TrainerSpec, sentencepiece_TrainerSpec, + sentencepiece_ModelProto_mutable_trainer_spec, + sentencepiece_ModelProto_set_trainer_spec, + sentencepiece_ModelProto_trainer_spec>( + mutable_trainer_spec_); } inline ::sentencepiece::SelfTestData* upb::ModelProtoWrapper::mutable_self_test_data() const { if (!mutable_self_test_data_) { - if (msg_) { + auto* m = static_cast(holder_.msg()); + if (m) { sentencepiece_SelfTestData* sub_msg = - sentencepiece_ModelProto_mutable_self_test_data(msg_, arena_); + sentencepiece_ModelProto_mutable_self_test_data(m, holder_.arena()); mutable_self_test_data_ = - std::make_unique<::sentencepiece::SelfTestData>(sub_msg, arena_); + std::make_unique<::sentencepiece::SelfTestData>(sub_msg, holder_.arena()); } else { mutable_self_test_data_ = std::make_unique<::sentencepiece::SelfTestData>(nullptr, nullptr); @@ -1965,55 +1635,40 @@ upb::ModelProtoWrapper::mutable_self_test_data() const { } inline void upb::ModelProtoWrapper::set_trainer_spec(const TrainerSpec& spec) { - if (msg_ && spec.msg_) { - size_t size = 0; - upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = - sentencepiece_TrainerSpec_serialize(spec.msg_, tmp_arena, &size); - if (buf) { - sentencepiece_TrainerSpec* sub_msg = - sentencepiece_TrainerSpec_parse(buf, size, arena_); - sentencepiece_ModelProto_set_trainer_spec(msg_, sub_msg); - mutable_trainer_spec_ = - std::make_unique<::sentencepiece::TrainerSpec>(sub_msg, arena_); - } - upb_Arena_Free(tmp_arena); + auto* m = static_cast(holder_.msg()); + if (m) { + UPB_WRAPPER_COPY_AND_SET_SUB_MSG(m, spec, + sentencepiece_ModelProto_set_trainer_spec, + sentencepiece_TrainerSpec_parse, + mutable_trainer_spec_, + ::sentencepiece::TrainerSpec, + holder_.arena()); } } inline void upb::ModelProtoWrapper::set_normalizer_spec( const NormalizerSpec& spec) { - if (msg_ && spec.msg_) { - size_t size = 0; - upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = - sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); - if (buf) { - sentencepiece_NormalizerSpec* sub_msg = - sentencepiece_NormalizerSpec_parse(buf, size, arena_); - sentencepiece_ModelProto_set_normalizer_spec(msg_, sub_msg); - normalizer_spec_cache_ = - std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); - } - upb_Arena_Free(tmp_arena); + auto* m = static_cast(holder_.msg()); + if (m) { + UPB_WRAPPER_COPY_AND_SET_SUB_MSG(m, spec, + sentencepiece_ModelProto_set_normalizer_spec, + sentencepiece_NormalizerSpec_parse, + normalizer_spec_cache_, + ::sentencepiece::NormalizerSpec, + holder_.arena()); } } inline void upb::ModelProtoWrapper::set_denormalizer_spec( const NormalizerSpec& spec) { - if (msg_ && spec.msg_) { - size_t size = 0; - upb_Arena* tmp_arena = upb_Arena_New(); - char* buf = - sentencepiece_NormalizerSpec_serialize(spec.msg_, tmp_arena, &size); - if (buf) { - sentencepiece_NormalizerSpec* sub_msg = - sentencepiece_NormalizerSpec_parse(buf, size, arena_); - sentencepiece_ModelProto_set_denormalizer_spec(msg_, sub_msg); - mutable_denormalizer_spec_ = - std::make_unique<::sentencepiece::NormalizerSpec>(sub_msg, arena_); - } - upb_Arena_Free(tmp_arena); + auto* m = static_cast(holder_.msg()); + if (m) { + UPB_WRAPPER_COPY_AND_SET_SUB_MSG(m, spec, + sentencepiece_ModelProto_set_denormalizer_spec, + sentencepiece_NormalizerSpec_parse, + mutable_denormalizer_spec_, + ::sentencepiece::NormalizerSpec, + holder_.arena()); } } diff --git a/src/builtin_upb/upb_wrapper_test.cc b/src/builtin_upb/upb_wrapper_test.cc index d5d9344c4..20e10d3e7 100644 --- a/src/builtin_upb/upb_wrapper_test.cc +++ b/src/builtin_upb/upb_wrapper_test.cc @@ -292,5 +292,68 @@ TEST(UpbWrapperTest, ThreadSafetyLazyInitRace) { } } +// 7. Verify that OOB write on ModelProto pieces is safely ignored and doesn't crash. +TEST(UpbWrapperTest, OOBWritePoC) { + ModelProto model; + // Initialize with 0 pieces. + ASSERT_EQ(model.pieces_size(), 0); + + // Get a wrapper pointing to index 99999 (which is OOB) + auto piece = model.pieces(99999); + + std::cout << "DEBUG: Executing OOB write. Expecting NO crash (safe ignore)..." << std::endl; + // With boundary checks, these should be safe no-ops and not crash. + piece.set_score(1.0f); + piece.set_type(ModelProto_SentencePiece::NORMAL); + piece.set_piece("test"); + + // We should reach here successfully. + ASSERT_TRUE(true); +} + +// 8. Verify that OOB write on SentencePieceText is safely ignored and doesn't crash. +TEST(UpbWrapperTest, SentencePieceTextOOBWritePoC) { + SentencePieceText spt; + // Initialize with 0 pieces. + ASSERT_EQ(spt.pieces_size(), 0); + + std::cout << "DEBUG: Executing SentencePieceText OOB write. Expecting NO crash..." << std::endl; + // With boundary checks, these should be safe no-ops or return nullptr. + spt.set_id_at(99999, 123); + spt.set_piece_at(99999, "test"); + spt.set_surface_at(99999, "test"); + spt.set_begin_at(99999, 0); + spt.set_end_at(99999, 1); + spt.SwapElementsData(99999, 0); + + auto* piece = spt.mutable_pieces(99999); + ASSERT_EQ(piece, nullptr); + + // We should reach here successfully. + ASSERT_TRUE(true); +} + +// 9. Verify that OOB access on NBestSentencePieceText is safely ignored and doesn't crash. +TEST(UpbWrapperTest, NBestSentencePieceTextOOBWritePoC) { + NBestSentencePieceText nbest; + // Initialize with 0 nbests. + ASSERT_EQ(nbest.nbests_size(), 0); + + std::cout << "DEBUG: Executing NBestSentencePieceText OOB write. Expecting NO crash..." << std::endl; + // With boundary checks, this should return nullptr instead of crashing. + auto* sub = nbest.mutable_nbests_at(99999); + ASSERT_EQ(sub, nullptr); +} + +// 10. Verify that OOB access on ModelProto mutable_pieces is safely ignored. +TEST(UpbWrapperTest, ModelProtoMutablePiecesOOBPoC) { + ModelProto model; + ASSERT_EQ(model.pieces_size(), 0); + + std::cout << "DEBUG: Executing ModelProto mutable_pieces OOB. Expecting nullptr..." << std::endl; + // With boundary checks, this should return nullptr instead of crashing. + auto* p = model.mutable_pieces(99999); + ASSERT_EQ(p, nullptr); +} } // namespace } // namespace sentencepiece From dff3247776741eefd81cf53a8caa453bb864d824 Mon Sep 17 00:00:00 2001 From: Taku Kudo Date: Wed, 15 Jul 2026 13:54:28 +0000 Subject: [PATCH 5/5] Refactor UPB wrappers to use template helper classes and const repeated wrappers --- src/builtin_upb/upb_message_wrapper.h | 154 ++++++ src/builtin_upb/upb_wrapper.h | 680 ++++++++------------------ src/sentencepiece_processor_test.cc | 4 +- 3 files changed, 363 insertions(+), 475 deletions(-) diff --git a/src/builtin_upb/upb_message_wrapper.h b/src/builtin_upb/upb_message_wrapper.h index a1ef06424..e0b1a226a 100644 --- a/src/builtin_upb/upb_message_wrapper.h +++ b/src/builtin_upb/upb_message_wrapper.h @@ -464,6 +464,160 @@ namespace upb { } } // namespace upb +namespace upb_internal { + + +// Generic const repeated field wrapper (ForwardIterator) +// Generic const repeated field wrapper (ForwardIterator) +template +class ConstRepeatedWrapperBase { + public: + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = ElementWrapperType; + using difference_type = std::ptrdiff_t; + using pointer = ElementWrapperType*; + using reference = ElementWrapperType; + + Iterator(const ConstRepeatedWrapperBase* container, int index) + : container_(container), index_(index) {} + + Iterator& operator++() { + ++index_; + return *this; + } + Iterator operator++(int) { + Iterator tmp = *this; + ++index_; + return tmp; + } + bool operator==(const Iterator& other) const { + return index_ == other.index_; + } + bool operator!=(const Iterator& other) const { + return index_ != other.index_; + } + + ElementWrapperType operator*() const { + return static_cast(container_)->Get(index_); + } + + private: + const ConstRepeatedWrapperBase* container_; + int index_; + }; + + Iterator begin() const { return Iterator(this, 0); } + Iterator end() const { return Iterator(this, Size()); } + int size() const { return Size(); } + bool empty() const { return size() == 0; } + ElementWrapperType operator[](int index) const { + return static_cast(this)->Get(index); + } + + private: + int Size() const { + return static_cast(this)->Size(); + } +}; + +// Generic mutable repeated field wrapper (RandomAccessIterator) +template +class MutableRepeatedWrapperBase { + public: + class Iterator { + public: + using iterator_category = std::random_access_iterator_tag; + using value_type = ElementWrapperType; + using difference_type = std::ptrdiff_t; + using pointer = ElementWrapperType*; + using reference = ElementWrapperType&; + + Iterator(MutableRepeatedWrapperBase* container, int index) + : container_(container), index_(index) {} + + ElementWrapperType& operator*() const { + return *static_cast(container_)->GetMutable(index_); + } + + Iterator& operator++() { + ++index_; + return *this; + } + Iterator operator++(int) { + Iterator tmp = *this; + ++index_; + return tmp; + } + Iterator& operator--() { + --index_; + return *this; + } + Iterator operator--(int) { + Iterator tmp = *this; + --index_; + return tmp; + } + + Iterator& operator+=(difference_type n) { + index_ += n; + return *this; + } + Iterator& operator-=(difference_type n) { + index_ -= n; + return *this; + } + + friend Iterator operator+(Iterator it, difference_type n) { + it += n; + return it; + } + friend Iterator operator+(difference_type n, Iterator it) { + it += n; + return it; + } + friend Iterator operator-(Iterator it, difference_type n) { + it -= n; + return it; + } + friend difference_type operator-(const Iterator& a, const Iterator& b) { + return a.index_ - b.index_; + } + + ElementWrapperType& operator[](difference_type n) const { + return *static_cast(container_)->GetMutable(index_ + n); + } + + bool operator==(const Iterator& other) const { + return index_ == other.index_ && container_ == other.container_; + } + bool operator!=(const Iterator& other) const { return !(*this == other); } + bool operator<(const Iterator& other) const { return index_ < other.index_; } + bool operator>(const Iterator& other) const { return index_ > other.index_; } + bool operator<=(const Iterator& other) const { return index_ <= other.index_; } + bool operator>=(const Iterator& other) const { return index_ >= other.index_; } + + private: + MutableRepeatedWrapperBase* container_; + int index_; + }; + + Iterator begin() { return Iterator(this, 0); } + Iterator end() { return Iterator(this, Size()); } + int size() const { return Size(); } + bool empty() const { return size() == 0; } + ElementWrapperType* Mutable(int index) { + return static_cast(this)->GetMutable(index); + } + + private: + int Size() const { + return static_cast(this)->Size(); + } +}; + +} // namespace upb_internal } // namespace sentencepiece #endif // CORE_UPB_MESSAGE_WRAPPER_H_ diff --git a/src/builtin_upb/upb_wrapper.h b/src/builtin_upb/upb_wrapper.h index 6ba4aa2a7..ecbbe88e7 100644 --- a/src/builtin_upb/upb_wrapper.h +++ b/src/builtin_upb/upb_wrapper.h @@ -33,7 +33,7 @@ // Include the upb generated headers #include "sentencepiece.upb.h" #include "sentencepiece_model.upb.h" -#include "builtin_upb/upb_message_wrapper.h" +#include "upb_message_wrapper.h" namespace sentencepiece { @@ -330,6 +330,23 @@ class SelfTestData_SampleWrapper { std::function on_change_; }; +class SelfTestDataSamplesRepeatedWrapper : public ::sentencepiece::upb_internal::ConstRepeatedWrapperBase< + SelfTestDataSamplesRepeatedWrapper, SelfTestData_SampleWrapper> { + public: + SelfTestDataSamplesRepeatedWrapper( + const sentencepiece_SelfTestData_Sample* const* elements, size_t count) + : elements_(elements), count_(count) {} + + SelfTestData_SampleWrapper Get(int index) const { + return SelfTestData_SampleWrapper(elements_[index]); + } + int Size() const { return count_; } + + private: + const sentencepiece_SelfTestData_Sample* const* elements_; + size_t count_; +}; + class SelfTestDataWrapper { public: explicit SelfTestDataWrapper(const sentencepiece_SelfTestData* msg) @@ -337,67 +354,16 @@ class SelfTestDataWrapper { SelfTestDataWrapper(sentencepiece_SelfTestData* msg, upb_Arena* arena) : holder_(msg, arena, false) {} - class SamplesRepeatedWrapper { - public: - SamplesRepeatedWrapper( - const sentencepiece_SelfTestData_Sample* const* elements, size_t count) - : elements_(elements), count_(count) {} - class Iterator { - public: - using iterator_category = std::forward_iterator_tag; - using value_type = SelfTestData_SampleWrapper; - using difference_type = std::ptrdiff_t; - using pointer = SelfTestData_SampleWrapper*; - using reference = SelfTestData_SampleWrapper; - - Iterator(const sentencepiece_SelfTestData_Sample* const* elements, - size_t index) - : elements_(elements), index_(index) {} - Iterator& operator++() { - ++index_; - return *this; - } - Iterator operator++(int) { - Iterator tmp = *this; - ++index_; - return tmp; - } - bool operator==(const Iterator& other) const { - return index_ == other.index_; - } - bool operator!=(const Iterator& other) const { - return index_ != other.index_; - } - SelfTestData_SampleWrapper operator*() const { - return SelfTestData_SampleWrapper(elements_[index_]); - } - - private: - const sentencepiece_SelfTestData_Sample* const* elements_; - size_t index_; - }; - - Iterator begin() const { return Iterator(elements_, 0); } - Iterator end() const { return Iterator(elements_, count_); } - size_t size() const { return count_; } - bool empty() const { return count_ == 0; } - SelfTestData_SampleWrapper operator[](size_t index) const { - return SelfTestData_SampleWrapper(elements_[index]); - } - - private: - const sentencepiece_SelfTestData_Sample* const* elements_; - size_t count_; - }; + using SamplesRepeatedWrapper = SelfTestDataSamplesRepeatedWrapper; - SamplesRepeatedWrapper samples() const { + SelfTestDataSamplesRepeatedWrapper samples() const { size_t size = 0; const sentencepiece_SelfTestData_Sample* const* elements = nullptr; const auto* m = static_cast(holder_.msg()); if (m) { elements = sentencepiece_SelfTestData_samples(m, &size); } - return SamplesRepeatedWrapper(elements, size); + return SelfTestDataSamplesRepeatedWrapper(elements, size); } int samples_size() const { return samples().size(); } SelfTestData_SampleWrapper* add_samples() { @@ -423,22 +389,17 @@ class SelfTestDataWrapper { sample_wrappers_; }; +class ModelProtoPiecesRepeatedWrapper; +class ModelProtoMutablePiecesRepeatedWrapper; + class ModelProtoWrapper { public: - ModelProtoWrapper() { - upb_Arena* arena = upb_Arena_New(); - holder_.Reset(sentencepiece_ModelProto_new(arena), arena, true); - normalizer_spec_cache_ = std::make_unique( - sentencepiece_ModelProto_normalizer_spec(static_cast(holder_.msg())), holder_.arena()); - denormalizer_spec_cache_ = std::make_unique( - sentencepiece_ModelProto_denormalizer_spec(static_cast(holder_.msg())), holder_.arena()); - } + ModelProtoWrapper(); explicit ModelProtoWrapper(const sentencepiece_ModelProto* msg, - upb_Arena* arena) - : holder_(const_cast(msg), arena, false) {} + upb_Arena* arena); - virtual ~ModelProtoWrapper() = default; + virtual ~ModelProtoWrapper(); ModelProtoWrapper(const ModelProtoWrapper& other) : ModelProtoWrapper() { CopyFrom(other); @@ -492,148 +453,8 @@ class ModelProtoWrapper { return ::sentencepiece::ModelProto_SentencePiece(this, index); } - class PiecesRepeatedWrapper { - public: - PiecesRepeatedWrapper(const ModelProtoWrapper* parent) : parent_(parent) {} - class Iterator { - public: - using iterator_category = std::forward_iterator_tag; - using value_type = ModelProto_SentencePiece; - using difference_type = std::ptrdiff_t; - using pointer = ModelProto_SentencePiece*; - using reference = ModelProto_SentencePiece; - - Iterator(const ModelProtoWrapper* parent, int index) - : parent_(parent), index_(index) {} - ModelProto_SentencePiece operator*() const { - return ModelProto_SentencePiece(const_cast(parent_), - index_); - } - Iterator& operator++() { - ++index_; - return *this; - } - bool operator!=(const Iterator& other) const { - return index_ != other.index_; - } - - private: - const ModelProtoWrapper* parent_; - int index_; - }; - Iterator begin() const { return Iterator(parent_, 0); } - Iterator end() const { return Iterator(parent_, parent_->pieces_size()); } - int size() const { return parent_->pieces_size(); } - - private: - const ModelProtoWrapper* parent_; - }; - - PiecesRepeatedWrapper pieces() const { return PiecesRepeatedWrapper(this); } - - class MutablePiecesRepeatedWrapper { - public: - MutablePiecesRepeatedWrapper(ModelProtoWrapper* parent) : parent_(parent) {} - class Iterator { - public: - using iterator_category = std::random_access_iterator_tag; - using value_type = ModelProto_SentencePiece; - using difference_type = std::ptrdiff_t; - using pointer = ModelProto_SentencePiece*; - using reference = ModelProto_SentencePiece&; - - Iterator(ModelProtoWrapper* parent, int index) - : parent_(parent), index_(index) {} - ModelProto_SentencePiece& operator*() const { - return *(parent_->mutable_pieces(index_)); - } - Iterator& operator++() { - ++index_; - return *this; - } - Iterator operator++(int) { - Iterator tmp = *this; - ++index_; - return tmp; - } - Iterator& operator--() { - --index_; - return *this; - } - Iterator operator--(int) { - Iterator tmp = *this; - --index_; - return tmp; - } - - Iterator& operator+=(difference_type n) { - index_ += n; - return *this; - } - Iterator& operator-=(difference_type n) { - index_ -= n; - return *this; - } - - friend Iterator operator+(Iterator it, difference_type n) { - it += n; - return it; - } - friend Iterator operator+(difference_type n, Iterator it) { - it += n; - return it; - } - friend Iterator operator-(Iterator it, difference_type n) { - it -= n; - return it; - } - friend difference_type operator-(const Iterator& a, const Iterator& b) { - return a.index_ - b.index_; - } - - ModelProto_SentencePiece& operator[](difference_type n) const { - return *(parent_->mutable_pieces(index_ + n)); - } - - bool operator==(const Iterator& other) const { - return index_ == other.index_ && parent_ == other.parent_; - } - bool operator!=(const Iterator& other) const { return !(*this == other); } - bool operator<(const Iterator& other) const { - return index_ < other.index_; - } - bool operator>(const Iterator& other) const { - return index_ > other.index_; - } - bool operator<=(const Iterator& other) const { - return index_ <= other.index_; - } - bool operator>=(const Iterator& other) const { - return index_ >= other.index_; - } - - private: - ModelProtoWrapper* parent_; - int index_; - }; - Iterator begin() { return Iterator(parent_, 0); } - Iterator end() { return Iterator(parent_, parent_->pieces_size()); } - int size() const { return parent_->pieces_size(); } - ModelProto_SentencePiece* Mutable(int index) { - return parent_->mutable_pieces(index); - } - - private: - ModelProtoWrapper* parent_; - }; - - MutablePiecesRepeatedWrapper* mutable_pieces() { - if (!mutable_pieces_wrapper_) { - mutable_pieces_wrapper_ = - std::make_unique(this); - } - return mutable_pieces_wrapper_.get(); - } + ModelProtoPiecesRepeatedWrapper pieces() const; + ModelProtoMutablePiecesRepeatedWrapper* mutable_pieces(); inline ModelProto_SentencePiece* mutable_pieces(int index) { UPB_WRAPPER_RET_VAL_IF_OOB(index, pieces_size(), nullptr); @@ -738,7 +559,7 @@ class ModelProtoWrapper { mutable sentencepiece::upb_internal::UpbMessageHolder holder_; std::vector> piece_wrappers_; - std::unique_ptr mutable_pieces_wrapper_; + std::unique_ptr mutable_pieces_wrapper_; mutable std::unique_ptr mutable_normalizer_spec_; mutable std::unique_ptr mutable_trainer_spec_; mutable std::unique_ptr normalizer_spec_cache_; @@ -781,20 +602,53 @@ class ModelProtoWrapper { } void LazyInitPieceWrappersCache() { - upb_internal::LazyInitCache(piece_wrappers_, this, pieces_size()); + ::sentencepiece::upb_internal::LazyInitCache(piece_wrappers_, this, pieces_size()); } + }; -class SentencePieceTextWrapper { +class ModelProtoPiecesRepeatedWrapper : public ::sentencepiece::upb_internal::ConstRepeatedWrapperBase< + ModelProtoPiecesRepeatedWrapper, ModelProto_SentencePiece> { public: - SentencePieceTextWrapper() { - upb_Arena* arena = upb_Arena_New(); - holder_.Reset(sentencepiece_SentencePieceText_new(arena), arena, true); + ModelProtoPiecesRepeatedWrapper(const ModelProtoWrapper* parent) : parent_(parent) {} + ModelProto_SentencePiece Get(int index) const { return parent_->pieces(index); } + int Size() const { return parent_->pieces_size(); } + private: + const ModelProtoWrapper* parent_; +}; + +class ModelProtoMutablePiecesRepeatedWrapper : public ::sentencepiece::upb_internal::MutableRepeatedWrapperBase< + ModelProtoMutablePiecesRepeatedWrapper, ModelProto_SentencePiece> { + public: + ModelProtoMutablePiecesRepeatedWrapper(ModelProtoWrapper* parent) : parent_(parent) {} + ModelProto_SentencePiece* GetMutable(int index) { return parent_->mutable_pieces(index); } + int Size() const { return parent_->pieces_size(); } + private: + ModelProtoWrapper* parent_; +}; + +inline ModelProtoPiecesRepeatedWrapper ModelProtoWrapper::pieces() const { + return ModelProtoPiecesRepeatedWrapper(this); +} + +inline ModelProtoMutablePiecesRepeatedWrapper* +ModelProtoWrapper::mutable_pieces() { + if (!mutable_pieces_wrapper_) { + mutable_pieces_wrapper_ = + std::make_unique(this); } - explicit SentencePieceTextWrapper(std::nullptr_t) - : holder_(nullptr, nullptr, false) {} + return mutable_pieces_wrapper_.get(); +} + +class SentencePieceTextPiecesRepeatedWrapper; +class SentencePieceTextMutablePiecesRepeatedWrapper; + +class SentencePieceTextWrapper { + public: + SentencePieceTextWrapper(); + explicit SentencePieceTextWrapper(std::nullptr_t); - virtual ~SentencePieceTextWrapper() = default; + virtual ~SentencePieceTextWrapper(); SentencePieceTextWrapper(const SentencePieceTextWrapper& other) : SentencePieceTextWrapper() { @@ -867,6 +721,7 @@ class SentencePieceTextWrapper { } const SentencePieceText_SentencePiece& pieces(int index) const { + LazyInitPieceWrappersCache(); UPB_WRAPPER_RET_VAL_IF_OOB(index, piece_wrappers_.size(), SentencePieceText_SentencePiece::default_instance()); return *piece_wrappers_[index]; @@ -897,158 +752,10 @@ class SentencePieceTextWrapper { return ptr; } - class ConstPiecesRepeatedWrapper { - public: - ConstPiecesRepeatedWrapper(const SentencePieceTextWrapper* parent) - : parent_(parent) {} - class Iterator { - public: - using iterator_category = std::forward_iterator_tag; - using value_type = SentencePieceText_SentencePiece; - using difference_type = std::ptrdiff_t; - using pointer = SentencePieceText_SentencePiece*; - using reference = SentencePieceText_SentencePiece&; - - Iterator(const SentencePieceTextWrapper* parent, int index) - : parent_(parent), index_(index) {} - SentencePieceText_SentencePiece& operator*() const { - return *(const_cast(parent_)->mutable_pieces( - index_)); - } - Iterator& operator++() { - ++index_; - return *this; - } - bool operator!=(const Iterator& other) const { - return index_ != other.index_; - } - - private: - const SentencePieceTextWrapper* parent_; - int index_; - }; - Iterator begin() const { return Iterator(parent_, 0); } - Iterator end() const { return Iterator(parent_, parent_->pieces_size()); } - int size() const { return parent_->pieces_size(); } - - private: - const SentencePieceTextWrapper* parent_; - }; - - ConstPiecesRepeatedWrapper pieces() const { - return ConstPiecesRepeatedWrapper(this); - } - - class MutablePiecesRepeatedWrapper { - public: - MutablePiecesRepeatedWrapper(SentencePieceTextWrapper* parent) - : parent_(parent) {} - class Iterator { - public: - using iterator_category = std::random_access_iterator_tag; - using value_type = SentencePieceText_SentencePiece; - using difference_type = std::ptrdiff_t; - using pointer = SentencePieceText_SentencePiece*; - using reference = SentencePieceText_SentencePiece&; - - Iterator(SentencePieceTextWrapper* parent, int index) - : parent_(parent), index_(index) {} - Iterator() : parent_(nullptr), index_(-1) {} - - SentencePieceText_SentencePiece& operator*() const { - return *(parent_->mutable_pieces(index_)); - } - Iterator& operator++() { - ++index_; - return *this; - } - Iterator operator++(int) { - Iterator tmp = *this; - ++index_; - return tmp; - } - Iterator& operator--() { - --index_; - return *this; - } - Iterator operator--(int) { - Iterator tmp = *this; - --index_; - return tmp; - } - - Iterator& operator+=(difference_type n) { - index_ += n; - return *this; - } - Iterator& operator-=(difference_type n) { - index_ -= n; - return *this; - } - - friend Iterator operator+(Iterator it, difference_type n) { - it += n; - return it; - } - friend Iterator operator+(difference_type n, Iterator it) { - it += n; - return it; - } - friend Iterator operator-(Iterator it, difference_type n) { - it -= n; - return it; - } - friend difference_type operator-(const Iterator& a, const Iterator& b) { - return a.index_ - b.index_; - } - - SentencePieceText_SentencePiece& operator[](difference_type n) const { - return *(parent_->mutable_pieces(index_ + n)); - } - - bool operator==(const Iterator& other) const { - return index_ == other.index_ && parent_ == other.parent_; - } - bool operator!=(const Iterator& other) const { return !(*this == other); } - bool operator<(const Iterator& other) const { - return index_ < other.index_; - } - bool operator>(const Iterator& other) const { - return index_ > other.index_; - } - bool operator<=(const Iterator& other) const { - return index_ <= other.index_; - } - bool operator>=(const Iterator& other) const { - return index_ >= other.index_; - } + SentencePieceTextPiecesRepeatedWrapper pieces() const; + SentencePieceTextMutablePiecesRepeatedWrapper* mutable_pieces(); - private: - SentencePieceTextWrapper* parent_; - int index_; - }; - Iterator begin() { return Iterator(parent_, 0); } - Iterator end() { return Iterator(parent_, parent_->pieces_size()); } - int size() const { return parent_->pieces_size(); } - - SentencePieceText_SentencePiece* Add() { return parent_->add_pieces(); } - void SwapElements(int i, int j) { parent_->SwapElementsData(i, j); } - void Reserve(int size) { parent_->ReservePieces(size); } - SentencePieceText_SentencePiece* Mutable(int index) { - return parent_->mutable_pieces(index); - } - - private: - SentencePieceTextWrapper* parent_; - }; - MutablePiecesRepeatedWrapper* mutable_pieces() { - if (!mutable_pieces_wrapper_) { - mutable_pieces_wrapper_ = - std::make_unique(this); - } - return mutable_pieces_wrapper_.get(); - } SentencePieceText_SentencePiece* mutable_pieces(int index) { if (index < 0 || index >= pieces_size()) return nullptr; @@ -1144,7 +851,7 @@ class SentencePieceTextWrapper { } void LazyInitPieceWrappersCache() const { - upb_internal::LazyInitCache( + ::sentencepiece::upb_internal::LazyInitCache( piece_wrappers_, const_cast(this), pieces_size()); } @@ -1154,9 +861,14 @@ class SentencePieceTextWrapper { mutable std::vector> piece_wrappers_; - std::unique_ptr mutable_pieces_wrapper_; + std::unique_ptr mutable_pieces_wrapper_; }; + + +class NBestSentencePieceTextConstNbestsRepeatedWrapper; +class NBestSentencePieceTextMutableNbestsRepeatedWrapper; + class NBestSentencePieceTextWrapper { public: NBestSentencePieceTextWrapper() { @@ -1214,47 +926,11 @@ class NBestSentencePieceTextWrapper { inline NBestSentencePieceText_Sub* add_nbests(); inline const NBestSentencePieceText_Sub& nbests(int index) const; - class ConstNbestsRepeatedWrapper { - public: - ConstNbestsRepeatedWrapper(const NBestSentencePieceTextWrapper* parent) - : parent_(parent) {} - class Iterator { - public: - using iterator_category = std::forward_iterator_tag; - using value_type = NBestSentencePieceText_Sub; - using difference_type = std::ptrdiff_t; - using pointer = const NBestSentencePieceText_Sub*; - using reference = const NBestSentencePieceText_Sub&; - - Iterator(const NBestSentencePieceTextWrapper* parent, int index) - : parent_(parent), index_(index) {} - inline const NBestSentencePieceText_Sub& operator*() const; - Iterator& operator++() { - ++index_; - return *this; - } - bool operator!=(const Iterator& other) const { - return index_ != other.index_; - } - - private: - const NBestSentencePieceTextWrapper* parent_; - int index_; - }; - Iterator begin() const { return Iterator(parent_, 0); } - Iterator end() const { return Iterator(parent_, parent_->nbests_size()); } - int size() const { return parent_->nbests_size(); } + NBestSentencePieceTextConstNbestsRepeatedWrapper nbests() const; - private: - const NBestSentencePieceTextWrapper* parent_; - }; - - ConstNbestsRepeatedWrapper nbests() const { - return ConstNbestsRepeatedWrapper(this); - } void LazyInitNbestWrappersCache() const { - upb_internal::LazyInitCache( + ::sentencepiece::upb_internal::LazyInitCache( nbest_wrappers_, const_cast(this), nbests_size()); } @@ -1268,6 +944,8 @@ class NBestSentencePieceTextWrapper { private: }; + + } // namespace upb class TrainerSpec : public sentencepiece::upb::TrainerSpecWrapper { @@ -1319,6 +997,34 @@ class ModelProto : public sentencepiece::upb::ModelProtoWrapper { } }; +class NBestSentencePieceText; + +class NBestSentencePieceText + : public sentencepiece::upb::NBestSentencePieceTextWrapper { + public: + using NBestSentencePieceTextWrapper::nbests; + using NBestSentencePieceTextWrapper::NBestSentencePieceTextWrapper; + using SentencePieceTextWrapperSub = ::sentencepiece::NBestSentencePieceText_Sub; + + upb::NBestSentencePieceTextMutableNbestsRepeatedWrapper* mutable_nbests(); + inline ::sentencepiece::NBestSentencePieceText_Sub* mutable_nbests_at(int index); + + inline const ::sentencepiece::NBestSentencePieceText_Sub& nbests(int index) const; + inline ::sentencepiece::NBestSentencePieceText_Sub* add_nbests(); + + static const NBestSentencePieceText& default_instance() { + static NBestSentencePieceText instance; + return instance; + } + + private: + std::unique_ptr mutable_nbests_wrapper_; +}; + + + +// --- SentencePieceText public classes (namespace sentencepiece) --- + class SentencePieceText : public sentencepiece::upb::SentencePieceTextWrapper { public: using SentencePieceTextWrapper::SentencePieceTextWrapper; @@ -1330,8 +1036,6 @@ class SentencePieceText : public sentencepiece::upb::SentencePieceTextWrapper { } }; -class NBestSentencePieceText; - class NBestSentencePieceText_Sub : public SentencePieceText { public: NBestSentencePieceText_Sub(upb::NBestSentencePieceTextWrapper* parent, @@ -1348,75 +1052,85 @@ class NBestSentencePieceText_Sub : public SentencePieceText { int index_; }; -class NBestSentencePieceText - : public sentencepiece::upb::NBestSentencePieceTextWrapper { + +// --- repeated wrappers and inline implementations (namespace sentencepiece::upb) --- +namespace upb { + +class SentencePieceTextPiecesRepeatedWrapper : public ::sentencepiece::upb_internal::ConstRepeatedWrapperBase< + SentencePieceTextPiecesRepeatedWrapper, SentencePieceText_SentencePiece> { public: - using NBestSentencePieceTextWrapper::nbests; - using NBestSentencePieceTextWrapper::NBestSentencePieceTextWrapper; - using SentencePieceTextWrapperSub = NBestSentencePieceText_Sub; + SentencePieceTextPiecesRepeatedWrapper(const SentencePieceTextWrapper* parent) : parent_(parent) {} + SentencePieceText_SentencePiece Get(int index) const { return parent_->pieces(index); } + int Size() const { return parent_->pieces_size(); } + private: + const SentencePieceTextWrapper* parent_; +}; - inline const NBestSentencePieceText_Sub& nbests(int index) const; - inline NBestSentencePieceText_Sub* add_nbests(); +class SentencePieceTextMutablePiecesRepeatedWrapper : public ::sentencepiece::upb_internal::MutableRepeatedWrapperBase< + SentencePieceTextMutablePiecesRepeatedWrapper, SentencePieceText_SentencePiece> { + public: + SentencePieceTextMutablePiecesRepeatedWrapper(SentencePieceTextWrapper* parent) : parent_(parent) {} + SentencePieceText_SentencePiece* GetMutable(int index) { return parent_->mutable_pieces(index); } + int Size() const { return parent_->pieces_size(); } - class MutableNbestsRepeatedWrapper { - public: - MutableNbestsRepeatedWrapper(NBestSentencePieceText* parent) - : parent_(parent) {} - - class Iterator { - public: - using iterator_category = std::forward_iterator_tag; - using value_type = NBestSentencePieceText_Sub; - using difference_type = std::ptrdiff_t; - using pointer = NBestSentencePieceText_Sub*; - using reference = NBestSentencePieceText_Sub&; - - Iterator(NBestSentencePieceText* parent, int index) - : parent_(parent), index_(index) {} - inline NBestSentencePieceText_Sub& operator*() const { - return *(parent_->mutable_nbests_at(index_)); - } - Iterator& operator++() { - ++index_; - return *this; - } - bool operator!=(const Iterator& other) const { - return index_ != other.index_; - } + SentencePieceText_SentencePiece* Add() { return parent_->add_pieces(); } + void SwapElements(int i, int j) { parent_->SwapElementsData(i, j); } + void Reserve(int size) { parent_->ReservePieces(size); } + private: + SentencePieceTextWrapper* parent_; +}; - private: - NBestSentencePieceText* parent_; - int index_; - }; +class NBestSentencePieceTextConstNbestsRepeatedWrapper : public ::sentencepiece::upb_internal::ConstRepeatedWrapperBase< + NBestSentencePieceTextConstNbestsRepeatedWrapper, ::sentencepiece::NBestSentencePieceText_Sub> { + public: + NBestSentencePieceTextConstNbestsRepeatedWrapper(const NBestSentencePieceTextWrapper* parent) : parent_(parent) {} + ::sentencepiece::NBestSentencePieceText_Sub Get(int index) const { return parent_->nbests(index); } + int Size() const { return parent_->nbests_size(); } + private: + const NBestSentencePieceTextWrapper* parent_; +}; - Iterator begin() { return Iterator(parent_, 0); } - Iterator end() { return Iterator(parent_, parent_->nbests_size()); } - int size() const { return parent_->nbests_size(); } +class NBestSentencePieceTextMutableNbestsRepeatedWrapper : public ::sentencepiece::upb_internal::MutableRepeatedWrapperBase< + NBestSentencePieceTextMutableNbestsRepeatedWrapper, ::sentencepiece::NBestSentencePieceText_Sub> { + public: + NBestSentencePieceTextMutableNbestsRepeatedWrapper(NBestSentencePieceText* parent) : parent_(parent) {} + ::sentencepiece::NBestSentencePieceText_Sub* GetMutable(int index) { return parent_->mutable_nbests_at(index); } + int Size() const { return parent_->nbests_size(); } - NBestSentencePieceText_Sub* Add() { return parent_->add_nbests(); } + ::sentencepiece::NBestSentencePieceText_Sub* Add() { return parent_->add_nbests(); } + private: + NBestSentencePieceText* parent_; +}; - private: - NBestSentencePieceText* parent_; - }; +// Inline getters for SentencePieceTextWrapper +inline SentencePieceTextPiecesRepeatedWrapper SentencePieceTextWrapper::pieces() const { + return SentencePieceTextPiecesRepeatedWrapper(this); +} - MutableNbestsRepeatedWrapper* mutable_nbests() { - if (!mutable_nbests_wrapper_) { - mutable_nbests_wrapper_ = - std::make_unique(this); - } - return mutable_nbests_wrapper_.get(); +inline SentencePieceTextMutablePiecesRepeatedWrapper* +SentencePieceTextWrapper::mutable_pieces() { + if (!mutable_pieces_wrapper_) { + mutable_pieces_wrapper_ = + std::make_unique(this); } + return mutable_pieces_wrapper_.get(); +} - inline NBestSentencePieceText_Sub* mutable_nbests_at(int index); +// Inline getters for NBestSentencePieceTextWrapper +inline NBestSentencePieceTextConstNbestsRepeatedWrapper NBestSentencePieceTextWrapper::nbests() const { + return NBestSentencePieceTextConstNbestsRepeatedWrapper(this); +} - static const NBestSentencePieceText& default_instance() { - static NBestSentencePieceText instance; - return instance; - } +} // namespace upb - private: - std::unique_ptr mutable_nbests_wrapper_; -}; +inline upb::NBestSentencePieceTextMutableNbestsRepeatedWrapper* +NBestSentencePieceText::mutable_nbests() { + if (!mutable_nbests_wrapper_) { + mutable_nbests_wrapper_ = + std::make_unique(this); + } + return mutable_nbests_wrapper_.get(); +} inline ProtoStr ModelProto_SentencePiece::piece() const { return parent_ ? parent_->piece_at(index_) : ""; @@ -1473,15 +1187,12 @@ upb::NBestSentencePieceTextWrapper::add_nbests() { inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper::nbests(int index) const { + LazyInitNbestWrappersCache(); static const NBestSentencePieceText_Sub default_sub(nullptr, -1); UPB_WRAPPER_RET_VAL_IF_OOB(index, nbest_wrappers_.size(), default_sub); return *nbest_wrappers_[index]; } -inline const NBestSentencePieceText_Sub& upb::NBestSentencePieceTextWrapper:: - ConstNbestsRepeatedWrapper::Iterator::operator*() const { - return parent_->nbests(index_); -} inline sentencepiece_SentencePieceText* NBestSentencePieceText_Sub::mutable_msg() { @@ -1563,14 +1274,14 @@ inline void swap(SentencePieceText_SentencePiece a, a.parent_->SwapElementsData(a.index_, b.index_); } -inline const NBestSentencePieceText_Sub& NBestSentencePieceText::nbests( +inline const ::sentencepiece::NBestSentencePieceText_Sub& NBestSentencePieceText::nbests( int index) const { return NBestSentencePieceTextWrapper::nbests(index); } -inline NBestSentencePieceText_Sub* NBestSentencePieceText::add_nbests() { +inline ::sentencepiece::NBestSentencePieceText_Sub* NBestSentencePieceText::add_nbests() { return NBestSentencePieceTextWrapper::add_nbests(); } -inline NBestSentencePieceText_Sub* NBestSentencePieceText::mutable_nbests_at( +inline ::sentencepiece::NBestSentencePieceText_Sub* NBestSentencePieceText::mutable_nbests_at( int index) { UPB_WRAPPER_RET_VAL_IF_OOB(index, nbests_size(), nullptr); LazyInitNbestWrappersCache(); @@ -1679,6 +1390,29 @@ inline void ModelProto_SentencePiece::set_score(float score) { parent_->set_score_at(index_, score); } +// ModelProtoWrapper implementations +inline upb::ModelProtoWrapper::ModelProtoWrapper() { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_ModelProto_new(arena), arena, true); + normalizer_spec_cache_ = std::make_unique( + sentencepiece_ModelProto_normalizer_spec(static_cast(holder_.msg())), holder_.arena()); + denormalizer_spec_cache_ = std::make_unique( + sentencepiece_ModelProto_denormalizer_spec(static_cast(holder_.msg())), holder_.arena()); +} +inline upb::ModelProtoWrapper::ModelProtoWrapper(const sentencepiece_ModelProto* msg, + upb_Arena* arena) + : holder_(const_cast(msg), arena, false) {} +inline upb::ModelProtoWrapper::~ModelProtoWrapper() = default; + +// SentencePieceTextWrapper implementations +inline upb::SentencePieceTextWrapper::SentencePieceTextWrapper() { + upb_Arena* arena = upb_Arena_New(); + holder_.Reset(sentencepiece_SentencePieceText_new(arena), arena, true); +} +inline upb::SentencePieceTextWrapper::SentencePieceTextWrapper(std::nullptr_t) + : holder_(nullptr, nullptr, false) {} +inline upb::SentencePieceTextWrapper::~SentencePieceTextWrapper() = default; + } // namespace sentencepiece #endif // CORE_UPB_WRAPPER_H_ diff --git a/src/sentencepiece_processor_test.cc b/src/sentencepiece_processor_test.cc index 194290d83..47797eb11 100644 --- a/src/sentencepiece_processor_test.cc +++ b/src/sentencepiece_processor_test.cc @@ -111,7 +111,7 @@ std::vector GetIdVec(const EncodeResult& pieces) { std::vector GetSpVec(const SentencePieceText& spt) { std::vector sps; - for (auto& sp : spt.pieces()) { + for (const auto& sp : spt.pieces()) { sps.emplace_back(sp.piece()); } return sps; @@ -1679,7 +1679,7 @@ TEST(SentencePieceProcessorTest, ImmutableSentencePieceTextTest) { auto check_proto = [&v](const ImmutableSentencePieceText& s) { int n = 0; - for (auto& p : s.pieces()) { + for (const auto& p : s.pieces()) { EXPECT_EQ(v->pieces(n).surface(), p.surface()); EXPECT_EQ(v->pieces(n).piece(), p.piece()); EXPECT_EQ(v->pieces(n).id(), p.id());